Thanks Ruwan!

Andreas

On 23 juin 08, at 19:41, [EMAIL PROTECTED] wrote:

Author: ruwan
Date: Mon Jun 23 10:41:11 2008
New Revision: 670682

URL: http://svn.apache.org/viewvc?rev=670682&view=rev
Log:
Fixing the issue SYNAPSE-365, REST support fixed

Modified:
synapse/trunk/java/modules/transports/src/main/java/org/apache/ synapse/transport/nhttp/ServerWorker.java synapse/trunk/java/modules/transports/src/main/java/org/apache/ synapse/transport/nhttp/util/RESTUtil.java

Modified: synapse/trunk/java/modules/transports/src/main/java/org/ apache/synapse/transport/nhttp/ServerWorker.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java?rev=670682&r1=670681&r2=670682&view=diff
= = = = = = = = ====================================================================== --- synapse/trunk/java/modules/transports/src/main/java/org/apache/ synapse/transport/nhttp/ServerWorker.java (original) +++ synapse/trunk/java/modules/transports/src/main/java/org/apache/ synapse/transport/nhttp/ServerWorker.java Mon Jun 23 10:41:11 2008
@@ -30,7 +30,6 @@
import org.apache.axis2.transport.RequestResponseTransport;
import org.apache.axis2.transport.http.HTTPTransportReceiver;
import org.apache.axis2.transport.http.HTTPTransportUtils;
-import org.apache.axis2.transport.http.util.RESTUtil;
import org.apache.axis2.util.MessageContextBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -38,6 +37,7 @@
import org.apache.http.nio.NHttpServerConnection;
import org.apache.http.protocol.HTTP;
import org.apache.synapse.transport.base.MetricsCollector;
+import org.apache.synapse.transport.nhttp.util.RESTUtil;
import org.apache.ws.commons.schema.XmlSchema;

import java.io.IOException;
@@ -76,6 +76,7 @@
    private OutputStream os = null;
    /** the metrics collector */
    private MetricsCollector metrics = null;
+
    private static final String SOAPACTION   = "SOAPAction";
    private static final String LOCATION     = "Location";
    private static final String CONTENT_TYPE = "Content-Type";
@@ -145,15 +146,14 @@
msgContext.setIncomingTransportName(Constants.TRANSPORT_HTTP);
        }
        msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, this);
- msgContext.setServiceGroupContextId(UUIDGenerator.getUUID()); // TODO check if this is valid
+        msgContext.setServiceGroupContextId(UUIDGenerator.getUUID());
        msgContext.setServerSide(true);
        msgContext.setProperty(
Constants.Configuration.TRANSPORT_IN_URL, request.getRequestLine().getUri());

-        Map headers = new HashMap();
-        Header[] headerArr = request.getAllHeaders();
-        for (int i = 0; i < headerArr.length; i++) {
- headers.put(headerArr[i].getName(), headerArr[i].getValue());
+        Map<String, String> headers = new HashMap<String, String>();
+        for (Header header : request.getAllHeaders()) {
+            headers.put(header.getName(), header.getValue());
        }
msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, headers);

@@ -259,14 +259,6 @@

        String uri = request.getRequestLine().getUri();

-        String contextPath = cfgCtx.getContextRoot();
-        if (!contextPath.startsWith("/")) {
-            contextPath = "/" + contextPath;
-        }
-        if (!contextPath.endsWith("/")) {
-            contextPath = contextPath + "/";
-        }
-
        String servicePath = cfgCtx.getServiceContextPath();
        if (!servicePath.startsWith("/")) {
            servicePath = "/" + servicePath;
@@ -292,9 +284,10 @@
            }
        }

-        Map parameters = new HashMap();
+ Map<String, String> parameters = new HashMap<String, String>();
        int pos = uri.indexOf("?");
        if (pos != -1) {
+ msgContext.setTo(new EndpointReference(uri.substring(0, pos))); StringTokenizer st = new StringTokenizer(uri.substring(pos+1), "&");
            while (st.hasMoreTokens()) {
                String param = st.nextToken();
@@ -305,6 +298,8 @@
                    parameters.put(param, null);
                }
            }
+        } else {
+            msgContext.setTo(new EndpointReference(uri));
        }

if ("GET".equalsIgnoreCase(request.getRequestLine().getMethod())) {
@@ -375,7 +370,7 @@

            } else {
                //cater for named xsds - check for the xsd name
-                String schemaName = (String) parameters.get("xsd");
+                String schemaName = parameters.get("xsd");
AxisService service = (AxisService) cfgCtx.getAxisConfiguration()
                    .getServices().get(serviceName);

@@ -439,9 +434,11 @@

            } else {
                try {
- Header contentType = request.getFirstHeader(HTTP.CONTENT_TYPE);
-                    RESTUtil.processURLRequest(msgContext, os,
- contentType != null ? contentType.getValue() : null);
+
+                    RESTUtil.processURLRequest(
+ msgContext, os, (request.getFirstHeader(SOAPACTION) != null ? + request.getFirstHeader(SOAPACTION).getValue() : null), + request.getRequestLine().getUri(), cfgCtx, parameters); // do not let the output stream close (as by default below) since // we are serving this GET request through the Synapse engine
                    return;
@@ -568,26 +565,26 @@
        if ((services != null) && !services.isEmpty()) {

            servicesFound = true;
-            Collection serviceCollection = services.values();
            resultBuf.append("<h2>" + "Deployed services" + "</h2>");

- for (Iterator it = serviceCollection.iterator(); it.hasNext();) {
+            for (Object service : services.values()) {

-                AxisService axisService = (AxisService) it.next();
+                AxisService axisService = (AxisService) service;
                if (axisService.getName().startsWith("__")) {
                    continue;    // skip private services
                }

                Iterator iterator = axisService.getOperations();
- resultBuf.append("<h3><a href=\"" + axisService.getName() + "?wsdl\">" +
-                        axisService.getName() + "</a></h3>");
+ resultBuf.append("<h3><a href= \"").append(axisService.getName()).append( + "?wsdl \">").append(axisService.getName()).append("</a></h3>");

                if (iterator.hasNext()) {
                    resultBuf.append("Available operations <ul>");

                    for (; iterator.hasNext();) {
AxisOperation axisOperation = (AxisOperation) iterator.next(); - resultBuf.append("<li>" + axisOperation.getName().getLocalPart() + "</li>");
+                        resultBuf.append("<li>").append(
+ axisOperation.getName().getLocalPart()).append("</li>");
                    }
                    resultBuf.append("</ul>");
                } else {
@@ -603,7 +600,8 @@

            while (faultyservices.hasMoreElements()) {
String faultyserviceName = (String) faultyservices.nextElement(); - resultBuf.append("<h3><font color=\"blue\">" + faultyserviceName + "</font></h3>");
+                resultBuf.append("<h3><font color=\"blue\">").append(
+                        faultyserviceName).append("</font></h3>");
            }
        }


Modified: synapse/trunk/java/modules/transports/src/main/java/org/ apache/synapse/transport/nhttp/util/RESTUtil.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/util/RESTUtil.java?rev=670682&r1=670681&r2=670682&view=diff
= = = = = = = = ====================================================================== --- synapse/trunk/java/modules/transports/src/main/java/org/apache/ synapse/transport/nhttp/util/RESTUtil.java (original) +++ synapse/trunk/java/modules/transports/src/main/java/org/apache/ synapse/transport/nhttp/util/RESTUtil.java Mon Jun 23 10:41:11 2008
@@ -20,14 +20,36 @@
package org.apache.synapse.transport.nhttp.util;

import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;
import org.apache.axis2.AxisFault;
+import org.apache.axis2.Constants;
+import org.apache.axis2.util.Utils;
+import org.apache.axis2.addressing.EndpointReference;
+import org.apache.axis2.i18n.Messages;
+import org.apache.axis2.builder.BuilderUtil;
import org.apache.axis2.context.MessageContext;
-import org.apache.axis2.description.WSDL20DefaultValueHolder;
-import org.apache.axis2.description.WSDL2Constants;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.description.*;
+import org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher;
+import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
+import org.apache.axis2.dispatchers.RequestURIOperationDispatcher;
+import org.apache.axis2.engine.AxisEngine;
+import org.apache.axis2.engine.Handler;
+import org.apache.axis2.transport.TransportUtils;
+import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.util.URIEncoderDecoder;

+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import java.io.IOException;
+import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.io.InputStream;
import java.util.Iterator;
+import java.util.Map;

/**
* This class provides a set of utility methods to manage the REST invocation calls
@@ -38,14 +60,14 @@
    /**
* This method will return the URI part for the GET HTTPRequest by converting
     * the SOAP infoset to the URL-encoded GET format
-     *
+     *
* @param messageContext - from which the SOAP infoset will be extracted to encode
     * @param address        - address of the actual service
     * @return uri       - ERI of the GET request
* @throws AxisFault - if the SOAP infoset cannot be converted in to the GET URL-encoded format
     */
public static String getURI(MessageContext messageContext, String address) throws AxisFault {
-
+
        OMElement firstElement;
        address = address.substring(address.indexOf("//") + 2);
        address = address.substring(address.indexOf("/"));
@@ -69,7 +91,7 @@
            Iterator iter = firstElement.getChildElements();

            String legalCharacters = WSDL2Constants
- .LEGAL_CHARACTERS_IN_QUERY .replaceAll(queryParameterSeparator, ""); + .LEGAL_CHARACTERS_IN_QUERY .replaceAll(queryParameterSeparator, "");
            StringBuffer buff = new StringBuffer(params);

// iterate through the child elements and find the request parameters
@@ -77,11 +99,11 @@
                OMElement element = (OMElement) iter.next();
                try {
buff.append(URIEncoderDecoder.quoteIllegal(element.getLocalName(), - legalCharacters )).append ("=").append(URIEncoderDecoder.quoteIllegal(element.getText(), - legalCharacters)).append(queryParameterSeparator); + legalCharacters )).append ("=").append(URIEncoderDecoder.quoteIllegal(element.getText(), + legalCharacters)).append(queryParameterSeparator);
                } catch (UnsupportedEncodingException e) {
throw new AxisFault("URI Encoding error : " + element.getLocalName()
-                        + "=" + element.getText(), e);
+                            + "=" + element.getText(), e);
                }
            }

@@ -99,10 +121,99 @@

            } else {
                address = address
- + queryParameterSeparator + params.substring(0, params.length() - 1); + + queryParameterSeparator + params.substring(0, params.length() - 1);
            }
        }
