String baseLoc= "http://axis.org/attachTest";
Vector refs= new Vector(); //holds a string of references to attachments.
Service service = new Service(); //A new axis Service.
Call call = (Call) service.createCall(); //Create a call to the service.
/*Un comment the below statement to do HTTP/1.1 protocol*/
//call.setScopedProperty(MessageContext.HTTP_TRANSPORT_VERSION,HTTPConstants.HEADER_PROTOCOL_V11);
Hashtable myhttp= new Hashtable();
myhttp.put(HTTPConstants.HEADER_CONTENT_LOCATION, baseLoc); //Send extra soap headers
/*Un comment the below to do http chunking to avoid the need to calculate content-length. (Needs HTTP/1.1)*/
//myhttp.put(HTTPConstants.HEADER_TRANSFER_ENCODING, HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED);
/*Un comment the below to force a 100-Continue... This will cause httpsender to wait for
* this response on a post. If HTTP 1.1 and this is not set, *SOME* servers *MAY* reply with this anyway.
* Currently httpsender won't handle this situation, this will require the resp. which it will handle.
*/
//myhttp.put(HTTPConstants.HEADER_EXPECT, HTTPConstants.HEADER_EXPECT_100_Continue);
call.setScopedProperty(HTTPConstants.REQUEST_HEADERS,myhttp);
call.setTargetEndpointAddress( new URL(opts.getURL()) ); //Set the target service host and service location,
java.util.Stack rev= new java.util.Stack();
//Create an attachment referenced by a generated contentId.
AttachmentPart ap= new AttachmentPart(new javax.activation.DataHandler(
"Now is the time", "text/plain" ));
refs.add(ap.getContentIdRef()); //reference the attachment by contentId.
ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
call.addAttachmentPart(ap);
rev.push(ap);
//Create an attachment referenced by a set contentId.
String setContentId="rick_did_this";
ap= new AttachmentPart(new DataHandler(" for all good", "text/plain" ));
//new MemoryOnlyDataSource(
ap.setContentId(setContentId);
refs.add("cid:" + setContentId); //reference the attachment by contentId.
ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
call.addAttachmentPart(ap);
rev.push(ap);
//Create an attachment referenced by a absolute contentLocation.
ap= new AttachmentPart(new DataHandler( " men to", "text/plain" ));
//new MemoryOnlyDataSource( " men to", "text/plain" )));
ap.setContentLocation(baseLoc+ "/firstLoc");
refs.add(baseLoc+ "/firstLoc"); //reference the attachment by contentId.
ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
call.addAttachmentPart(ap);
rev.push(ap);
//Create an attachment referenced by relative location to a absolute location.
ap= new AttachmentPart(new DataHandler( " come to", "text/plain" ));
// new MemoryOnlyDataSource( " come to", "text/plain" )));
ap.setContentLocation(baseLoc+ "/secondLoc");
refs.add("secondLoc"); //reference the attachment by contentId.
ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
call.addAttachmentPart(ap);
rev.push(ap);
//Create an attachment referenced by relative location to a relative location.
ap= new AttachmentPart(new DataHandler( " the aid of their country.", "text/plain" ));
// new MemoryOnlyDataSource( " the aid of their country.", "text/plain" )));
ap.setContentLocation("thirdLoc");
refs.add("thirdLoc"); //reference the attachment by contentId.
ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); //create a MIME header indicating postion.
call.addAttachmentPart(ap);
rev.push(ap);
//Now build the message....
String namespace="urn:attachmentsTestRef"; //needs to match name of service.
StringBuffer msg = new StringBuffer("\n<attachments xmlns=\"" +namespace +"\">\n");
for (java.util.Iterator i = refs.iterator(); i.hasNext() ; )
msg.append(" <attachment href=\"" + (String) i.next() + "\"/>\n");
msg.append( "</attachments>");
call.setUsername( opts.getUser());
call.setPassword( opts.getPassword() );
call.setOperationStyle("document");
call.setOperationUse("literal");
//Now do the call....
Object ret = call.invoke(new Object[]{
new SOAPBodyElement(new ByteArrayInputStream(msg.toString().getBytes("UTF-8"))) } );
validate(ret, call, "12345");
//Note: that even though the attachments are sent in reverse they are still
// retreived by reference so the ordinal will still match.
int revc=1;
for( ap= (AttachmentPart)rev.pop(); ap!=null ;ap= rev.empty()? null : (AttachmentPart)rev.pop()){
call.addAttachmentPart(ap);
}
//Now do the call....
ret = call.invoke(new Object[]{
new SOAPBodyElement(new ByteArrayInputStream(msg.toString().getBytes("UTF-8"))) } );
validate(ret, call, "54321");