Skip to content

Commit

Permalink
RED-149 Rollback the URLConnectionWeServiceCaller + updated the versi…
Browse files Browse the repository at this point in the history
…on name to 2.4.8
  • Loading branch information
ludovicroland committed Feb 15, 2017
1 parent 82e4aa8 commit b05e799
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion droid4me/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.8.007-SNAPSHOT
2.4.8
2 changes: 1 addition & 1 deletion droid4me/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<groupId>com.smartnsoft</groupId>
<artifactId>droid4me</artifactId>
<name>droid4me</name>
<version>2.4.8.007-SNAPSHOT</version>
<version>2.4.8</version>
<packaging>jar</packaging>
<description>droid4me is a framework library dedicated to the development of Android applications.</description>
<url>https://github.com/smartnsoft/droid4me</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -152,6 +148,10 @@ public HttpResponse runRequest(String uri, CallType callType, Map<String, String
}
}

protected abstract int getReadTimeout();

protected abstract int getConnectTimeout();

/**
* Invoked when the result of the HTTP request is not <code>20X</code>. The default implementation logs the problem and throws an exception.
*
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -317,6 +318,13 @@ protected InputStream getContent(String uri, CallType callType, HttpURLConnectio
return null;
}

protected HttpURLConnection performHttpRequest(String uri, CallType callType, Map<String, String> headers,
Map<String, String> parameters, String body, List<MultipartFile> 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.
* <p/>
Expand Down Expand Up @@ -350,39 +358,39 @@ 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
{
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
}
}

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)
Expand All @@ -405,16 +413,19 @@ private HttpURLConnection performHttpRequest(String uri, CallType callType, Map<
{
for (final Entry<String, String> 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());
}
}

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);
}
}
Expand All @@ -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)
{
Expand All @@ -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("\"");
}
}
}
Expand All @@ -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)
{
Expand All @@ -477,8 +491,10 @@ else if (paramaters != null && paramaters.size() > 0)
{
for (final Entry<String, String> 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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -568,13 +590,6 @@ else if (paramaters != null && paramaters.size() > 0)
return httpURLConnection;
}

protected HttpURLConnection performHttpRequest(String uri, CallType callType, Map<String, String> headers,
Map<String, String> parameters, String body, List<MultipartFile> files)
throws IOException, CallException
{
return performHttpRequest(uri, callType, headers, parameters, body, files, 0);
}

private String transformPostParametersToDataString(Map<String, String> params)
throws UnsupportedEncodingException
{
Expand Down

0 comments on commit b05e799

Please sign in to comment.