synapse-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ind...@apache.org
Subject svn commit: r648968 - in /synapse/trunk/java: modules/core/src/main/java/org/apache/synapse/config/ modules/core/src/main/java/org/apache/synapse/config/xml/ modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/ modules/core/src/main/java...
Date Thu, 17 Apr 2008 06:25:08 GMT
Author: indika
Date: Wed Apr 16 23:25:02 2008
New Revision: 648968

URL: http://svn.apache.org/viewvc?rev=648968&view=rev
Log:
Add non-xml text content  and binary content handling

Added:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseBinaryDataSource.java
  (contents, props changed)
      - copied, changed from r648536, synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianDataSource.java
Removed:
    synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianDataSource.java
Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EntryFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
    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/mediators/bsf/ScriptMediator.java
    synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransform.js

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java?rev=648968&r1=648967&r2=648968&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
Wed Apr 16 23:25:02 2008
@@ -19,23 +19,21 @@
 
 package org.apache.synapse.config;
 
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.*;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.synapse.SynapseConstants;
 import org.apache.synapse.SynapseException;
+import org.apache.synapse.util.SynapseBinaryDataSource;
 import org.xml.sax.InputSource;
 
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.stream.StreamSource;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import javax.activation.DataHandler;
+import java.io.*;
 import java.net.*;
 
 public class SynapseConfigUtils {
@@ -53,17 +51,27 @@
         if (o == null) {
             handleException("Cannot convert null to a StreamSource");
 
-        } else if (o instanceof OMNode) {
-            OMNode omNode = (OMNode) o;
+        } else if (o instanceof OMElement) {
+            OMElement omElement = (OMElement) o;
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             try {
-                omNode.serialize(baos);
+                omElement.serialize(baos);
                 return new StreamSource(new ByteArrayInputStream(baos.toByteArray()));
             } catch (XMLStreamException e) {
                 handleException("Error converting to a StreamSource", e);
             }
 
+        } else if (o instanceof OMText) {
+            DataHandler dataHandler = (DataHandler) ((OMText) o).getDataHandler();
+            if (dataHandler != null) {
+                try {
+                    return new StreamSource(dataHandler.getInputStream());
+                } catch (IOException e) {
+                    handleException("Error in reading content as a stream ");
+                }
+            }
         } else {
+
             handleException("Cannot convert object to a StreamSource");
         }
         return null;
@@ -84,6 +92,15 @@
                 handleException("Error converting to a StreamSource", e);
             }
 
+        } else if (o instanceof OMText) {
+            DataHandler dataHandler = (DataHandler) ((OMText) o).getDataHandler();
+            if (dataHandler != null) {
+                try {
+                    return dataHandler.getInputStream();
+                } catch (IOException e) {
+                    handleException("Error in reading content as a stream ");
+                }
+            }
         } else if (o instanceof URI) {
             try {
                 return ((URI) (o)).toURL().openStream();
@@ -115,13 +132,13 @@
                     String path = url.getPath();
                     if (log.isDebugEnabled()) {
                         log.debug("Can not open a connection to the URL with a path :" +
-                            path);
+                                path);
                     }
                     String synapseHome = System.getProperty(SynapseConstants.SYNAPSE_HOME);
                     if (synapseHome != null) {
                         if (log.isDebugEnabled()) {
                             log.debug("Trying  to resolve an absolute path of the " +
-                                " URL using the synapse.home : " + synapseHome);
+                                    " URL using the synapse.home : " + synapseHome);
                         }
                         if (synapseHome.endsWith("/")) {
                             synapseHome = synapseHome.substring(0, synapseHome.lastIndexOf("/"));
@@ -132,7 +149,7 @@
                         } catch (IOException e) {
                             if (log.isDebugEnabled()) {
                                 log.debug("Faild to resolve an absolute path of the " +
-                                    " URL using the synapse.home : " + synapseHome);
+                                        " URL using the synapse.home : " + synapseHome);
                             }
                             log.warn("IO Error reading from URL " + url.getPath() + e);
                         }
@@ -144,11 +161,11 @@
             }
             URLConnection urlc = url.openConnection();
             XMLToObjectMapper xmlToObject =
