From 11e1255139e74b2e7baaeaa80354b91d09fd80d9 Mon Sep 17 00:00:00 2001 From: lresende Date: Tue, 11 May 2010 17:44:59 +0000 Subject: Considering request encoding when processing body git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@943205 13f79535-47bb-0310-9956-ffa450edef68 --- .../json/provider/JSONWireFormatInterceptor.java | 58 ++++++++++++++++------ 1 file changed, 42 insertions(+), 16 deletions(-) (limited to 'sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json') diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java index 7e47d5c04d..e73ec961b3 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java @@ -52,28 +52,21 @@ public class JSONWireFormatInterceptor implements Interceptor { this.next = next; } - private String read(InputStream in) throws IOException { - StringWriter sw = new StringWriter(); - InputStreamReader reader = new InputStreamReader(in, "UTF-8"); - char[] buf = new char[8192]; - while (true) { - int size = reader.read(buf); - if (size < 0) { - break; - } - sw.write(buf, 0, size); - } - return sw.toString(); - } - public Message invoke(Message msg) { HTTPContext bindingContext = (HTTPContext) msg.getBindingContext(); + // Decode using the charset in the request if it exists otherwise + // use UTF-8 as this is what all browser implementations use. + String charset = bindingContext.getHttpRequest().getCharacterEncoding(); + if (charset == null) { + charset = "UTF-8"; + } + try { - if(bindingContext.getHttpRequest().getMethod().equalsIgnoreCase("get") == false && bindingContext.getHttpRequest().getMethod().equalsIgnoreCase("delete") == false && msg.getBody() != null) { + if( isPayloadSupported(bindingContext.getHttpRequest().getMethod()) && msg.getBody() != null) { Object[] args = msg.getBody(); InputStream in = (InputStream) args[0]; - String data = read(in); + String data = read(in, charset); JSONObject jsonPayload = new JSONObject(data); msg.setBody(new Object[]{jsonPayload}); } @@ -83,5 +76,38 @@ public class JSONWireFormatInterceptor implements Interceptor { return getNext().invoke(msg); } + + /** + * Check if HTTP Operation should support payload + * @param operation + * @return + */ + private static boolean isPayloadSupported(String operation) { + boolean isGet = "get".equalsIgnoreCase(operation); + boolean isDelete = "delete".equalsIgnoreCase(operation); + + return isGet == false && isDelete == false; + } + + /** + * Read JSON payload from HTTP Request Body + * @param in + * @param charset + * @return + * @throws IOException + */ + private static String read(InputStream in, String charset) throws IOException { + StringWriter sw = new StringWriter(); + InputStreamReader reader = new InputStreamReader(in, "UTF-8"); + char[] buf = new char[8192]; + while (true) { + int size = reader.read(buf); + if (size < 0) { + break; + } + sw.write(buf, 0, size); + } + return sw.toString(); + } } -- cgit v1.2.3