Child pages
  • Web Services-Sending Large Data
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Kristoff Cossement

Soap with mime attachments was indeed the way to go.

Remark: this only works with java 1.4.2_09 (I did not get it working with java 1.5.x)

For those who are interested I included some sample code on how to exchange large binary data between a java client and and webobjects server without taking all data in memory.
You also need the java mail and java activation framework from sun.

Client.java

import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
import java.io.*;
import org.apache.axis.attachments.AttachmentPart;
import org.apache.axis.message.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.soap.SOAPElement;
import java.util.*;
import javax.mail.*;

public class Client {

public static File resize(String sPath)
{
File file = new File(sPath);

String endPoint = "http://localhost:55555/cgi-bin/WebObjects/project.woa/ws/FileUpload";
try{
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage message = (javax.xml.soap.SOAPMessage)MessageFactory.newInstance().createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = (org.apache.axis.message.SOAPEnvelope)part.getEnvelope();
SOAPBody body = (org.apache.axis.message.SOAPBody)envelope.getBody();
SOAPBodyElement operation = (org.apache.axis.message.SOAPBodyElement)body.addBodyElement(
envelope.createName("upload",
"ns",
"http://localhost:55555/cgi-bin/WebObjects/project.woa/ws/FileUpload"));
operation.setEncodingStyle("http://schemas.xmlsoaporg/soap/encoding/");

DataHandler dh = new DataHandler(new FileDataSource(file));
AttachmentPart attachment = (org.apache.axis.attachments.AttachmentPart)message.createAttachmentPart(dh);
SOAPElement filename = operation.addChildElement("filename","");
SOAPElement source = operation.addChildElement("source","");
message.addAttachmentPart(attachment);
filename.addTextNode(file.getName());
source.addTextNode(attachment.getContentId());

SOAPMessage result = connection.call(message,endPoint);
System.out.println(result);
part = result.getSOAPPart();
envelope = (org.apache.axis.message.SOAPEnvelope)part.getEnvelope();
body = (org.apache.axis.message.SOAPBody)envelope.getBody();

if(!body.hasFault())
________

Unknown macro: {__________System.out.println("answer_}

______}
______catch(Exception_e)
______

Unknown macro: {________e.printStackTrace();______}

______return_null;
____}
__
___public_static_void_main(String[]_args){
__
_____try

Unknown macro: {__________resize(args[0]);________}

_____catch(Exception_e)_

Unknown macro: {________System.out.println(e.getMessage());______}

____}
__}

_Webobjects_Application.java_

override_dispatchRequest

__public_WOResponse_dispatchRequest(WORequest_request)
__{
___WOResponse_result=_null;
___String_sURI=_request.uri();
___NSLog.debug.appendln("Accessing"_+_sURI);
__
___Pattern_p=_Pattern.compile("/ws/FileUpload");
___Matcher_m=_p.matcher(sURI);
____if(m.find())
____{
_____String_sContType=_request.headerForKey("content-type");
_____p=_Pattern.compile("multipart/related");
_____m=_p.matcher(sContType);
______if(m.find())
______

Unknown macro: {________result_=_Dispatcher.handleFileUpload(request);______}

______else
______

Unknown macro: {________result_=_super.dispatchRequest(request);______}

____}
____else
____

Unknown macro: {______result_=_super.dispatchRequest(request);____}

__
____return_result;
__
__}

_Dispatcher.java

__//
__//_Dispatcher.java
__//_project
__//
__//_Created_by_admin_on_5/5/06.
_//_Copyright_2006_MyCompanyName_._All_rights_reserved.
__//
__import_com.webobjects.foundation.*;
__import_com.webobjects.appserver.*;
__import_com.webobjects.eocontrol.*;
__
__import_java.io.*;
__import_java.util.regex.*;
__import_org.w3c.dom.*;
__import_javax.xml.transform.*;
__import_javax.xml.transform.stream.*;
__import_javax.xml.transform.dom.*;
__import_javax.xml.parsers.*;
__import_org.apache.axis.attachments.*;
__
__
_public_class_Dispatcher{
__
____public_static_WOResponse_handleFileUpload(WORequest_request)
____{
_____String_sContType=_request.headerForKey("content-type");
_____String_sSoapXml=_"";
_____InputStream_requestStream=_request.contentInputStream();
__
_____String_sTempDir=_System.getProperty("java.io.tmpdir");
_____String_timestamp=(new_Long(System.currentTimeMillis())).toString();
_____File_fTempFile=_new_File(sTempDir+"/"+timestamp);
_____File_fSavedFile=_new_File(sTempDir+"/"timestamp".out");
__
______try
______{
_______BufferedOutputStream_fOut=_new_BufferedOutputStream(new_FileOutputStream(fTempFile));
________byte[)_buffer_=_new_byte(32_*_1024];
_______int_bytesRead=_0;
_______while((bytesRead_=requestStream.read(buffer))!= -1)

Unknown macro: { fOut.write(buffer, 0, bytesRead); }

fOut.close();
}
catch (Exception e)

Unknown macro: { NSLog.debug.appendln(e.getMessage()); }

try
{
InputStream iStream = new FileInputStream(fTempFile);

MultiPartRelatedInputStream mis = new MultiPartRelatedInputStream(sContType,iStream);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(mis);
sSoapXml = toString(doc);
NSLog.debug.appendln("SOAP Envelope: " + sSoapXml);
mis.close();
iStream.close();

Node nEnvelope = getNamedChildNode(doc,"Envelope");
if(null !=_nEnvelope)
________{
_________Node_nBody=_getNamedChildNode(nEnvelope,"Body");
_________if(null!= nBody)
{
NSArray nSubNodes = getElementChildNodes(nBody);
if((null !=nSubNodes)&&(nSubNodes.count()>_0))
____________{
_____________Node_nFileUpload=_(Node)nSubNodes.get(0);
_____________nSubNodes=_getElementChildNodes(nFileUpload);
_____________if((null!= nSubNodes) && (nSubNodes.count() > 1))
{
Node nFileName = (Node)nSubNodes.get(0);
Node nData = (Node)nSubNodes.get(1);

String sFileName = getTextFromNode(nFileName);
String sFileMimeID = getTextFromNode(nData);

fSavedFile = new File(sTempDir+"/"+sFileName);

Pattern pattern = Pattern.compile("(boundary=\".+?\")");
Matcher match = pattern.matcher(sContType);
if(match.find())
{
String boundary= match.group(1);
String sSubContentType = "multipart/related; type=\"text/xml\"; start=\"<"sFileMimeID">\"; "+boundary;
iStream = new FileInputStream(fTempFile);
mis = new MultiPartRelatedInputStream(sSubContentType,iStream);
try
{
BufferedOutputStream fOut = new BufferedOutputStream(new FileOutputStream(fSavedFile));
byte[) buffer = new byte(32 * 1024];
int bytesRead = 0;
while ((bytesRead = mis.read(buffer)) !=_-1)
____________________

Unknown macro: {______________________fOut.write(buffer,_0,_bytesRead);____________________}

____________________mis.close();
____________________fOut.close();
__________________}
_________________catch(Exception_e)
__________________

Unknown macro: {____________________System.out.println(e.getMessage());____________________//throw_new_IOException(me_+_"_failed,_got}

________________}
______________}
___________}_
__________}
________}
______}
______catch(Exception_e)
______

Unknown macro: {________e.printStackTrace();______}

__
__
_____WOResponse_result=_new_WOResponse();
_____result.setContent("<?xml_version="1.0"_encoding="UTF-8"?>n"+
__________"<soapenv:Envelope_xmlns:soapenv="http://schemas.xmlsoaporg/soap/envelope/"_xmlns:xsd="http://www.w3.org/2001/XMLSchema"_xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">n"+
__________"<soapenv:Body>n"+
__________"<ns:uploadResponse_soapenv:encodingStyle="http://schemas.xmlsoaporg/soap/encoding/"_xmlns:ns="http://localhost:55555/cgi-bin/WebObjects/project.woa/ws/FileUpload">n"+
__________"<ns:uploadReturn_xsi:type="xsd:boolean">true</ns:uploadReturn>n"+
__________"</ns:uploadResponse>n"+
__________"</soapenv:Body>n"+
__________"</soapenv:Envelope>");
__
__
______result.setHeader("text/xml;_charset=utf-8","content-type");
__
__
______return_result;
__
____}
__
__
___static_public_String_toString(Document_document){
_____String_result=_null;
__
_____if(document_!= null) {
StringWriter strWtr = new StringWriter();
StreamResult strResult = new StreamResult(strWtr);
TransformerFactory tfac = TransformerFactory.newInstance();
try {
Transformer t = tfac.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "utf-8");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml"); //xml, html, text
t.setOutputProperty("

Unknown macro: {http}

indent-amount", "4");
t.transform(new DOMSource(document.getDocumentElement()), strResult);
} catch (Exception e)

Unknown macro: { System.err.println("XML.toString(Document)}

result = strResult.getWriter().toString();
}

return result;
}

static public Node getNamedChildNode(Node n, String sChildNodeName)
{

NodeList nl = n.getChildNodes();

for(int i=0;i<nl.getLength();i++)
{
Node s = nl.item;
String sChild = s.getNodeName();
if(sChildNodeName.equalsIgnoreCase(sChild) || sChild.endsWith(":"+sChildNodeName))

Unknown macro: { NSLog.debug.appendln("childnode = " + s.getNodeName()); return s; }

}

return null;

}
static public NSArray getElementChildNodes(Node n)
{
NSMutableArray elements = new NSMutableArray();
NodeList nl = n.getChildNodes();

for(int i=0;i<nl.getLength();i++)
{
Node s = nl.item;
if(s.getNodeType() == Node.ELEMENT_NODE)

Unknown macro: { NSLog.debug.appendln("childnode = " + s.getNodeName()); elements.addObject(s); }

}

return elements;
}
static public String getTextFromNode(Node n)
{
String text = "";

NodeList nl = n.getChildNodes();

for(int i=0;i<nl.getLength();i++)
{
Node s = nl.item;
if(s.getNodeType() == Node.TEXT_NODE)

Unknown macro: { return s.getNodeValue(); }

}
return text;
}
}

Category:WebObjects

  • No labels