-                getXmlToObjectMapper(urlc.getContentType());
-
+                    getXmlToObjectMapper(urlc.getContentType());
+            InputStream inputStream = urlc.getInputStream();
             try {
                 XMLStreamReader parser = XMLInputFactory.newInstance().
-                    createXMLStreamReader(urlc.getInputStream());
+                        createXMLStreamReader(inputStream);
                 StAXOMBuilder builder = new StAXOMBuilder(parser);
                 OMElement omElem = builder.getDocumentElement();
 
@@ -164,7 +181,12 @@
 
             } catch (XMLStreamException e) {
                 log.warn("Content at URL : " + url + " is non XML..");
-                return urlc.getContent();
+                return readNonXML(url);
+            } catch (OMException e) {
+                log.warn("Content at URL : " + url + " is non XML..");
+                return readNonXML(url);
+            } finally {
+                inputStream.close();
             }
 
         } catch (IOException e) {
@@ -174,13 +196,40 @@
     }
 
     /**
+     * Helper method to handle non-XMl resources
+     *
+     * @param url The resource url
+     * @return The content as an OMNode
+     */
+    public static OMNode readNonXML(URL url) {
+
+        try {
+            // Open a new connection
+            URLConnection newConnection = url.openConnection();
+
+            BufferedInputStream newInputStream = new BufferedInputStream(
+                    newConnection.getInputStream());
+
+            OMFactory omFactory = OMAbstractFactory.getOMFactory();
+            return omFactory.createOMText(
+                    new DataHandler(new SynapseBinaryDataSource(newInputStream,
+                            newConnection.getContentType())), true);
+
+        } catch (IOException e) {
+            return null;
+        }
+
+    }
+
+    /**
      * Return an OMElement from a URL source
      *
      * @param urlStr a URL string
      * @return an OMElement of the resource
      * @throws IOException for invalid URL's or IO errors
      */
