added support for configuring request headers.
[utils] / crawler / basic / src / org / wamblee / crawler / AbstractPageRequest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.crawler;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21
22 import javax.xml.transform.OutputKeys;
23 import javax.xml.transform.Transformer;
24 import javax.xml.transform.TransformerConfigurationException;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamResult;
29
30 import org.apache.commons.httpclient.Header;
31 import org.apache.commons.httpclient.HttpClient;
32 import org.apache.commons.httpclient.HttpMethod;
33 import org.apache.commons.httpclient.HttpStatus;
34 import org.apache.commons.httpclient.NameValuePair;
35 import org.apache.commons.httpclient.methods.GetMethod;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.xml.serialize.OutputFormat;
39 import org.apache.xml.serialize.XMLSerializer;
40 import org.w3c.dom.Document;
41 import org.w3c.tidy.Tidy;
42 import org.wamblee.xml.DomUtils;
43 import org.wamblee.xml.XslTransformer;
44
45 /**
46  * General support claas for all kinds of requests.
47  */
48 public abstract class AbstractPageRequest implements PageRequest {
49
50     private static final Log LOG = LogFactory.getLog(AbstractPageRequest.class);
51
52     private static final String REDIRECT_HEADER = "Location";
53
54     private int _maxTries;
55
56     private int _maxDelay;
57
58     private NameValuePair[] _params;
59     
60     private NameValuePair[] _headers;
61
62     private String _xslt;
63     
64     private XslTransformer _transformer; 
65
66     /**
67      * Constructs the request.
68      * 
69      * @param aMaxTries
70      *            Maximum retries to perform.
71      * @param aMaxDelay
72      *            Maximum delay before executing a request.
73      * @param aParams
74      *            Request parameters to use.
75      * @param aHeaders
76      *            Request headers to use. 
77      * @param aXslt
78      *            XSLT used to convert the response.
79      */
80     protected AbstractPageRequest(int aMaxTries, int aMaxDelay,
81             NameValuePair[] aParams, NameValuePair[] aHeaders, String aXslt, XslTransformer aTransformer) {
82         if (aParams == null) {
83             throw new IllegalArgumentException("aParams is null");
84         }
85         if (aHeaders == null) {
86             throw new IllegalArgumentException("aHeaders is null");
87         }
88         if (aXslt == null) {
89             throw new IllegalArgumentException("aXslt is null");
90         }
91         _maxTries = aMaxTries;
92         _maxDelay = aMaxDelay;
93         _params = aParams;
94         _headers = aHeaders;
95         _xslt = aXslt;
96         _transformer = aTransformer;
97     }
98
99     /*
100      * (non-Javadoc)
101      * 
102      * @see org.wamblee.crawler.PageRequest#overrideXslt(java.lang.String)
103      */
104     public void overrideXslt(String aXslt) {
105         _xslt = aXslt;
106     }
107
108     /**
109      * Gets the parameters for the request.
110      * 
111      * @return Request parameters.
112      */
113     protected NameValuePair[] getParameters() {
114         return _params;
115     }
116     
117     /**
118      * Gets the headers for the request. 
119      * @return Request headers. 
120      */
121     protected NameValuePair[] getHeaders() { 
122         return _headers;
123     }
124
125     /**
126      * Executes the request with a random delay and with a maximum number of
127      * retries.
128      * 
129      * @param aClient
130      *            HTTP client to use.
131      * @param aMethod
132      *            Method representing the request.
133      * @return XML document describing the response.
134      * @throws IOException
135      *             In case of IO problems.
136      * @throws TransformerException
137      *             In case transformation of the HTML to XML fails.
138      */
139     protected Document executeMethod(HttpClient aClient, HttpMethod aMethod)
140             throws IOException, TransformerException {
141         
142         for (NameValuePair header: getHeaders()) { 
143             aMethod.setRequestHeader(header.getName(), header.getValue());
144         }
145         
146         int triesLeft = _maxTries;
147         while (triesLeft > 0) {
148             triesLeft--;
149             try {
150                 return executeMethodWithoutRetries(aClient, aMethod);
151             } catch (TransformerException e) {
152                 if (triesLeft == 0) {
153                     throw e;
154                 }
155             }
156         }
157         throw new RuntimeException("Code should never reach this point");
158     }
159
160     /**
161      * Executes the request without doing any retries in case XSLT
162      * transformation fails.
163      * 
164      * @param aClient
165      *            HTTP client to use.
166      * @param aMethod
167      *            Method to execute.
168      * @return XML document containing the result.
169      * @throws IOException
170      *             In case of IO problems.
171      * @throws TransformerException
172      *             In case transformation of the result to XML fails.
173      */
174     protected Document executeMethodWithoutRetries(HttpClient aClient,
175             HttpMethod aMethod) throws IOException, TransformerException {
176         try {
177             aMethod = executeWithRedirects(aClient, aMethod);
178             byte[] xhtmlData = getXhtml(aMethod);
179      
180             Document transformed = _transformer.transform(xhtmlData,
181                     _transformer.resolve(_xslt));
182             ByteArrayOutputStream os = new ByteArrayOutputStream(); 
183             Transformer transformer = TransformerFactory.newInstance()
184                     .newTransformer();
185             transformer.setParameter(OutputKeys.INDENT, "yes");
186             transformer.setParameter(OutputKeys.METHOD, "xml");
187             transformer.transform(new DOMSource(transformed), new StreamResult(
188                     os));
189             LOG.debug("Transformed result is \n" + os.toString());
190             return transformed;
191         } catch (TransformerConfigurationException e) {
192             throw new TransformerException("Transformer configuration problem", e);
193         } finally {
194             // Release the connection.
195             aMethod.releaseConnection();
196         }
197     }
198
199     /**
200      * Gets the result of the HTTP method as an XHTML document.
201      * 
202      * @param aMethod
203      *            Method to invoke.
204      * @return XHTML as a byte array.
205      * @throws IOException
206      *             In case of problems obtaining the XHTML.
207      */
208     private byte[] getXhtml(HttpMethod aMethod) throws IOException {
209         // Transform the HTML into wellformed XML.
210         Tidy tidy = new Tidy();
211         tidy.setXHTML(true);
212         tidy.setQuiet(true);
213         tidy.setShowWarnings(false);
214       
215         // We write the jtidy output to XML since the DOM tree it produces is
216         // not namespace aware and namespace awareness is required by XSLT.
217         // An alternative is to configure namespace awareness of the XML parser
218         // in a system wide way.
219         ByteArrayOutputStream os = new ByteArrayOutputStream(); 
220         Document w3cDoc = tidy.parseDOM(aMethod.getResponseBodyAsStream(), os);
221         DomUtils.removeDuplicateAttributes(w3cDoc);
222         LOG.debug("Content of response is \n" + os.toString()); 
223
224         ByteArrayOutputStream xhtml = new ByteArrayOutputStream();
225         XMLSerializer serializer = new XMLSerializer(xhtml, new OutputFormat());
226         serializer.serialize(w3cDoc);
227         xhtml.flush();
228
229         return xhtml.toByteArray();
230     }
231
232     /**
233      * Sleeps for a random time but no more than the maximum delay.
234      * 
235      */
236     private void delay() {
237         try {
238             Thread.sleep((long) ((float) _maxDelay * Math.random()));
239         } catch (InterruptedException e) {
240             return; // to satisfy checkstyle
241         }
242     }
243
244     /**
245      * Executes the request and follows redirects if needed.
246      * 
247      * @param aClient
248      *            HTTP client to use.
249      * @param aMethod
250      *            Method to use.
251      * @return Final HTTP method used (differs from the parameter passed in in
252      *         case of redirection).
253      * @throws IOException
254      *             In case of network problems.
255      */
256     private HttpMethod executeWithRedirects(HttpClient aClient,
257             HttpMethod aMethod) throws IOException {
258         delay();
259         int statusCode = aClient.executeMethod(aMethod);
260
261         switch (statusCode) {
262         case HttpStatus.SC_OK: {
263             return aMethod;
264         }
265         case HttpStatus.SC_MOVED_PERMANENTLY:
266         case HttpStatus.SC_MOVED_TEMPORARILY:
267         case HttpStatus.SC_SEE_OTHER: {
268             aMethod.releaseConnection();
269             Header header = aMethod.getResponseHeader(REDIRECT_HEADER);
270             aMethod = new GetMethod(header.getValue());
271             return executeWithRedirects(aClient, aMethod); // TODO protect
272             // against infinite
273             // recursion.
274         }
275         default: {
276             throw new IOException("Method failed: "
277                     + aMethod.getStatusLine());
278         }
279         }
280     }
281 }