diff --git a/droid4me/VERSION.txt b/droid4me/VERSION.txt index 48a6b508..752a79ef 100755 --- a/droid4me/VERSION.txt +++ b/droid4me/VERSION.txt @@ -1 +1 @@ -2.4.7 \ 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 6f175df7..5f6d6359 100644 --- a/droid4me/pom.xml +++ b/droid4me/pom.xml @@ -10,7 +10,7 @@ com.smartnsoft droid4me droid4me - 2.4.7 + 2.4.8 jar droid4me is a framework library dedicated to the development of Android applications. https://github.com/smartnsoft/droid4me @@ -404,7 +404,7 @@ deployment Internal Releases SNAPSHOT - http://developer.smartnsoft.com:8081/nexus/content/repositories/snapshots/ + http://nexus2.smartnsoft.com/nexus/content/repositories/snapshots/ diff --git a/droid4me/src/com/smartnsoft/droid4me/support/v7/app/SmartAppCompatActivity.java b/droid4me/src/com/smartnsoft/droid4me/support/v7/app/SmartAppCompatActivity.java index 043af9dd..71d5efc5 100644 --- a/droid4me/src/com/smartnsoft/droid4me/support/v7/app/SmartAppCompatActivity.java +++ b/droid4me/src/com/smartnsoft/droid4me/support/v7/app/SmartAppCompatActivity.java @@ -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)); } diff --git a/droid4me/src/com/smartnsoft/droid4me/ws/URLConnectionWebServiceCaller.java b/droid4me/src/com/smartnsoft/droid4me/ws/URLConnectionWebServiceCaller.java index 5566fc23..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}. @@ -130,9 +126,10 @@ public HttpResponse runRequest(String uri, CallType callType, Map> 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) { @@ -151,6 +148,10 @@ public HttpResponse runRequest(String uri, CallType callType, Map20X. The default implementation logs the problem and throws an exception. * @@ -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); } } @@ -316,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. *

@@ -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 { @@ -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) @@ -403,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()); } @@ -411,11 +422,17 @@ 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); } } + else + { + logBuilder.append(transformPostParametersToDataString(paramaters)); + } } else if (paramaters != null && paramaters.size() > 0) { @@ -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) { @@ -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("\""); } } } @@ -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) { @@ -471,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); @@ -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); } @@ -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(); @@ -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(); @@ -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); } @@ -562,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 { diff --git a/droid4me/src/com/smartnsoft/droid4me/ws/WebServiceCaller.java b/droid4me/src/com/smartnsoft/droid4me/ws/WebServiceCaller.java index e252fbca..a0afaa49 100644 --- a/droid4me/src/com/smartnsoft/droid4me/ws/WebServiceCaller.java +++ b/droid4me/src/com/smartnsoft/droid4me/ws/WebServiceCaller.java @@ -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. * @@ -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; } } @@ -72,11 +65,14 @@ public static final class HttpResponse public final Map> headers; + public final int statusCode; + public final InputStream inputStream; - public HttpResponse(Map> headers, InputStream inputStream) + public HttpResponse(Map> headers, int statusCode, InputStream inputStream) { this.headers = headers; + this.statusCode = statusCode; this.inputStream = inputStream; }