You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2010/08/31 08:19:36 UTC

svn commit: r991088 - in /tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src: main/java/org/apache/tuscany/sca/binding/http/format/ test/java/org/apache/tuscany/sca/binding/http/ test/resources/

Author: antelder
Date: Tue Aug 31 06:19:36 2010
New Revision: 991088

URL: http://svn.apache.org/viewvc?rev=991088&view=rev
Log:
Start function for handling xml format 

Modified:
    tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java
    tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java
    tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite

Modified: tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java?rev=991088&r1=991087&r2=991088&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/format/HTTPXMLWireFormatServiceInterceptor.java Tue Aug 31 06:19:36 2010
@@ -45,25 +45,14 @@ import org.apache.tuscany.sca.runtime.Ru
 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 HTTPXMLWireFormatServiceInt
     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 HTTPXMLWireFormatServiceInt
 
     /**
      * 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<DataType> types = operation.getInputType().getLogical();
-//        int typesIndex = 0;
-//
-//        List<String> jsonRequestArray = new ArrayList<String>();
-//
-//        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<Object> xmlRequestArray = new ArrayList<Object>();
+        for (String name : getOrderedParameterNames(servletRequest)) {
+            xmlRequestArray.add(domHelper.load("<" + name + ">" + servletRequest.getParameter(name) + "</" + name + ">"));
+        }
+        return xmlRequestArray.toArray();
     }
 
     /**
@@ -194,10 +155,7 @@ public class HTTPXMLWireFormatServiceInt
                     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);
         }

Modified: tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java?rev=991088&r1=991087&r2=991088&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HelloworldTestCase.java Tue Aug 31 06:19:36 2010
@@ -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</return>"));
+        Assert.assertTrue(read(is).endsWith(">Hello Petra</return>"));
     }
 
     private static String read(InputStream is) throws IOException {

Modified: tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite?rev=991088&r1=991087&r2=991088&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-http-runtime/src/test/resources/helloworld.composite Tue Aug 31 06:19:36 2010
@@ -26,6 +26,7 @@
         <implementation.java class="org.apache.tuscany.sca.binding.http.HelloworldImpl"/>
         <service name="Helloworld">
     		<tuscany:binding.http />
+    		<binding.ws name="ws" />
     	</service>
     </component>