added support for configuring request headers.
[utils] / crawler / basic / src / org / wamblee / crawler / AbstractPageRequest.java
index 7607f764b3c20999842d14089e22cc52ee61ee40..28482d7fbd096fb8641db821cfcbaab339841726 100644 (file)
@@ -56,6 +56,8 @@ public abstract class AbstractPageRequest implements PageRequest {
     private int _maxDelay;
 
     private NameValuePair[] _params;
+    
+    private NameValuePair[] _headers;
 
     private String _xslt;
     
@@ -70,20 +72,26 @@ public abstract class AbstractPageRequest implements PageRequest {
      *            Maximum delay before executing a request.
      * @param aParams
      *            Request parameters to use.
+     * @param aHeaders
+     *            Request headers to use. 
      * @param aXslt
      *            XSLT used to convert the response.
      */
     protected AbstractPageRequest(int aMaxTries, int aMaxDelay,
-            NameValuePair[] aParams, String aXslt, XslTransformer aTransformer) {
+            NameValuePair[] aParams, NameValuePair[] aHeaders, String aXslt, XslTransformer aTransformer) {
         if (aParams == null) {
             throw new IllegalArgumentException("aParams is null");
         }
+        if (aHeaders == null) {
+            throw new IllegalArgumentException("aHeaders is null");
+        }
         if (aXslt == null) {
             throw new IllegalArgumentException("aXslt is null");
         }
         _maxTries = aMaxTries;
         _maxDelay = aMaxDelay;
         _params = aParams;
+        _headers = aHeaders;
         _xslt = aXslt;
         _transformer = aTransformer;
     }
@@ -105,6 +113,14 @@ public abstract class AbstractPageRequest implements PageRequest {
     protected NameValuePair[] getParameters() {
         return _params;
     }
+    
+    /**
+     * Gets the headers for the request. 
+     * @return Request headers. 
+     */
+    protected NameValuePair[] getHeaders() { 
+        return _headers;
+    }
 
     /**
      * Executes the request with a random delay and with a maximum number of
@@ -122,6 +138,11 @@ public abstract class AbstractPageRequest implements PageRequest {
      */
     protected Document executeMethod(HttpClient aClient, HttpMethod aMethod)
             throws IOException, TransformerException {
+        
+        for (NameValuePair header: getHeaders()) { 
+            aMethod.setRequestHeader(header.getName(), header.getValue());
+        }
+        
         int triesLeft = _maxTries;
         while (triesLeft > 0) {
             triesLeft--;
@@ -155,7 +176,7 @@ public abstract class AbstractPageRequest implements PageRequest {
         try {
             aMethod = executeWithRedirects(aClient, aMethod);
             byte[] xhtmlData = getXhtml(aMethod);
-
+     
             Document transformed = _transformer.transform(xhtmlData,
                     _transformer.resolve(_xslt));
             ByteArrayOutputStream os = new ByteArrayOutputStream();