Author: asankha
Date: Sat Mar 14 15:51:26 2009
New Revision: 754464

URL: http://svn.apache.org/viewvc?rev=754464&view=rev
Log:
commit fix for SYNAPSE-517 by Eric Hubert

Added:
    
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageFormatterTest.java
    
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianTestHelper.java
Modified:
    
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageBuilder.java
    
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageFormatter.java
    
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianUtils.java
    
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageBuilderTest.java

Modified: 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageBuilder.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageBuilder.java?rev=754464&r1=754463&r2=754464&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageBuilder.java
 (original)
+++ 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageBuilder.java
 Sat Mar 14 15:51:26 2009
@@ -35,12 +35,12 @@
 import org.apache.synapse.core.SynapseEnvironment;
 import org.apache.synapse.util.SynapseBinaryDataSource;
 
+import javax.activation.DataHandler;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PushbackInputStream;
 
-import javax.activation.DataHandler;
-
 /**
  * Enables a message encoded using the Hessian binary protocol to be received 
by axis2/synapse
  * and this builds the HessianDataSource to represent the hessian message 
inside the SOAP info-set

Modified: 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageFormatter.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageFormatter.java?rev=754464&r1=754463&r2=754464&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageFormatter.java
 (original)
+++ 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianMessageFormatter.java
 Sat Mar 14 15:51:26 2009
@@ -29,11 +29,13 @@
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.transport.MessageFormatter;
 import org.apache.axis2.transport.http.util.URLTemplatingUtil;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.synapse.util.SynapseBinaryDataSource;
 
 import javax.activation.DataHandler;
+
 import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -43,175 +45,251 @@
 
 /**
  * Enables a message encoded using the Hessian binary protocol to be written 
to transport by
- * axis2/synapse and this formats the HessianDataSource to a hessian message
+ * axis2/synapse and this formats the HessianDataSource to a Hessian message.
  *
  * @see org.apache.axis2.transport.MessageFormatter
  * @see org.apache.synapse.util.SynapseBinaryDataSource
  */
 public class HessianMessageFormatter implements MessageFormatter {
 
-       private static final Log log = 
LogFactory.getLog(HessianMessageFormatter.class);
+    private static final Log log = 
LogFactory.getLog(HessianMessageFormatter.class);
 
     /**
-        * Formats the content type to be written in to the transport
-        *
-        * @param msgCtxt message of which the content type has to be formatted
-        * @param format fomat of the expected formatted message
-        * @param soapActionString soap action of the message
-        * @return contentType formatted content type as a String
-        */
-       public String getContentType(MessageContext msgCtxt, OMOutputFormat 
format,
-        String soapActionString) {
+     * Formats the content type to be written in to the transport
+     *
+     * @param msgCtxt message of which the content type has to be formatted
+     * @param format fomat of the expected formatted message
+     * @param soapActionString soap action of the message
+     * 
+     * @return contentType formatted content type as a String
+     */
+    public String getContentType(MessageContext msgCtxt, OMOutputFormat format,
+            String soapActionString) {
 
         String contentType = (String) 
msgCtxt.getProperty(Constants.Configuration.CONTENT_TYPE);
-               String encoding = format.getCharSetEncoding();
-
         if (contentType == null) {
-                       contentType = HessianConstants.HESSIAN_CONTENT_TYPE;
-               }
+            contentType = HessianConstants.HESSIAN_CONTENT_TYPE;
+        }
 
+        String encoding = format.getCharSetEncoding();
         if (encoding != null) {
-                       contentType += "; charset=" + encoding;
-               }
+            contentType += "; charset=" + encoding;
+        }
 
         return contentType;
-       }
+    }
 
