import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
+import org.wamblee.io.FileResource;
import org.wamblee.xml.XSLT;
/**
private static final Log LOG = LogFactory.getLog(AbstractPageRequest.class);
private static final String REDIRECT_HEADER = "Location";
+
+ private int _maxTries;
+ private int _maxDelay;
private NameValuePair[] _params;
private PrintStream _os;
- protected AbstractPageRequest(NameValuePair[] aParams, String aXslt, PrintStream aOs) {
+ protected AbstractPageRequest(int aMaxTries, int aMaxDelay, NameValuePair[] aParams, String aXslt, PrintStream aOs) {
if ( aParams == null ) {
throw new IllegalArgumentException("aParams is null");
}
if ( aXslt == null ) {
throw new IllegalArgumentException("aXslt is null");
}
+ _maxTries = aMaxTries;
+ _maxDelay = aMaxDelay;
_params = aParams;
_xslt = aXslt;
_os = aOs;
protected NameValuePair[] getParameters() {
return _params;
}
+
+ protected Document executeMethod(HttpClient client, HttpMethod method) throws TransformerException {
+ int triesLeft = _maxTries;
+ while ( triesLeft > 0 ) {
+ triesLeft--;
+ try {
+ return executeMethodWithoutRetries(client, method);
+ } catch (TransformerException e) {
+ if ( triesLeft == 0 ) {
+ throw e;
+ }
+ }
+ }
+ throw new RuntimeException("Code should never reach this point");
+ }
+
- protected Document executeMethod(HttpClient client, HttpMethod method) {
+ protected Document executeMethodWithoutRetries(HttpClient client, HttpMethod method) throws TransformerException {
try {
// Execute the method.
method = executeWithRedirects(client, method);
}
xhtml.flush();
byte[] xhtmlData = xhtml.toByteArray();
- Document transformed = XSLT.transform(xhtmlData, new File(_xslt));
+ Document transformed = new XSLT().transform(xhtmlData, new FileResource(new File(_xslt)));
_os.println("Transformed result is: ");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setParameter(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(transformed), new StreamResult(_os));
return transformed;
- } catch (Exception e) {
+ } catch (HttpException e) {
+ throw new RuntimeException(e.getMessage(), e);
+ } catch (IOException e) {
+ throw new RuntimeException(e.getMessage(), e);
+ } catch (TransformerConfigurationException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
// Release the connection.
method.releaseConnection();
}
}
+
+ private void delay() {
+ try {
+ Thread.sleep((long)((float)_maxDelay* Math.random()));
+ } catch (InterruptedException e) {
+ //
+ }
+ }
+
/**
* @param aClient
* @throws HttpException
*/
private HttpMethod executeWithRedirects(HttpClient aClient, HttpMethod aMethod) throws IOException, HttpException {
+ delay();
int statusCode = aClient.executeMethod(aMethod);
switch (statusCode) {