From 9376cf7266b7b264879cec02a48555eaa9a20299 Mon Sep 17 00:00:00 2001 From: antelder Date: Tue, 31 Aug 2010 06:19:36 +0000 Subject: Start function for handling xml format git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@991088 13f79535-47bb-0310-9956-ffa450edef68 --- .../HTTPXMLWireFormatServiceInterceptor.java | 68 +++++----------------- .../sca/binding/http/HelloworldTestCase.java | 2 +- .../src/test/resources/helloworld.composite | 1 + 3 files changed, 15 insertions(+), 56 deletions(-) (limited to 'sca-java-2.x') diff --git a/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java b/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java index c326b0f355..8bd8b8b9f0 100644 --- a/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java +++ b/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java @@ -45,25 +45,14 @@ import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.oasisopen.sca.ServiceRuntimeException; import org.w3c.dom.Element; import org.w3c.dom.Node; +import org.xml.sax.SAXException; /** * Handles the xml wire format for the http binding - * - * 1- determine the request and response format (xml, json, etc) from the - * binding config or content type header and accept headers - * - TODO: need a way to configure the databinding framework based on that format - * 2- get the request contents from the HttpServletRequest - * - for a post its just the request body - * - for a get need to convert the query string into a body based on the format (xml, json, etc) - * 3- send the request on down the wire - * 4- set the response contents in the HttpServletResponse - * (the databinding should already have put it in the correct format) - * */ public class HTTPXMLWireFormatServiceInterceptor implements Interceptor { private Invoker next; - private String jsonpCallbackName = "callback"; private DOMHelper domHelper; public HTTPXMLWireFormatServiceInterceptor(RuntimeEndpoint endpoint, DOMHelper domHelper) { @@ -84,27 +73,28 @@ public class HTTPXMLWireFormatServiceInterceptor implements Interceptor { public Message invoke(Message msg) { try { return invokeResponse(getNext().invoke(invokeRequest(msg))); - } catch (IOException e) { + } catch (Exception e) { throw new ServiceRuntimeException(e); } } - private Message invokeRequest(Message msg) throws IOException { + private Message invokeRequest(Message msg) throws IOException, SAXException { HTTPContext context = msg.getBindingContext(); HttpServletRequest servletRequest = context.getRequest(); if ("GET".equals(servletRequest.getMethod())) { msg.setBody(getRequestFromQueryString(msg.getOperation(), servletRequest)); } else { - msg.setBody(read(servletRequest)); + msg.setBody(new Object[]{domHelper.load(read(servletRequest))}); } return msg; } private Message invokeResponse(Message msg) throws IOException { HTTPContext context = msg.getBindingContext(); - HttpServletRequest servletRequest = context.getRequest(); HttpServletResponse servletResponse = context.getResponse(); + servletResponse.setContentType("text/xml"); + Object o = msg.getBody(); if (msg.isFault()) { String xml = domHelper.saveAsString((Node)((FaultException)o).getFaultInfo()); @@ -126,42 +116,13 @@ public class HTTPXMLWireFormatServiceInterceptor implements Interceptor { /** * Turn the query request into XML. - * - * From ML thread: http://apache.markmail.org/message/ix3vvyomronellmi - * 1- if the binding configuration contains a mapping from query parameter name to operation parameter then use that. - * 2- if the service interface or impl uses jaxrs annotations to name the parameters then use that mapping - * 3- if the query parameters are name arg0, arg1 etc than use those names for the mapping, - * 4- otherwise use the order in the query string. */ - protected Object[] getRequestFromQueryString(Operation operation, ServletRequest servletRequest) { - -// List types = operation.getInputType().getLogical(); -// int typesIndex = 0; -// -// List jsonRequestArray = new ArrayList(); -// -// for (String name : getOrderedParameterNames(servletRequest)) { -// String jsonRequest = ""; -// // quote string parameters so clients work in the usual javascript way -// if (typesIndex < types.size() && String.class.equals(types.get(typesIndex).getGenericType())) { -// String x = servletRequest.getParameter(name); -// if (x.startsWith("\"") || x.startsWith("'")) { -// jsonRequest += x; -// } else { -// if (x.contains("\"")) { -// jsonRequest += "'" + x + "'"; -// } else { -// jsonRequest += "\"" + x + "\""; -// } -// } -// } else { -// jsonRequest += servletRequest.getParameter(name); -// } -// jsonRequestArray.add(jsonRequest); -// } -// -// return jsonRequestArray.toArray(); - return new Object[operation.getInputType().getLogical().size()]; + protected Object[] getRequestFromQueryString(Operation operation, ServletRequest servletRequest) throws IOException, SAXException { + List xmlRequestArray = new ArrayList(); + for (String name : getOrderedParameterNames(servletRequest)) { + xmlRequestArray.add(domHelper.load("<" + name + ">" + servletRequest.getParameter(name) + "")); + } + return xmlRequestArray.toArray(); } /** @@ -194,10 +155,7 @@ public class HTTPXMLWireFormatServiceInterceptor implements Interceptor { return i - j; }}); for (String name : parameterNames) { - // ignore system and jsonpCallbackName parameters - if (!name.startsWith("_") && !name.equals(jsonpCallbackName)) { - sortedNames.add(name); - } + sortedNames.add(name); } orderedNames.addAll(sortedNames); } diff --git a/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java b/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java index c7f2403af6..89598b91c7 100644 --- a/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java @@ -70,7 +70,7 @@ public class HelloworldTestCase { public void testXml() throws Exception { URL url = new URL("http://localhost:8080/HelloworldXmlComponent/Helloworld/sayHello?arg0=Petra"); InputStream is = url.openStream(); - Assert.assertTrue(read(is).endsWith(">Hello null")); + Assert.assertTrue(read(is).endsWith(">Hello Petra")); } private static String read(InputStream is) throws IOException { diff --git a/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite b/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite index 7497cfcbac..d836ed3860 100644 --- a/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite +++ b/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite @@ -26,6 +26,7 @@ + -- cgit v1.2.3