Interview Questions

Axis2 File Receiver

In this article lets discuss about how to receive a file using axis2 web service.

Consider a scenario where you want to receive a file from a server using web service. It can be done using MTOM.

MTOM is the W3C Message Transmission Optimization Mechanism, a method of efficiently sending binary data to and from web services. It uses XOP (XML-binary Optimized Packaging) to transmit binary data.

Axis2 framework supports MTOM to receive files from a web service. Lets see a simple example for the same.

First lets expose a webservice which sends a file to the client which access it.

AxisFileService.java

package in.techdive.axis2.samples;

public class AxisFileService
{
    public DataHandler getFile() throws Exception
    {
         DataHandler actualDH = new DataHandler(new URL("file:///C:\\test.txt"));
         return actualDH;
    }
}

Create a XML file named services.xml file as follows,

services.xml

<service name="AxisWebService">
   
    <description>
        Simple WebService Demo
    </description>
   
    <parameter name="ServiceClass">in.techdive.axis2.samples.AxisFileService</parameter>
   
    <operation name="getFile">
                <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>

</service>

Note:
1. Create src directory inside the Project Home. Place the AxisFileService.java inside src\in\techdive\axis2\samples directory
2. Place the services.xml inside src\in\techdive\axis2\samples\META-INF directory.

Web Service Deployment:

Create an Axis archive(aar) file as AxisFileService.aar. This file should contain the contents as below,

AxisFileService.aar

in\techdive\axis2\samples\AxisFileService.class
META-INF\services.xml

Place the AxisFileService.aar file inside <AXIS2_HOME>/repository/services

Now the deployed web-service will be available in
http://localhost:8080/axis2/services/AxisFileService?wsdl

Now lets look into the client code to access the web service.

Its pretty simple and the only thing that we should enable from client side is the MTOM support. Have a look at the following Axis2FileServiceClient class.

Axis2FileServiceClient.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.activation.DataHandler;
import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;

public class Axis2FileServiceClient
{

    public static void main(String[] args)
    {
        RPCServiceClient serviceClient = null;;
        try
        {
            serviceClient = new RPCServiceClient();
        }
        catch (AxisFault e1)
        {
            e1.printStackTrace();
        }

        /*
         * If the web service to be accessed is authenticated then set the
         * User Name and Password in the below authenticator object
         */

        HttpTransportProperties.Authenticator authenticator = new HttpTransportProperties.Authenticator();
        authenticator.setUsername("<userName>");
        authenticator.setPassword("<passWord>");

        Options options = serviceClient.getOptions();
        options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);

        // Provide the EndPoint Reference available in the wsdl
        // ,<soap12:address> tag.
        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/axis2/services/AxisFileService.AxisFileServiceHttpSoap12Endpoint/");

        // Enabling MTOM feature.
        options.setProperty(Constants.Configuration.ENABLE_MTOM,
                Constants.VALUE_TRUE);

        // provide the action name available in the wsdl, under <wsdl:operation> tag.
        options.setAction("urn:getFile");
        options.setTo(targetEPR);
        QName opName = new QName("http://example.com", "getFile");

        Object[] opSetArgs = new Object[0];
        Class[] returnTypes = new Class[] { DataHandler.class };

        try
        {
            Object[] response = serviceClient.invokeBlocking(opName, opSetArgs,
                    returnTypes);
            DataHandler actualDH;
            actualDH = (DataHandler) response[0];// get the binary data
            System.out.println(actualDH);

            //Received file will be available in this location.
            File graphFile = new File("c:/axis2/test.txt");
            if (!graphFile.exists())
                try
                {
                    graphFile.createNewFile();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            FileOutputStream outputStream = null;
            try
            {
                outputStream = new FileOutputStream(graphFile);
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            try
            {
                actualDH.writeTo(outputStream);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        catch (AxisFault e)
        {
            e.printStackTrace();
        }
    }
}

When the above client code is executed, it will access the Web Service method getFile() using MTOM feature available in axis2 framework and the received file will be stored in the given location at client side.

In this way axis2 framework can be used to receive files using Web Service.