Required since kiss checks for this header now.
private int _maxDelay;
private NameValuePair[] _params;
+
+ private NameValuePair[] _headers;
private String _xslt;
* 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;
}
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
*/
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--;
try {
aMethod = executeWithRedirects(aClient, aMethod);
byte[] xhtmlData = getXhtml(aMethod);
-
+
Document transformed = _transformer.transform(xhtmlData,
_transformer.resolve(_xslt));
ByteArrayOutputStream os = new ByteArrayOutputStream();
* @param aMaxTries Maximum number of retries.
* @param aMaxDelay Maximum delay before executing the request.
* @param aParams Request parameters to use.
+ * @param aHeaders Request headers to use.
* @param aXslt XSLT to use.
*/
- public GetPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams, String aXslt, XslTransformer aTransformer) {
- super(aMaxTries, aMaxDelay, aParams, aXslt, aTransformer);
+ public GetPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams,
+ NameValuePair[] aHeaders, String aXslt, XslTransformer aTransformer) {
+ super(aMaxTries, aMaxDelay, aParams, aHeaders, aXslt, aTransformer);
}
/*
* @param aMaxTries Maximum number of retries.
* @param aMaxDelay Maximum delay before executing the request.
* @param aParams Request parameters to use.
+ * @param aHeaders Request headers to use.
* @param aXslt XSLT to use.
*/
- public PostPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams, String aXslt, XslTransformer aTransformer) {
- super(aMaxTries, aMaxDelay, aParams, aXslt, aTransformer);
+ public PostPageRequest(int aMaxTries, int aMaxDelay,
+ NameValuePair[] aParams,
+ NameValuePair[] aHeaders,
+ String aXslt, XslTransformer aTransformer) {
+ super(aMaxTries, aMaxDelay, aParams, aHeaders, aXslt, aTransformer);
}
/*
private static final String ELEM_XSLT = "xslt";
private static final String ELEM_PARAM = "param";
+
+ private static final String ELEM_HEADER = "header";
private static final String AT_NAME = "name";
private PageRequest parseRequestConfig(Element aElem) {
String method = aElem.elementText(ELEM_METHOD);
String xslt = aElem.elementText(ELEM_XSLT);
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- for (Iterator i = aElem.elementIterator(ELEM_PARAM); i.hasNext();) {
- Element paramElem = (Element) i.next();
- NameValuePair param = parseParameter(paramElem);
- params.add(param);
- }
-
+ List<NameValuePair> params = parseNameValuePairs(aElem, ELEM_PARAM);
+ List<NameValuePair> headers = parseNameValuePairs(aElem, ELEM_HEADER);
+
NameValuePair[] paramsArray = params.toArray(new NameValuePair[0]);
+ NameValuePair[] headersArray = headers.toArray(new NameValuePair[0]);
PageRequest request;
if (METHOD_POST.equals(method)) {
- request = new PostPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
+ request = new PostPageRequest(MAX_TRIES, MAX_DELAY, paramsArray, headersArray,
xslt, _transformer);
} else if (METHOD_GET.equals(method) || method == null) {
- request = new GetPageRequest(MAX_TRIES, MAX_DELAY, paramsArray,
+ request = new GetPageRequest(MAX_TRIES, MAX_DELAY, paramsArray, headersArray,
xslt, _transformer);
} else {
throw new RuntimeException("Unknown request method '" + method
return request;
}
+ /**
+ * @param aElem
+ * @return
+ */
+ private List<NameValuePair> parseNameValuePairs(Element aElem, String aElemName) {
+ List<NameValuePair> headers = new ArrayList<NameValuePair>();
+ for (Iterator i = aElem.elementIterator(aElemName); i.hasNext();) {
+ Element paramElem = (Element) i.next();
+ NameValuePair header = parseParameter(paramElem);
+ headers.add(header);
+ }
+ return headers;
+ }
+
/**
* Parses a parameter definition.
* @param aParam Parameter.