Axis2 - Transfer document using MTOM

I am trying to transfer content using MTOM. The service sends some file (say PDF file) and client saves that file on the disk.

The service does the following
      public GetResumeContentResponse getResumeContent(GetResumeContent getResumeContent) {
       String resumeId = getResumeContent.getResumeId();
       System.out.println("Resume ID is --"+resumeId);
       GetResumeContentResponse grs = new GetResumeContentResponse();
       javax.activation.DataHandler dataHandler = new DataHandler(new FileDataSource("C:\\source\\LTA.pdf"));
       grs.setContent(dataHandler);
       return grs;
      }

and client does the following

        public static void writeToFile(String fileName, InputStream iStream) throws IOException  {
              fileName = "C:\\target\\"+fileName;
             File theFile = new File(fileName);


             BufferedOutputStream fOut = null;
             try {
                   fOut = new BufferedOutputStream(new FileOutputStream(theFile));
                   byte[] buffer = new byte[32 * 1024];
                   int bytesRead = 0;
                   while ((bytesRead = iStream.read(buffer)) != -1) {
                          fOut.write(buffer, 0, bytesRead);
                   }
              }catch (Exception e) {
                        e.printStackTrace();
              }finally {
                        fOut.close();
                        iStream.close();
              }
          }

But in client, the "handler" is coming out to be null.

Please help me in resolving this.

Thank You
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://www.examples.com/wsdl/ResumeService.wsdl" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ResumeService"
	targetNamespace="http://www.examples.com/wsdl/ResumeService.wsdl">
	<wsdl:types>
		<xsd:schema targetNamespace="http://www.examples.com/wsdl/ResumeService.wsdl">
			<xsd:element name="getResumeId">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="firstName" type="xsd:string" />
						<xsd:element name="lastName" type="xsd:string"></xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="getResumeIdResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="resumeId" type="xsd:string" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="getResumeDetails">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="resumeId" type="xsd:string"></xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="getResumeDetailsResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="firstName" type="xsd:string"></xsd:element>
						<xsd:element name="lastName" type="xsd:string"></xsd:element>
						<xsd:element name="address" type="tns:addressType"></xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>

			<xsd:complexType name="addressType">
				<xsd:sequence>
					<xsd:element name="city" type="xsd:string" />
					<xsd:element name="state" type="xsd:string" />
					<xsd:element name="country" type="xsd:string" />
					<xsd:element name="mobile" type="xsd:string" />
					<xsd:element name="email" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>

			<xsd:element name="getAllResumeIdsResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="allResumeIds" type="xsd:string"
							maxOccurs="unbounded" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>

			<xsd:element name="updateResumeDetails">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="resumeId" type="xsd:string"></xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="updateResumeDetailsResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="updateStatus" type="xsd:boolean"></xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>

			<xsd:element name="getResumeContent">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="resumeId" type="xsd:string">
						</xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="getResumeContentResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="content" type="xsd:base64Binary">
						</xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			
			<xsd:element name="updateResumeContent">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="resumeId" type="xsd:string">
						</xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="updateResumeContentResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="content" type="xsd:base64Binary">
						</xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
		</xsd:schema>
	</wsdl:types>
	<wsdl:message name="getResumeIdRequest">
		<wsdl:part element="tns:getResumeId" name="parameters" />
	</wsdl:message>
	<wsdl:message name="getResumeIdResponse">
		<wsdl:part element="tns:getResumeIdResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="getResumeDetailsRequest">
		<wsdl:part name="parameters" element="tns:getResumeDetails"></wsdl:part>
	</wsdl:message>
	<wsdl:message name="getResumeDetailsResponse">
		<wsdl:part name="parameters" element="tns:getResumeDetailsResponse"></wsdl:part>
	</wsdl:message>

	<wsdl:message name="getAllResumeIdsRequest" />
	<wsdl:message name="getAllResumeIdsResponse">
		<wsdl:part name="parameters" element="tns:getAllResumeIdsResponse"></wsdl:part>
	</wsdl:message>

	<wsdl:message name="updateResumeDetailsRequest">
		<wsdl:part name="parameters" element="tns:updateResumeDetails"></wsdl:part>
	</wsdl:message>
	<wsdl:message name="updateResumeDetailsResponse">
		<wsdl:part name="parameters" element="tns:updateResumeDetailsResponse"></wsdl:part>
	</wsdl:message>

	<wsdl:message name="getResumeContentRequest">
		<wsdl:part name="parameters" element="tns:getResumeContent"></wsdl:part>
	</wsdl:message>
	<wsdl:message name="getResumeContentResponse">
		<wsdl:part name="parameters" element="tns:getResumeContentResponse"></wsdl:part>
	</wsdl:message>
	
	<wsdl:message name="updateResumeContentRequest">
		<wsdl:part name="parameters" element="tns:updateResumeContent"></wsdl:part>
	</wsdl:message>
	<wsdl:message name="updateResumeContentResponse">
		<wsdl:part name="parameters" element="tns:updateResumeContentResponse"></wsdl:part>
	</wsdl:message>
	
	<wsdl:portType name="ResumeService">
		<wsdl:operation name="getResumeId">
			<wsdl:input message="tns:getResumeIdRequest" />
			<wsdl:output message="tns:getResumeIdResponse" />
		</wsdl:operation>
		<wsdl:operation name="getResumeDetails">
			<wsdl:input message="tns:getResumeDetailsRequest"></wsdl:input>
			<wsdl:output message="tns:getResumeDetailsResponse"></wsdl:output>
		</wsdl:operation>

		<wsdl:operation name="getAllResumeIds">
			<wsdl:input message="tns:getAllResumeIdsRequest"></wsdl:input>
			<wsdl:output message="tns:getAllResumeIdsResponse"></wsdl:output>
		</wsdl:operation>

		<wsdl:operation name="updateResumeDetails">
			<wsdl:input message="tns:updateResumeDetailsRequest"></wsdl:input>
			<wsdl:output message="tns:updateResumeDetailsResponse"></wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="getResumeContent">
			<wsdl:input message="tns:getResumeContentRequest"></wsdl:input>
			<wsdl:output message="tns:getResumeContentResponse"></wsdl:output>
		</wsdl:operation>
		
		<wsdl:operation name="updateResumeContent">
			<wsdl:input message="tns:updateResumeContentRequest"></wsdl:input>
			<wsdl:output message="tns:updateResumeContentResponse"></wsdl:output>
		</wsdl:operation>
	</wsdl:portType>

	<wsdl:binding name="ResumeServiceSOAP" type="tns:ResumeService">

		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="getResumeId">

			<soap:operation
				soapAction="http://www.examples.com/wsdl/ResumeService.wsdl/getResumeId" />
			<wsdl:input>

				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>

				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="getResumeDetails">

			<soap:operation
				soapAction="http://www.examples.com/wsdl/ResumeService.wsdl/getResumeDetails" />
			<wsdl:input>

				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>

				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>

		<wsdl:operation name="getAllResumeIds">

			<soap:operation
				soapAction="http://www.examples.com/wsdl/ResumeService.wsdl/getAllResumeIds" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>

				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="updateResumeDetails">

			<soap:operation
				soapAction="http://www.examples.com/wsdl/ResumeService.wsdl/updateResumeDetails" />
			<wsdl:input>

				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>

				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="getResumeContent">
			<soap:operation
				soapAction="http://www.examples.com/wsdl/ResumeService.wsdl/getResumeContent" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="updateResumeContent">
			<soap:operation
				soapAction="http://www.examples.com/wsdl/ResumeService.wsdl/updateResumeContent" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="ResumeService">
		<wsdl:port binding="tns:ResumeServiceSOAP" name="ResumeServiceSOAP">
			<soap:address location="http://www.example.org/" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

