You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by hi...@apache.org on 2009/10/02 08:37:48 UTC

svn commit: r820897 - in /synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp: ClientWorker.java NhttpConstants.java

Author: hiranya
Date: Fri Oct  2 06:37:48 2009
New Revision: 820897

URL: http://svn.apache.org/viewvc?rev=820897&view=rev
Log:
Implementing the proposal for handling responses which do not contain a Content-Type header. See SYNAPSE-587 for details. Now when the content type is not present on the response Synapse will try to provide a content type by looking at the context properties or if not assign a default value (application/octet-stream) as specified in the HTTP specs.


Modified:
    synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ClientWorker.java
    synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java

Modified: synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ClientWorker.java
URL: http://svn.apache.org/viewvc/synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ClientWorker.java?rev=820897&r1=820896&r2=820897&view=diff
==============================================================================
--- synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ClientWorker.java (original)
+++ synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/ClientWorker.java Fri Oct  2 06:37:48 2009
@@ -27,6 +27,7 @@
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.description.WSDL2Constants;
+import org.apache.axis2.description.Parameter;
 import org.apache.axis2.engine.AxisEngine;
 import org.apache.axis2.transport.TransportUtils;
 import org.apache.axis2.transport.http.HTTPTransportUtils;
@@ -153,65 +154,63 @@
 
         SOAPEnvelope envelope = null;
         try {
-
             Header cType = response.getFirstHeader(HTTP.CONTENT_TYPE);
-
+            String contentType;
             if (cType != null) {
-                String contentType = cType.getValue();
+                // This is the most common case - Most of the time servers send the Content-Type
+                contentType = cType.getValue();
+            } else {
+                // Server hasn't sent the header - Try to infer the content type
+                contentType = inferContentType();
+            }
 
-                String charSetEnc = BuilderUtil.getCharSetEncoding(contentType);
-                if (charSetEnc == null) {
-                    charSetEnc = MessageContext.DEFAULT_CHAR_SET_ENCODING;
-                }
+            String charSetEnc = BuilderUtil.getCharSetEncoding(contentType);
+            if (charSetEnc == null) {
+                charSetEnc = MessageContext.DEFAULT_CHAR_SET_ENCODING;
+            }
 
-                responseMsgCtx.setProperty(
-                    Constants.Configuration.CHARACTER_SET_ENCODING,
-                    contentType.indexOf(HTTP.CHARSET_PARAM) > 0 ?
-                        charSetEnc : MessageContext.DEFAULT_CHAR_SET_ENCODING);
-
-                // workaround for Axis2 TransportUtils.createSOAPMessage() issue, where a response
-                // of content type "text/xml" is thought to be REST if !MC.isServerSide(). This
-                // question is still under debate and due to the timelines, I am commiting this
-                // workaround as Axis2 1.2 is about to be released and Synapse 1.0
-                responseMsgCtx.setServerSide(false);
-                try {
-                    envelope = TransportUtils.createSOAPMessage(
-                            responseMsgCtx,
-                            HTTPTransportUtils.handleGZip(responseMsgCtx, in),
-                            contentType);
-
-                } catch (OMException e) {
-                    // handle non SOAP and POX/REST payloads (probably text/html)
-                    String errorMessage = "Unexpected response received. HTTP response code : "
-                        + this.response.getStatusLine().getStatusCode() + " HTTP status : "
-                        + this.response.getStatusLine().getReasonPhrase() + " exception : "
-                        + e.getMessage();
-
-                    log.warn(errorMessage);
-                    if (log.isDebugEnabled()) {
-                        log.debug(errorMessage, e);
-                        log.debug("Creating the SOAPFault to be injected...");
-                    }
-                    SOAPFactory factory = new SOAP11Factory();
-                    envelope = factory.getDefaultFaultEnvelope();
-                    SOAPFaultDetail detail = factory.createSOAPFaultDetail();
-                    detail.setText(errorMessage);
-                    envelope.getBody().getFault().setDetail(detail);
-                    SOAPFaultReason reason = factory.createSOAPFaultReason();
-                    reason.setText(errorMessage);
-                    envelope.getBody().getFault().setReason(reason);
-                    SOAPFaultCode code = factory.createSOAPFaultCode();
-                    code.setText(Integer.toString(this.response.getStatusLine().getStatusCode()));
-                    envelope.getBody().getFault().setCode(code);
-                }
-                responseMsgCtx.setServerSide(true);
-                responseMsgCtx.setEnvelope(envelope);
+            responseMsgCtx.setProperty(
+                Constants.Configuration.CHARACTER_SET_ENCODING,
+                contentType.indexOf(HTTP.CHARSET_PARAM) > 0 ?
+                    charSetEnc : MessageContext.DEFAULT_CHAR_SET_ENCODING);
 
-            } else {
-                // there is no response entity-body
-                responseMsgCtx.setProperty(NhttpConstants.NO_ENTITY_BODY, Boolean.TRUE);
-                responseMsgCtx.setEnvelope(new SOAP11Factory().getDefaultEnvelope());
+            // workaround for Axis2 TransportUtils.createSOAPMessage() issue, where a response
+            // of content type "text/xml" is thought to be REST if !MC.isServerSide(). This
+            // question is still under debate and due to the timelines, I am commiting this
+            // workaround as Axis2 1.2 is about to be released and Synapse 1.0
+            responseMsgCtx.setServerSide(false);
+            try {
+                envelope = TransportUtils.createSOAPMessage(
+                        responseMsgCtx,
+                        HTTPTransportUtils.handleGZip(responseMsgCtx, in),
+                        contentType);
+
+            } catch (OMException e) {
+                // handle non SOAP and POX/REST payloads (probably text/html)
+                String errorMessage = "Unexpected response received. HTTP response code : "
+                    + this.response.getStatusLine().getStatusCode() + " HTTP status : "
+                    + this.response.getStatusLine().getReasonPhrase() + " exception : "
+                    + e.getMessage();
+
+                log.warn(errorMessage);
+                if (log.isDebugEnabled()) {
+                    log.debug(errorMessage, e);
+                    log.debug("Creating the SOAPFault to be injected...");
+                }
+                SOAPFactory factory = new SOAP11Factory();
+                envelope = factory.getDefaultFaultEnvelope();
+                SOAPFaultDetail detail = factory.createSOAPFaultDetail();
+                detail.setText(errorMessage);
+                envelope.getBody().getFault().setDetail(detail);
+                SOAPFaultReason reason = factory.createSOAPFaultReason();
+                reason.setText(errorMessage);
+                envelope.getBody().getFault().setReason(reason);
+                SOAPFaultCode code = factory.createSOAPFaultCode();
+                code.setText(Integer.toString(this.response.getStatusLine().getStatusCode()));
+                envelope.getBody().getFault().setCode(code);
             }
+            responseMsgCtx.setServerSide(true);
+            responseMsgCtx.setEnvelope(envelope);
 
             // copy the HTTP status code as a message context property with the key HTTP_SC to be
             // used at the sender to set the propper status code when passing the message
@@ -251,6 +250,24 @@
         }
     }
 
