diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-05-11 17:44:59 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-05-11 17:44:59 +0000 |
commit | 11e1255139e74b2e7baaeaa80354b91d09fd80d9 (patch) | |
tree | 3dad177b6fabf327c5b2cc68a8112d6c55796ba7 /sca-java-2.x/trunk | |
parent | 51cbd5d5fbdf1cec3437db653fb9bc92de6717c1 (diff) |
Considering request encoding when processing body
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@943205 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk')
2 files changed, 54 insertions, 18 deletions
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(); + } } diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java index 07fdc57082..3ece7c07a3 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java @@ -24,6 +24,7 @@ import java.io.InputStream; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.common.http.HTTPContext; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.invocation.Interceptor; @@ -54,12 +55,21 @@ public class XMLWireFormatInterceptor implements Interceptor { this.next = next; } - public Message invoke(Message msg) { + 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(msg.getBody() != null) { Object[] args = msg.getBody(); InputStream data = (InputStream) args[0]; - XMLStreamReader xmlPayload = inputFactory.createXMLStreamReader(data); + XMLStreamReader xmlPayload = inputFactory.createXMLStreamReader(data, charset); msg.setBody(new Object[]{xmlPayload}); } } catch(Exception e) { |