|
| 1 | +package com.cybersource.ws.client; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.net.MalformedURLException; |
| 7 | +import java.net.ProtocolException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import javax.xml.parsers.DocumentBuilder; |
| 13 | +import javax.xml.transform.TransformerConfigurationException; |
| 14 | +import javax.xml.transform.TransformerException; |
| 15 | + |
| 16 | +import org.apache.commons.httpclient.HttpClient; |
| 17 | +import org.apache.commons.httpclient.HttpMethod; |
| 18 | +import org.apache.commons.httpclient.HttpMethodRetryHandler; |
| 19 | +import org.apache.commons.httpclient.HttpState; |
| 20 | +import org.apache.commons.httpclient.NoHttpResponseException; |
| 21 | +import org.apache.commons.httpclient.UsernamePasswordCredentials; |
| 22 | +import org.apache.commons.httpclient.auth.AuthPolicy; |
| 23 | +import org.apache.commons.httpclient.auth.AuthScope; |
| 24 | +import org.apache.commons.httpclient.methods.PostMethod; |
| 25 | +import org.apache.commons.httpclient.methods.StringRequestEntity; |
| 26 | +import org.apache.commons.httpclient.params.HttpConnectionManagerParams; |
| 27 | +import org.apache.commons.httpclient.params.HttpMethodParams; |
| 28 | +import org.w3c.dom.Document; |
| 29 | + |
| 30 | + |
| 31 | +public class AmazonHttpConnection extends Connection{ |
| 32 | + private PostMethod postMethod = null; |
| 33 | + |
| 34 | + AmazonHttpConnection(MerchantConfig mc, DocumentBuilder builder, LoggerWrapper logger) { |
| 35 | + super(mc, builder, logger); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean isRequestSent() { |
| 40 | + return postMethod != null && postMethod.isRequestSent(); |
| 41 | + } |
| 42 | + @Override |
| 43 | + public void release() { |
| 44 | + if (postMethod != null) { |
| 45 | + postMethod.releaseConnection(); |
| 46 | + postMethod = null; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + void postDocument(Document request) throws IOException, |
| 52 | + TransformerConfigurationException, TransformerException, |
| 53 | + MalformedURLException, ProtocolException { |
| 54 | + HttpClient httpClient = new HttpClient(); |
| 55 | + setTimeout(httpClient, mc.getTimeout() * 1000); |
| 56 | + setProxy(httpClient); |
| 57 | + |
| 58 | + String serverURL = mc.getEffectiveServerURL(); |
| 59 | + postMethod = new PostMethod(serverURL); |
| 60 | + postMethod.getParams().setParameter( |
| 61 | + HttpMethodParams.RETRY_HANDLER, new MyRetryHandler()); |
| 62 | + |
| 63 | + String requestString = documentToString(request); |
| 64 | + logger.log(Logger.LT_INFO, |
| 65 | + "Sending " + requestString.length() + " bytes to " + serverURL); |
| 66 | + |
| 67 | + postMethod.setRequestEntity( |
| 68 | + new StringRequestEntity(requestString, null, "UTF-8")); |
| 69 | + |
| 70 | + httpClient.executeMethod(postMethod); |
| 71 | + |
| 72 | + } |
| 73 | + @Override |
| 74 | + int getHttpResponseCode() throws IOException { |
| 75 | + return postMethod != null ? postMethod.getStatusCode() : -1; |
| 76 | + } |
| 77 | + @Override |
| 78 | + InputStream getResponseStream() throws IOException { |
| 79 | + return postMethod != null ? postMethod.getResponseBodyAsStream() : null; |
| 80 | + } |
| 81 | + @Override |
| 82 | + InputStream getResponseErrorStream() throws IOException { |
| 83 | + return getResponseStream(); |
| 84 | + } |
| 85 | + /** |
| 86 | + * This method is useful in Client environment where Firewall is set to prevent accessing the external services. |
| 87 | + * Proxy settings are required in such scenarios. |
| 88 | + * @param httpClient |
| 89 | + */ |
| 90 | + private void setProxy(HttpClient httpClient) { |
| 91 | + if (mc.getProxyHost() != null) { |
| 92 | + httpClient.getHostConfiguration().setProxy( |
| 93 | + mc.getProxyHost(), mc.getProxyPort()); |
| 94 | + |
| 95 | + if (mc.getProxyUser() != null) { |
| 96 | + List<String> authPrefs = new ArrayList<String>(); |
| 97 | + authPrefs.add(AuthPolicy.BASIC); |
| 98 | + httpClient.getParams().setParameter( |
| 99 | + AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); |
| 100 | + |
| 101 | + HttpState state = new HttpState(); |
| 102 | + state.setProxyCredentials( |
| 103 | + AuthScope.ANY, |
| 104 | + new UsernamePasswordCredentials( |
| 105 | + mc.getProxyUser(), mc.getProxyPassword())); |
| 106 | + httpClient.setState(state); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Method converts Document to String using java.xml package library. |
| 113 | + * @param doc - Document object |
| 114 | + * @return - String object |
| 115 | + * @throws TransformerConfigurationException |
| 116 | + * @throws TransformerException |
| 117 | + * @throws IOException |
| 118 | + */ |
| 119 | + private static String documentToString(Document doc) |
| 120 | + throws TransformerConfigurationException, TransformerException, |
| 121 | + IOException { |
| 122 | + ByteArrayOutputStream baos = null; |
| 123 | + try { |
| 124 | + baos = makeStream(doc); |
| 125 | + return baos.toString("utf-8"); |
| 126 | + } finally { |
| 127 | + if (baos != null) { |
| 128 | + baos.close(); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Methods helps to set the timeout for HTTP request call. |
| 135 | + * cybs.properties can be used to configure the timeout details. |
| 136 | + * @param httpClient |
| 137 | + * @param timeoutInMs |
| 138 | + */ |
| 139 | + private void setTimeout(HttpClient httpClient, int timeoutInMs) { |
| 140 | + HttpConnectionManagerParams params |
| 141 | + = httpClient.getHttpConnectionManager().getParams(); |
| 142 | + params.setConnectionTimeout(timeoutInMs); |
| 143 | + params.setSoTimeout(timeoutInMs); |
| 144 | + } |
| 145 | + |
| 146 | + private class MyRetryHandler implements HttpMethodRetryHandler { |
| 147 | + |
| 148 | + long retryWaitInterval=mc.getRetryInterval(); |
| 149 | + int maxRetries= mc.getNumberOfRetries(); |
| 150 | + |
| 151 | + // I copied this code from |
| 152 | + // http://jakarta.apache.org/commons/httpclient/exception-handling.html#HTTP%20transport%20safety |
| 153 | + // and changed the NoHttpResponseException case to |
| 154 | + // return false. |
| 155 | + public boolean retryMethod( |
| 156 | + final HttpMethod method, |
| 157 | + final IOException exception, |
| 158 | + int executionCount) { |
| 159 | + if (executionCount > maxRetries) { |
| 160 | + // Do not retry if over max retry count |
| 161 | + return false; |
| 162 | + } |
| 163 | + if (exception instanceof NoHttpResponseException) { |
| 164 | + // Retry if the server dropped connection on us |
| 165 | + // return true; <-- this was the original behavior. |
| 166 | + return false; |
| 167 | + } |
| 168 | + if (!method.isRequestSent()) { |
| 169 | + // Retry if the request has not been sent fully or |
| 170 | + // if it's OK to retry methods that have been sent |
| 171 | + try { |
| 172 | + Thread.sleep(retryWaitInterval); |
| 173 | + logger.log( Logger.LT_INFO+" Retrying Request -- ",mc.getUniqueKey().toString()+ " Retry Count -- "+executionCount); |
| 174 | + } catch (InterruptedException e) { |
| 175 | + // TODO Auto-generated catch block |
| 176 | + e.printStackTrace(); |
| 177 | + } |
| 178 | + return true; |
| 179 | + } |
| 180 | + // otherwise do not retry |
| 181 | + return false; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments