Author: supun
Date: Fri Jul 29 06:47:03 2011
New Revision: 1152123

URL: http://svn.apache.org/viewvc?rev=1152123&view=rev
Log:
adding support for defining endpoints at the url level, the endpoints 
configuration will be in a separate file

Added:
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/Endpoint.java
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/EndpointsConfiguration.java
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointFactory.java
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointsConfigurationFactory.java
Modified:
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ListenerContext.java
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java
    
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/util/RESTUtil.java

Modified: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ListenerContext.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ListenerContext.java?rev=1152123&r1=1152122&r2=1152123&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ListenerContext.java
 (original)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ListenerContext.java
 Fri Jul 29 06:47:03 2011
@@ -40,6 +40,8 @@ import org.apache.synapse.commons.evalua
 import org.apache.synapse.commons.executors.ExecutorConstants;
 import org.apache.synapse.commons.executors.PriorityExecutor;
 import org.apache.synapse.commons.executors.config.PriorityExecutorFactory;
+import org.apache.synapse.transport.nhttp.endpoints.EndpointsConfiguration;
+import 
org.apache.synapse.transport.nhttp.endpoints.config.EndpointsConfigurationFactory;
 import org.apache.synapse.transport.nhttp.util.NhttpMetricsCollector;
 
 import javax.xml.namespace.QName;
@@ -81,6 +83,8 @@ public class ListenerContext {
 
     private HttpParams params = null;
 
+    /** Endpoints configuration for specific HTTP Urls */
+    private EndpointsConfiguration endpoints = null;
 
     public ListenerContext(ConfigurationContext cfgCtx,
                            TransportInDescription transportIn,
@@ -149,6 +153,12 @@ public class ListenerContext {
         }
 
         params = getListenerParameters();
+
+
+        param = 
transportIn.getParameter(NhttpConstants.ENDPOINTS_CONFIGURATION);
+        if (param != null && param.getValue() != null) {
+            endpoints = new 
EndpointsConfigurationFactory().create(param.getValue().toString());
+        }
     }
 
 /**
@@ -303,6 +313,10 @@ public class ListenerContext {
         return params;
     }
 
+    public EndpointsConfiguration getEndpoints() {
+        return endpoints;
+    }
+
     private void handleException(String msg, Exception e) throws AxisFault {
         log.error(msg, e);
         throw new AxisFault(msg, e);

Modified: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java?rev=1152123&r1=1152122&r2=1152123&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java
 (original)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java
 Fri Jul 29 06:47:03 2011
@@ -142,4 +142,5 @@ public class NhttpConstants {
     /** Use to flag a connection close */
     public static final String FORCE_CONNECTION_CLOSE = 
"FORCE_CONNECTION_CLOSE";
     public static final String PORT_OFFSET = "portOffset";
+    public static final String ENDPOINTS_CONFIGURATION = 
"endpointsConfiguration";
 }

Modified: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java?rev=1152123&r1=1152122&r2=1152123&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java
 (original)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ServerWorker.java
 Fri Jul 29 06:47:03 2011
@@ -21,12 +21,14 @@ package org.apache.synapse.transport.nht
 import org.apache.axiom.util.UIDGenerator;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
+import org.apache.axis2.builder.Builder;
 import org.apache.axis2.builder.BuilderUtil;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.Parameter;
 import org.apache.axis2.engine.AxisEngine;
 import org.apache.axis2.transport.RequestResponseTransport;
+import org.apache.axis2.transport.TransportUtils;
 import org.apache.axis2.transport.base.MetricsCollector;
 import org.apache.axis2.transport.http.HTTPTransportUtils;
 import org.apache.axis2.util.MessageContextBuilder;
@@ -35,6 +37,8 @@ import org.apache.commons.logging.LogFac
 import org.apache.http.*;
 import org.apache.http.nio.NHttpServerConnection;
 import org.apache.http.protocol.HTTP;
+import org.apache.synapse.transport.nhttp.endpoints.Endpoint;
+import org.apache.synapse.transport.nhttp.endpoints.EndpointsConfiguration;
 import org.apache.synapse.transport.nhttp.util.NhttpUtil;
 import org.apache.synapse.transport.nhttp.util.RESTUtil;
 
@@ -78,6 +82,8 @@ public class ServerWorker implements Run
     private boolean isRestDispatching = true;
     /** WSDL processor for Get requests */
     private HttpGetRequestProcessor httpGetRequestProcessor = null;
+
+    private EndpointsConfiguration endpointsConfiguration = null;
     
     private static final String SOAPACTION   = "SOAPAction";
     private static final String LOCATION     = "Location";
@@ -122,6 +128,7 @@ public class ServerWorker implements Run
         this.msgContext = createMessageContext(request);
         this.isRestDispatching = listenerContext.isRestDispatching();
         this.httpGetRequestProcessor = 
listenerContext.getHttpGetRequestProcessor();
+        this.endpointsConfiguration = listenerContext.getEndpoints();
     }
 
     /**
@@ -397,22 +404,48 @@ public class ServerWorker implements Run
             String charSetEncoding = 
BuilderUtil.getCharSetEncoding(contentTypeStr);
             msgContext.setProperty(
                     Constants.Configuration.CHARACTER_SET_ENCODING, 
charSetEncoding);
+            boolean eprFound = false;
+            if (endpointsConfiguration != null) {
+                Endpoint epr = 
endpointsConfiguration.getEndpoint(request.getRequestLine().getUri());
+                if (epr != null) {
+                    eprFound = true;
+                    String type = 
TransportUtils.getContentType(contentTypeStr, msgContext);
+                    
msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE, type);
+                    epr.setParameters(msgContext);
+
+                    Builder builder = epr.getBuilder(type);
+                    if (HTTPTransportUtils.isRESTRequest(contentTypeStr)) {
+                        RESTUtil.processPOSTRequest(msgContext, is, os,
+                                request.getRequestLine().getUri(), 
contentType, builder, isRestDispatching);
+                    } else {
+
+                        Header soapAction = request.getFirstHeader(SOAPACTION);
+                        HTTPTransportUtils.processHTTPPostRequest(
+                                msgContext, is,
+                                os,
+                                contentTypeStr, builder,
+                                (soapAction != null ? soapAction.getValue() : 
null),
+                                request.getRequestLine().getUri());
+                    }
+                }
+            }
 
+            if (!eprFound) {
+                if (HTTPTransportUtils.isRESTRequest(contentTypeStr)) {
+                    RESTUtil.processPOSTRequest(msgContext, is, os,
+                            request.getRequestLine().getUri(), contentType, 
isRestDispatching);
+                } else {
 
-            if (HTTPTransportUtils.isRESTRequest(contentTypeStr)) {
-                RESTUtil.processPOSTRequest(msgContext, is, os,
-                        request.getRequestLine().getUri(), contentType, 
isRestDispatching);
-            } else {
+                    Header soapAction = request.getFirstHeader(SOAPACTION);
+                    HTTPTransportUtils.processHTTPPostRequest(
+                            msgContext, is,
+                            os,
+                            contentTypeStr,
+                            (soapAction != null ? soapAction.getValue() : 
null),
+                            request.getRequestLine().getUri());
+                }
 
-                Header soapAction  = request.getFirstHeader(SOAPACTION);
-                HTTPTransportUtils.processHTTPPostRequest(
-                        msgContext, is,
-                        os,
-                        contentTypeStr,
-                        (soapAction != null  ? soapAction.getValue()  : null),
-                        request.getRequestLine().getUri());
             }
-
         } catch (AxisFault e) {
             handleException("Error processing POST request ", e);
         }
@@ -451,9 +484,33 @@ public class ServerWorker implements Run
      */
     private void processGetAndDelete(String method) {
         try {
-            RESTUtil.processGetAndDeleteRequest(
-                    msgContext, os, request.getRequestLine().getUri(),
-                    request.getFirstHeader(HTTP.CONTENT_TYPE), method, 
isRestDispatching);
+            Header contentType = request.getFirstHeader(HTTP.CONTENT_TYPE);
+            String contentTypeStr = contentType != null ?
+                    contentType.getValue() : inferContentType();
+
+            boolean eprFound = false;
+            if (endpointsConfiguration != null) {
+                Endpoint epr = 
endpointsConfiguration.getEndpoint(request.getRequestLine().getUri());
+                if (epr != null) {
+                    eprFound = true;
+                    String type = 
TransportUtils.getContentType(contentTypeStr, msgContext);
+                    
msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE, type);
+
+                    epr.setParameters(msgContext);
+
+                    Builder builder = epr.getBuilder(type);
+                    RESTUtil.processGetAndDeleteRequest(
+                            msgContext, os, request.getRequestLine().getUri(),
+                            request.getFirstHeader(HTTP.CONTENT_TYPE), builder,
+                            method, isRestDispatching);
+                }
+            }
+
+            if (!eprFound) {
+                RESTUtil.processGetAndDeleteRequest(
+                        msgContext, os, request.getRequestLine().getUri(),
+                        request.getFirstHeader(HTTP.CONTENT_TYPE), method, 
isRestDispatching);
+            }
             // do not let the output stream close (as by default below) since
             // we are serving this GET/DELETE request through the Synapse 
engine
         } catch (AxisFault axisFault) {

Added: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/Endpoint.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/Endpoint.java?rev=1152123&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/Endpoint.java
 (added)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/Endpoint.java
 Fri Jul 29 06:47:03 2011
@@ -0,0 +1,98 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.transport.nhttp.endpoints;
+
+import org.apache.axis2.builder.Builder;
+import org.apache.axis2.builder.SOAPBuilder;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.Parameter;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Represents an URL endpoint. A url endpoint will have a set of message 
builders and parameters.
+ * When a request comes, it will be matched against the URL pattern to see 
weather it is for
+ * this endpoint. If the request is for this endpoint it will be processed by 
the message builders
+ * in this endpoint. If a message builder is not configured for this endpoint, 
the request will
+ * be processed by the global builders.
+ */
+public class Endpoint {
+    private Pattern urlPattern;
+
+    private Map<String, Builder> messageBuilders = new HashMap<String, 
Builder>();
+
+    private List<Parameter> parameters = new ArrayList<Parameter>();
+
+    private Builder defaultBuilder = new SOAPBuilder();
+
+    public Endpoint(Pattern urlPattern) {
+        this.urlPattern = urlPattern;
+    }
+
+    public boolean isMatching(String url) {
+        Matcher matcher = urlPattern.matcher(url);
+        return matcher.matches();
+    }
+
+    public void addBuilder(String name, Builder builder) {
+        messageBuilders.put(name, builder);
+    }
+
+    public void addParameter(Parameter parameter) {
+        parameters.add(parameter);
+    }
+
+    public Parameter getParameter(String name) {
+        for (Parameter p : parameters) {
+            if (p.getName().equals(name)) {
+                return p;
+            }
+        }
+        return null;
+    }
+
+    public List<Parameter> getParameters() {
+        return parameters;
+    }
+
+    public void setDefaultBuilder(Builder defaultBuilder) {
+        this.defaultBuilder = defaultBuilder;
+    }
+
+    public Builder getBuilder(String contentType) {
+        Builder builder = messageBuilders.get(contentType);
+        if (builder == null) {
+            return defaultBuilder;
+        }
+
+        return builder;
+    }
+
+    public void setParameters(MessageContext msgCtx) {
+        for (Parameter p : parameters) {
+            msgCtx.setProperty(p.getName(), p.getValue());
+        }
+    }
+}

Added: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/EndpointsConfiguration.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/EndpointsConfiguration.java?rev=1152123&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/EndpointsConfiguration.java
 (added)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/EndpointsConfiguration.java
 Fri Jul 29 06:47:03 2011
@@ -0,0 +1,62 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.transport.nhttp.endpoints;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The set of endpoints for a listener.
+ */
+public class EndpointsConfiguration {
+
+    public static final String URL_PATTERN = "urlPattern";
+    public static final String MESSAGE_BUILDERS = "messageBuilders";
+    public static final String MESSAGE_BUILDER = "messageBuilder";
+    public static final String CONTENT_TYPE = "contentType";
+    public static final String CLASS = "class";
+    public static final String PARAMETER = "parameter";
+    public static final String NAME = "name";
+    public static final String ENDPOINT = "endpoint";
+    private List<Endpoint> endpoints = new ArrayList<Endpoint>();
+
+    /**
+     * Return the endpoint matching the given URL.
+     * @param url url of the request
+     * @return the endpoint matching the given url
+     */
+    public Endpoint getEndpoint(String url) {
+        for (Endpoint epr : endpoints) {
+            if (epr.isMatching(url)) {
+                return epr;
+            }
+        }
+
+        return null;
+    }
+
+    public List<Endpoint> getEndpoints() {
+        return endpoints;
+    }
+
+    public void addEndpoint(Endpoint endpoint) {
+        endpoints.add(endpoint);
+    }
+}

Added: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointFactory.java?rev=1152123&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointFactory.java
 (added)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointFactory.java
 Fri Jul 29 06:47:03 2011
@@ -0,0 +1,143 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.transport.nhttp.endpoints.config;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.builder.Builder;
+import org.apache.axis2.description.Parameter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.transport.nhttp.endpoints.Endpoint;
+import org.apache.synapse.transport.nhttp.endpoints.EndpointsConfiguration;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+import java.util.regex.Pattern;
+
+/**
+ * Create the Endpoint from the given XML.
+ */
+public class EndpointFactory {
+    private static final Log log = LogFactory.getLog(EndpointFactory.class);
+
+    public Endpoint create(OMElement xml) throws AxisFault {
+        OMAttribute urlPatternAttr = xml.getAttribute(new 
QName(EndpointsConfiguration.URL_PATTERN));
+        if (urlPatternAttr == null) {
+            handleException(EndpointsConfiguration.URL_PATTERN +
+                    " attribute is mandory for an endpoint configuration");
+            return null;
+        }
+
+        String pattern = urlPatternAttr.getAttributeValue();
+        Endpoint endpoint = new Endpoint(Pattern.compile(pattern));
+
+        OMElement messageBuilders = xml.getFirstChildWithName(
+                new QName(EndpointsConfiguration.MESSAGE_BUILDERS));
+
+        if (messageBuilders != null) {
+            OMAttribute defaultBuilderAttr = messageBuilders.getAttribute(
+                    new QName("defaultBuilder"));
+            if (defaultBuilderAttr != null) {
+                Builder builder = 
loadBuilder(defaultBuilderAttr.getAttributeValue());
+                if (builder != null) {
+                    endpoint.setDefaultBuilder(builder);
+                }
+            }
+
+            Iterator it = messageBuilders.getChildrenWithName(
+                    new QName(EndpointsConfiguration.MESSAGE_BUILDER));
+            while(it.hasNext()) {
+                OMElement builderElement = (OMElement) it.next();
+
+                OMAttribute contentTypeAttr = builderElement.getAttribute(
+                        new QName(EndpointsConfiguration.CONTENT_TYPE));
+                if (contentTypeAttr == null) {
+                    handleException(EndpointsConfiguration.CONTENT_TYPE +
+                            " attribute cannot be null for endpoint " +
+                            "with the " + EndpointsConfiguration.URL_PATTERN + 
" : " + pattern);
+                }
+
+                OMAttribute classAttr = builderElement.getAttribute(
+                        new QName(EndpointsConfiguration.CLASS));
+                if (classAttr == null) {
+                    handleException(EndpointsConfiguration.CLASS +
+                            " attribute cannot be null for endpoint " +
+                            "with the " + EndpointsConfiguration.URL_PATTERN + 
" : " + pattern);
+                }
+
+                if (classAttr != null && contentTypeAttr != null) {
+                    Builder builder = 
loadBuilder(classAttr.getAttributeValue());
+                    if (builder != null) {
+                        
endpoint.addBuilder(contentTypeAttr.getAttributeValue(), builder);
+                    }
+                }
+            }
+        }
+
+        Iterator paramItr = xml.getChildrenWithName(
+                new QName(EndpointsConfiguration.PARAMETER));
+        while (paramItr.hasNext()) {
+            OMElement p = (OMElement) paramItr.next();
+            OMAttribute paramNameAttr = p.getAttribute(new 
QName(EndpointsConfiguration.NAME));
+            if (paramNameAttr == null) {
+                handleException("Parameter " + EndpointsConfiguration.NAME + " 
cannot be null");
+            } else {
+                endpoint.addParameter(new 
Parameter(paramNameAttr.getAttributeValue(), p.getText()));
+            }
+        }
+
+        return endpoint;
+    }
+
+    private Builder loadBuilder(String name) throws AxisFault {
+        try {
+            if (name != null) {
+                Class c = Class.forName(name);
+                Object o = c.newInstance();
+                if (o instanceof Builder) {
+                    return (Builder) o;
+                } else {
+                    handleException("Class : " + name +
+                            " should be a Builder");
+                }
+            }
+        } catch (ClassNotFoundException e) {
+            handleException("Error creating builder: " + name, e);
+        } catch (InstantiationException e) {
+            handleException("Error initializing builder: " + name, e);
+        } catch (IllegalAccessException e) {
+            handleException("Error initializing builder: " + name, e);
+        }
+
+        return null;
+    }
+
+    private void handleException(String msg) throws AxisFault {
+        log.error(msg);
+        throw new AxisFault(msg);
+    }
+
+    private void handleException(String msg, Exception e) throws AxisFault {
+        log.error(msg, e);
+        throw new AxisFault(msg, e);
+    }
+}

Added: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointsConfigurationFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointsConfigurationFactory.java?rev=1152123&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointsConfigurationFactory.java
 (added)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/endpoints/config/EndpointsConfigurationFactory.java
 Fri Jul 29 06:47:03 2011
@@ -0,0 +1,83 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.synapse.transport.nhttp.endpoints.config;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axis2.AxisFault;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.transport.nhttp.endpoints.EndpointsConfiguration;
+import org.apache.synapse.transport.nhttp.endpoints.Endpoint;
+
+import javax.xml.namespace.QName;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.util.Iterator;
+
+public class EndpointsConfigurationFactory {
+    private static final Log log = 
LogFactory.getLog(EndpointsConfigurationFactory.class);
+
+    public EndpointsConfiguration create(OMElement element) throws AxisFault {
+        Iterator iterator = element.getChildrenWithName(new 
QName(EndpointsConfiguration.ENDPOINT));
+        EndpointsConfiguration configuration = new EndpointsConfiguration();
+        EndpointFactory fac = new EndpointFactory();
+        while (iterator.hasNext()) {
+            OMElement endpoint = (OMElement) iterator.next();
+
+            Endpoint epr = fac.create(endpoint);
+            configuration.addEndpoint(epr);
+        }
+
+        return configuration;
+    }
+
+    public EndpointsConfiguration create(String fileName) throws AxisFault {
+        File synapseConfigLocation = new File(fileName);
+
+        FileInputStream is = null;
+        try {
+            is = new FileInputStream(synapseConfigLocation);
+        } catch (FileNotFoundException e) {
+            handleException("Error reading file: " + fileName + "for creating 
the " +
+                    EndpointsConfiguration.ENDPOINT + " configurations");
+        }
+        OMElement element = 
OMXMLBuilderFactory.createOMBuilder(is).getDocumentElement();
+        element.build();
+
+        Iterator iterator = element.getChildrenWithName(new 
QName(EndpointsConfiguration.ENDPOINT));
+        EndpointsConfiguration configuration = new EndpointsConfiguration();
+        EndpointFactory fac = new EndpointFactory();
+        while (iterator.hasNext()) {
+            OMElement endpoint = (OMElement) iterator.next();
+
+            Endpoint epr = fac.create(endpoint);
+            configuration.addEndpoint(epr);
+        }
+
+        return configuration;
+    }
+
+    private void handleException(String msg) throws AxisFault {
+        log.error(msg);
+        throw new AxisFault(msg);
+    }
+}

Modified: 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/util/RESTUtil.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/util/RESTUtil.java?rev=1152123&r1=1152122&r2=1152123&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/util/RESTUtil.java
 (original)
+++ 
synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/util/RESTUtil.java
 Fri Jul 29 06:47:03 2011
@@ -23,6 +23,7 @@ import org.apache.axiom.om.OMAbstractFac
 import org.apache.axiom.om.OMElement;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.addressing.EndpointReference;
+import org.apache.axis2.builder.Builder;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.AxisService;
 import org.apache.axis2.description.WSDL20DefaultValueHolder;
@@ -141,6 +142,34 @@ public class RESTUtil {
     }
 
     /**
+     * Processes the HTTP GET / DELETE 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 requestURI        The URL that the request came to
+     * @param contentTypeHeader The contentType header of the request
+     * @param builder           The message builder to use
+     * @param httpMethod        The http method of the request
+     * @param dispatching   Weather we should do service dispatching
+     * @throws AxisFault - Thrown in case a fault occurs
+     */
+    public static void processGetAndDeleteRequest(MessageContext msgContext, 
OutputStream out,
+                                                  String requestURI, Header 
contentTypeHeader,
+                                                  Builder builder, String 
httpMethod,
+                                                  boolean dispatching)
+            throws AxisFault {
+
+        String contentType = contentTypeHeader != null ? 
contentTypeHeader.getValue() : null;
+
+        prepareMessageContext(msgContext, requestURI, httpMethod, out, 
contentType, dispatching);
+
+        msgContext.setProperty(NhttpConstants.NO_ENTITY_BODY, Boolean.TRUE);
+
+        
org.apache.axis2.transport.http.util.RESTUtil.processURLRequest(msgContext, out,
+                contentType, builder);
+    }
+
+    /**
      * Processes the HTTP GET request and builds the SOAP info-set of the REST 
message
      *
      * @param msgContext The MessageContext of the Request Message
@@ -190,6 +219,30 @@ public class RESTUtil {
     }
 
     /**
+     * Processes the HTTP POST request and builds the SOAP info-set of the 
REST message
+     *
+     * @param msgContext        The MessageContext of the Request Message
+     * @param is                The  input stream of the request
+     * @param os                The output stream of the response
+     * @param requestURI        The URL that the request came to
+     * @param contentTypeHeader The contentType header of the request
+     * @param builder           The message builder to use
+     * @param dispatching  Weather we should do dispatching
+     * @throws AxisFault - Thrown in case a fault occurs
+     */
+    public static void processPOSTRequest(MessageContext msgContext, 
InputStream is,
+                                          OutputStream os, String requestURI,
+                                          Header contentTypeHeader, Builder 
builder,
+                                          boolean dispatching) throws 
AxisFault {
+
+        String contentType = contentTypeHeader != null ? 
contentTypeHeader.getValue() : null;
+        prepareMessageContext(msgContext, requestURI, 
HTTPConstants.HTTP_METHOD_POST,
+                os, contentType, dispatching);
+        
org.apache.axis2.transport.http.util.RESTUtil.processXMLRequest(msgContext, is, 
os,
+                contentType, builder);
+    }
+
+    /**
      * prepare message context prior to call axis2 RestUtils
      *
      * @param msgContext  The MessageContext of the Request Message


Reply via email to