-    public static OMElement getOMElementFromURL(String urlStr) throws IOException {
+    public static OMNode getOMElementFromURL(String urlStr) throws IOException {
+
         URL url = getURLFromPath(urlStr);
         if (url == null) {
             return null;
@@ -189,22 +238,24 @@
         conn.setReadTimeout(getReadTimeout());
         conn.setConnectTimeout(getConnectionTimeout());
         conn.setRequestProperty("Connection", "close"); // if http is being used
-        InputStream urlInStream = conn.getInputStream();
-
-        if (urlInStream != null) {
+        BufferedInputStream urlInStream = new BufferedInputStream(
+                conn.getInputStream());
+        try {
+            StAXOMBuilder builder = new StAXOMBuilder(urlInStream);
+            OMElement doc = builder.getDocumentElement();
+            doc.build();
+            return doc;
+        } catch (Exception e) {
+            log.warn("Error parsing resource at URL : " + url +
+                    " as XML");
+            Object content = readNonXML(url);
+            if (content instanceof OMNode) {
+                return (OMNode) content;
+            }
+        } finally {
             try {
-                StAXOMBuilder builder = new StAXOMBuilder(urlInStream);
-                OMElement doc = builder.getDocumentElement();
-                doc.build();
-                return doc;
-            } catch (Exception e) {
-                handleException("Error parsing resource at URL : " + url +
-                    " as XML", e);
-            } finally {
-                try {
-                    urlInStream.close();
-                } catch (IOException ignore) {
-                }
+                urlInStream.close();
+            } catch (IOException ignore) {
             }
         }
         return null;
@@ -228,7 +279,8 @@
             conn.setReadTimeout(getReadTimeout());
             conn.setConnectTimeout(getConnectionTimeout());
             conn.setRequestProperty("Connection", "close"); // if http is being used
-            InputStream urlInStream = conn.getInputStream();
+            BufferedInputStream urlInStream = new BufferedInputStream(
+                    conn.getInputStream());
             return new InputSource(urlInStream);
         } catch (MalformedURLException e) {
             handleException("Invalid URL ' " + uri + " '", e);
@@ -309,7 +361,7 @@
                 } catch (IOException ignored) {
                     if (log.isDebugEnabled()) {
                         log.debug("Can not open a connection to the URL with a path :" +
-                            path);
+                                path);
                     }
                     String synapseHome = System.getProperty(SynapseConstants.SYNAPSE_HOME);
                     if (synapseHome != null) {
@@ -318,18 +370,18 @@
                         }
                         if (log.isDebugEnabled()) {
                             log.debug("Trying  to resolve an absolute path of the " +
-                                " URL using the synapse.home : " + synapseHome);
+                                    " URL using the synapse.home : " + synapseHome);
                         }
                         try {
                             url = new URL(url.getProtocol() + ":" + synapseHome + "/" +
-                                url.getPath());
+                                    url.getPath());
                             url.openStream();
                         } catch (MalformedURLException e) {
                             handleException("Invalid URL reference " + url.getPath() + e);
                         } catch (IOException e) {
                             if (log.isDebugEnabled()) {
                                 log.debug("Faild to resolve an absolute path of the " +
-                                    " URL using the synapse.home : " + synapseHome);
+                                        " URL using the synapse.home : " + synapseHome);
                             }
                             log.warn("IO Error reading from URL : " + url.getPath() + e);
                         }

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EntryFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EntryFactory.java?rev=648968&r1=648967&r2=648968&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EntryFactory.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EntryFactory.java
Wed Apr 16 23:25:02 2008
@@ -47,7 +47,8 @@
         } else {
 
             Entry entry = new Entry(key.getAttributeValue());
-            String src  = elem.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE,
"src"));
+            String src  = elem.getAttributeValue(
+                    new QName(XMLConfigConstants.NULL_NAMESPACE, "src"));
 
             // if a src attribute is present, this is a URL source resource,
             // it would now be loaded from the URL source, as all static properties

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java?rev=648968&r1=648967&r2=648968&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java
Wed Apr 16 23:25:02 2008
@@ -132,17 +132,18 @@
 
                 wsdlEndpoint.setWsdlURI(wsdlURI.trim());
                 try {
-                    OMElement wsdlOM = SynapseConfigUtils.getOMElementFromURL(
-                        new URL(wsdlURI).toString());
-                    if (wsdlOM != null) {
-                        OMNamespace ns = wsdlOM.getNamespace();
+                    OMNode wsdlOM = SynapseConfigUtils.getOMElementFromURL(
+                            new URL(wsdlURI).toString());
+                    if (wsdlOM != null && wsdlOM instanceof OMElement) {
+                        OMElement omElement = (OMElement) wsdlOM;
+                        OMNamespace ns = omElement.getNamespace();
                         if (ns != null) {
-                            String nsUri = wsdlOM.getNamespace().getNamespaceURI();
+                            String nsUri = omElement.getNamespace().getNamespaceURI();
                             if (org.apache.axis2.namespace.Constants.NS_URI_WSDL11.equals(nsUri))
{
 
                                 endpoint = new WSDL11EndpointBuilder().
                                         createEndpointDefinitionFromWSDL(
-                                                wsdlURI.trim(),wsdlOM, serviceName, portName);
+                                                wsdlURI.trim(), omElement, serviceName, portName);
 
                             } else if (WSDL2Constants.WSDL_NAMESPACE.equals(nsUri)) {
                                 //endpoint = new WSDL20EndpointBuilder().
@@ -154,7 +155,7 @@
                     }
                 } catch (Exception e) {
                     handleException("Couldn't create endpoint from the given WSDL URI : "
-                        + e.getMessage(), e);
+                            + e.getMessage(), e);
                 }
             }
 

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java?rev=648968&r1=648967&r2=648968&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/core/axis2/ProxyService.java
Wed Apr 16 23:25:02 2008
@@ -21,6 +21,7 @@
 
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.description.*;
 import org.apache.axis2.engine.AxisConfiguration;
