1 16 17 package samples.attachments; 18 19 20 import org.apache.axis.AxisFault; 21 import org.apache.axis.client.Call; 22 import org.apache.axis.client.Service; 23 import org.apache.axis.encoding.XMLType; 24 import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory; 25 import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory; 26 import org.apache.axis.transport.http.HTTPConstants; 27 import org.apache.axis.utils.Options; 28 29 import javax.activation.DataHandler ; 30 import javax.activation.FileDataSource ; 31 import javax.xml.namespace.QName ; 32 import javax.xml.rpc.ParameterMode ; 33 import javax.xml.soap.AttachmentPart ; 34 import javax.xml.soap.MessageFactory ; 35 import javax.xml.soap.SOAPBody ; 36 import javax.xml.soap.SOAPBodyElement ; 37 import javax.xml.soap.SOAPConnection ; 38 import javax.xml.soap.SOAPConnectionFactory ; 39 import javax.xml.soap.SOAPEnvelope ; 40 import javax.xml.soap.SOAPMessage ; 41 import javax.xml.soap.SOAPPart ; 42 import java.io.File ; 43 import java.net.URL ; 44 import java.util.Hashtable ; 45 import java.util.Iterator ; 46 import java.util.ListIterator ; 47 import java.util.Vector ; 48 49 50 54 55 62 public class EchoAttachment { 63 64 Options opts = null; 65 66 67 public EchoAttachment(Options opts) { 68 this.opts = opts; 69 } 70 71 78 public boolean echo(final boolean doTheDIME, String filename) throws Exception { 79 80 83 84 DataHandler dhSource = new DataHandler (new FileDataSource (filename)); 86 87 Service service = new Service(); 88 89 Call call = (Call) service.createCall(); 90 91 92 call.setTargetEndpointAddress(new URL (opts.getURL())); 94 call.setOperationName(new QName ("urn:EchoAttachmentsService", "echo")); 96 QName qnameAttachment = new QName ("urn:EchoAttachmentsService", "DataHandler"); 97 98 call.registerTypeMapping(dhSource.getClass(), qnameAttachment, 100 JAFDataHandlerSerializerFactory.class, 101 JAFDataHandlerDeserializerFactory.class); 102 103 104 call.addParameter("source", qnameAttachment, 105 ParameterMode.IN); 107 call.setReturnType(qnameAttachment); 108 109 call.setUsername(opts.getUser()); 110 111 call.setPassword(opts.getPassword()); 112 113 if (doTheDIME) 114 call.setProperty(call.ATTACHMENT_ENCAPSULATION_FORMAT, 115 call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME); 116 117 118 Object ret = call.invoke(new Object []{ 119 dhSource 120 } 121 ); 123 if (null == ret) { 124 System.out.println("Received null "); 125 throw new AxisFault("", "Received null", null, null); 126 } 127 128 if (ret instanceof String ) { 129 System.out.println("Received problem response from server: " + ret); 130 throw new AxisFault("", (String ) ret, null, null); 131 } 132 133 if (!(ret instanceof DataHandler )) { 134 System.out.println("Received problem response from server:" + 136 ret.getClass().getName()); 137 throw new AxisFault("", "Received problem response from server:" + 138 ret.getClass().getName(), null, null); 139 140 } 141 DataHandler rdh = (DataHandler ) ret; 145 146 String receivedfileName = rdh.getName(); 149 if (receivedfileName == null) { 150 System.err.println("Could not get the file name."); 151 throw new AxisFault("", "Could not get the file name.", null, null); 152 } 153 154 155 System.out.println("Going to compare the files.."); 156 boolean retv = compareFiles(filename, receivedfileName); 157 158 java.io.File receivedFile = new java.io.File (receivedfileName); 159 160 receivedFile.delete(); 161 162 return retv; 163 } 164 165 170 public boolean echoDir(final boolean doTheDIME, String filename) throws Exception { 171 boolean rc = true; 172 173 174 DataHandler [] attachments = getAttachmentsFromDir(filename); 176 if (attachments.length == 0) { 177 throw new java.lang.IllegalArgumentException ( 178 "The directory \"" + filename + "\" has no files to send."); 179 } 180 181 Service service = new Service(); 183 Call call = (Call) service.createCall(); 185 186 Hashtable myhttp = new Hashtable (); 188 myhttp.put("dddd", "yyy"); myhttp.put("SOAPAction", "dyyy"); 190 myhttp.put("SOAPActions", "prova"); 191 192 193 195 199 call.setProperty(HTTPConstants.REQUEST_HEADERS, myhttp); 201 202 call.setTargetEndpointAddress(new URL (opts.getURL())); 204 call.setOperationName(new QName ("urn:EchoAttachmentsService", "echoDir")); 206 QName qnameAttachment = new QName ("urn:EchoAttachmentsService", "DataHandler"); 207 208 call.registerTypeMapping(attachments[0].getClass(), qnameAttachment, 210 JAFDataHandlerSerializerFactory.class, 211 JAFDataHandlerDeserializerFactory.class); 212 213 call.addParameter("source", XMLType.SOAP_ARRAY, ParameterMode.IN); 216 call.setReturnType(XMLType.SOAP_ARRAY); 218 call.setUsername(opts.getUser()); 219 220 call.setPassword(opts.getPassword()); 221 222 if (doTheDIME) 223 call.setProperty(call.ATTACHMENT_ENCAPSULATION_FORMAT, 224 call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME); 225 226 Object ret = call.invoke(new Object []{ 227 attachments 228 } 229 ); 231 if (null == ret) { 232 System.out.println("Received null "); 233 throw new AxisFault("", "Received null", null, null); 234 } 235 236 if (ret instanceof String ) { 237 System.out.println("Received problem response from server: " + ret); 238 throw new AxisFault("", (String ) ret, null, null); 239 } 240 241 if (!(ret instanceof javax.activation.DataHandler [])) { 242 System.out.println("Received unexpected type :" + 244 ret.getClass().getName()); 245 throw new AxisFault("", "Received unexpected type:" + 246 ret.getClass().getName(), null, null); 247 248 } 249 javax.activation.DataHandler [] received = (javax.activation.DataHandler []) ret; 253 254 int i = 0; 255 for (i = 0; i < received.length && i < attachments.length; ++i) { 256 DataHandler recDH = received[i]; 257 DataHandler orginalDH = attachments[i]; 258 259 if (!compareFiles(filename + java.io.File.separator + orginalDH.getName(), recDH.getName())) { 260 System.err.println("The attachment with the file name: \"" + orginalDH.getName() + 261 "\" was not received the same!."); 262 rc = false; 263 } 264 java.io.File receivedFile = new java.io.File (recDH.getName()); 265 266 receivedFile.delete(); 267 } 268 269 if (i < received.length) { 270 System.err.println("There are more file received than sent!!!!"); 271 272 rc = false; 273 } 274 if (i < attachments.length) { 275 System.err.println("Not all the files were received!"); 276 rc = false; 277 } 278 279 280 return rc; 281 } 282 283 288 public static void main(String args[]) { 289 try { 290 291 Options opts = new Options(args); 292 EchoAttachment echoattachment = new EchoAttachment(opts); 293 294 args = opts.getRemainingArgs(); 295 int argpos = 0; 296 297 if (args == null || args.length == 0) { 298 System.err.println("Need a file or directory argument."); 299 System.exit(8); 300 } 301 302 boolean doTheDIME = false; 303 if (args[0].trim().equalsIgnoreCase("+FDR")) { 304 doTheDIME = true; 305 ++argpos; 306 } 307 308 if (argpos >= args.length) { 309 System.err.println("Need a file or directory argument."); 310 System.exit(8); 311 } 312 313 String argFile = args[argpos]; 314 315 java.io.File source = new java.io.File (argFile); 316 317 if (!source.exists()) { 318 System.err.println("Error \"" + argFile + "\" does not exist!"); 319 System.exit(8); 320 } 321 322 if (source.isFile()) { 323 if (echoattachment.echoUsingSAAJ(argFile) && echoattachment.echo(doTheDIME, argFile)) { 324 System.out.println("Attachment sent and received ok!"); 325 System.exit(0); 326 } else { 327 System.err.println("Problem in matching attachments"); 328 System.exit(8); 329 } 330 } else { if (echoattachment.echoDir(doTheDIME, argFile)) { 332 System.out.println("Attachments sent and received ok!"); 333 System.exit(0); 334 } else { 335 System.err.println("Problem in matching attachments"); 336 System.exit(8); 337 } 338 } 339 } catch (Exception e) { 340 System.err.println(e); 341 e.printStackTrace(); 342 } 343 System.exit(18); 344 } 345 346 354 protected boolean compareFiles(String one, String other) 355 throws java.io.FileNotFoundException , java.io.IOException { 356 357 java.io.BufferedInputStream oneStream = null; 358 java.io.BufferedInputStream otherStream = null; 359 360 File f1 = new File (one); 362 File f2 = new File (other); 363 if (f1.length() != f2.length()) 364 return false; 365 366 try { 367 oneStream = new java.io.BufferedInputStream ( 368 new java.io.FileInputStream (one), 1024 * 64); 369 otherStream = new java.io.BufferedInputStream ( 370 new java.io.FileInputStream (other), 1024 * 64); 371 372 byte[] bufOne = new byte[1024 * 64]; 373 byte[] bufOther = new byte[1024 * 64]; 374 int breadOne = -1; 375 int breadOther = -1; 376 int available = 0; 377 378 do { 379 available = oneStream.available(); 380 available = Math.min(available, otherStream.available()); 381 available = Math.min(available, bufOther.length); 382 383 if (0 != available) { 384 java.util.Arrays.fill(bufOne, (byte) 0); 385 java.util.Arrays.fill(bufOther, (byte) 0); 386 387 breadOne = oneStream.read(bufOne, 0, available); 388 breadOther = otherStream.read(bufOther, 0, available); 389 if (breadOne != breadOther) 390 throw new RuntimeException ( 391 "Sorry couldn't really read whats available!"); 392 if (!java.util.Arrays.equals(bufOne, bufOther)) { 393 return false; 394 } 395 } 396 397 } while (available != 0 && breadOne != -1 && breadOther != -1); 398 if (available != 0 && (breadOne != -1 || breadOther != -1)) { 399 return false; 400 } 401 return true; 402 } finally { 403 if (null != oneStream) oneStream.close(); 404 if (null != otherStream) otherStream.close(); 405 } 406 } 407 408 413 414 protected DataHandler [] getAttachmentsFromDir(String dirName) { 415 java.util.LinkedList retList = new java.util.LinkedList (); 416 DataHandler [] ret = new DataHandler [0]; 418 java.io.File sourceDir = new java.io.File (dirName); 419 420 java.io.File [] files = sourceDir.listFiles(); 421 422 for (int i = files.length - 1; i >= 0; --i) { 423 java.io.File cf = files[i]; 424 425 if (cf.isFile() && cf.canRead()) { 426 String fname = null; 427 428 try { 429 fname = cf.getAbsoluteFile().getCanonicalPath(); 430 } catch (java.io.IOException e) { 431 System.err.println("Couldn't get file \"" + fname + "\" skipping..."); 432 continue; 433 } 434 retList.add(new DataHandler (new FileDataSource (fname))); 435 } 436 } 437 if (!retList.isEmpty()) { 438 ret = new DataHandler [retList.size()]; 439 ret = (DataHandler []) retList.toArray(ret); 440 } 441 442 return ret; 443 } 444 445 452 public boolean echoUsingSAAJ(String filename) throws Exception { 453 String endPointURLString = "http://localhost:" +opts.getPort() + "/axis/services/urn:EchoAttachmentsService"; 454 455 SOAPConnectionFactory soapConnectionFactory = 456 javax.xml.soap.SOAPConnectionFactory.newInstance(); 457 SOAPConnection soapConnection = 458 soapConnectionFactory.createConnection(); 459 460 MessageFactory messageFactory = 461 MessageFactory.newInstance(); 462 SOAPMessage soapMessage = 463 messageFactory.createMessage(); 464 SOAPPart soapPart = soapMessage.getSOAPPart(); 465 SOAPEnvelope requestEnvelope = 466 soapPart.getEnvelope(); 467 SOAPBody body = requestEnvelope.getBody(); 468 SOAPBodyElement operation = body.addBodyElement 469 (requestEnvelope.createName("echo")); 470 471 Vector dataHandlersToAdd = new Vector (); 472 dataHandlersToAdd.add(new DataHandler (new FileDataSource (new 473 File (filename)))); 474 475 if (dataHandlersToAdd != null) { 476 ListIterator dataHandlerIterator = 477 dataHandlersToAdd.listIterator(); 478 479 while (dataHandlerIterator.hasNext()) { 480 DataHandler dataHandler = (DataHandler ) 481 dataHandlerIterator.next(); 482 javax.xml.soap.SOAPElement element = 483 operation.addChildElement(requestEnvelope.createName("source")); 484 javax.xml.soap.AttachmentPart attachment = 485 soapMessage.createAttachmentPart(dataHandler); 486 soapMessage.addAttachmentPart(attachment); 487 element.addAttribute(requestEnvelope.createName 488 ("href"), "cid:" + attachment.getContentId()); 489 } 490 } 491 javax.xml.soap.SOAPMessage returnedSOAPMessage = 492 soapConnection.call(soapMessage, endPointURLString); 493 Iterator iterator = returnedSOAPMessage.getAttachments(); 494 if (!iterator.hasNext()) { 495 System.out.println("Received problem response from server"); 497 throw new AxisFault("", "Received problem response from server", null, null); 498 499 } 500 DataHandler rdh = (DataHandler ) ((AttachmentPart )iterator.next()).getDataHandler(); 504 505 String receivedfileName = rdh.getName(); 508 if (receivedfileName == null) { 509 System.err.println("Could not get the file name."); 510 throw new AxisFault("", "Could not get the file name.", null, null); 511 } 512 513 514 System.out.println("Going to compare the files.."); 515 boolean retv = compareFiles(filename, receivedfileName); 516 517 java.io.File receivedFile = new java.io.File (receivedfileName); 518 519 receivedFile.delete(); 520 521 return retv; 522 } 523 } 524 | Popular Tags |