From 8ca06b026beb19466e7903642e817c9deb58b4e8 Mon Sep 17 00:00:00 2001 From: 4everalone Date: Thu, 20 Jun 2013 13:32:07 +0300 Subject: [PATCH 1/4] Possibility to add own pattern checker and possibility of transformation the date, because android sdk has not all of date patterns --- .../nano/transform/DateTransform.java | 60 +++++++++++++++---- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/leansoft/nano/transform/DateTransform.java b/src/main/java/com/leansoft/nano/transform/DateTransform.java index 214cf61..fa0e30b 100644 --- a/src/main/java/com/leansoft/nano/transform/DateTransform.java +++ b/src/main/java/com/leansoft/nano/transform/DateTransform.java @@ -7,6 +7,8 @@ import java.util.HashMap; import java.util.Map; import java.util.TimeZone; +import java.util.LinkedList; +import java.util.List; /** * Transformer between a string and a java.util.Date object @@ -14,7 +16,13 @@ * @author bulldog * */ -class DateTransform implements Transformable { +public class DateTransform implements Transformable { + + public static interface PatternChecker + { + public String getPattern(String text); + public String transform(String text); + } public static String FULL = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; @@ -25,9 +33,17 @@ class DateTransform implements Transformable { public static String SHORT = "yyyy-MM-dd"; public static String TIME_ZONE = "GMT"; + + private static final List patterns = new LinkedList(); + + public static void addPattern(PatternChecker checker) + { + patterns.add(checker); + } public Date read(String value) throws Exception { String pattern = getPattern(value); + value = transform(value); Date date = ThreadLocalDateFormatter.parse(value, pattern); return date; } @@ -36,20 +52,40 @@ public String write(Date value) throws Exception { String text = ThreadLocalDateFormatter.format(value, FULL); return text; } + + private static String transform(String text) { + String szRet = text; + for (PatternChecker checker : patterns) { + String val = checker.transform(text); + if (val != null) + { + szRet = val; + break; + } + } + return szRet; + } public static String getPattern(String text) { - int length = text.length(); + for (PatternChecker checker : patterns) { + String szPattern = checker.getPattern(text); + if (szPattern != null) + { + return szPattern; + } + } + int length = text.length(); - if(length > 23) { - return FULL; - } - if(length > 20) { - return LONG; - } - if(length > 11) { - return NORMAL; - } - return SHORT; + if(length > 23) { + return FULL; + } + if(length > 20) { + return LONG; + } + if(length > 11) { + return NORMAL; + } + return SHORT; } public static class ThreadLocalDateFormatter { From 00fc645e16ae1ea2a45ba969bf048638d423948a Mon Sep 17 00:00:00 2001 From: 4everalone Date: Thu, 20 Jun 2013 17:44:17 +0300 Subject: [PATCH 2/4] Support for canceling http requests --- .../java/com/leansoft/nano/ws/NanoSOAPClient.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java b/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java index 4be5ebb..da0fdba 100644 --- a/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java +++ b/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java @@ -9,6 +9,8 @@ import com.leansoft.nano.util.MapPrettyPrinter; import com.loopj.android.http.AsyncHttpClient; +import android.content.Context; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -36,6 +38,8 @@ public abstract class NanoSOAPClient { private List customSOAPHeaders = null; private AsyncHttpClient asyncHttpClient = null; + + private Context context = null; private final Map urlParams = new HashMap(); @@ -43,6 +47,11 @@ public NanoSOAPClient() { asyncHttpClient = new AsyncHttpClient(); asyncHttpClient.addHeader("Accept", "text/xml"); } + + public void setContext(Context context) + { + this.context = context; + } protected void invoke(Object requestObject, SOAPServiceCallback callback, Class bindClazz) { @@ -82,7 +91,7 @@ protected void invoke(Object requestObject, SOAPServiceCallback callback, Cla ALog.debugLongMessage(TAG, soapMessage); } - asyncHttpClient.post(null, urlWithQueryString, null, soapEntiry, contentType, soapHttpResponseHandler); + asyncHttpClient.post(context, urlWithQueryString, null, soapEntiry, contentType, soapHttpResponseHandler); } catch (Exception e) { ALog.e(TAG, "Fail to send request", e); From e5ed55c3d754b5c350bab67414155dc1a06a5a1c Mon Sep 17 00:00:00 2001 From: 4everalone Date: Fri, 21 Jun 2013 13:51:53 +0300 Subject: [PATCH 3/4] Handle soap responses and queries to get a possibility of logging --- .../com/leansoft/nano/ws/NanoSOAPClient.java | 20 ++++++++++++++++++- .../nano/ws/SOAPHttpResponseHandler.java | 10 ++++++++-- .../leansoft/nano/ws/SoapQueryHandler.java | 7 +++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/leansoft/nano/ws/SoapQueryHandler.java diff --git a/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java b/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java index da0fdba..dc1b8e0 100644 --- a/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java +++ b/src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java @@ -7,6 +7,8 @@ import com.leansoft.nano.impl.SOAPWriter; import com.leansoft.nano.log.ALog; import com.leansoft.nano.util.MapPrettyPrinter; +import com.leansoft.nano.ws.SoapQueryHandler; + import com.loopj.android.http.AsyncHttpClient; import android.content.Context; @@ -40,6 +42,7 @@ public abstract class NanoSOAPClient { private AsyncHttpClient asyncHttpClient = null; private Context context = null; + private SoapQueryHandler soapHandler; private final Map urlParams = new HashMap(); @@ -52,6 +55,16 @@ public void setContext(Context context) { this.context = context; } + + public void setSoapQueryHandler(SoapQueryHandler soapHandler) + { + this.soapHandler = soapHandler; + } + + public SoapQueryHandler getSoapQueryHandler(SoapQueryHandler soapHandler) + { + return soapHandler; + } protected void invoke(Object requestObject, SOAPServiceCallback callback, Class bindClazz) { @@ -76,12 +89,17 @@ protected void invoke(Object requestObject, SOAPServiceCallback callback, Cla StringEntity soapEntiry = new StringEntity(soapMessage, charset); - SOAPHttpResponseHandler soapHttpResponseHandler = new SOAPHttpResponseHandler(callback, bindClazz, soapVersion); + SOAPHttpResponseHandler soapHttpResponseHandler = new SOAPHttpResponseHandler(soapHandler, callback, bindClazz, soapVersion); soapHttpResponseHandler.setCharset(charset); soapHttpResponseHandler.setDebug(debug); String urlWithQueryString = NanoXMLClient.getUrlWithQueryString(endpointUrl, urlParams); + if (soapHandler != null) + { + soapHandler.handleRequest(urlWithQueryString, MapPrettyPrinter.printMap(asyncHttpClient.getHeaders()), soapMessage); + } + if (debug) { ALog.d(TAG, "Sending request to : " + urlWithQueryString); ALog.d(TAG, "Request HTTP headers : "); diff --git a/src/main/java/com/leansoft/nano/ws/SOAPHttpResponseHandler.java b/src/main/java/com/leansoft/nano/ws/SOAPHttpResponseHandler.java index e21a266..148cdf0 100644 --- a/src/main/java/com/leansoft/nano/ws/SOAPHttpResponseHandler.java +++ b/src/main/java/com/leansoft/nano/ws/SOAPHttpResponseHandler.java @@ -13,6 +13,7 @@ import android.os.Message; +import com.leansoft.nano.ws.SoapQueryHandler; import com.leansoft.nano.Format; import com.leansoft.nano.exception.UnmarshallException; import com.leansoft.nano.impl.SOAPReader; @@ -34,13 +35,15 @@ public class SOAPHttpResponseHandler extends AsyncHttpResponseHandler { private SOAPVersion soapVersion; private String charset; private boolean debug; + private SoapQueryHandler soapHandler; @SuppressWarnings("rawtypes") - public SOAPHttpResponseHandler(SOAPServiceCallback callback, Class bindClazz, SOAPVersion soapVersion) { + public SOAPHttpResponseHandler(SoapQueryHandler soapHandler, SOAPServiceCallback callback, Class bindClazz, SOAPVersion soapVersion) { super(); this.callback = callback; this.bindClazz = bindClazz; this.soapVersion = soapVersion; + this.soapHandler = soapHandler; } @Override @@ -154,7 +157,10 @@ protected void sendResponseMessage(HttpResponse response) { ALog.e(TAG, "error to get response body", e); return; } - + if (soapHandler != null) + { + soapHandler.handleResponse(status.getStatusCode(), MapPrettyPrinter.printMap(this.getHeaderMap(response)), responseBody); + } if (debug) { ALog.d(TAG, "Response HTTP status : " + status.getStatusCode()); Map headerMap = this.getHeaderMap(response); diff --git a/src/main/java/com/leansoft/nano/ws/SoapQueryHandler.java b/src/main/java/com/leansoft/nano/ws/SoapQueryHandler.java new file mode 100644 index 0000000..2fdb91b --- /dev/null +++ b/src/main/java/com/leansoft/nano/ws/SoapQueryHandler.java @@ -0,0 +1,7 @@ +package com.leansoft.nano.ws; + +public interface SoapQueryHandler +{ + public void handleRequest(String url, String httpHeaders, String soapMessage); + public void handleResponse(int status, String httpHeaders, String soapMessage); +} \ No newline at end of file From 71779b1ad546663ee90a29f1c2d4236a6948a621 Mon Sep 17 00:00:00 2001 From: 4everalone Date: Thu, 25 Jul 2013 16:28:41 +0300 Subject: [PATCH 4/4] StackOverFlow when trying to print to LogCat a huge SOAP response --- src/main/java/com/leansoft/nano/log/ALog.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/leansoft/nano/log/ALog.java b/src/main/java/com/leansoft/nano/log/ALog.java index 3baa762..0762951 100644 --- a/src/main/java/com/leansoft/nano/log/ALog.java +++ b/src/main/java/com/leansoft/nano/log/ALog.java @@ -49,10 +49,22 @@ public static void debugLongMessage(final String tag, String msg) { private static void println(final String tag, String msg) { if (msg == null) return; - int l = msg.length(); + + int length = msg.length(); int c = Log.println(Log.DEBUG, tag, msg); - if (c < l) { - println(tag, msg.substring(c+1)); + final int len = c; + while (c < length) + { + String str = null; + if (c + len > length) + { + str = msg.substring(c, length); + } + else + { + str = msg.substring(c, c + len); + } + c += Log.println(Log.DEBUG, tag, msg); } }