From b05e799b9773f8b24d208491834fd038c7735e71 Mon Sep 17 00:00:00 2001 From: Ludovic Roland Date: Wed, 15 Feb 2017 12:04:34 +0100 Subject: [PATCH] RED-149 Rollback the URLConnectionWeServiceCaller + updated the version name to 2.4.8 --- droid4me/VERSION.txt | 2 +- droid4me/pom.xml | 2 +- .../ws/URLConnectionWebServiceCaller.java | 111 ++++++++++-------- 3 files changed, 65 insertions(+), 50 deletions(-) diff --git a/droid4me/VERSION.txt b/droid4me/VERSION.txt index 3a9c5314..752a79ef 100755 --- a/droid4me/VERSION.txt +++ b/droid4me/VERSION.txt @@ -1 +1 @@ -2.4.8.007-SNAPSHOT \ No newline at end of file +2.4.8 \ No newline at end of file diff --git a/droid4me/pom.xml b/droid4me/pom.xml index dd86fa03..5f6d6359 100644 --- a/droid4me/pom.xml +++ b/droid4me/pom.xml @@ -10,7 +10,7 @@ com.smartnsoft droid4me droid4me - 2.4.8.007-SNAPSHOT + 2.4.8 jar droid4me is a framework library dedicated to the development of Android applications. https://github.com/smartnsoft/droid4me diff --git a/droid4me/src/com/smartnsoft/droid4me/ws/URLConnectionWebServiceCaller.java b/droid4me/src/com/smartnsoft/droid4me/ws/URLConnectionWebServiceCaller.java index 13d26d5f..26c78c3f 100644 --- a/droid4me/src/com/smartnsoft/droid4me/ws/URLConnectionWebServiceCaller.java +++ b/droid4me/src/com/smartnsoft/droid4me/ws/URLConnectionWebServiceCaller.java @@ -62,18 +62,14 @@ public abstract class URLConnectionWebServiceCaller extends WebServiceCaller { + protected final static Logger log = LoggerFactory.getInstance(URLConnectionWebServiceCaller.class); + private final static String BOUNDARY = "URLConnectionWebServiceCaller"; private final static String HYPHEN_HYPHEN = "--"; private final static String NEW_LINE = "\r\n"; - protected final static Logger log = LoggerFactory.getInstance(URLConnectionWebServiceCaller.class); - - protected abstract int getReadTimeout(); - - protected abstract int getConnectTimeout(); - /** * Equivalent to calling {@link #runRequest(String, CallType, Map, String)} with {@code callType} parameter set to * {@code CallType.Get} and {@code body} and {@code parameters} parameters set to {@code null}. @@ -152,6 +148,10 @@ public HttpResponse runRequest(String uri, CallType callType, Map20X. The default implementation logs the problem and throws an exception. * @@ -283,7 +283,8 @@ protected InputStream getContent(String uri, CallType callType, HttpURLConnectio { if (log.isWarnEnabled()) { - log.error("Could not close the input stream corresponding to the copy of the HTTP response content", exception); + log.error("Could not close the input stream corresponding to the copy of the HTTP response content", + exception); } } @@ -317,6 +318,13 @@ protected InputStream getContent(String uri, CallType callType, HttpURLConnectio return null; } + protected HttpURLConnection performHttpRequest(String uri, CallType callType, Map headers, + Map parameters, String body, List files) + throws IOException, CallException + { + return performHttpRequest(uri, callType, headers, parameters, body, files, 0); + } + /** * Is responsible for returning an HTTP client instance, used for actually running the HTTP requests. *

