Browse Source

Follows redirection when posting

tags/Production_2014_08_01
ymlam 13 years ago
parent
commit
9d7883e60f
1 changed files with 68 additions and 28 deletions
  1. +68
    -28
      src/altk/comm/engine/postback/PostBack.java

+ 68
- 28
src/altk/comm/engine/postback/PostBack.java View File

@@ -18,7 +18,9 @@ import javax.xml.xpath.XPathFactory;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
@@ -169,53 +171,91 @@ public class PostBack
}
xml.append("</"); xml.append(xmlTopElement); xml.append(">");
CommonLogger.activity.info(getName() + ": posting " + xml.toString());
PostMethod post = new PostMethod(postBackURL);
PostMethod post = new PostMethod();
String responseBody = null;
StringRequestEntity requestEntity = null;
try
{
post.setRequestEntity(new StringRequestEntity(xml.toString(),
"application/xml", "utf-8"));
requestEntity = new StringRequestEntity(xml.toString(), "application/xml", "utf-8");
}
catch (UnsupportedEncodingException e)
{
CommonLogger.alarm.warn(getName() + ": While adding this application/xml content to PostBack: " + xml + " -- " + e);
return PostBackStatus.IRRECOVERABLE_ERROR;
}
post.setRequestEntity(requestEntity);
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));

/*
CommonLogger.activity.debug("Before calling setFolloweRedirects()");
post.setFollowRedirects(false);
CommonLogger.activity.debug("After calling setFolloweRedirects()");
*/
HttpClient client = new HttpClient();
try
String url = postBackURL;
for (int redirectCount = 0; redirectCount < 3; redirectCount++)
{
client.getHttpConnectionManager().getParams().setConnectionTimeout(5 * 1000);
int statusCode = client.executeMethod(post);

if (statusCode != 200)
try
{
client.getHttpConnectionManager().getParams().setConnectionTimeout(5 * 1000);
post.setURI(new HttpURL(url));
if (redirectCount == 0)
{
CommonLogger.activity.info(getName() + ": posting " + xml.toString() + " to " + url);
}
else
{
CommonLogger.activity.info(getName() + ": redirected to " + url);
}
int statusCode = client.executeMethod(post);
if (statusCode == 302)
{
Header locationHeader = post.getResponseHeader("Location");
if (locationHeader != null)
{
url = locationHeader.getValue();
post.releaseConnection();
continue;
}
else
{
CommonLogger.alarm.warn(getName() + ": When posting to \"" + url + "\": " + " received status 302 but without location header");
return PostBackStatus.IRRECOVERABLE_ERROR;
}
}
else if (statusCode != 200)
{
CommonLogger.alarm.warn(getName() + ": Received problem status code " + statusCode + " from posting to \"" + url + "\": " + xml);
return PostBackStatus.HTTP_STATUS_ERROR;
}
responseBody = post.getResponseBodyAsString().trim();
post.releaseConnection();
CommonLogger.activity.info(getName() + ": Received response: " + (responseBody.length() == 0? "[empty]" : responseBody));
if (responseBody.trim().length() == 0) return PostBackStatus.SUCCESS;
break;
}
catch (ConnectTimeoutException e)
{
CommonLogger.alarm.warn(getName() + ": Received problem status code " + statusCode + " from posting to \"" + postBackURL + "\": " + xml);
return PostBackStatus.HTTP_STATUS_ERROR;
CommonLogger.alarm.warn(getName() + ": IO problem while posting to \"" + url + "\": " + xml + " -- " + e.getMessage());
return PostBackStatus.SERVER_IO_ERROR;
}
catch (IOException e)
{
CommonLogger.alarm.warn(getName() + ": IO problem while posting to \"" + url + "\": " + xml + " -- " + e.getMessage());
return PostBackStatus.SERVER_IO_ERROR;
}
catch (IllegalArgumentException e)
{
CommonLogger.alarm.warn(getName() + ": When posting to \"" + url + "\": " + e.getMessage());
return PostBackStatus.IRRECOVERABLE_ERROR;
}
responseBody = post.getResponseBodyAsString().trim();
CommonLogger.activity.info(getName() + ": Received response: " + (responseBody.length() == 0? "[empty]" : responseBody));
if (responseBody.trim().length() == 0) return PostBackStatus.SUCCESS;
}
catch (ConnectTimeoutException e)
{
CommonLogger.alarm.warn(getName() + ": IO problem while posting to \"" + postBackURL + "\": " + xml + " -- " + e.getMessage());
return PostBackStatus.SERVER_IO_ERROR;
}
catch (IOException e)
{
CommonLogger.alarm.warn(getName() + ": IO problem while posting to \"" + postBackURL + "\": " + xml + " -- " + e.getMessage());
return PostBackStatus.SERVER_IO_ERROR;
}
catch (IllegalArgumentException e)
if (responseBody == null)
{
CommonLogger.alarm.warn(getName() + ": When posting to \"" + postBackURL + "\": " + e.getMessage());
CommonLogger.alarm.warn(getName() + ": When posting to \"" + url + "\": " + " Exhausted allowable redirects");
return PostBackStatus.IRRECOVERABLE_ERROR;
}
// parse into xml doc
Document xmlDoc = null;
try


Loading…
Cancel
Save