Return-Path: Delivered-To: apmail-ws-axis-user-archive@ws.apache.org Received: (qmail 17063 invoked by uid 500); 13 May 2003 17:22:25 -0000 Mailing-List: contact axis-user-help@ws.apache.org; run by ezmlm Precedence: bulk Reply-To: axis-user@ws.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list axis-user@ws.apache.org Received: (qmail 17043 invoked from network); 13 May 2003 17:22:25 -0000 Date: Tue, 13 May 2003 13:22:27 -0400 Subject: Re: File upload example Content-Type: multipart/mixed; boundary=Apple-Mail-2--622580289 Mime-Version: 1.0 (Apple Message framework v552) From: Duane Gran To: axis-user@ws.apache.org In-Reply-To: <61C1CA24B8657047893FCF3570BC757D017D90A2@daebe008.americas.nokia.com> Message-Id: <7815E4A0-8567-11D7-BE9D-00306571470A@spinweb.net> X-Mailer: Apple Mail (2.552) X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N --Apple-Mail-2--622580289 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Bruno, I cut my teeth on this one recently, so I'll be happy to share what I have. From what I've seen, there are two typical ways of doing this from the client side: 1) Encode the attachment as a DataHandler, serialize it and send it as a parameter 2) Like above, encode as a DataHandler and put into an attachment (dime or mime) My code supports both ways, but at the moment I prefer #2. Someone else may be able to shed more light, but it seems to have more promise for interoperability, but at this time my environment is Java-only, so that is just conjecture on my part. My application deals specifically with converting a binary file (MARC) into an XML format (MARCXML), so don't get hung up on the extraneous details. In MarcXmlMapper the key parts to look at are the two convert methods and getAttachments. The TextMarcXmlMapper is a JUnit test that loosely ensures that the connection was accepted and some manner of String data came back. The test is pretty weak right now, but it will do. I am including my wsdd file if that may be helpful. Feel free to email me if you have any questions. Duane --Apple-Mail-2--622580289 Content-Disposition: attachment; filename=deploy.wsdd Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0644; name="deploy.wsdd" --Apple-Mail-2--622580289 Content-Disposition: attachment; filename=TestMarcXmlMapper.java Content-Transfer-Encoding: 7bit Content-Type: text/plain; x-unix-mode=0644; name="TestMarcXmlMapper.java" package gov.loc.mets.marc2mets; import junit.framework.TestCase; import junit.framework.TestSuite; 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 javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import java.rmi.RemoteException; import java.io.IOException; import javax.xml.rpc.ServiceException; import javax.activation.DataHandler; import javax.activation.FileDataSource; import java.net.URL; public class TestMarcXmlMapper extends TestCase { private static final String ENDPOINT = "http://localhost:8080/metsws/servlet/AxisServlet"; private static final String FILENAME = "/Users/ragnar/Desktop/marc2mets/ck.mrc"; public TestMarcXmlMapper(String name) { super(name); } /** * Tests utilizing the web service, using the parameter list to conduct * attachments. */ public void testWebServiceParameter() throws IOException, RemoteException, ServiceException { Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new URL(ENDPOINT)); call.setOperationName(new QName("urn:marc2mets", "convert")); // setup attachment DataHandler dh = new DataHandler(new FileDataSource(FILENAME)); call.setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT, Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME); //call.addAttachmentPart(dh); QName qnameAttachment = new QName("urn:marc2mets", "DataHandler"); call.registerTypeMapping(dh.getClass(), //Add serializer for attachment. qnameAttachment, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class); call.addParameter("attachment", qnameAttachment, ParameterMode.IN); //Add the file. call.setReturnType(XMLType.XSD_STRING); String result = (String) call.invoke(new Object[] { dh }); // System.out.println(result); assertTrue("Result of web service should contain MARCXML", result != null && result.length() > 0); } /** * Tests calling the web service with the attachment in the soap envelope */ public void testWebServiceAttachment() throws IOException, RemoteException, ServiceException { Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new URL(ENDPOINT)); call.setOperationName(new QName("urn:marc2mets", "convert")); // setup attachment DataHandler dh = new DataHandler(new FileDataSource(FILENAME)); call.setProperty(Call.ATTACHMENT_ENCAPSULATION_FORMAT, Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME); call.addAttachmentPart(dh); call.setReturnType(XMLType.XSD_STRING); String result = (String) call.invoke(new Object[] {}); assertTrue("Result of web service should contain MARCXML", result != null && result.length() > 0); } public static void main(String[] args) { junit.textui.TestRunner.run(new TestSuite(TestMarcXmlMapper.class)); } } --Apple-Mail-2--622580289 Content-Disposition: attachment; filename=MarcXmlMapper.java Content-Transfer-Encoding: 7bit Content-Type: text/plain; x-unix-mode=0644; name="MarcXmlMapper.java" package gov.loc.mets.marc2mets; import java.io.*; import org.xml.sax.XMLFilter; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.w3c.dom.Element; import java.rmi.RemoteException; import javax.xml.transform.Source; import javax.xml.transform.Result; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerException; import org.apache.axis.MessageContext; import org.apache.axis.AxisFault; import org.apache.axis.Message; import org.apache.axis.attachments.AttachmentPart; import org.apache.axis.attachments.Attachments; import org.apache.log4j.Category; import org.apache.log4j.BasicConfigurator; import java.util.Iterator; import org.marc4j.marcxml.MarcXmlReader; import org.marc4j.marcxml.MarcXmlFilter; import org.marc4j.marcxml.Converter; import org.marc4j.helpers.ErrorHandlerImpl; import org.marc4j.util.AnselToUnicode; import javax.activation.DataHandler; /** * Maps MARC binary data into the MARCXML format (see * http://www.loc.gov/marc/marcxml.html) by utilizing the marc4j library. * This class acts as an Adaptor to make the conversion simpler for clients. * * @author Duane Gran * @author Corey Keith * @version $Revision: 1.0 $ */ public class MarcXmlMapper { private static Category log; public MarcXmlMapper() { // setup the logger log = Category.getInstance(MarcXmlMapper.class); BasicConfigurator.configure(); } /** * Method that does most of the heavy lifting involved in converting a * binary MARC file to MARCXML. */ private Source doConversion(InputStream marcFile) throws IOException, SAXException { XMLFilter producer = new MarcXmlFilter(); producer.setProperty("http://marc4j.org/properties/error-handler", new ErrorHandlerImpl()); producer.setProperty("http://marc4j.org/properties/schema-location", "http://www.loc.gov/MARC21/slim " + "http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"); //producer.setFeature("http://marc4j.org/features/ansel-to-unicode", true); producer.setProperty("http://marc4j.org/properties/character-conversion", new AnselToUnicode()); BufferedReader reader = new BufferedReader(new InputStreamReader(marcFile, "ISO8859_1")); InputSource in = new InputSource(reader); Source source = new SAXSource(producer, in); return source; } /** * Formats the result of the conversion operation (@see doConversion) * into a String. */ private String resultAsString(InputStream marcFile) throws TransformerException, SAXException, IOException { Source source = doConversion(marcFile); ByteArrayOutputStream xml = new ByteArrayOutputStream(); Writer writer = new BufferedWriter(new OutputStreamWriter( xml, "UTF8")); Result result = new StreamResult(writer); Converter converter = new Converter(); converter.convert(source, result); return xml.toString(); // return sw.toString(); } /** * Converts a binary MARC file into MARCXML and returns it as a String. * This approach uses the attachment in the parameter, which requires * serialization of the DataHandler object. This works fine for Java * clients, but the convert method with no parameters may offer better * interoperability for mixed clients. * * @param attachment A DataHandler holding the MARC binary file * * @return A String with the MARCXML content */ public String convert(DataHandler attachment) throws RemoteException { String marcXml = "conversion failed"; try { InputStream marcFile = attachment.getInputStream(); marcXml = this.resultAsString(marcFile); } catch (Exception e) { throw new RemoteException(e.getMessage()); } return marcXml; } /** * Coverts a binary MARC file into MARCXML and returns it as a String. * The MARC file should be attached in the SOAP envelope. * * @return A String with the MARCXML content */ public String convert() throws RemoteException { AttachmentPart[] attachments = getAttachments(); log.debug("received request with " + attachments.length + " attachments"); if (attachments.length != 0) { // handle just one attachment for time being DataHandler dh = attachments[0].getActivationDataHandler(); return convert(dh); } else { throw new RemoteException("There was no MARC file attached to the" + " SOAP envelop."); } } /** * extract attachments from the current request * @return a list of attachmentparts or * an empty array for no attachments support in this axis * buid/runtime */ private AttachmentPart[] getAttachments() throws AxisFault { MessageContext msgContext = MessageContext.getCurrentContext(); Message reqMsg = msgContext.getRequestMessage(); Attachments messageAttachments = reqMsg.getAttachmentsImpl(); if (null == messageAttachments) { log.debug("no attachment support"); return new AttachmentPart[0]; } int attachmentCount= messageAttachments.getAttachmentCount(); AttachmentPart attachments[] = new AttachmentPart[attachmentCount]; Iterator it = messageAttachments.getAttachments().iterator(); int count = 0; while (it.hasNext()) { AttachmentPart part = (AttachmentPart) it.next(); attachments[count++] = part; } return attachments; } public static void main(String[] args) throws Exception { MarcXmlMapper mapper = new MarcXmlMapper(); InputStream marcFile = new FileInputStream(args[0]); String output = mapper.resultAsString(marcFile); System.out.println(output); } } --Apple-Mail-2--622580289 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On Tuesday, May 13, 2003, at 12:13 PM, wrote: > Hello, > > Does anyone have a sample of a web service and client call for doing a > file upload using Axis? > > I have found samples for the old Apache Soap, but it seems that things > are usually simpler when done with Axis. > > Thanks, > > Bruno --Apple-Mail-2--622580289--