@@ -350,7 +358,8 @@ private HttpURLConnection performHttpRequest(String uri, CallType callType, Map< { if (files != null && files.size() > 0) { - httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + URLConnectionWebServiceCaller.BOUNDARY); + httpURLConnection.setRequestProperty("Content-Type", + "multipart/form-data; boundary=" + URLConnectionWebServiceCaller.BOUNDARY); } else { @@ -358,31 +367,30 @@ private HttpURLConnection performHttpRequest(String uri, CallType callType, Map< } } - httpURLConnection.setInstanceFollowRedirects(true); httpURLConnection.setReadTimeout(getReadTimeout()); httpURLConnection.setConnectTimeout(getConnectTimeout()); httpURLConnection.setDoInput(true); switch (callType.verb) { - default: - case Get: - httpURLConnection.setRequestMethod("GET"); - break; - case Head: - httpURLConnection.setRequestMethod("HEAD"); - break; - case Post: - httpURLConnection.setRequestMethod("POST"); - httpURLConnection.setDoOutput(true); - break; - case Put: - httpURLConnection.setRequestMethod("PUT"); - httpURLConnection.setDoOutput(true); - break; - case Delete: - httpURLConnection.setRequestMethod("DELETE"); - break; + default: + case Get: + httpURLConnection.setRequestMethod("GET"); + break; + case Head: + httpURLConnection.setRequestMethod("HEAD"); + break; + case Post: + httpURLConnection.setRequestMethod("POST"); + httpURLConnection.setDoOutput(true); + break; + case Put: + httpURLConnection.setRequestMethod("PUT"); + httpURLConnection.setDoOutput(true); + break; + case Delete: + httpURLConnection.setRequestMethod("DELETE"); + break; } if (headers != null && headers.size() > 0) @@ -405,7 +413,8 @@ private HttpURLConnection performHttpRequest(String uri, CallType callType, Map< { for (final Entry parameter : paramaters.entrySet()) { - logBuilder.append(" " + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY); + logBuilder.append( + " " + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY); logBuilder.append(" Content-Disposition: form-data; name=\"" + parameter.getKey() + "\""); logBuilder.append(" " + parameter.getValue()); } @@ -413,8 +422,10 @@ private HttpURLConnection performHttpRequest(String uri, CallType callType, Map< for (final MultipartFile file : files) { - logBuilder.append(" " + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY); - logBuilder.append(" Content-Disposition: form-data; name=\"" + file.name + "\"; filename=\"" + file.fileName + "\""); + logBuilder.append( + " " + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY); + logBuilder.append( + " Content-Disposition: form-data; name=\"" + file.name + "\"; filename=\"" + file.fileName + "\""); logBuilder.append(" Content-Type: " + file.contentType); } } @@ -435,7 +446,8 @@ else if (paramaters != null && paramaters.size() > 0) try { - curlSb.append("\n>> ").append("curl --request ").append(callType.toString().toUpperCase()).append(" \"").append(uri).append("\""); + curlSb.append("\n>> ").append("curl --request ").append(callType.toString().toUpperCase()).append( + " \"").append(uri).append("\""); if (logBuilder != null && "".equals(logBuilder.toString()) == false) { @@ -448,7 +460,8 @@ else if (paramaters != null && paramaters.size() > 0) { for (final String headerValue : header.getValue()) { - curlSb.append(" --header \"").append(header.getKey()).append(": ").append(headerValue.replace("\"", "\\\"")).append("\""); + curlSb.append(" --header \"").append(header.getKey()).append(": ").append( + headerValue.replace("\"", "\\\"")).append("\""); } } } @@ -458,7 +471,8 @@ else if (paramaters != null && paramaters.size() > 0) // We simply ignore the issue because it is only a debug feature } - log.debug("Running the HTTP " + callType + " request '" + uri + "'" + sb.toString() + (logCurlCommand == true ? curlSb.toString() : "")); + log.debug( + "Running the HTTP " + callType + " request '" + uri + "'" + sb.toString() + (logCurlCommand == true ? curlSb.toString() : "")); } catch (Exception exception) { @@ -477,8 +491,10 @@ else if (paramaters != null && paramaters.size() > 0) { for (final Entry parameter : paramaters.entrySet()) { - outputStream.writeBytes(URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY); - outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE + "Content-Disposition: form-data; name=\"" + parameter.getKey() + "\""); + outputStream.writeBytes( + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY); + outputStream.writeBytes( + URLConnectionWebServiceCaller.NEW_LINE + "Content-Disposition: form-data; name=\"" + parameter.getKey() + "\""); outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE + URLConnectionWebServiceCaller.NEW_LINE); outputStream.write(parameter.getValue().getBytes(getContentEncoding())); outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE); @@ -489,7 +505,8 @@ else if (paramaters != null && paramaters.size() > 0) for (final MultipartFile file : files) { outputStream.writeBytes(URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY); - outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE + "Content-Disposition: form-data; name=\"" + file.name + "\"; filename=\"" + file.fileName + "\""); + outputStream.writeBytes( + URLConnectionWebServiceCaller.NEW_LINE + "Content-Disposition: form-data; name=\"" + file.name + "\"; filename=\"" + file.fileName + "\""); outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE + "Content-Type: " + file.contentType); outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE + URLConnectionWebServiceCaller.NEW_LINE); outputStream.flush(); @@ -509,7 +526,8 @@ else if (paramaters != null && paramaters.size() > 0) } } - outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.NEW_LINE); + outputStream.writeBytes( + URLConnectionWebServiceCaller.NEW_LINE + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.BOUNDARY + URLConnectionWebServiceCaller.HYPHEN_HYPHEN + URLConnectionWebServiceCaller.NEW_LINE); outputStream.writeBytes(URLConnectionWebServiceCaller.NEW_LINE); outputStream.flush(); outputStream.close(); @@ -522,7 +540,8 @@ else if (paramaters != null && paramaters.size() > 0) if ("".equals(body) == false && body != null) { final OutputStream outputStream = httpURLConnection.getOutputStream(); - final BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, getContentEncoding())); + final BufferedWriter bufferedWriter = new BufferedWriter( + new OutputStreamWriter(outputStream, getContentEncoding())); bufferedWriter.write(body); bufferedWriter.flush(); bufferedWriter.close(); @@ -547,19 +566,22 @@ else if (paramaters != null && paramaters.size() > 0) responseHeadersSb.append(","); } - responseHeadersSb.append("(\"").append(header.getKey()).append(": ").append(headerValue.replace("\"", "\\\"")).append("\")"); + responseHeadersSb.append("(\"").append(header.getKey()).append(": ").append( + headerValue.replace("\"", "\\\"")).append("\")"); } } } if (log.isDebugEnabled() == true) { - log.debug("The call to the HTTP " + callType + " request '" + uri + "' took " + (System.currentTimeMillis() - start) + " ms and returned the status code " + responseCode + (responseHeadersSb.length() <= 0 ? "" : " with the HTTP headers:" + responseHeadersSb.toString())); + log.debug( + "The call to the HTTP " + callType + " request '" + uri + "' took " + (System.currentTimeMillis() - start) + " ms and returned the status code " + responseCode + (responseHeadersSb.length() <= 0 ? "" : " with the HTTP headers:" + responseHeadersSb.toString())); } if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_MULT_CHOICE)) { - if (onStatusCodeNotOk(uri, callType, paramaters, body, httpURLConnection, url, responseCode, responseMessage, attemptsCount + 1) == true) + if (onStatusCodeNotOk(uri, callType, paramaters, body, httpURLConnection, url, responseCode, responseMessage, + attemptsCount + 1) == true) { return performHttpRequest(uri, callType, headers, paramaters, body, files, attemptsCount + 1); } @@ -568,13 +590,6 @@ else if (paramaters != null && paramaters.size() > 0) return httpURLConnection; } - protected HttpURLConnection performHttpRequest(String uri, CallType callType, Map headers, - Map parameters, String body, List files) - throws IOException, CallException - { - return performHttpRequest(uri, callType, headers, parameters, body, files, 0); - } - private String transformPostParametersToDataString(Map params) throws UnsupportedEncodingException {