[jboss-svn-commits] JBL Code SVN: r29136 - in labs/jbossesb/workspace/tfennelly/httpg2/product: rosetta/src/org/jboss/soa/esb/listeners/config/mappers120 and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 1 09:32:37 EDT 2009


Author: tfennelly
Date: 2009-09-01 09:32:36 -0400 (Tue, 01 Sep 2009)
New Revision: 29136

Modified:
   labs/jbossesb/workspace/tfennelly/httpg2/product/etc/schemas/xml/jbossesb-1.2.0.xsd
   labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/config/mappers120/HttpGatewayMapper.java
   labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/http/HttpGatewayServlet.java
   labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/http_gateway/jboss-esb.xml
   labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/webservice_proxy_basic/jboss-esb.xml
Log:
Added the asyncResponse element

Modified: labs/jbossesb/workspace/tfennelly/httpg2/product/etc/schemas/xml/jbossesb-1.2.0.xsd
===================================================================
--- labs/jbossesb/workspace/tfennelly/httpg2/product/etc/schemas/xml/jbossesb-1.2.0.xsd	2009-09-01 12:55:28 UTC (rev 29135)
+++ labs/jbossesb/workspace/tfennelly/httpg2/product/etc/schemas/xml/jbossesb-1.2.0.xsd	2009-09-01 13:32:36 UTC (rev 29136)
@@ -1814,7 +1814,21 @@
 			<xsd:complexContent>
 				<xsd:extension base="jesb:gateway-only-listener">
                     <xsd:choice>
-                        <xsd:element name="exception" type="jesb:httpExceptionMappings" minOccurs="0" maxOccurs="1" />
+                        <xsd:element name="exception" type="jesb:httpExceptionMappings" minOccurs="0" maxOccurs="1">
+                            <xsd:annotation>
+                                <xsd:documentation xml:lang="en">
+                                    Exception to HTTP status code mappings.
+                                </xsd:documentation>
+                            </xsd:annotation>
+                        </xsd:element>
+                        <xsd:element name="asyncResponse" type="jesb:asyncHttpResponse" minOccurs="0" maxOccurs="1">
+                            <xsd:annotation>
+                                <xsd:documentation xml:lang="en">
+                                    Aynchronous HTTP Response.  If set, the gateway will asynchronously deliver the
+                                    message to the target Service.
+                                </xsd:documentation>
+                            </xsd:annotation>
+                        </xsd:element>
                     </xsd:choice>
                     <xsd:attribute name="urlPattern" type="xsd:string" use="optional">
                         <xsd:annotation>
@@ -1830,13 +1844,6 @@
                             </xsd:documentation>
                         </xsd:annotation>
                     </xsd:attribute>
-                    <xsd:attribute name="sync" type="xsd:boolean" use="required">
-                        <xsd:annotation>
-                            <xsd:documentation xml:lang="en">
-                                Should the HTTP Gateway make a Synchronous invocation on the Service Endpoint.
-                            </xsd:documentation>
-                        </xsd:annotation>
-                    </xsd:attribute>
                 </xsd:extension>
 			</xsd:complexContent>
 		</xsd:complexType>
@@ -1864,6 +1871,33 @@
             </xsd:annotation>
         </xsd:attribute>
     </xsd:complexType>
+    <xsd:complexType name="asyncHttpResponse">
+        <xsd:sequence>
+            <xsd:element name="payload" minOccurs="0" maxOccurs="1">
+                <xsd:annotation>
+                    <xsd:documentation xml:lang="en">
+                        Static HTTP Gateway response payload for async message delivery to service.
+                        <p/>
+                        If not set, an empty (zero length) payload will be returned in the HTTP Response.
+                    </xsd:documentation>
+                </xsd:annotation>
+                <xsd:complexType>
+                    <xsd:attribute name="classpathResource" type="xsd:string" use="required" />
+                    <xsd:attribute name="contentType" type="xsd:string" use="required" />
+                    <xsd:attribute name="characterEncoding" type="xsd:string" use="optional" />
+                </xsd:complexType>
+            </xsd:element>
+        </xsd:sequence>
+        <xsd:attribute name="statusCode" type="xsd:int" default="200" use="optional">
+            <xsd:annotation>
+                <xsd:documentation xml:lang="en">
+                    Static HTTP Gateway response code for async message delivery to service.
+                    <p/>
+                    If not set, a 200 status (OK) will be returned in the HTTP Response.
+                </xsd:documentation>
+            </xsd:annotation>
+        </xsd:attribute>
+    </xsd:complexType>
 
 	<!-- 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
 		Groovy Gateway Type Implementations.

