A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Tuesday, November 20, 2012

Web Service using JAX-WS (MTOM)

9:01:00 PM Posted by Satish Kumar , , No comments
A complete JAX-WS SOAP-based example to show how to use Message Transmission Optimization Mechanism (MTOM) and XML-Binary Optimized Packaging (XOP) technique to send a binary attachment from server to client and vice verse.

For this tutorial I will be using the following tools

1. JDK 7
2. Eclipse Juno
3. Maven2

SOAP Message Transmission Optimization Mechanism (MTOM), is a W3C Recommendation designed for optimizing the electronic transmission of attachments. MTOM combines the composability of Base 64 encoding with the transport efficiency of SOAP with Attachments. The data is simply streamed as binary data in one of the MIME message parts. 

1. Create a SOAP-based Document style web service end point by using JAX-WS and MTOM.
2. Create a Web Service Publisher
3. Create a Java web service client manually.

Before start coding let's create a java project using the following maven command. 

1
mvn archetype:generate -DgroupId=com.techiekernel -DartifactId=webservice-JAX-WS-MTOM -Dpackagename=com.techiekernel

After you execute, the project will be get created with pom.xml file. As I am using JDK 7 and annotations, I have to specify the updated maven plugin. And I also specified a configuration for executable jar. So the final pom.xml is shown bellow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.techiekernel</groupId>
  <artifactId>webservice-JAX-WS-MTOM</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>webservice-JAX-WS-MTOM</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
   </plugin>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>
   <plugin>
    <!-- Build an executable JAR -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
     <archive>
      <manifest>
       <addClasspath>true</addClasspath>
       <classpathPrefix>lib/</classpathPrefix>
       <mainClass>com.techiekernel.ws.jaxws.mtom.ImageServerPublisher</mainClass>
      </manifest>
     </archive>
    </configuration>
   </plugin>
  </plugins>
  <finalName>webservice-JAX-WS-MTOM</finalName>
 </build>
</project>

Here in this example I will upload a image to server using the web service method and after wards I will download the same image from server to client.

JAX-WS Web Service End Point:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.techiekernel.ws.jaxws.mtom;

import java.awt.Image;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;

/**
 * Service end point interface
 * 
 * @author satish
 * 
 */
@WebService
@SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL)
public interface ImageServer {
 // download a image from server
 @WebMethod
 public Image downloadImage(String name);

 // update image to server
 @WebMethod
 public String uploadImage(Image data, String name);
}

Web Service Endpoint Implementation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.techiekernel.ws.jaxws.mtom;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.xml.ws.WebServiceException;
import javax.imageio.ImageIO;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(endpointInterface = "com.techiekernel.ws.jaxws.mtom.ImageServer")
public class FileImageServer implements ImageServer {

 final static String PATH = "/home/satish/";
 
 public Image downloadImage(String name) {
  try {
   //Create a file object with file name and read the image
   File image = new File(PATH + name);
   return ImageIO.read(image);
  } catch (IOException e) {
   e.printStackTrace();
   throw new WebServiceException("Download Failed");
  }
 }

 public String uploadImage(Image data, String name) {
  if (data != null) {
   try {
    File image = new File(PATH + name);
    ImageIO.write((BufferedImage)data, "jpg", image);
   } catch (IOException e) {
    e.printStackTrace();
    throw new WebServiceException("Upload Failed");
   }
   return "Upload Successful";
  }
  throw new WebServiceException("No data to upload.");
 }
}

Endpoint Publisher:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.techiekernel.ws.jaxws.mtom;

import javax.xml.ws.Endpoint;

/**
 * End point publisher
 * @author satish
 *
 */
public class ImageServerPublisher {
 public static void main(String[] args) {
  System.out.println("main called");
  Endpoint.publish("http://localhost:8080/webservice-JAX-WS-MTOM/imageserver", new FileImageServer());
 }
}

Once you run FooBarPublisher, the web service "imageserver" will be ready with localhost and port 8080. You can curl the service at "http://localhost:8080/webservice-JAX-WS-MTOM/imageserver?wsdl" and can see the xml defination of the service.

Java Web Service Client:

Here in this client I will upload a image to the server and after words I will download the same image to the client.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.techiekernel.ws.jaxws.mtom;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class ImageServerClient {
 
 final static String PATH = "/home/satish/Desktop/";
 
 public static void main(String[] args) {
  URL url = null;
  try {
   url = new URL("http://localhost:8080/webservice-JAX-WS-MTOM/imageserver?wsdl");
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
  // 1st argument service URI, refer to wsdl document above
  // 2nd argument is service name, refer to wsdl document above
  QName qname = new QName("http://mtom.jaxws.ws.techiekernel.com/",
    "FileImageServerService");

  Service service = Service.create(url, qname);

  ImageServer imageServer = service.getPort(ImageServer.class);

  File imageFile = new File(PATH + "natgeo.jpg");
  
  Image uimage = null;
  
  try {
   uimage =  ImageIO.read(imageFile);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  // now upload the image 
  imageServer.uploadImage((BufferedImage)uimage, "natgeo_uploaded.jpg");
  
  //lets now download the uploaded image
  Image dimage = imageServer.downloadImage("natgeo_uploaded.jpg");
  File dimageFile = new File(PATH + "natgeo_downloaded.jpg");

  try {
   ImageIO.write((BufferedImage)dimage, "jpg", dimageFile);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

So now we are ready to test our webservice. We are going to do that in two steps.

1. Making the service ready by running the following command in target folder.

1
java -jar webservice-JAX-WS-MTOM.jar

2. Test the web service by running client.

1
java com.techiekernel.ws.jaxws.mtom.ImageServerClient

Code Base:

You can pull the entire source code from GitHub.

0 comments:

Post a Comment