Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom date format #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/main/java/com/leansoft/nano/log/ALog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
60 changes: 48 additions & 12 deletions src/main/java/com/leansoft/nano/transform/DateTransform.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
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
*
* @author bulldog
*
*/
class DateTransform implements Transformable<Date> {
public class DateTransform implements Transformable<Date> {

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'";

Expand All @@ -25,9 +33,17 @@ class DateTransform implements Transformable<Date> {
public static String SHORT = "yyyy-MM-dd";

public static String TIME_ZONE = "GMT";

private static final List<PatternChecker> patterns = new LinkedList<PatternChecker>();

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;
}
Expand All @@ -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 {
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/com/leansoft/nano/ws/NanoSOAPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
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;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -36,13 +40,31 @@ public abstract class NanoSOAPClient {
private List<Object> customSOAPHeaders = null;

private AsyncHttpClient asyncHttpClient = null;

private Context context = null;
private SoapQueryHandler soapHandler;

private final Map<String, String> urlParams = new HashMap<String, String>();

public NanoSOAPClient() {
asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.addHeader("Accept", "text/xml");
}

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) {

Expand All @@ -67,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 : ");
Expand All @@ -82,7 +109,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);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/leansoft/nano/ws/SOAPHttpResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<String, String> headerMap = this.getHeaderMap(response);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/leansoft/nano/ws/SoapQueryHandler.java
Original file line number Diff line number Diff line change
@@ -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);
}