Modified: labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/config/mappers120/HttpGatewayMapper.java
===================================================================
--- labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/config/mappers120/HttpGatewayMapper.java	2009-09-01 12:55:28 UTC (rev 29135)
+++ labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/config/mappers120/HttpGatewayMapper.java	2009-09-01 13:32:36 UTC (rev 29136)
@@ -11,6 +11,7 @@
 import org.jboss.soa.esb.listeners.config.xbeanmodel120.HttpProviderDocument.HttpProvider;
 import org.jboss.soa.esb.listeners.config.xbeanmodel120.PayloadAs;
 import org.jboss.soa.esb.listeners.config.xbeanmodel120.HttpExceptionMappings;
+import org.jboss.soa.esb.listeners.config.xbeanmodel120.AsyncHttpResponse;
 import org.jboss.internal.soa.esb.listeners.war.HttpGatewayDeploymentFactory;
 import org.jboss.internal.soa.esb.assertion.AssertArgument;
 import org.w3c.dom.Element;
@@ -54,8 +55,22 @@
 
         String urlPattern = listener.getUrlPattern();
         PayloadAs.Enum payloadAs = listener.getPayloadAs();
+        AsyncHttpResponse asyncResponse = listener.getAsyncResponse();
 
-        listenerNode.setAttribute(HttpGatewayServlet.SYNC_SERVICE_INVOKE, Boolean.toString(listener.getSync()));        
+        if(asyncResponse != null) {
+            listenerNode.setAttribute(HttpGatewayServlet.ASYNC_SERVICE_INVOKE, "true");
+            listenerNode.setAttribute(HttpGatewayServlet.ASYNC_STATUS_CODE, Integer.toString(asyncResponse.getStatusCode()));
+            AsyncHttpResponse.Payload payload = asyncResponse.getPayload();
+            if(payload != null) {
+                listenerNode.setAttribute(HttpGatewayServlet.ASYNC_PAYLOAD, payload.getClasspathResource());
+                listenerNode.setAttribute(HttpGatewayServlet.ASYNC_PAYLOAD_CONTENT_TYPE, payload.getContentType());
+                if(payload.getCharacterEncoding() != null) {
+                    listenerNode.setAttribute(HttpGatewayServlet.ASYNC_PAYLOAD_CHARACTER_ENCODING, payload.getCharacterEncoding());
+                }
+            }
+        } else {
+            listenerNode.setAttribute(HttpGatewayServlet.ASYNC_SERVICE_INVOKE, "false");
+        }
 
         if(bus != null) {
             try {

Modified: labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/http/HttpGatewayServlet.java
===================================================================
--- labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/http/HttpGatewayServlet.java	2009-09-01 12:55:28 UTC (rev 29135)
+++ labs/jbossesb/workspace/tfennelly/httpg2/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/http/HttpGatewayServlet.java	2009-09-01 13:32:36 UTC (rev 29136)
@@ -21,6 +21,7 @@
 
 import org.jboss.soa.esb.Service;
 import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.util.ClassUtil;
 import org.jboss.soa.esb.couriers.FaultMessageException;
 import org.jboss.soa.esb.services.registry.RegistryException;
 import org.jboss.soa.esb.message.Message;
@@ -30,6 +31,8 @@
 import org.jboss.soa.esb.listeners.ListenerTagNames;
 import org.jboss.soa.esb.listeners.config.mappers120.HttpGatewayMapper;
 import org.jboss.soa.esb.client.ServiceInvoker;
+import org.jboss.internal.soa.esb.util.StreamUtils;
+import org.apache.log4j.Logger;
 
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -37,6 +40,7 @@
 import javax.servlet.ServletException;
 import javax.servlet.ServletConfig;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Enumeration;
 import java.util.Map;
 
@@ -50,15 +54,26 @@
  */
 public class HttpGatewayServlet extends HttpServlet {
 
+    private static final Logger logger = Logger.getLogger(HttpGatewayServlet.class);
+
     public static final String PAYLOAD_AS = "payloadAs";
     public static final String EXCEPTION_MAPPINGS = "httpExceptionMappings";
-    public static final String SYNC_SERVICE_INVOKE = "syncServiceInvoke";
 
+    public static final String ASYNC_SERVICE_INVOKE = "asyncServiceInvoke";
+    public static final String ASYNC_STATUS_CODE = "asyncStatusCode";
+    public static final String ASYNC_PAYLOAD = "asyncPayloadPath";
+    public static final String ASYNC_PAYLOAD_CONTENT_TYPE= "asyncPayloadContentType";
+    public static final String ASYNC_PAYLOAD_CHARACTER_ENCODING = "asyncPayloadCharacterEncoding";
+
     private Service service;
     private ServiceInvoker serviceInvoker;
-    private boolean syncInvoke;
+    private boolean asyncInvoke;
+    private int asyncStatusCode = HttpServletResponse.SC_OK;
+    private byte[] asyncPayload;
+    private String asyncContentType;
+    private String asyncCharacterEncoding;
     private MessageComposer<HttpRequestWrapper> messageComposer;
-    private long blockingTimeout;
+    private long blockingTimeout = 30000L;
     private Map<String, Integer> exceptionMappings;
 
     public void init(ServletConfig config) throws ServletException {
@@ -78,9 +93,29 @@
             throw new ServletException("Failed to create message composer.", e);
         }
 
-        blockingTimeout = configTree.getLongAttribute("synchronousTimeout", 30000);
-        syncInvoke = configTree.getBooleanAttribute(SYNC_SERVICE_INVOKE, true);
+        asyncInvoke = configTree.getBooleanAttribute(ASYNC_SERVICE_INVOKE, false);
+        if(asyncInvoke) {
+            String asyncSCConfig = configTree.getAttribute(ASYNC_STATUS_CODE);
+            try {
+                asyncStatusCode = Integer.parseInt(asyncSCConfig);
+            } catch (NumberFormatException e) {
+                throw new ServletException("Invalid static asynchronous response code configuration '" + asyncSCConfig + "'.", e);
+            }
 
+            String payloadPath = configTree.getAttribute(ASYNC_PAYLOAD);
+            if(payloadPath != null) {
+                try {
+                    asyncPayload = readStaticAsyncResponse(payloadPath);
+                } catch (ConfigurationException e) {
+                    throw new ServletException("Invalid Exception to HTTP Status mapping configuration.", e);
+                }
+                asyncContentType = configTree.getAttribute(ASYNC_PAYLOAD_CONTENT_TYPE);
+                asyncCharacterEncoding = configTree.getAttribute(ASYNC_PAYLOAD_CHARACTER_ENCODING);
+            }
+        } else {
+            blockingTimeout = configTree.getLongAttribute("synchronousTimeout", 30000L);
+        }
+
         String exceptionMappingsCSV = configTree.getAttribute(EXCEPTION_MAPPINGS);
         if(exceptionMappingsCSV != null) {
             try {
@@ -103,20 +138,30 @@
 
         Message outMessage;
         try {
-            // Dispatch the message to the action pipeline, using the service sync flag on the gateway to decide
-            // whether to invoke sync or async...
-            if(syncInvoke) {
+            // Dispatch the message to the action pipeline...
+            if(!asyncInvoke) {
                 outMessage = serviceInvoker.deliverSync(inMessage, blockingTimeout);
 
                 // Set the mep as a header on the response...
-                resp.setHeader(SYNC_SERVICE_INVOKE, "true");
+                resp.setHeader(ASYNC_SERVICE_INVOKE, "false");
             } else {
                 serviceInvoker.deliverAsync(inMessage);
-                resp.setContentLength(0);
-                resp.setStatus(HttpServletResponse.SC_ACCEPTED);
 
+                if(asyncPayload != null) {
+                    resp.getOutputStream().write(asyncPayload);
+                    resp.setContentLength(asyncPayload.length);
+                    resp.setContentType(asyncContentType);
+                    if(asyncCharacterEncoding != null) {
+                        resp.setCharacterEncoding(asyncCharacterEncoding);
+                    }
+                } else {
+                    resp.setContentLength(0);
+                }
+
+                resp.setStatus(asyncStatusCode);
+
                 // Set the mep as a header on the response...
-                resp.setHeader(SYNC_SERVICE_INVOKE, "false");
+                resp.setHeader(ASYNC_SERVICE_INVOKE, "true");
 
                 return;
             }
@@ -161,8 +206,26 @@
         while(configNames.hasMoreElements()) {
             String name = (String) configNames.nextElement();
             configTree.setAttribute(name, config.getInitParameter(name));
-        }        
+        }
 
         return configTree;
     }
+
+    private byte[] readStaticAsyncResponse(String payloadPath) throws ConfigurationException {
+        InputStream stream = ClassUtil.getResourceAsStream(payloadPath, HttpGatewayServlet.class);
+
+        if(stream == null) {
+            throw new ConfigurationException("Failed to access static HTTP response payload file '" + payloadPath + "' on classpath.");
+        }
+
+        try {
+            return StreamUtils.readStream(stream);
+        } finally {
+            try {
+                stream.close();
+            } catch (IOException e) {
+                logger.error("Unexpected Error closing static HTTP response payload file '" + payloadPath + "' ", e);
+            }
+        }
+    }
 }

Modified: labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/http_gateway/jboss-esb.xml
===================================================================
--- labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/http_gateway/jboss-esb.xml	2009-09-01 12:55:28 UTC (rev 29135)
+++ labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/http_gateway/jboss-esb.xml	2009-09-01 13:32:36 UTC (rev 29136)
@@ -29,7 +29,7 @@
             <listeners>
                 <!-- Receives: http://<host>:<port>/Quickstart_http_gateway/http/sales/* but will be forced to
                                authenticate because the "sales" bus has basic auth configured (above)... -->
-                <http-gateway name="sales" busidref="secureFriends" urlPattern="sales/*" sync="true" />
+                <http-gateway name="sales" busidref="secureFriends" urlPattern="sales/*" />
             </listeners>
             <actions mep="RequestResponse">
                 <action name="print" class="org.jboss.soa.esb.samples.quickstart.httpgateway.MyAction"/>
@@ -40,7 +40,7 @@
             <listeners>
                 <!-- Receives: http://<host>:<port>/Quickstart_http_gateway/http/index/*
                                Uses the default http bus configuration... -->
-                <http-gateway name="Index" urlPattern="index/*" sync="true" />
+                <http-gateway name="Index" urlPattern="index/*" />
             </listeners>
             <actions mep="RequestResponse">
                 <action name="print" class="org.jboss.soa.esb.samples.quickstart.httpgateway.MyAction"/>
@@ -49,7 +49,7 @@
 
         <service category="Exceptions" name="Exception1" description="" invmScope="GLOBAL">
             <listeners>
-                <http-gateway name="Exception2" sync="true" />
+                <http-gateway name="Exception2" />
             </listeners>
             <actions mep="RequestResponse">
                 <!-- Uses the globally defined exception mappings defined on the <http-provider>... -->
@@ -59,7 +59,7 @@
 
         <service category="Exceptions" name="Exception2" description="" invmScope="GLOBAL">
             <listeners>
-                <http-gateway name="Exception1" sync="true">
+                <http-gateway name="Exception1">
                     <!-- Override the exception mappings defined on the <http-provider>... -->
                     <exception>
                         <mapping class="org.jboss.soa.esb.samples.quickstart.httpgateway.MyActionException" status="503" />

Modified: labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/webservice_proxy_basic/jboss-esb.xml
===================================================================
--- labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/webservice_proxy_basic/jboss-esb.xml	2009-09-01 12:55:28 UTC (rev 29135)
+++ labs/jbossesb/workspace/tfennelly/httpg2/product/samples/quickstarts/webservice_proxy_basic/jboss-esb.xml	2009-09-01 13:32:36 UTC (rev 29136)
@@ -6,7 +6,7 @@
 				 description="Basic WebService Proxy"
 				 invmScope="GLOBAL" >
 			<listeners>                
-                <http-gateway name="basicProxy" sync="true" />
+                <http-gateway name="basicProxy"/>
 			</listeners>
 			<actions mep="RequestResponse">
 				<action name="echo-request"



More information about the jboss-svn-commits mailing list