@@ -236,7 +237,10 @@
         } else if (wsdlURI != null) {
             try {
                 URL url = wsdlURI.toURL();
-                wsdlElement = SynapseConfigUtils.getOMElementFromURL(url.toString());
+                OMNode node = SynapseConfigUtils.getOMElementFromURL(url.toString());
+                if (node instanceof OMElement) {
+                    wsdlElement = (OMElement) node;
+                }
             } catch (MalformedURLException e) {
                 handleException("Malformed URI for wsdl", e);
             } catch (IOException e) {

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java?rev=648968&r1=648967&r2=648968&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
Wed Apr 16 23:25:02 2008
@@ -20,6 +20,7 @@
 package org.apache.synapse.registry.url;
 
 import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -49,39 +50,77 @@
     private static final Log log = LogFactory.getLog(SimpleURLRegistry.class);
 
     private static final int MAX_KEYS = 200;
-    private String root ="";
+    private String root = "";
 
     public OMNode lookup(String key) {
 
         log.info("==> Repository fetch of resource with key : " + key);
-        URLConnection urlc = null;
+
+        URL url = SynapseConfigUtils.getURLFromPath(root + key);
+        if (url == null) {
+            return null;
+        }
+
+        BufferedInputStream inputStream;
         try {
-            URL url = SynapseConfigUtils.getURLFromPath(root + key);
-            if (url == null) {
-                return null;
-            }
-            urlc = url.openConnection();
-            urlc.connect();
+            URLConnection connection = url.openConnection();
+            connection.connect();
+            inputStream = new BufferedInputStream(connection.getInputStream());
         } catch (IOException e) {
             return null;
         }
 
-        try {
-            XMLStreamReader parser = XMLInputFactory.newInstance().
-                    createXMLStreamReader(urlc.getInputStream());
-            StAXOMBuilder builder = new StAXOMBuilder(parser);
-            return builder.getDocumentElement();
+        OMNode result = null;
+
+        if (inputStream != null) {
+
+            try {
+
+                XMLStreamReader parser = XMLInputFactory.newInstance().
+                        createXMLStreamReader(inputStream);
+                StAXOMBuilder builder = new StAXOMBuilder(parser);
+                result = builder.getDocumentElement();
+
+            } catch (OMException ignored) {
+
+                if (log.isDebugEnabled()) {
+                    log.debug("The resource at the provided URL isn't " +
+                            "well-formed XML,So,takes it as a text");
+                }
+
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    log.error("Error in closing the input stream. ", e);
+                }
+                result = SynapseConfigUtils.readNonXML(url);
+
+            } catch (XMLStreamException ignored) {
+
+                if (log.isDebugEnabled()) {
+                    log.debug("The resource at the provided URL isn't " +
+                            "well-formed XML,So,takes it as a text");
+                }
+
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    log.error("Error in closing the input stream. ", e);
+                }
+                result = SynapseConfigUtils.readNonXML(url);
+
+            } finally {
+
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    log.error("Error in closing the input stream.", e);
+                }
+
+            }
 
-        } catch (MalformedURLException e) {
-            handleException("Invalid URL reference " + root + key, e);
-        } catch (FileNotFoundException fnf) {
-            return null;
-        } catch (IOException e) {
-            handleException("IO Error reading from URL " + root + key, e);
-        } catch (XMLStreamException e) {
-            handleException("XML Error reading from URL " + root + key, e);
         }
-        return null;
+        return result;
     }
 
     public RegistryEntry getRegistryEntry(String key) {
@@ -148,7 +187,7 @@
 
 
     public void delete(String path) {
-       //TODO
+        //TODO
     }
 
     public void newResource(String path, boolean isDirectory) {

Copied: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseBinaryDataSource.java
(from r648536, synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianDataSource.java)
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseBinaryDataSource.java?p2=synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseBinaryDataSource.java&p1=synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianDataSource.java&r1=648536&r2=648968&rev=648968&view=diff
==============================================================================
--- synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/format/hessian/HessianDataSource.java
(original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseBinaryDataSource.java
Wed Apr 16 23:25:02 2008
@@ -17,7 +17,7 @@
  *  under the License.
  */
 
-package org.apache.synapse.format.hessian;
+package org.apache.synapse.util;
 
 import org.apache.synapse.util.TemporaryData;
 
@@ -29,7 +29,7 @@
  *
  * @see javax.activation.DataSource
  */
-public class HessianDataSource implements DataSource {
+public class SynapseBinaryDataSource implements DataSource {
 
     /** Content type of the DataSource */
     private String contentType;
@@ -42,41 +42,40 @@
      * data is stored in a byte[] or in a temp file format inorder to be able to get the
stream any
      * number of time, otherwise the stream can only be read once
      *
-	 * @param contentType message content type
-	 * @param inputstream contains the Hessian message for later retrieval
-	 * @throws IOException failure in reading from the InputStream
-	 */
-	public HessianDataSource(String contentType, InputStream inputstream) throws IOException
{
+     * @param inputstream contains the Hessian message for later retrieval
+     * @param contentType message content type
+     * @throws IOException failure in reading from the InputStream
+     */
+    public SynapseBinaryDataSource(InputStream inputstream, String contentType) throws IOException
{
 
-		this.contentType = contentType;
+        this.contentType = contentType;
         this.data = new TemporaryData(4, 1024, "tmp_", ".dat");
 
         OutputStream out = this.data.getOutputStream();
         byte[] buffer = new byte[1024];
         int c;
-        while ((c=inputstream.read(buffer)) != -1) {
+        while ((c = inputstream.read(buffer)) != -1) {
             out.write(buffer, 0, c);
         }
         out.flush();
         out.close();
         inputstream.close();
-	}
+    }
 
-	public String getContentType() {
-		return contentType;
-	}
-
-	public InputStream getInputStream() throws IOException {
-		return data.getInputStream();
-	}
-
-	public String getName() {
-		return HessianConstants.HESSIAN_DATA_SOURCE_NAME;
-	}
-
-	public OutputStream getOutputStream() throws IOException {
-		throw new UnsupportedOperationException("OutputStream can " +
-                "not be retrieved from a HessianDataSource");
-	}
+    public String getContentType() {
+        return contentType;
+    }
+
+    public InputStream getInputStream() throws IOException {
+        return data.getInputStream();
+    }
+
+    public String getName() {
+        return this.getClass().getName();
+    }
+
+    public OutputStream getOutputStream() throws IOException {
+        return data.getOutputStream();
+    }
 
 }

Propchange: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SynapseBinaryDataSource.java
------------------------------------------------------------------------------
    svn:executable = *

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=648968&r1=648967&r2=648968&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
Wed Apr 16 23:25:02 2008
@@ -25,6 +25,7 @@
 import org.apache.axis2.context.MessageContext;
 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.IOException;
@@ -35,7 +36,7 @@
  * and this builds the HessianDataSource to represent the hessian message inside the SOAP
info-set
  *
  * @see org.apache.axis2.builder.Builder
- * @see org.apache.synapse.format.hessian.HessianDataSource
+ * @see org.apache.synapse.util.SynapseBinaryDataSource
  */
 public class HessianMessageBuilder implements Builder {
 
@@ -70,7 +71,7 @@
 
             // add Hessian data inside a data handler
             DataHandler dataHandler = new DataHandler(
-                    new HessianDataSource(contentType, inputStream));
+                    new SynapseBinaryDataSource(inputStream,contentType));
             OMText textData = factory.createOMText(dataHandler, true);
             element.addChild(textData);
 

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=648968&r1=648967&r2=648968&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
Wed Apr 16 23:25:02 2008
@@ -31,6 +31,7 @@
 import org.apache.axis2.transport.http.util.URLTemplatingUtil;
 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;
@@ -45,7 +46,7 @@
  * axis2/synapse and this formats the HessianDataSource to a hessian message
  *
  * @see org.apache.axis2.transport.MessageFormatter
- * @see org.apache.synapse.format.hessian.HessianDataSource
+ * @see org.apache.synapse.util.SynapseBinaryDataSource
  */
 public class HessianMessageFormatter implements MessageFormatter {
 
@@ -139,7 +140,7 @@
 
                     OMText tempNode = (OMText) hessianElement;
                     if (tempNode.getDataHandler() != null && ((DataHandler) tempNode
-                            .getDataHandler()).getDataSource() instanceof HessianDataSource)
{
+                            .getDataHandler()).getDataSource() instanceof SynapseBinaryDataSource)
{
 
                         hessianOMText = tempNode;
                     }
@@ -150,10 +151,10 @@
 
                 try {
 
-                    HessianDataSource hessianDataSource = (HessianDataSource) (
+                    SynapseBinaryDataSource synapseBinaryDataSource = (SynapseBinaryDataSource)
(
                             (DataHandler) hessianOMText.getDataHandler()).getDataSource();
 
-                    InputStream inputStream = hessianDataSource.getInputStream();
+                    InputStream inputStream = synapseBinaryDataSource.getInputStream();
                     BufferedOutputStream outputStream = new BufferedOutputStream(out);
 
                     byte[] buffer = new byte[1024];

Modified: synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java?rev=648968&r1=648967&r2=648968&view=diff
==============================================================================
--- synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java
(original)
+++ synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/bsf/ScriptMediator.java
Wed Apr 16 23:25:02 2008
@@ -20,19 +20,21 @@
 package org.apache.synapse.mediators.bsf;
 
 import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMText;
 import org.apache.bsf.xml.XMLHelper;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.synapse.SynapseConstants;
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.config.Entry;
 import org.apache.synapse.mediators.AbstractMediator;
 
 import javax.script.*;
+import javax.activation.DataHandler;
 import java.util.TreeMap;
 import java.util.Map;
 import java.util.Iterator;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.IOException;
 
 /**
  * A Synapse mediator that calls a function in any scripting language supported by the BSF.
@@ -280,6 +282,7 @@
 
     /**
      * Prepares the mediator for the invocation of an external script
+     *
      * @throws ScriptException
      */
     protected synchronized void prepareExternalScript(MessageContext synCtx) throws ScriptException
{
@@ -295,34 +298,83 @@
                 Object o = synCtx.getEntry(key);
                 if (o instanceof OMElement) {
                     scriptSourceCode = ((OMElement) (o)).getText();
+                    scriptEngine.eval(scriptSourceCode);
                 } else if (o instanceof String) {
                     scriptSourceCode = (String) o;
+                    scriptEngine.eval(scriptSourceCode);
+                } else if (o instanceof OMText) {
+
+                    DataHandler dataHandler = (DataHandler) ((OMText) o).getDataHandler();
+                    if (dataHandler != null) {
+                        BufferedReader reader = null;
+                        try {
+                            reader = new BufferedReader(
+                                    new InputStreamReader(dataHandler.getInputStream()));
+                            scriptEngine.eval(reader);
+
+                        } catch (IOException e) {
+                            handleException("Error in reading script as a stream ", e, synCtx);
+                        } finally {
+
+                            if (reader != null) {
+                                try {
+                                    reader.close();
+                                } catch (IOException e) {
+                                    handleException("Error in closing input stream ", e,
synCtx);
+                                }
+                            }
+
+                        }
+                    }
                 }
 
-                scriptEngine.eval(scriptSourceCode);
             }
         }
 
         // load <include /> scripts; reload each script if needed
-    	for(Iterator iter = includes.keySet().iterator(); iter.hasNext();) {
-    		String includeKey = (String) iter.next();
-    		String includeSourceCode = (String) includes.get(includeKey);
+        for (Iterator iter = includes.keySet().iterator(); iter.hasNext();) {
+            String includeKey = (String) iter.next();
+            String includeSourceCode = (String) includes.get(includeKey);
             Entry includeEntry = synCtx.getConfiguration().getEntryDefinition(includeKey);
             boolean includeEntryNeedsReload = (entry != null) && entry.isDynamic()
-            			&& (!entry.isCached() || entry.isExpired());
+                    && (!entry.isCached() || entry.isExpired());
             synchronized (resourceLock) {
                 if (includeSourceCode == null || needsReload) {
                     Object o = synCtx.getEntry(includeKey);
                     if (o instanceof OMElement) {
-                    	includeSourceCode = ((OMElement) (o)).getText();
+                        includeSourceCode = ((OMElement) (o)).getText();
+                        scriptEngine.eval(includeSourceCode);
                     } else if (o instanceof String) {
-                    	includeSourceCode = (String) o;
+                        includeSourceCode = (String) o;
+                        scriptEngine.eval(includeSourceCode);
+                    } else if (o instanceof OMText) {
+
+                        DataHandler dataHandler = (DataHandler) ((OMText) o).getDataHandler();
+                        if (dataHandler != null) {
+                            BufferedReader reader = null;
+                            try {
+                                reader = new BufferedReader(
+                                        new InputStreamReader(dataHandler.getInputStream()));
+                                scriptEngine.eval(reader);
+
+                            } catch (IOException e) {
+                                handleException("Error in reading script as a stream ", e,
synCtx);
+                            } finally {
+
+                                if (reader != null) {
+                                    try {
+                                        reader.close();
+                                    } catch (IOException e) {
+                                        handleException("Error in closing input" +
+                                                " stream ", e, synCtx);
+                                    }
+                                }
+                            }
+                        }
                     }
-                    includes.put(includeKey, includeSourceCode);
-                    scriptEngine.eval(includeSourceCode);
                 }
             }
-    	}
+        }
     }
 
     protected void initScriptEngine() {

Modified: synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransform.js
URL: http://svn.apache.org/viewvc/synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransform.js?rev=648968&r1=648967&r2=648968&view=diff
==============================================================================
--- synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransform.js (original)
+++ synapse/trunk/java/repository/conf/sample/resources/script/stockquoteTransform.js Wed
Apr 16 23:25:02 2008
@@ -1,4 +1,4 @@
-<!--
+/*<!--
   ~  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
@@ -15,8 +15,7 @@
   ~  KIND, either express or implied.  See the License for the
   ~  specific language governing permissions and limitations
   ~  under the License.
-  -->
-<x><![CDATA[
+  -->*/
 
   function transformRequest(mc) {
      var symbol = mc.getPayloadXML()..*::Code.toString();
@@ -38,6 +37,6 @@
         </m:CheckPriceResponse>);
   }
 
-]]></x>
+
 
 



Mime
View raw message