1 16 17 package samples.attachments; 18 19 20 import org.apache.axis.AxisFault; 21 import org.apache.axis.attachments.AttachmentPart; 22 import org.apache.axis.client.Call; 23 import org.apache.axis.client.Service; 24 import org.apache.axis.message.SOAPBodyElement; 25 import org.apache.axis.transport.http.HTTPConstants; 26 import org.apache.axis.utils.Options; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 31 import javax.activation.DataHandler ; 32 import javax.activation.FileDataSource ; 33 import java.io.ByteArrayInputStream ; 34 import java.net.URL ; 35 import java.util.Hashtable ; 36 import java.util.Vector ; 37 38 39 43 44 65 public class TestRef { 66 67 Options opts = null; 68 public static final String positionHTTPHeader="Ordinal"; 69 public static final String TheKey= "Now is the time for all good men to come to the aid of their country."; 70 71 72 public TestRef( Options opts) { 73 this.opts = opts; 74 } 75 76 77 82 public boolean testit() throws Exception { 83 boolean rc = true; 84 String baseLoc= "http://axis.org/attachTest"; 85 Vector refs= new Vector (); 87 Service service = new Service(); 89 Call call = (Call) service.createCall(); 91 92 Hashtable myhttp= new Hashtable (); 94 myhttp.put(HTTPConstants.HEADER_CONTENT_LOCATION, baseLoc); 96 97 99 103 call.setProperty(HTTPConstants.REQUEST_HEADERS,myhttp); 105 106 call.setTargetEndpointAddress( new URL (opts.getURL()) ); 108 java.util.Stack rev= new java.util.Stack (); 109 AttachmentPart ap= new AttachmentPart(new javax.activation.DataHandler ( 111 "Now is the time", "text/plain" )); 112 refs.add(ap.getContentIdRef()); ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); call.addAttachmentPart(ap); 115 rev.push(ap); 116 117 String setContentId="rick_did_this"; 119 ap= new AttachmentPart(new DataHandler (" for all good", "text/plain" )); 120 ap.setContentId(setContentId); 122 refs.add("cid:" + setContentId); ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); call.addAttachmentPart(ap); 125 rev.push(ap); 126 127 ap= new AttachmentPart(new DataHandler ( " men to", "text/plain" )); 129 ap.setContentLocation(baseLoc+ "/firstLoc"); 131 refs.add(baseLoc+ "/firstLoc"); ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); call.addAttachmentPart(ap); 134 rev.push(ap); 135 136 ap= new AttachmentPart(new DataHandler ( " come to", "text/plain" )); 138 ap.setContentLocation(baseLoc+ "/secondLoc"); 140 refs.add("secondLoc"); ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); call.addAttachmentPart(ap); 143 rev.push(ap); 144 145 ap= new AttachmentPart(new DataHandler ( " the aid of their country.", "text/plain" )); 147 ap.setContentLocation("thirdLoc"); 149 refs.add("thirdLoc"); ap.setMimeHeader(positionHTTPHeader, ""+refs.size() ); call.addAttachmentPart(ap); 152 rev.push(ap); 153 154 155 String namespace="urn:attachmentsTestRef"; 158 StringBuffer msg = new StringBuffer ("\n<attachments xmlns=\"" +namespace +"\">\n"); 159 for (java.util.Iterator i = refs.iterator(); i.hasNext() ; ) 160 msg.append(" <attachment HREF=\"" + (String ) i.next() + "\"/>\n"); 161 162 msg.append( "</attachments>"); 163 164 call.setUsername( opts.getUser()); 165 166 call.setPassword( opts.getPassword() ); 167 168 call.setOperationStyle("document"); 169 call.setOperationUse("literal"); 170 171 Object ret = call.invoke(new Object []{ 173 new SOAPBodyElement(new ByteArrayInputStream (msg.toString().getBytes("UTF-8"))) } ); 174 175 validate(ret, call, "12345"); 176 177 int revc=1; 180 for( ap= (AttachmentPart)rev.pop(); ap!=null ;ap= rev.empty()? null : (AttachmentPart)rev.pop()){ 181 call.addAttachmentPart(ap); 182 } 183 184 ret = call.invoke(new Object []{ 186 new SOAPBodyElement(new ByteArrayInputStream (msg.toString().getBytes("UTF-8"))) } ); 187 188 validate(ret, call, "54321"); 189 190 191 return rc; 192 } 193 194 void validate(Object ret, Call call, final String expOrdPattern) throws Exception { 195 if (null == ret) { 196 System.out.println("Received null "); 197 throw new AxisFault("", "Received null", null, null); 198 } 199 200 if (ret instanceof String ) { 201 System.out.println("Received problem response from server: " + ret); 202 throw new AxisFault("", (String ) ret, null, null); 203 } 204 205 Vector vret= (Vector ) ret; 206 207 if (!(ret instanceof java.util.Vector )) { 208 System.out.println("Received unexpected type :" + 210 ret.getClass().getName()); 211 throw new AxisFault("", "Received unexpected type:" + 212 ret.getClass().getName(), null, null); 213 214 } 215 216 org.apache.axis.message.RPCElement retrpc= (org.apache.axis.message.RPCElement ) 217 ((Vector )ret).elementAt(0); 218 219 Document retDoc= org.apache.axis.utils.XMLUtils.newDocument( 220 new org.xml.sax.InputSource (new java.io.ByteArrayInputStream ( 221 retrpc.toString().getBytes()))); 222 223 org.apache.axis.attachments.Attachments attachments= 225 call.getResponseMessage().getAttachmentsImpl(); 226 227 Element rootEl= retDoc.getDocumentElement(); 229 230 Element caEl= getNextFirstChildElement(rootEl); 231 String HREF= caEl.getAttribute("href"); 234 org.apache.axis.Part p= attachments.getAttachmentByReference(href); 235 if(null == p) 236 throw new org.apache.axis.AxisFault("Attachment for ref='"+href+"' not found." ); 237 238 String ordPattern= caEl.getAttribute("ordinalPattern"); 240 if(!expOrdPattern.equals(ordPattern)) 241 throw new org.apache.axis.AxisFault( 242 "Attachments sent out of order expected:'" +expOrdPattern + "', got:'"+ordPattern+"'." ); 243 244 DataHandler dh= ((org.apache.axis.attachments.AttachmentPart)p).getDataHandler(); 246 System.err.println("content-type:" + dh.getContentType()); 247 248 java.util.Vector rspVector= null; 249 Object rspObject = dh.getContent(); if(rspObject == null) 251 throw new AxisFault("", "Received unexpected object:null", null, null); 252 else if(rspObject instanceof java.util.Vector ) rspVector= (java.util.Vector )rspObject; 253 else if(rspObject instanceof java.io.InputStream ) 254 rspVector= (java.util.Vector ) 255 new java.io.ObjectInputStream ((java.io.InputStream )rspObject ).readObject(); 256 else 257 throw new AxisFault("", "Received unexpected object:" + 258 rspObject.getClass().getName(), null, null); 259 260 StringBuffer fullmsg= new StringBuffer (); 261 for(java.util.Iterator ri= rspVector.iterator(); ri.hasNext();){ 262 String part= (String )ri.next(); 263 fullmsg.append(part); 264 System.out.print(part); 265 } 266 System.out.println(""); 267 268 if(!(TheKey.equals (fullmsg.toString()))) 269 throw new org.apache.axis.AxisFault("Fullmsg not correct'"+fullmsg +"'." ); 270 } 271 272 Element getNextFirstChildElement(Node n) { 273 if(n== null) return null; 274 n= n.getFirstChild(); 275 for(; n!= null && !(n instanceof Element); n= n.getNextSibling()); 276 return (Element)n; 277 } 278 279 Element getNextSiblingElement(Node n) { 280 if(n== null) return null; 281 n= n.getNextSibling(); 282 for(; n!= null && !(n instanceof Element); n= n.getNextSibling()); 283 return (Element)n; 284 } 285 286 291 public static void main(String args[]) { 292 try { 293 294 Options opts = new Options(args); 295 TestRef echoattachment = new TestRef(opts); 296 297 args = opts.getRemainingArgs(); 298 int argpos=0; 299 300 if (echoattachment.testit()) { 301 System.out.println("Attachments sent and received ok!"); 302 System.exit(0); 303 } 304 } 305 catch ( Exception e ) { 306 System.err.println(e); 307 e.printStackTrace(); 308 } 309 System.exit(18); 310 } 311 312 313 318 319 protected DataHandler [] getAttachmentsFromDir(String dirName) { 320 java.util.LinkedList retList = new java.util.LinkedList (); 321 DataHandler [] ret = new DataHandler [0]; 323 java.io.File sourceDir = new java.io.File (dirName); 324 325 java.io.File [] files = sourceDir.listFiles(); 326 327 for ( int i = files.length - 1; i >= 0; --i) { 328 java.io.File cf = files[i]; 329 330 if (cf.isFile() && cf.canRead()) { 331 String fname = null; 332 333 try { 334 fname = cf.getAbsoluteFile().getCanonicalPath(); 335 } 336 catch ( java.io.IOException e) { 337 System.err.println("Couldn't get file \"" + fname + "\" skipping..."); 338 continue; 339 } 340 retList.add( new DataHandler ( new FileDataSource ( fname ))); 341 } 342 } 343 if (!retList.isEmpty()) { 344 ret = new DataHandler [ retList.size()]; 345 ret = (DataHandler []) retList.toArray(ret); 346 } 347 348 return ret; 349 } 350 351 352 static class MemoryOnlyDataSource extends org.apache.axis.attachments.ManagedMemoryDataSource{ 353 354 MemoryOnlyDataSource( byte [] in, String contentType) throws java.io.IOException { 355 super( new java.io.ByteArrayInputStream ( in) , Integer.MAX_VALUE -2, contentType, true); 356 } 357 MemoryOnlyDataSource( String in, String contentType)throws java.io.IOException { 358 this( in.getBytes() , contentType); 359 } 360 } 361 } 362 | Popular Tags |