A step-by-step guide to writing a web service which supports MTOM and a web service client to call it. The example assumes a Windows environment and Eclipse as the development environment.
Setup the Development Environment
1. Download Tomcat version 5.5 from
2. Set the JAVA_HOME environment variable to the Java JDK directory. Note that you have to set the variable to the JDK and not the JRE install folder.
3. Run startup.bat from the Tomcat distribution bin directory to make sure Tomcat will run. If all is good, run shutdown.bat from the bin directory to stop the Tomcat server.
4. Download Axis2 version 1.3 (
a. Download the Standard Distribution and the WAR distribution.
5. Copy the axis2.war file from the WAR distribution in step 4 to the webapps folder under Tomcat. Start Tomcat by running startup.bat. If Tomcat was able to consume the war file, then you should be able to browse to
6. Upon startup, Tomcat unpacked the axis2.war file. Have a look at the axis2.xml configuration file within axis2\WEB-INF\conf\ folder.
7. The axis2.xml file contains the administrator username and password for axis2 administration. By default the username and password to admin and axis2, respectively. You can modify this by changing the values and restarting Tomcat.
8. Point your browser to the axis2 web application and choose the Administration link.
9. So far we have Tomcat and Axis2 up and running. To make this exercise less painful, we should also download TCPMon so we can view the SOAP message we create. Download TCPMon from:
Create MTOM Enabled Web Service
1. Create a new Java Project in Eclipse. Name the project TestMTOM.
2. Add the axis2 jar files to the classpath. The axis2 jar files are within the lib directory of the axis2 standard distribution.
3. Create a new class named TestService within a package named com.test. Paste the following for the class implementation.
package
import
public
{
System.
OMText binaryNode = (OMText) (element.getFirstElement()).getFirstOMChild();
binaryNode.setOptimize(
DataHandler dh = (DataHandler) binaryNode.getDataHandler();
InputStream is = dh.getInputStream();
writeOutput(buf);
}
File of =
of.delete();
of.createNewFile();
setContents(
OutputStream output =
output =
output.write(aContents);
output.close();
got = is.read(ret, off, size - off);
off += got;
size *= 2;
System.arraycopy(ret, 0, tmp, 0, off);
ret = tmp;
The receiveMTOM() method expects an
prior to calling the getDataHandler() method.
4. The service implementation is done. Now we have to create a service.xml file. To do that, first create a folder under the root of the project and name it META-INF. Within this folder, create an xml file and name it services.xml. Paste the following into that file.
<
This service is to get the running Axis version
</
To enable MTOM, we have to add a parameter for the service called enableMTOM and set its value to true. Note that MTOM is disabled by default.
5. The next thing we have to do is package the service as a jar file but with a "aar" extension. You can do this several ways, but the easiest approach is to just use Eclipse's Export functionality.
6. Once you have the aar file, copy it under the axis2 deployment within Tomcat. This would be at TOMCAT_INSTALL\webapps\axis2\WEB-INF\services\
7. Restart Tomcat.
Create MTOM enabled Web Service Client
1. Before we can call the service, we need a proxy to the web service. Assuming Tomcat is running, browse to the following URL.
2. To create the proxy, save the WSDL file to the bin directory of the Axis2 installation. Name the file TestService.wsdl. Note that within this directory, you'll also have the WSDL2Java.bat file that we need to use to create the web service proxy.
3. After you save the WSDL file, open a command prompt and execute
WSDL2JAVA -uri TestService.wsdl
This should create the proxy files with a folder name src.
4. Now we need to create a java project within Eclipse for the web service client. Create a new Project and name it TestMTOMClient.
5. Add the Axis2 jar files to the classpath.
6. Copy the src folder from step 3 under the TestMTOMClient project and refresh the project in Eclipse.
7. Now create a class to execute the web service. Name the class TestClient and paste the following in its place.
TestServiceStub stub =
stub._getServiceClient().getOptions().setProperty(
Constants.Configuration.
stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(10000);
String base64String = Base64.encode(readFully(
ReceiveMTOM rmtom =
OMFactory fac = OMAbstractFactory.getOMFactory();
OMText binaryNode =fac.createOMText(base64String,
OMNamespace omNs = fac.createOMNamespace(
OMElement method = fac.createOMElement(
method.addChild(binaryNode);
rmtom.setElement(method);
stub.receiveMTOM(rmtom);
The testMTOM() method creates a stub and sets the enableMTOM property. It then reads the a file and then does a base64 encoded on the file's contents. Finally, it calls the receiveMTOM() metod on the stub.
9. Before you call the service, start TCPMon so we can view the SOAP message that the client sends to the service so you can verify that the SOAP Message is really an MTOM enabled SOAP request. Configurre TCPMon so that the listen port is 9080 and the listener's Target port is 8080. Assuming that Tomcat is running on port 8080. Also realize that the client above is setup to call the service on 9080 so as long as we configure TCPMon correctly we should be okay. If you are not using TCPMon, change the EPR variable in the client class so that it points to port 8080 or whatever port your service is running on.
10. Assuming you did everything correctly, you should be able to send some MTOM SOAP messages to the service. When the service receives a request, it save the binary file to c:\hold\att.pdf.
References
1. Download Tomcat:
2. Download Axis2 version 1.3:
3. Download TCPMonitor:
4. Using TCPMonitor:
5. A very good article on MTOM with Axis2:
6. Apache MTOM User Guide:
Remember Me
Powered by: newtelligence dasBlog 1.8.5223.2
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
E-mail
Theme design by Jelle Druyts