Hi,

I've been able to return strings in SOAP attachments, but I haven't been successful 
returning a byte array.  I wrote in previously about this problem because I was 
getting error messages such as "javax.activation.UnsupportedDataTypeException: no 
object DCH for MIME type application/octet-stream".  It was suggested that I try 
running with the latest build from CVS because Axis release 1.1 had a problem with 
byte arrays in attachments.  So I'm running with the July 9 build.  With this build, I 
no longer get the UnsupportedDataTypeException errors, but now I get no attachment in 
the response message.

Here's the simple example I used to get a string returned in an attachment.  Then a 
few small changes to return a byte array.  I would appreciate it if someone could 
detect where I'm going wrong with this approach.

Thanks!

. . . Phil


******
****** Client code ******
******

package us.pooch.soaptest.attachments;

import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.axis.utils.Options;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import java.io.File;
import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;

public class SOAPAttachmentMimeTypeDemoClient {

        public static void main(String args[]) {
                try {

                        SOAPAttachmentMimeTypeDemoClient demoClient =
                                new SOAPAttachmentMimeTypeDemoClient();

                        demoClient.demoGetString();
                        demoClient.demoGetBytes();
                } catch (Exception e) {
                        System.err.println(e);
                        e.printStackTrace();
                }
                System.exit(18);
        }

        protected DataHandler runDemo(String functionName) throws Exception {
                String endPointURLString =
                        
"http://localhost:8080/SOAPAttachmentMimeTypeDemoProjectWeb/services/SOAPAttachmentMimeTypeDemoService";;

                SOAPConnectionFactory soapConnectionFactory =
                        javax.xml.soap.SOAPConnectionFactory.newInstance();
                SOAPConnection soapConnection =
                        soapConnectionFactory.createConnection();

                MessageFactory messageFactory = MessageFactory.newInstance();
                SOAPMessage soapMessage = messageFactory.createMessage();
                SOAPPart soapPart = soapMessage.getSOAPPart();
                SOAPEnvelope requestEnvelope = soapPart.getEnvelope();
                SOAPBody body = requestEnvelope.getBody();
                SOAPBodyElement operation =
                        body.addBodyElement(requestEnvelope.createName(functionName));
                javax.xml.soap.SOAPMessage returnedSOAPMessage =
                        soapConnection.call(soapMessage, endPointURLString);
                Iterator iterator = returnedSOAPMessage.getAttachments();
                if (!iterator.hasNext()) {
                        //The wrong type of object that what was expected.
                        System.out.println("Received problem response from server");
                        throw new AxisFault(
                                "",
                                "Received problem response from server",
                                null,
                                null);

                }

                return  (DataHandler) ((AttachmentPart) 
iterator.next()).getDataHandler();
        }

        public void demoGetString() throws Exception {

                DataHandler rdh = runDemo("getString");
                String returnedData = (String) rdh.getContent();

                if (!returnedData.equals("You got me!")) {
                        System.err.println("Did not get the expected string.");
                        throw new AxisFault("", "Did not get the expected string.", 
null, null);
                }

                System.out.println("getString() succeeded");
        }

        public void demoGetBytes() throws Exception {

                DataHandler rdh = runDemo("getBytes");
                byte[] returnedData = (byte[]) rdh.getContent();
        byte[] expectedArray = new byte[] { 01, 02, 03, 04 };

                if (!returnedData.equals(expectedArray)) {
                        System.err.println("Did not get the expected byte[].");
                        throw new AxisFault("", "Did not get the expected byte[].", 
null, null);
                }

                System.out.println("getBytes() succeeded");
        }
}


******
****** Service code ******
******

package us.pooch.soaptest.attachments;

import javax.activation.DataHandler;

public class SOAPAttachmentMimeTypeDemoService {

    public DataHandler getString() {
        
        return new DataHandler("You got me!", "text/plain");
    }

    public DataHandler getBytes() {
        
        byte[] arrayToReturn = new byte[] { 01, 02, 03, 04 };
        return new DataHandler(arrayToReturn, "application/octet-stream");
    }
}


******
****** WSDD
******
<deployment xmlns="http://xml.apache.org/axis/wsdd/"; 
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"; 
xmlns:ns1="SOAPAttachmentMimeTypeDemoService" >
  <service name="SOAPAttachmentMimeTypeDemoService" provider="java:RPC" >
    <parameter name="className" 
value="us.pooch.soaptest.attachments.SOAPAttachmentMimeTypeDemoService"/>
    <parameter name="allowedMethods" value="getString getBytes"/>
    <operation name="getString" returnQName="returnqname" returnType="ns1:DataHandler" 
/>
    <operation name="getBytes" returnQName="returnqname" returnType="ns1:DataHandler" 
/>

 <typeMapping 
deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory"
   languageSpecificType="java:javax.activation.DataHandler" qname="ns1:DataHandler"
    serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory" 
    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/";
     />
  </service>

</deployment>

Reply via email to