+    private String inferContentType() {
+        // Try to get the content type from the message context
+        Object cTypeProperty = responseMsgCtx.getProperty(NhttpConstants.CONTENT_TYPE);
+        if (cTypeProperty != null) {
+            return cTypeProperty.toString();
+        }
+
+        // Try to get the content type from the axis configuration
+        Parameter cTypeParam = cfgCtx.getAxisConfiguration().getParameter(
+                NhttpConstants.CONTENT_TYPE);
+        if (cTypeParam != null) {
+            return cTypeParam.getValue().toString();
+        }
+
+        // Unable to determine the content type - Return default value
+        return NhttpConstants.DEFAULT_CONTENT_TYPE;
+    }
+
     // -------------- utility methods -------------
     private void handleException(String msg, Exception e) throws AxisFault {
         log.error(msg, e);

Modified: synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java
URL: http://svn.apache.org/viewvc/synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java?rev=820897&r1=820896&r2=820897&view=diff
==============================================================================
--- synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java (original)
+++ synapse/branches/1.3/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/nhttp/NhttpConstants.java Fri Oct  2 06:37:48 2009
@@ -46,6 +46,8 @@
     public static final String NON_BLOCKING_TRANSPORT = "NonBlockingTransport";
     public static final String SERIALIZED_BYTES = "SerializedBytes";
     public static final String REQUEST_READ = "REQUEST_READ";
+    public static final String CONTENT_TYPE = "CONTENT_TYPE";
+    public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
 
     public static final String SEND_TIMEOUT = "SEND_TIMEOUT";