서비스용 자바 소스#

package kr.or.openframework.service;

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

import javax.activation.DataHandler;

import org.apache.axiom.attachments.Attachments;
import org.apache.axis2.context.MessageContext;

public class AttachmentService {

  public String uploadFile(String name, String attchmentIDthrows IOException {
    MessageContext msgCtx = MessageContext.getCurrentMessageContext();
    Attachments attachment = msgCtx.getAttachmentMap();
    DataHandler dataHandler = attachment.getDataHandler(attchmentID);
    File file = new File(name);
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    dataHandler.writeTo(fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();

    return "File saved succesfully.";
  }

}

services.xml#

<service name="MyService" >
  <description>
    Please Type your service description here
  </description>
  <messageReceivers>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" 
    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"  
    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
  </messageReceivers>
  <parameter name="ServiceClass" locked="false">kr.or.openframework.service.AttachmentService</parameter>
</service>

클라이언트 소스#

이 클라이언트 소스는 d: 드라이브에 존재하는 test.txt 파일을 서버의 c: 드라이브에 testresult.txt 라는 파일로 저장하는 로직을 가진다.

package client;

import java.io.File;
import java.io.FileNotFoundException;

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

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.wsdl.WSDLConstants;

public class SWAClient {

  private static EndpointReference targetEPR = new EndpointReference(
      "http://localhost:9000/axis2/services/SoapWithAttachments");

  @SuppressWarnings("unchecked")
  public static void main(String[] argsthrows Exception {
    File file = new File("d:/test.txt");

    if (file.exists()) {
      transferFile(file, "c:/testresult.txt");
    else {
      throw new FileNotFoundException();
    }
  }

  public static void transferFile(File file, String destinationFilethrows Exception {

    Options options = new Options();
    options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);
    options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
    // Increase the time out when sending large attachments
    options.setTimeOutInMilliSeconds(10000);
    options.setTo(targetEPR);
    options.setAction("urn:uploadFile");

    // assume the use runs this sample at
    // <axis2home>/samples/soapwithattachments/ dir
    ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(
        "D:/apache/axis2-1.2/repository"null);

    ServiceClient sender = new ServiceClient(configContext, null);
    sender.setOptions(options);
    OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);

    MessageContext mc = new MessageContext();
    FileDataSource fileDataSource = new FileDataSource(file);

    // Create a dataHandler using the fileDataSource. Any implementation of
    // javax.activation.DataSource interface can fit here.
    DataHandler dataHandler = new DataHandler(fileDataSource);
    String attachmentID = mc.addAttachment(dataHandler);

    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope env = fac.getDefaultEnvelope();
    OMNamespace omNs = fac.createOMNamespace("http://service.openframework.or.kr/xsd""swa");
    OMElement uploadFile = fac.createOMElement("uploadFile", omNs);
    OMElement nameEle = fac.createOMElement("name", omNs);
    nameEle.setText(destinationFile);
    OMElement idEle = fac.createOMElement("attchmentID", omNs);
    idEle.setText(attachmentID);
    uploadFile.addChild(nameEle);
    uploadFile.addChild(idEle);
    env.getBody().addChild(uploadFile);
    mc.setEnvelope(env);

    mepClient.addMessageContext(mc);
    mepClient.execute(true);
    MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    SOAPBody body = response.getEnvelope().getBody();
    OMElement element = body.getFirstElement().getFirstChildWithName(
        new QName("http://service.openframework.or.kr/xsd""return"));
    System.out.println(element.getText());
  }
}

다음은 위 서비스의 wsdl 일부이다.

soapwithattachment_wsdl.gif

다음은 클라이언트 실행시 필요한 jar파일들이다.

soapwithattachment.gif

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
gif
soapwithattachment.gif 10.1 kB 1 03-Aug-2007 12:35 DongGukLee
gif
soapwithattachment_wsdl.gif 31.7 kB 1 03-Aug-2007 13:12 DongGukLee
« This page (revision-12) was last changed on 03-Aug-2007 13:12 by DongGukLee