-       /**
-        * Extract Hessian bytes from the received SOAP message and write it 
onto the wire
-        *
-        * @param msgCtxt message from which the hessian message has to be 
extracted
-        * @param format mwssage format to be written
-        * @param out stream to which the message is written
-        * @param preserve whether to preserve the indentations
-        * @throws AxisFault in case of a failure in writing the message to the 
provided stream
-        */
-       public void writeTo(MessageContext msgCtxt, OMOutputFormat format, 
OutputStream out,
-        boolean preserve) throws AxisFault {
+    /**
+     * Extract Hessian bytes from the received SOAP message and write it onto 
the wire
+     *
+     * @param msgCtx message from which the Hessian message has to be extracted
+     * @param format message format to be written
+     * @param out stream to which the message is written
+     * @param preserve whether to preserve the indentations
+     * 
+     * @throws AxisFault in case of a failure in writing the message to the 
provided stream
+     */
+    public void writeTo(MessageContext msgCtx, OMOutputFormat format, 
OutputStream out,
+            boolean preserve) throws AxisFault {
 
         if (log.isDebugEnabled()) {
-                       log.debug("Start writing the message to OutputStream");
-               }
-
-        // Check whther the message to be written is a fault message
-        if (msgCtxt.getFLOW() == MessageContext.OUT_FAULT_FLOW
-                || msgCtxt.getEnvelope().hasFault()) {
-
-            try {
-
-                SOAPFault fault = msgCtxt.getEnvelope().getBody().getFault();
-                String hessianFaultDetail = "";
-                String hessianFaultMessage = "";
-                String hessianFaultCode = "500";
-
-                if (fault.getDetail() != null) {
-                                       hessianFaultDetail = 
fault.getDetail().getText();
-                               }
-
-                if (fault.getReason() != null) {
-                                       hessianFaultMessage = 
fault.getReason().getText();
-                               }
+            log.debug("Start writing the Hessian message to OutputStream");
+        }
+        
+        // Check whether the message to be written is a fault message
+        if (msgCtx.getFLOW() == MessageContext.OUT_FAULT_FLOW || 
msgCtx.getEnvelope().hasFault()) {
+            
+            SOAPFault soapFault = msgCtx.getEnvelope().getBody().getFault();
+            convertAndWriteHessianFault(soapFault, out);
+        } else {
+            
+            // no differentiation between normal reply and fault (pass the 
original message through)
+            writeHessianMessage(msgCtx, out);
+        }
 
-                if (fault.getCode() != null) {
-                    hessianFaultCode = fault.getCode().getText();
-                }
+        if (log.isDebugEnabled()) {
+            log.debug("Writing message as a Hessian message is successful");
+        }
+    }
 
-                BufferedOutputStream faultOutStream = new 
BufferedOutputStream(out);
-                HessianUtils.writeFault(
-                        hessianFaultCode, hessianFaultMessage, 
hessianFaultDetail, faultOutStream);
-                faultOutStream.flush();
-                faultOutStream.close();
+    /**
+     * This method is not supported because of large file handling limitations
+     *
+     * @param msgCtxt message which contains the Hessian message inside the 
HessianDataSource
+     * @param format message format to be written
+     * 
+     * @return Hessian binary bytes of the message
+     * 
+     * @throws AxisFault for any invocation
+     */
+    public byte[] getBytes(MessageContext msgCtxt, OMOutputFormat format) 
throws AxisFault {
+        throw new AxisFault("Method not supported. Use the "
+                + "HessianMessageFormatter#writeTo method instead");
+    }
 
-            } catch (IOException e) {
-                               handleException("Unalbe to write the fault as a 
hessian message", e);
-                       }
+    /**
+     * {...@inheritdoc}
+     * 
+     * Simply returns the soapAction unchanged.
+     */
+    public String formatSOAPAction(MessageContext messageContext, 
OMOutputFormat format,
+            String soapAction) {
 
-            // if the message is not a fault extract the Hessian bytes and 
write it to the wire
-        } else {
+        return soapAction;
+    }
 
-            OMText hessianOMText = null;
-            OMElement omElement = 
msgCtxt.getEnvelope().getBody().getFirstElement();
+    /**
+     * {...@inheritdoc}
+     * 
+     * @return A templated URL based on the given target URL. 
+     */
+    public URL getTargetAddress(MessageContext messageContext, OMOutputFormat 
format, URL targetURL)
+            throws AxisFault {
 
-            Iterator it = omElement.getChildren();
-            while (it.hasNext()) {
+        return URLTemplatingUtil.getTemplatedURL(targetURL, messageContext, 
false);
+    }
 
-                OMNode hessianElement =  (OMNode) it.next();
-                if (hessianElement instanceof OMText) {
+    /**
+     * Writes the Hessian message contained in the message context to the 
provided output stream.
+     * 
+     * @param msgCtxt the message context containing the Hessian message
+     * @param out the provided output stream to which the message shall be 
written
+     * 
+     * @throws AxisFault if an error occurs writing to the output stream
+     */
+    private void writeHessianMessage(MessageContext msgCtxt, OutputStream out) 
throws AxisFault {
+ 
+        OMElement omElement = 
msgCtxt.getEnvelope().getBody().getFirstElement();
+        SynapseBinaryDataSource synapseBinaryDataSource = 
extractSynapseBinaryDataSource(omElement);
 
-                    OMText tempNode = (OMText) hessianElement;
-                    if (tempNode.getDataHandler() != null && ((DataHandler) 
tempNode
-                            .getDataHandler()).getDataSource() instanceof 
SynapseBinaryDataSource) {
+        if (synapseBinaryDataSource != null) {
 
-                        hessianOMText = tempNode;
+            InputStream inputStream = null;
+            try {
+                inputStream = synapseBinaryDataSource.getInputStream();
+                IOUtils.copy(inputStream, out);
+            } catch (IOException e) {
+                handleException("Couldn't get the bytes from the 
HessianDataSource", e);
+            } finally {
+                if (inputStream != null) {
+                    try {
+                        inputStream.close();
+                    } catch (IOException ignore) {
+                        log.warn("Error closing input stream.", ignore);
+                    }
+                }
+                if (out != null) {
+                    try {
+                        out.close();
+                    } catch (IOException ignore) {
+                        log.warn("Error closing output stream.", ignore);
                     }
                 }
             }
 
-            if (hessianOMText != null) {
-
-                try {
-
-                    SynapseBinaryDataSource synapseBinaryDataSource = 
(SynapseBinaryDataSource) (
-                            (DataHandler) 
hessianOMText.getDataHandler()).getDataSource();
-
-                    InputStream inputStream = 
synapseBinaryDataSource.getInputStream();
-                    BufferedOutputStream outputStream = new 
BufferedOutputStream(out);
-
-                    byte[] buffer = new byte[1024];
-                    int byteCount;
-                    while ((byteCount=inputStream.read(buffer)) != -1) {
-                        outputStream.write(buffer, 0, byteCount);
-                    }
+        } else {
+            handleException("Unable to find the Hessian content in the 
payload");
+        }
+    }
 
-                    inputStream.close();
-                    outputStream.flush();
-                    outputStream.close();
+    /**
+     * Tries to extract the binary data source containing the Hessian message.
+     * 
+     * @param omElement
+     * 
+     * @return the binary data source containing the Hessian message or null, 
if the OMElement
+     *         does not contain a binary datasource.
+     */
+    private SynapseBinaryDataSource extractSynapseBinaryDataSource(OMElement 
omElement) {
+        
+        SynapseBinaryDataSource synapseBinaryDataSource = null;
+        Iterator it = omElement.getChildren();
+
+        while (it.hasNext() && synapseBinaryDataSource == null) {
+
+            OMNode hessianElement = (OMNode) it.next();
+            if (hessianElement instanceof OMText) {
+
+                OMText tempNode = (OMText) hessianElement;
+                if (tempNode.getDataHandler() != null
+                        && ((DataHandler) 
tempNode.getDataHandler()).getDataSource() instanceof SynapseBinaryDataSource) {
 
-                } catch (IOException e) {
-                    handleException("Couldn't get the bytes from the 
HessianDataSource", e);
+                    synapseBinaryDataSource = (SynapseBinaryDataSource) 
((DataHandler) tempNode
+                            .getDataHandler()).getDataSource();
                 }
-
-            } else {
-                handleException("Unable to find the hessian content in the 
payload");
             }
-
         }
 
-        if (log.isDebugEnabled()) {
-            log.debug("Writing message as a hessian message is successful");
-        }
+        return synapseBinaryDataSource;
     }
 
     /**
-     * This method is not supported because of large file handling limitations
-     *
-     * @param msgCtxt message which contains the Hessian message inside the 
HessianDataSource
-     * @param format message fromat to be written
-     * @return Hessian binary bytes of the message
-     * @throws AxisFault for any invoke
+     * Reads details from the SOAPFault and creates a new Hessian fault using 
those details and
+     * writes it to the output stream.
+     * 
+     * @param soapFault the SOAP fault to convert and write as a Hessian fault
+     * @param out the output stream to write the Hessian fault to
+     * 
+     * @throws AxisFault if an error occurs writing the message to the output 
stream
      */
-    public byte[] getBytes(MessageContext msgCtxt, OMOutputFormat format) 
throws AxisFault {
-        throw new AxisFault("Method not supported. Use the " +
-                "HessianMessageFormatter#writeTo method instead");
-    }
-
-    public String formatSOAPAction(MessageContext messageContext, 
OMOutputFormat format,
-        String soapAction) {
+    private void convertAndWriteHessianFault(SOAPFault soapFault, OutputStream 
out) throws AxisFault {
 
-        return soapAction;
-       }
+        BufferedOutputStream faultOutStream = new BufferedOutputStream(out);
 
-    public URL getTargetAddress(MessageContext messageContext, OMOutputFormat 
format,
-        URL targetURL) throws AxisFault {
+        try {
+            String hessianFaultCode = "500";
+            String hessianFaultMessage = "";
+            String hessianFaultDetail = "";
+            
+            if (soapFault.getCode() != null) {
+                hessianFaultCode = soapFault.getCode().getText();
+            }
+            
+            if (soapFault.getReason() != null) {
+                hessianFaultMessage = soapFault.getReason().getText();
+            }
 
-        return URLTemplatingUtil.getTemplatedURL(targetURL, messageContext, 
false);
-       }
+            if (soapFault.getDetail() != null) {
+                hessianFaultDetail = soapFault.getDetail().getText();
+            }            
+
+            HessianUtils.writeFault(hessianFaultCode, hessianFaultMessage, 
hessianFaultDetail,
+                    faultOutStream);
+            faultOutStream.flush();
+
+        } catch (IOException e) {
+            handleException("Unalbe to write the fault as a Hessian message", 
e);
+        } finally {
+            try {
+                if (faultOutStream != null) {
+                    faultOutStream.close();
+                }
+            } catch (IOException ignore) {
+                log.warn("Error closing output stream.", ignore);
+            }
+        }
+    }
 
-    private void handleException(String msg, Throwable e) throws AxisFault {
+    /**
+     * Logs the original exception, wrappes it in an AxisFault and rethrows it.
+     * 
+     * @param msg the error message
+     * @param e the original exception
+     * 
+     * @throws AxisFault 
+     */
+    private void handleException(String msg, Exception e) throws AxisFault {
         log.error(msg, e);
         throw new AxisFault(msg, e);
     }
-
+    
+    /**
+     * Logs an error message and throws a newly created AxisFault. 
+     * 
+     * @param msg the error message
+     * 
+     * @throws AxisFault 
+     */
     private void handleException(String msg) throws AxisFault {
         log.error(msg);
         throw new AxisFault(msg);

Modified: 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianUtils.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianUtils.java?rev=754464&r1=754463&r2=754464&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianUtils.java
 (original)
+++ 
synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianUtils.java
 Sat Mar 14 15:51:26 2009
@@ -23,39 +23,112 @@
 import java.io.OutputStream;
 
 /**
- * Util class for writing the Hessian Fault to the output stream
+ * Util class for writing the Hessian Fault to the output stream.
+ * Most of the implementation is taken from the Hessian Java implementation in
+ * com.caucho.hessian.io.HessianOutput.
  */
-public class HessianUtils {
+public final class HessianUtils {
 
-    public static void printString(String v, int offset, int length, 
OutputStream os) throws IOException {
-        for (int i = 0; i < length; i++) {
-            char ch = v.charAt(i + offset);
+    private HessianUtils() {
+    }
 
-            if (ch < 0x80)
-                os.write(ch);
-            else if (ch < 0x800) {
-                os.write(0xc0 + ((ch >> 6) & 0x1f));
-                os.write(0x80 + (ch & 0x3f));
-            }
-            else {
-                os.write(0xe0 + ((ch >> 12) & 0xf));
-                os.write(0x80 + ((ch >> 6) & 0x3f));
-                os.write(0x80 + (ch & 0x3f));
-            }
+    /**
+     * Creates and writes a Hessian fault using the provided code, message and 
detail to the
+     * provided output stream.
+     * 
+     * @param code the error code of the message
+     * @param message the error message
+     * @param detail an error detail
+     * @param os the output stream to write the message to
+     * 
+     * @throws IOException if an error occurs writing to the output stream
+     */
+    public static void writeFault(String code, String message, String detail, 
OutputStream os)
+            throws IOException {
+
+        startReply(os);
+
+        os.write('f');
+        writeString("code", os);
+        writeString(code, os);
+
+        writeString("message", os);
+        writeString(message, os);
+
+        if (detail != null) {
+            writeString("detail", os);
+            writeString(detail, os);
         }
+
+        os.write('z');
+
+        completeReply(os);
     }
 
-    public static void writeString(String value, OutputStream os) throws 
IOException {
+    /**
+     * Writes the bytes to start a Hessian reply (including protocol version 
information) to the 
+     * provided output stream.
+     * 
+     * @param os the output stream to write to
+     * 
+     * @throws IOException if an error occurs writing to the output stream
+     */
+    private static void startReply(OutputStream os) throws IOException {
+        os.write('r');
+        os.write(1);
+        os.write(0);
+    }
+
+    /**
+     * Writes the byte to complete a Hessian reply to the provided output 
stream.
+     * 
+     * @param os the output stream to write to
+     * 
+     * @throws IOException if an error occurs writing to the output stream
+     */
+    private static void completeReply(OutputStream os) throws IOException {
+        os.write('z');
+    }
+
+    /**
+     * Writes a the provided string in a Hessian string representation to the 
provided output 
+     * stream using UTF-8 encoding.<br>
+     * 
+     * The string will be written with the following syntax:
+     *
+     * <code><pre>
+     * S b16 b8 string-value
+     * </pre></code>
+     *
+     * If the value is null, it will be written as
+     *
+     * <code><pre>
+     * N
+     * </pre></code>
+     *
+     * @param value the string to write to the output string
+     * @param os the output stream to write the string to
+     * 
+     * @throws IOException
+     */
+    private static void writeString(String value, OutputStream os) throws 
IOException {
+
         if (value == null) {
             os.write('N');
-        }
-        else {
+        } else {
             int length = value.length();
             int offset = 0;
 
             while (length > 0x8000) {
                 int sublen = 0x8000;
 
+                // chunk can't end in high surrogate
+                char tail = value.charAt(offset + sublen - 1);
+
+                if (0xd800 <= tail && tail <= 0xdbff) {
+                    sublen--;
+                }
+
                 os.write('s');
                 os.write(sublen >> 8);
                 os.write(sublen);
@@ -74,35 +147,31 @@
         }
     }
 
-    public static void startReply(OutputStream os) throws IOException {
-        os.write('r');
-        os.write(1);
-        os.write(0);
-    }
-
-    public static void completeReply(OutputStream os) throws IOException {
-        os.write('z');
-    }
-
-    public static void writeFault(String code, String message, String detail, 
OutputStream os)
+    /**
+     * Prints a string (or parts of it) to the provided output stream encoded 
as UTF-8.<br>
+     *
+     * @param v the string to print to the output stream
+     * @param offset an offset indicating at which character of the string to 
start
+     * @param length the number of characters to write
+     * 
+     * @throws IOException if an error occurs writing to the output stream
+     */
+    private static void printString(String v, int offset, int length, 
OutputStream os)
             throws IOException {
-
-        startReply(os);
-
-        os.write('f');
-        writeString("code", os);
-        writeString(code, os);
-
-        writeString("message", os);
-        writeString(message, os);
-
-        if (detail != null) {
-            writeString("detail", os);
-            writeString(detail, os);
-        }
         
-        os.write('z');
+        for (int i = 0; i < length; i++) {
+            char ch = v.charAt(i + offset);
 
-        completeReply(os);
+            if (ch < 0x80) {
+                os.write(ch);
+            } else if (ch < 0x800) {
+                os.write(0xc0 + ((ch >> 6) & 0x1f));
+                os.write(0x80 + (ch & 0x3f));
+            } else {
+                os.write(0xe0 + ((ch >> 12) & 0xf));
+                os.write(0x80 + ((ch >> 6) & 0x3f));
+                os.write(0x80 + (ch & 0x3f));
+            }
+        }
     }
 }

Modified: 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageBuilderTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageBuilderTest.java?rev=754464&r1=754463&r2=754464&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageBuilderTest.java
 (original)
+++ 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageBuilderTest.java
 Sat Mar 14 15:51:26 2009
@@ -19,14 +19,6 @@
 
 package org.apache.synapse.format.hessian;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Arrays;
-
-import javax.activation.DataHandler;
-
-import junit.framework.TestCase;
-
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMText;
@@ -41,6 +33,14 @@
 import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
 import org.apache.synapse.util.SynapseBinaryDataSource;
 
+import javax.activation.DataHandler;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
 /**
  * Test the HessianMessageBuilder using several Hessian example
  * messages (including normal and fault messages) provided as 
@@ -48,12 +48,6 @@
  */
 public class HessianMessageBuilderTest extends TestCase {
 
-    private static final String HESSIAN_DUMMY_FAULT_V1_RESPONSE = 
"hessianDummyFaultResponse_V1.bin";
-
-    private static final String HESSIAN_DUMMY_REQUEST = 
"hessianDummyRequest.bin";
-
-    private static final String HESSIAN_INCOMPLETE = "hessianIncomplete.bin";
-    
     public void testProcessDocumentFaultWithSynEnv() throws IOException {
         SynapseEnvironment synEnv = new Axis2SynapseEnvironment(new 
ConfigurationContext(
                 new AxisConfiguration()), new SynapseConfiguration());
@@ -73,29 +67,21 @@
     public void testProcessDocumentWithoutSynEnv() throws IOException {
         testProcessDocument(null);
     }
-    
+
     public void testIncompleteHessianMessage() throws IOException {
-        test(HESSIAN_INCOMPLETE, null);
+        test(HessianTestHelper.HESSIAN_INCOMPLETE, null);
     }
-    
+
     private MessageContext test(String testMessageName, SynapseEnvironment 
synEnv)
             throws IOException {
 
-        // create mock message context
-        MessageContext msgContext = new MessageContext();
-        AxisConfiguration axisConfig = new AxisConfiguration();
-        ConfigurationContext configContext = new 
ConfigurationContext(axisConfig);
-        axisConfig.addParameter(SynapseConstants.SYNAPSE_ENV, synEnv);
-        msgContext.setConfigurationContext(configContext);
-
-        HessianMessageBuilder messageBuilder = new HessianMessageBuilder();
-        InputStream is = getClass().getResourceAsStream(testMessageName);
-        OMElement element = messageBuilder.processDocument(is,
-                HessianConstants.HESSIAN_CONTENT_TYPE, msgContext);
+        HessianTestHelper hessianTestHelper = new HessianTestHelper();
+        MessageContext msgContext = 
hessianTestHelper.createAxis2MessageContext(synEnv);
+        OMElement element = 
hessianTestHelper.buildHessianTestMessage(testMessageName, msgContext);
         OMNode hessianNode = element.getFirstOMChild();
         OMText hessianTextNode = (OMText) hessianNode;
-        SynapseBinaryDataSource synapseBinaryDataSource = 
(SynapseBinaryDataSource) ((DataHandler) hessianTextNode
-                .getDataHandler()).getDataSource();
+        SynapseBinaryDataSource synapseBinaryDataSource = 
(SynapseBinaryDataSource) 
+            ((DataHandler) hessianTextNode.getDataHandler()).getDataSource();
         InputStream inputStream = synapseBinaryDataSource.getInputStream();
         byte[] originalByteArray = 
IOUtils.toByteArray(getClass().getResourceAsStream(
                 testMessageName));
@@ -107,14 +93,15 @@
 
     private void testProcessDocumentFault(SynapseEnvironment synEnv) throws 
IOException {
 
-        MessageContext axis2MessageContext = 
test(HESSIAN_DUMMY_FAULT_V1_RESPONSE, synEnv);
-        assertEquals(SynapseConstants.TRUE, axis2MessageContext
-                .getProperty(BaseConstants.FAULT_MESSAGE));
+        MessageContext axis2MessageContext = test(
+                HessianTestHelper.HESSIAN_DUMMY_FAULT_V1_RESPONSE, synEnv);
+        assertEquals(SynapseConstants.TRUE, 
+                axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE));
     }
 
     private void testProcessDocument(SynapseEnvironment synEnv) throws 
IOException {
 
-        MessageContext axis2MessageContext = test(HESSIAN_DUMMY_REQUEST, 
synEnv);
+        MessageContext axis2MessageContext = 
test(HessianTestHelper.HESSIAN_DUMMY_REQUEST, synEnv);
         
assertNull(axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE));
     }
 

Added: 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageFormatterTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageFormatterTest.java?rev=754464&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageFormatterTest.java
 (added)
+++ 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianMessageFormatterTest.java
 Sat Mar 14 15:51:26 2009
@@ -0,0 +1,115 @@
+/*
+ *  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.format.hessian;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.Constants;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.transport.base.BaseConstants;
+import org.apache.commons.io.IOUtils;
+import org.apache.synapse.SynapseConstants;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+/**
+ * Test the HessianMessageFormatter with a test message build using
+ * the HessianMessageBuilder as well as a dummy SOAP fault written out,
+ * read with HessianMessageBuilder and marked as a fault.
+ */
+public class HessianMessageFormatterTest extends TestCase {
+
+    public void testWriteToWithMessage() throws IOException {
+
+        String testMessageName = HessianTestHelper.HESSIAN_DUMMY_REQUEST;
+        HessianTestHelper testHelper = new HessianTestHelper();
+        MessageContext msgContext = testHelper.createAxis2MessageContext(null);
+        OMElement element = 
testHelper.buildHessianTestMessage(testMessageName, msgContext);
+        testHelper.addBodyToMessageContext(msgContext, element);
+        byte[] originalBytes = 
testHelper.getTestMessageAsBytes(testMessageName);
+        byte[] formatterBytes = writeTo(msgContext);
+
+        assertEquals(originalBytes.length, formatterBytes.length);
+        assertTrue(Arrays.equals(originalBytes, formatterBytes));
+    }
+
+    public void testWriteToWithSoapFault() throws IOException {
+
+        HessianTestHelper testHelper = new HessianTestHelper();
+        MessageContext faultMsgContext = 
testHelper.createAxis2MessageContext(null);
+        testHelper.addSoapFaultToMessageContext(faultMsgContext, "500", 
"test", "testDetail");
+        byte[] formatterBytes = writeTo(faultMsgContext);
+        HessianMessageBuilder messageBuilder = new HessianMessageBuilder();
+        MessageContext msgContext = testHelper.createAxis2MessageContext(null);
+        messageBuilder.processDocument(
+            IOUtils.toInputStream(new String(formatterBytes, 
HessianTestHelper.CHARSET_ENCODING)), 
+            HessianConstants.HESSIAN_CONTENT_TYPE, msgContext);
+        assertTrue(formatterBytes.length > 0);
+        assertEquals(SynapseConstants.TRUE, 
msgContext.getProperty(BaseConstants.FAULT_MESSAGE));
+        
+    }
+
+    public void testGetBytes() {
+
+        try {
+            HessianMessageFormatter formatter = new HessianMessageFormatter();
+            OMOutputFormat format = new OMOutputFormat();
+            HessianTestHelper testHelper = new HessianTestHelper();
+            MessageContext msgContext = 
testHelper.createAxis2MessageContext(null);
+            formatter.getBytes(msgContext, format);
+            fail("getBytes() should have thrown an AxisFault!");
+        } catch (AxisFault fault) {
+            assertTrue(fault.getMessage().length() > 0);
+        }
+    }
+
+    public void testGetContentType() throws AxisFault {
+        HessianMessageFormatter formatter = new HessianMessageFormatter();
+        OMOutputFormat format = new OMOutputFormat();
+        String soapActionString = "soapAction";
+        HessianTestHelper testHelper = new HessianTestHelper();
+        MessageContext msgContext = testHelper.createAxis2MessageContext(null);
+        String contentType = formatter.getContentType(msgContext, format, 
soapActionString);
+        assertEquals(HessianConstants.HESSIAN_CONTENT_TYPE, contentType);
+
+        msgContext.setProperty(Constants.Configuration.CONTENT_TYPE,
+            HessianConstants.HESSIAN_CONTENT_TYPE);
+        format.setCharSetEncoding(HessianTestHelper.CHARSET_ENCODING);
+        contentType = formatter.getContentType(msgContext, format, 
soapActionString);
+        String expectedContentType = HessianConstants.HESSIAN_CONTENT_TYPE + 
"; charset="
+            + HessianTestHelper.CHARSET_ENCODING;
+        assertEquals(expectedContentType, contentType);
+    }
+
+    private byte[] writeTo(MessageContext msgContext) throws IOException {
+
+        HessianMessageFormatter formatter = new HessianMessageFormatter();
+        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
+        formatter.writeTo(msgContext, null, out, false);
+
+        return out.toByteArray();
+    }
+
+}

Added: 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianTestHelper.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianTestHelper.java?rev=754464&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianTestHelper.java
 (added)
+++ 
synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/format/hessian/HessianTestHelper.java
 Sat Mar 14 15:51:26 2009
@@ -0,0 +1,95 @@
+package org.apache.synapse.format.hessian;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.soap.SOAPBody;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.SOAPFault;
+import org.apache.axiom.soap.SOAPFaultCode;
+import org.apache.axiom.soap.SOAPFaultDetail;
+import org.apache.axiom.soap.SOAPFaultReason;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.commons.io.IOUtils;
+import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.core.SynapseEnvironment;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Helper class to support both HessianMessageBuilder and 
HessianMessageFormatter tests.
+ */
+public class HessianTestHelper {
+    
+    static final String HESSIAN_DUMMY_FAULT_V1_RESPONSE = 
"hessianDummyFaultResponse_V1.bin";
+
+    static final String HESSIAN_DUMMY_REQUEST = "hessianDummyRequest.bin";
+
+    static final String HESSIAN_INCOMPLETE = "hessianIncomplete.bin";
+    
+    static final String CHARSET_ENCODING = "UTF-8";
+
+    public MessageContext createAxis2MessageContext(SynapseEnvironment synEnv) 
throws AxisFault {
+        MessageContext msgContext = new MessageContext();
+        AxisConfiguration axisConfig = new AxisConfiguration();
+        ConfigurationContext configContext = new 
ConfigurationContext(axisConfig);
+        if (synEnv != null) {
+            axisConfig.addParameter(SynapseConstants.SYNAPSE_ENV, synEnv);
+        }
+        msgContext.setConfigurationContext(configContext);
+
+        return msgContext;
+    }
+
+    public void addBodyToMessageContext(MessageContext msgContext, OMElement 
element) throws AxisFault { 
+    
+        SOAPEnvelope envelope = 
OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
+        SOAPBody body = envelope.getBody();
+        body.addChild(element);
+        msgContext.setEnvelope(envelope);
+    }
+    
+    public void addSoapFaultToMessageContext(MessageContext msgContext, String 
faultCode, 
+            String faultReason, String faultDetail) throws AxisFault {
+        
+        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
+        SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope();       
+        SOAPFault soapFault = faultEnvelope.getBody().getFault();
+
+        SOAPFaultCode soapFaultCode = factory.createSOAPFaultCode();
+        soapFaultCode.setText(faultCode);
+        soapFault.setCode(soapFaultCode);
+        
+        SOAPFaultReason soapFaultReason = factory.createSOAPFaultReason();
+        soapFaultReason.setText(faultReason);
+        soapFault.setReason(soapFaultReason);
+        
+        SOAPFaultDetail soapFaultDetail = factory.createSOAPFaultDetail();
+        soapFaultDetail.setText(faultDetail);
+        soapFault.setDetail(soapFaultDetail);
+        
+        msgContext.setEnvelope(faultEnvelope);
+    }
+    
+    public byte[] getTestMessageAsBytes(String testMessageName) throws 
IOException {
+        
+        InputStream is = getClass().getResourceAsStream(testMessageName);
+        return IOUtils.toByteArray(is);
+    }
+    
+
+    public OMElement buildHessianTestMessage(String testMessageName, 
MessageContext msgContext)
+            throws AxisFault {
+
+        HessianMessageBuilder messageBuilder = new HessianMessageBuilder();
+        InputStream is = getClass().getResourceAsStream(testMessageName);
+        OMElement element = messageBuilder.processDocument(is,
+                HessianConstants.HESSIAN_CONTENT_TYPE, msgContext);
+
+        return element;
+    }
+}


Reply via email to