You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2011/11/18 00:17:36 UTC

svn commit: r1203418 - in /webservices/commons/trunk/modules/axiom: modules/axiom-samples/ modules/axiom-samples/src/test/java/org/apache/axiom/samples/ src/site/apt/

Author: veithen
Date: Thu Nov 17 23:17:36 2011
New Revision: 1203418

URL: http://svn.apache.org/viewvc?rev=1203418&view=rev
Log:
Added a sample that shows how to process MTOM.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMSample.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMService.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMServiceImpl.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-samples/pom.xml
    webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-samples/pom.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-samples/pom.xml?rev=1203418&r1=1203417&r2=1203418&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-samples/pom.xml (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-samples/pom.xml Thu Nov 17 23:17:36 2011
@@ -56,10 +56,34 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-frontend-jaxws</artifactId>
+            <version>2.4.4</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-transports-http-jetty</artifactId>
+            <version>2.4.4</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.0</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
             <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.5</source>
+                    <target>1.5</target>
+                </configuration>
+            </plugin>
+            <plugin>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <configuration>
                     <includes>

Added: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMSample.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMSample.java?rev=1203418&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMSample.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMSample.java Thu Nov 17 23:17:36 2011
@@ -0,0 +1,93 @@
+/*
+ * 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.axiom.samples;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import javax.activation.DataHandler;
+import javax.xml.namespace.QName;
+import javax.xml.ws.Endpoint;
+
+import junit.framework.TestCase;
+
+import org.apache.axiom.attachments.Attachments;
+import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.cxf.helpers.IOUtils;
+
+public class MTOMSample extends TestCase {
+    // START SNIPPET: retrieveContent
+    public void retrieveContent(URL serviceURL, String id, OutputStream result) throws Exception {
+        // Build the SOAP request
+        SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
+        SOAPEnvelope request = soapFactory.getDefaultEnvelope();
+        OMElement retrieveContent = soapFactory.createOMElement(
+                new QName("urn:test", "retrieveContent"), request.getBody());
+        OMElement fileId = soapFactory.createOMElement(new QName("fileId"), retrieveContent);
+        fileId.setText(id);
+        
+        // Use the java.net.URL API to connect to the service
+        URLConnection connection = serviceURL.openConnection();
+        connection.setDoOutput(true);
+        connection.addRequestProperty("Content-Type", "text/xml; charset=UTF-8");
+        OutputStream out = connection.getOutputStream();
+        // Send the request
+        OMOutputFormat format = new OMOutputFormat();
+        format.setCharSetEncoding("UTF-8");
+        request.serialize(out, format);
+        out.close();
+        
+        // Get the SOAP response
+        InputStream in = connection.getInputStream();
+        Attachments attachments = new Attachments(in, connection.getContentType());
+        SOAPEnvelope response = OMXMLBuilderFactory.createSOAPModelBuilder(attachments).getSOAPEnvelope();
+        OMElement retrieveContentResponse = response.getBody().getFirstElement();
+        OMElement content = retrieveContentResponse.getFirstElement();
+        // Extract the DataHandler representing the optimized binary data
+        DataHandler dh = (DataHandler)((OMText)content.getFirstOMChild()).getDataHandler();
+        InputStream contentStream;
+        // If possible, stream the content of the MIME part (feature available in Axiom 1.2.13)
+        if (dh instanceof DataHandlerExt) {
+            contentStream = ((DataHandlerExt)dh).readOnce();
+        } else {
+            contentStream = dh.getInputStream();
+        }
+        // Write the content to the result stream
+        IOUtils.copy(contentStream, result);
+        contentStream.close();
+        
+        in.close();
+    }
+    // END SNIPPET: retrieveContent
+    
+    public void test() throws Exception {
+        Endpoint endpoint = Endpoint.publish("http://localhost:8080/mtom", new MTOMServiceImpl());
+        retrieveContent(new URL("http://localhost:8080/mtom"), "G87ZX20047", System.out);
+        endpoint.stop();
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMSample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMService.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMService.java?rev=1203418&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMService.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMService.java Thu Nov 17 23:17:36 2011
@@ -0,0 +1,36 @@
+/*
+ * 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.axiom.samples;
+
+import javax.activation.DataHandler;
+import javax.jws.WebMethod;
+import javax.jws.WebParam;
+import javax.jws.WebResult;
+import javax.jws.WebService;
+import javax.xml.ws.soap.MTOM;
+
+// START SNIPPET: iface
+@WebService(targetNamespace="urn:test")
+@MTOM
+public interface MTOMService {
+    @WebMethod
+    @WebResult(name="content")
+    DataHandler retrieveContent(@WebParam(name="fileId") String fileId);
+}
+// END SNIPPET: iface

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMServiceImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMServiceImpl.java?rev=1203418&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMServiceImpl.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMServiceImpl.java Thu Nov 17 23:17:36 2011
@@ -0,0 +1,29 @@
+/*
+ * 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.axiom.samples;
+
+import javax.activation.DataHandler;
+import javax.jws.WebService;
+
+@WebService(endpointInterface="org.apache.axiom.samples.MTOMService")
+public class MTOMServiceImpl implements MTOMService {
+    public DataHandler retrieveContent(String fileId) {
+        return new DataHandler("test", "text/xml");
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMServiceImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt?rev=1203418&r1=1203417&r2=1203418&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt (original)
+++ webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt Thu Nov 17 23:17:36 2011
@@ -51,6 +51,29 @@ Loading local chunks from a large XML do
   (corresponding to a given element) from a StAX stream reader, simply by passing an
   <<<XMLStreamReader>>> that is positioned on a <<<START_ELEMENT>>> event.
 
+Processing MTOM messages
+
+  This sample shows how to process MTOM messages with Axiom. The code actually sends a request to a Web service
+  with the following JAX-WS service endpoint interface:
+
+%{snippet|id=iface|file=modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMService.java}
+
+  It then extracts the binary content from the response and writes it to a given <<<OutputStream>>>:
+
+%{snippet|id=retrieveContent|file=modules/axiom-samples/src/test/java/org/apache/axiom/samples/MTOMSample.java}
+
+  The sample code shows that in order to parse an MTOM message one first needs to construct an
+  <<<Attachments>>> object that is then passed to the relevant method in <<<OMXMLBuilderFactory>>>.
+  In the object model, an XOP/MTOM attachment is represented as an <<<OMText>>> node for which <<<isBinary()>>> returns
+  <<<true>>>. Such a node is created for each <<<xop:Include>>> element in the original message.
+  The binary data is stored in a <<<DataHandler>>> object that can be obtained by a call to the
+  <<<getDataHandler()>>> method of the <<<OMText>>> node.
+  
+  By default attachments are loaded into memory, but the constructors of the <<<Attachments>>> class
+  allow to configure caching on disk. The sample actually shows an alternative method to reduce the
+  memory footprint of the MTOM processing, which is to enable streaming. This is supported starting
+  with Axiom 1.2.13.
+
 Logging MTOM messages without inlining optimized binary data
 
   A common way to log a SOAP message is to invoke the <<<toString>>> method on the corresponding <<<SOAPEnvelope>>>