Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicroland committed Feb 15, 2017
2 parents 94c326b + b05e799 commit 576fdf5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 74 deletions.
2 changes: 1 addition & 1 deletion droid4me/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.7
2.4.8
4 changes: 2 additions & 2 deletions 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.7</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 Expand Up @@ -404,7 +404,7 @@
<id>deployment</id>
<name>Internal Releases SNAPSHOT</name>
<url>
http://developer.smartnsoft.com:8081/nexus/content/repositories/snapshots/
http://nexus2.smartnsoft.com/nexus/content/repositories/snapshots/
</url>
</snapshotRepository>
</distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public LayoutInflater getLayoutInflater()
@Override
public Object getSystemService(String name)
{
// if (Context.LAYOUT_INFLATER_SERVICE.equals(name) == true && getWindow() != null)
// {
// return droid4mizer.getSystemService(name, getWindow().getLayoutInflater());
// }
// else
if (Context.LAYOUT_INFLATER_SERVICE.equals(name) == true && getWindow() != null)
{
return droid4mizer.getSystemService(name, getWindow().getLayoutInflater());
}
else
{
return droid4mizer.getSystemService(name, super.getSystemService(name));
}
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 @@ -130,9 +126,10 @@ public HttpResponse runRequest(String uri, CallType callType, Map<String, String
{
httpURLConnection = performHttpRequest(uri, callType, headers, parameters, body, files);
final Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
final int statusCode = httpURLConnection.getResponseCode();
final InputStream inputStream = getContent(uri, callType, httpURLConnection);

return new HttpResponse(headerFields, inputStream);
return new HttpResponse(headerFields, statusCode, inputStream);
}
catch (CallException exception)
{
Expand All @@ -151,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 @@ -282,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 @@ -316,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 @@ -349,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
{
Expand All @@ -363,24 +373,24 @@ private HttpURLConnection performHttpRequest(String uri, CallType callType, Map<

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 @@ -403,19 +413,26 @@ 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);
}
}
else
{
logBuilder.append(transformPostParametersToDataString(paramaters));
}
}
else if (paramaters != null && paramaters.size() > 0)
{
Expand All @@ -429,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 @@ -442,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 @@ -452,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 @@ -471,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 @@ -483,17 +505,18 @@ 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();

if (file.fileInputStream != null)
if (file.inputStream != null)
{
int bytesRead;
final byte[] dataBuffer = new byte[1024];

while ((bytesRead = file.fileInputStream.read(dataBuffer)) != -1)
while ((bytesRead = file.inputStream.read(dataBuffer)) != -1)
{
outputStream.write(dataBuffer, 0, bytesRead);
}
Expand All @@ -503,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 @@ -516,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 @@ -541,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 @@ -562,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
28 changes: 12 additions & 16 deletions droid4me/src/com/smartnsoft/droid4me/ws/WebServiceCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,16 @@

package com.smartnsoft.droid4me.ws;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import com.smartnsoft.droid4me.log.Logger;
import com.smartnsoft.droid4me.log.LoggerFactory;
import org.json.JSONException;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.smartnsoft.droid4me.log.Logger;
import com.smartnsoft.droid4me.log.LoggerFactory;

import org.json.JSONException;

/**
* A basis class for making web service calls easier.
*
Expand All @@ -55,14 +48,14 @@ public static final class MultipartFile

public final String contentType;

public final FileInputStream fileInputStream;
public final InputStream inputStream;

public MultipartFile(String name, String fileName, String contentType, FileInputStream fileInputStream)
public MultipartFile(String name, String fileName, String contentType, InputStream inputStream)
{
this.name = name;
this.fileName = fileName;
this.contentType = contentType;
this.fileInputStream = fileInputStream;
this.inputStream = inputStream;
}

}
Expand All @@ -72,11 +65,14 @@ public static final class HttpResponse

public final Map<String, List<String>> headers;

public final int statusCode;

public final InputStream inputStream;

public HttpResponse(Map<String, List<String>> headers, InputStream inputStream)
public HttpResponse(Map<String, List<String>> headers, int statusCode, InputStream inputStream)
{
this.headers = headers;
this.statusCode = statusCode;
this.inputStream = inputStream;
}

Expand Down

0 comments on commit 576fdf5

Please sign in to comment.