001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027:
028: package mypackage;
029:
030: import javax.xml.soap.*;
031: import java.net.*;
032: import java.util.*;
033: import java.io.*;
034: import javax.activation.*;
035:
036: public class Attachments {
037: public static void main(String[] args) {
038: FileReader fr = null;
039: BufferedReader br = null;
040: String line = "";
041:
042: try {
043: // One argument is passed in from build.xml
044: if (args.length != 1) {
045: System.err.println("Usage: asant run "
046: + "-Dtext-file=<filename>");
047: System.exit(1);
048: }
049:
050: // Create message factory
051: MessageFactory messageFactory = MessageFactory
052: .newInstance();
053:
054: // Create a message
055:
056: SOAPMessage message = messageFactory.createMessage();
057:
058: // Get the SOAP header and body from the message
059: // and remove the header
060: SOAPHeader header = message.getSOAPHeader();
061: SOAPBody body = message.getSOAPBody();
062: header.detachNode();
063:
064: // Create attachment part for text
065: AttachmentPart attachment1 = message.createAttachmentPart();
066:
067: fr = new FileReader(new File(args[0]));
068: br = new BufferedReader(fr);
069:
070: String stringContent = "";
071: line = br.readLine();
072:
073: while (line != null) {
074: stringContent = stringContent.concat(line);
075: stringContent = stringContent.concat("\n");
076: line = br.readLine();
077: }
078:
079: attachment1.setContent(stringContent, "text/plain");
080: attachment1.setContentId("attached_text");
081:
082: message.addAttachmentPart(attachment1);
083:
084: // Create attachment part for image
085: URL url = new URL("file:///../xml-pic.jpg");
086: DataHandler dataHandler = new DataHandler(url);
087: AttachmentPart attachment2 = message
088: .createAttachmentPart(dataHandler);
089: attachment2.setContentId("attached_image");
090:
091: message.addAttachmentPart(attachment2);
092:
093: // Now extract the attachments
094: Iterator iterator = message.getAttachments();
095:
096: while (iterator.hasNext()) {
097: AttachmentPart attached = (AttachmentPart) iterator
098: .next();
099: String id = attached.getContentId();
100: String type = attached.getContentType();
101: System.out.println("Attachment " + id
102: + " has content type " + type);
103:
104: if (type.equals("text/plain")) {
105: Object content = attached.getContent();
106: System.out.println("Attachment " + "contains:\n"
107: + content);
108: }
109: }
110: } catch (FileNotFoundException e) {
111: System.out.println("File not found: " + e.toString());
112: System.exit(1);
113: } catch (IOException e) {
114: System.out.println("I/O exception: " + e.toString());
115: System.exit(1);
116: } catch (Exception ex) {
117: ex.printStackTrace();
118: }
119: }
120: }
|