-
+
        return address;
    }
+
+    /**
+ * Processes the HTTP GET request and builds the SOAP info-set of the REST message
+     *
+     * @param msgContext The MessageContext of the Request Message
+     * @param out The output stream of the response
+     * @param soapAction SoapAction of the request
+     * @param requestURI The URL that the request came to
+     * @param configurationContext The Axis Configuration Context
+     * @param requestParameters The parameters of the request message
+ * @return boolean indication whether the operation was succesfull
+     * @throws AxisFault - Thrown in case a fault occurs
+     */
+ public static boolean processURLRequest(MessageContext msgContext, OutputStream out, + String soapAction, String requestURI, ConfigurationContext configurationContext,
+        Map requestParameters) throws AxisFault {
+
+ if ((soapAction != null) && soapAction.startsWith("\"") && soapAction.endsWith("\"")) { + soapAction = soapAction.substring(1, soapAction.length() - 1);
+        }
+
+        msgContext.setSoapAction(soapAction);
+        msgContext.setTo(new EndpointReference(requestURI));
+        msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
+        msgContext.setServerSide(true);
+        SOAPEnvelope envelope = createEnvelopeFromGetRequest(
+                requestURI, requestParameters, configurationContext);
+
+        if (envelope == null) {
+            return false;
+        } else {
+            msgContext.setDoingREST(true);
+            msgContext.setEnvelope(envelope);
+            AxisEngine.receive(msgContext);
+            return true;
+        }
+    }
+
+    /**
+ * Creates the [EMAIL PROTECTED] SOAPEnvelope} from the GET URL request. REST message building inside
+     * synapse will be handled in this manner
+     *
+     * @param requestUrl GET URL of the request
+     * @param map query parameters of the GET request
+     * @param configCtx axis configuration context
+     * @return created SOAPEnvelope or null if cannot be processed
+ * @throws AxisFault if the service represented by the GET request URL cannot be found
+     */
+ public static SOAPEnvelope createEnvelopeFromGetRequest(String requestUrl, Map map,
+        ConfigurationContext configCtx) throws AxisFault {
+
+ String[] values = Utils.parseRequestURLForServiceAndOperation(
+                requestUrl, configCtx.getServiceContextPath());
+
+        if (values == null) {
+            return new SOAP11Factory().getDefaultEnvelope();
+        }
+
+        if ((values[1] != null) && (values[0] != null)) {
+            String srvice = values[0];
+ AxisService service = configCtx.getAxisConfiguration().getService(srvice);
+            if (service == null) {
+                throw new AxisFault("service not found: " + srvice);
+            }
+            String operation = values[1];
+            SOAPFactory soapFactory = new SOAP11Factory();
+            SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
+ OMNamespace omNs = soapFactory.createOMNamespace(service.getSchemaTargetNamespace(),
+                    service.getSchemaTargetNamespacePrefix());
+ soapFactory.createOMNamespace(service.getSchemaTargetNamespace(),
+                    service.getSchemaTargetNamespacePrefix());
+ OMElement opElement = soapFactory.createOMElement(operation, omNs);
+
+            for (Object o : map.keySet()) {
+                String name = (String) o;
+                String value = (String) map.get(name);
+ OMElement omEle = soapFactory.createOMElement(name, omNs);
+
+                omEle.setText(value);
+                opElement.addChild(omEle);
+            }
+
+            envelope.getBody().addChild(opElement);
+
+            return envelope;
+        } else {
+            return null;
+        }
+    }
}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to