Open in new window

ank9Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Siva Prasanna KumarConnect With a Mentor Principal Solutions ArchitectCommented:
See this is again wrong!

Please don't use the ServiceClient way of invocation you don't need that.

Instead use the below code.

Also you are getting null because you have just created the response object your self with out even making the request to the service for getting the result.

ResumeServiceStub rs = new ResumeServiceStub();
//if you have a differnt end point use this
		
		rs = new ResumeServiceStub("http://localhost:8080/axis2/ResumeService");

GetResumeContentResponse grcr = rs.getResumeContent(resumeId);

and then continue as in the above code to extract the getCcontent();

Open in new window

0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
How are you extracting the content to write to the file at the client?

0
 
ank9Author Commented:
Sorry, missed to post that.
This is what I am doing at the client side
 

public static void getResumeContent(String resumeId) throws AxisFault {
		System.out.println("Resume ID is getResumeContent --"+resumeId);		
		ServiceClient serviceClient = new ServiceClient();
        try {
        	Options options = new Options();
            EndpointReference rs = new EndpointReference("http://localhost:8080/axis2/services/ResumeService");
            options.setTo(rs);
   //     options.setProperty(Constants.Configuration.FORCE_MIME, Constants.VALUE_TRUE);        
            serviceClient.setOptions(options);
            GetResumeContentResponse grcr = new GetResumeContentResponse();
            DataHandler handler = grcr.getContent();
            InputStream is = handler.getInputStream();
            writeToFile("abc.pdf", is);
            
        }catch(IOException io) {
        	io.printStackTrace();
        }
	}
	
	  public static void writeToFile(String fileName, InputStream iStream) throws IOException  {
		  fileName = "C:\\target\\"+fileName;
		 File theFile = new File(fileName);


		 BufferedOutputStream fOut = null;
		 try {
			 fOut = new BufferedOutputStream(new FileOutputStream(theFile));
			 byte[] buffer = new byte[32 * 1024];
			 int bytesRead = 0;
			 while ((bytesRead = iStream.read(buffer)) != -1) {
			        fOut.write(buffer, 0, bytesRead);
			 }
		  }catch (Exception e) {
			      e.printStackTrace();
		  }finally {
			      fOut.close();
			      iStream.close();
		  }
	    }

Open in new window

0
 
ank9Author Commented:
Thanks a lot.
0
All Courses

From novice to tech pro — start learning today.