Java Code Examples for com.sun.xml.internal.ws.api.message.Attachment#getContentId()

The following examples show how to use com.sun.xml.internal.ws.api.message.Attachment#getContentId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageContextImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 2
Source File: MessageContextImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 3
Source File: SAAJFactory.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
    for(Attachment att : message.getAttachments()) {
        AttachmentPart part = msg.createAttachmentPart();
        part.setDataHandler(att.asDataHandler());

        // Be safe and avoid double angle-brackets.
        String cid = att.getContentId();
        if (cid != null) {
            if (cid.startsWith("<") && cid.endsWith(">"))
                part.setContentId(cid);
            else
                part.setContentId('<' + cid + '>');
        }

        // Add any MIME headers beside Content-ID, which is already
        // accounted for above, and Content-Type, which is provided
        // by the DataHandler above.
        if (att instanceof AttachmentEx) {
            AttachmentEx ax = (AttachmentEx) att;
            Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
            while (imh.hasNext()) {
                AttachmentEx.MimeHeader ame = imh.next();
                if ((!"Content-ID".equals(ame.getName()))
                        && (!"Content-Type".equals(ame.getName())))
                    part.addMimeHeader(ame.getName(), ame.getValue());
            }
        }
        msg.addAttachmentPart(part);
    }
}
 
Example 4
Source File: MessageContextImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 5
Source File: SAAJFactory.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
    for(Attachment att : message.getAttachments()) {
        AttachmentPart part = msg.createAttachmentPart();
        part.setDataHandler(att.asDataHandler());

        // Be safe and avoid double angle-brackets.
        String cid = att.getContentId();
        if (cid != null) {
            if (cid.startsWith("<") && cid.endsWith(">"))
                part.setContentId(cid);
            else
                part.setContentId('<' + cid + '>');
        }

        // Add any MIME headers beside Content-ID, which is already
        // accounted for above, and Content-Type, which is provided
        // by the DataHandler above.
        if (att instanceof AttachmentEx) {
            AttachmentEx ax = (AttachmentEx) att;
            Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
            while (imh.hasNext()) {
                AttachmentEx.MimeHeader ame = imh.next();
                if ((!"Content-ID".equals(ame.getName()))
                        && (!"Content-Type".equals(ame.getName())))
                    part.addMimeHeader(ame.getName(), ame.getValue());
            }
        }
        msg.addAttachmentPart(part);
    }
}
 
Example 6
Source File: MessageContextImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 7
Source File: SAAJFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
    for(Attachment att : message.getAttachments()) {
        AttachmentPart part = msg.createAttachmentPart();
        part.setDataHandler(att.asDataHandler());

        // Be safe and avoid double angle-brackets.
        String cid = att.getContentId();
        if (cid != null) {
            if (cid.startsWith("<") && cid.endsWith(">"))
                part.setContentId(cid);
            else
                part.setContentId('<' + cid + '>');
        }

        // Add any MIME headers beside Content-ID, which is already
        // accounted for above, and Content-Type, which is provided
        // by the DataHandler above.
        if (att instanceof AttachmentEx) {
            AttachmentEx ax = (AttachmentEx) att;
            Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
            while (imh.hasNext()) {
                AttachmentEx.MimeHeader ame = imh.next();
                if ((!"Content-ID".equals(ame.getName()))
                        && (!"Content-Type".equals(ame.getName())))
                    part.addMimeHeader(ame.getName(), ame.getValue());
            }
        }
        msg.addAttachmentPart(part);
    }
}
 
Example 8
Source File: SAAJFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
    for(Attachment att : message.getAttachments()) {
        AttachmentPart part = msg.createAttachmentPart();
        part.setDataHandler(att.asDataHandler());

        // Be safe and avoid double angle-brackets.
        String cid = att.getContentId();
        if (cid != null) {
            if (cid.startsWith("<") && cid.endsWith(">"))
                part.setContentId(cid);
            else
                part.setContentId('<' + cid + '>');
        }

        // Add any MIME headers beside Content-ID, which is already
        // accounted for above, and Content-Type, which is provided
        // by the DataHandler above.
        if (att instanceof AttachmentEx) {
            AttachmentEx ax = (AttachmentEx) att;
            Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
            while (imh.hasNext()) {
                AttachmentEx.MimeHeader ame = imh.next();
                if ((!"Content-ID".equals(ame.getName()))
                        && (!"Content-Type".equals(ame.getName())))
                    part.addMimeHeader(ame.getName(), ame.getValue());
            }
        }
        msg.addAttachmentPart(part);
    }
}
 
Example 9
Source File: SAAJFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
    for(Attachment att : message.getAttachments()) {
        AttachmentPart part = msg.createAttachmentPart();
        part.setDataHandler(att.asDataHandler());

        // Be safe and avoid double angle-brackets.
        String cid = att.getContentId();
        if (cid != null) {
            if (cid.startsWith("<") && cid.endsWith(">"))
                part.setContentId(cid);
            else
                part.setContentId('<' + cid + '>');
        }

        // Add any MIME headers beside Content-ID, which is already
        // accounted for above, and Content-Type, which is provided
        // by the DataHandler above.
        if (att instanceof AttachmentEx) {
            AttachmentEx ax = (AttachmentEx) att;
            Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
            while (imh.hasNext()) {
                AttachmentEx.MimeHeader ame = imh.next();
                if ((!"Content-ID".equals(ame.getName()))
                        && (!"Content-Type".equals(ame.getName())))
                    part.addMimeHeader(ame.getName(), ame.getValue());
            }
        }
        msg.addAttachmentPart(part);
    }
}
 
Example 10
Source File: MessageContextImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 11
Source File: MessageContextImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 12
Source File: SAAJFactory.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static protected void addAttachmentsToSOAPMessage(SOAPMessage msg, Message message) {
    for(Attachment att : message.getAttachments()) {
        AttachmentPart part = msg.createAttachmentPart();
        part.setDataHandler(att.asDataHandler());

        // Be safe and avoid double angle-brackets.
        String cid = att.getContentId();
        if (cid != null) {
            if (cid.startsWith("<") && cid.endsWith(">"))
                part.setContentId(cid);
            else
                part.setContentId('<' + cid + '>');
        }

        // Add any MIME headers beside Content-ID, which is already
        // accounted for above, and Content-Type, which is provided
        // by the DataHandler above.
        if (att instanceof AttachmentEx) {
            AttachmentEx ax = (AttachmentEx) att;
            Iterator<AttachmentEx.MimeHeader> imh = ax.getMimeHeaders();
            while (imh.hasNext()) {
                AttachmentEx.MimeHeader ame = imh.next();
                if ((!"Content-ID".equals(ame.getName()))
                        && (!"Content-Type".equals(ame.getName())))
                    part.addMimeHeader(ame.getName(), ame.getValue());
            }
        }
        msg.addAttachmentPart(part);
    }
}
 
Example 13
Source File: MessageContextImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 14
Source File: MessageContextImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Object get(Object key) {
    if(key == null)
        return null;
    Object value = asMapIncludingInvocationProperties.get(key);
    //add the attachments from the Message to the corresponding attachment property
    if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
        key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
        Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
        if(atts == null)
            atts = new HashMap<String, DataHandler>();
        AttachmentSet attSet = packet.getMessage().getAttachments();
        for(Attachment att : attSet){
            String cid = att.getContentId();
            if (cid.indexOf("@jaxws.sun.com") == -1) {
                Object a = atts.get(cid);
                if (a == null) {
                    a = atts.get("<" + cid + ">");
                    if (a == null) atts.put(att.getContentId(), att.asDataHandler());
                }
            } else {
                atts.put(att.getContentId(), att.asDataHandler());
            }
        }
        return atts;
    }
    return value;
}
 
Example 15
Source File: MimeCodec.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public ContentType encode(Packet packet, OutputStream out) throws IOException {
    Message msg = packet.getMessage();
    if (msg == null) {
        return null;
    }
    ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet);
    String boundary = ctImpl.getBoundary();
    boolean hasAttachments = (boundary != null);
    Codec rootCodec = getMimeRootCodec(packet);
    if (hasAttachments) {
        writeln("--"+boundary, out);
        ContentType ct = rootCodec.getStaticContentType(packet);
        String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType();
        writeln("Content-Type: " + ctStr, out);
        writeln(out);
    }
    ContentType primaryCt = rootCodec.encode(packet, out);

    if (hasAttachments) {
        writeln(out);
        // Encode all the attchments
        for (Attachment att : msg.getAttachments()) {
            writeln("--"+boundary, out);
            //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with
            //angle brackets. For now put angle bracket only if its not there
            String cid = att.getContentId();
            if(cid != null && cid.length() >0 && cid.charAt(0) != '<')
                cid = '<' + cid + '>';
            writeln("Content-Id:" + cid, out);
            writeln("Content-Type: " + att.getContentType(), out);
            writeCustomMimeHeaders(att, out);
            writeln("Content-Transfer-Encoding: binary", out);
            writeln(out);                    // write \r\n
            att.writeTo(out);
            writeln(out);                    // write \r\n
        }
        writeAsAscii("--"+boundary, out);
        writeAsAscii("--", out);
    }
    // TODO not returing correct multipart/related type(no boundary)
    return hasAttachments ? ctImpl : primaryCt;
}
 
Example 16
Source File: MimeCodec.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public ContentType encode(Packet packet, OutputStream out) throws IOException {
    Message msg = packet.getMessage();
    if (msg == null) {
        return null;
    }
    ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet);
    String boundary = ctImpl.getBoundary();
    boolean hasAttachments = (boundary != null);
    Codec rootCodec = getMimeRootCodec(packet);
    if (hasAttachments) {
        writeln("--"+boundary, out);
        ContentType ct = rootCodec.getStaticContentType(packet);
        String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType();
        writeln("Content-Type: " + ctStr, out);
        writeln(out);
    }
    ContentType primaryCt = rootCodec.encode(packet, out);

    if (hasAttachments) {
        writeln(out);
        // Encode all the attchments
        for (Attachment att : msg.getAttachments()) {
            writeln("--"+boundary, out);
            //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with
            //angle brackets. For now put angle bracket only if its not there
            String cid = att.getContentId();
            if(cid != null && cid.length() >0 && cid.charAt(0) != '<')
                cid = '<' + cid + '>';
            writeln("Content-Id:" + cid, out);
            writeln("Content-Type: " + att.getContentType(), out);
            writeCustomMimeHeaders(att, out);
            writeln("Content-Transfer-Encoding: binary", out);
            writeln(out);                    // write \r\n
            att.writeTo(out);
            writeln(out);                    // write \r\n
        }
        writeAsAscii("--"+boundary, out);
        writeAsAscii("--", out);
    }
    // TODO not returing correct multipart/related type(no boundary)
    return hasAttachments ? ctImpl : primaryCt;
}
 
Example 17
Source File: MimeCodec.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public ContentType encode(Packet packet, OutputStream out) throws IOException {
    Message msg = packet.getMessage();
    if (msg == null) {
        return null;
    }
    ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet);
    String boundary = ctImpl.getBoundary();
    boolean hasAttachments = (boundary != null);
    Codec rootCodec = getMimeRootCodec(packet);
    if (hasAttachments) {
        writeln("--"+boundary, out);
        ContentType ct = rootCodec.getStaticContentType(packet);
        String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType();
        writeln("Content-Type: " + ctStr, out);
        writeln(out);
    }
    ContentType primaryCt = rootCodec.encode(packet, out);

    if (hasAttachments) {
        writeln(out);
        // Encode all the attchments
        for (Attachment att : msg.getAttachments()) {
            writeln("--"+boundary, out);
            //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with
            //angle brackets. For now put angle bracket only if its not there
            String cid = att.getContentId();
            if(cid != null && cid.length() >0 && cid.charAt(0) != '<')
                cid = '<' + cid + '>';
            writeln("Content-Id:" + cid, out);
            writeln("Content-Type: " + att.getContentType(), out);
            writeCustomMimeHeaders(att, out);
            writeln("Content-Transfer-Encoding: binary", out);
            writeln(out);                    // write \r\n
            att.writeTo(out);
            writeln(out);                    // write \r\n
        }
        writeAsAscii("--"+boundary, out);
        writeAsAscii("--", out);
    }
    // TODO not returing correct multipart/related type(no boundary)
    return hasAttachments ? ctImpl : primaryCt;
}
 
Example 18
Source File: MimeCodec.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public ContentType encode(Packet packet, OutputStream out) throws IOException {
    Message msg = packet.getMessage();
    if (msg == null) {
        return null;
    }
    ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet);
    String boundary = ctImpl.getBoundary();
    boolean hasAttachments = (boundary != null);
    Codec rootCodec = getMimeRootCodec(packet);
    if (hasAttachments) {
        writeln("--"+boundary, out);
        ContentType ct = rootCodec.getStaticContentType(packet);
        String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType();
        writeln("Content-Type: " + ctStr, out);
        writeln(out);
    }
    ContentType primaryCt = rootCodec.encode(packet, out);

    if (hasAttachments) {
        writeln(out);
        // Encode all the attchments
        for (Attachment att : msg.getAttachments()) {
            writeln("--"+boundary, out);
            //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with
            //angle brackets. For now put angle bracket only if its not there
            String cid = att.getContentId();
            if(cid != null && cid.length() >0 && cid.charAt(0) != '<')
                cid = '<' + cid + '>';
            writeln("Content-Id:" + cid, out);
            writeln("Content-Type: " + att.getContentType(), out);
            writeCustomMimeHeaders(att, out);
            writeln("Content-Transfer-Encoding: binary", out);
            writeln(out);                    // write \r\n
            att.writeTo(out);
            writeln(out);                    // write \r\n
        }
        writeAsAscii("--"+boundary, out);
        writeAsAscii("--", out);
    }
    // TODO not returing correct multipart/related type(no boundary)
    return hasAttachments ? ctImpl : primaryCt;
}
 
Example 19
Source File: MimeCodec.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public ContentType encode(Packet packet, OutputStream out) throws IOException {
    Message msg = packet.getMessage();
    if (msg == null) {
        return null;
    }
    ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet);
    String boundary = ctImpl.getBoundary();
    String rootId = ctImpl.getRootId();
    boolean hasAttachments = (boundary != null);
    Codec rootCodec = getMimeRootCodec(packet);
    if (hasAttachments) {
        writeln("--"+boundary, out);
        ContentType ct = rootCodec.getStaticContentType(packet);
        String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType();
        if (rootId != null) writeln("Content-ID: " + rootId, out);
        writeln("Content-Type: " + ctStr, out);
        writeln(out);
    }
    ContentType primaryCt = rootCodec.encode(packet, out);

    if (hasAttachments) {
        writeln(out);
        // Encode all the attchments
        for (Attachment att : msg.getAttachments()) {
            writeln("--"+boundary, out);
            //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with
            //angle brackets. For now put angle bracket only if its not there
            String cid = att.getContentId();
            if(cid != null && cid.length() >0 && cid.charAt(0) != '<')
                cid = '<' + cid + '>';
            writeln("Content-Id:" + cid, out);
            writeln("Content-Type: " + att.getContentType(), out);
            writeCustomMimeHeaders(att, out);
            writeln("Content-Transfer-Encoding: binary", out);
            writeln(out);                    // write \r\n
            att.writeTo(out);
            writeln(out);                    // write \r\n
        }
        writeAsAscii("--"+boundary, out);
        writeAsAscii("--", out);
    }
    // TODO not returing correct multipart/related type(no boundary)
    return hasAttachments ? ctImpl : primaryCt;
}
 
Example 20
Source File: MimeCodec.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public ContentType encode(Packet packet, OutputStream out) throws IOException {
    Message msg = packet.getMessage();
    if (msg == null) {
        return null;
    }
    ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet);
    String boundary = ctImpl.getBoundary();
    boolean hasAttachments = (boundary != null);
    Codec rootCodec = getMimeRootCodec(packet);
    if (hasAttachments) {
        writeln("--"+boundary, out);
        ContentType ct = rootCodec.getStaticContentType(packet);
        String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType();
        writeln("Content-Type: " + ctStr, out);
        writeln(out);
    }
    ContentType primaryCt = rootCodec.encode(packet, out);

    if (hasAttachments) {
        writeln(out);
        // Encode all the attchments
        for (Attachment att : msg.getAttachments()) {
            writeln("--"+boundary, out);
            //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with
            //angle brackets. For now put angle bracket only if its not there
            String cid = att.getContentId();
            if(cid != null && cid.length() >0 && cid.charAt(0) != '<')
                cid = '<' + cid + '>';
            writeln("Content-Id:" + cid, out);
            writeln("Content-Type: " + att.getContentType(), out);
            writeCustomMimeHeaders(att, out);
            writeln("Content-Transfer-Encoding: binary", out);
            writeln(out);                    // write \r\n
            att.writeTo(out);
            writeln(out);                    // write \r\n
        }
        writeAsAscii("--"+boundary, out);
        writeAsAscii("--", out);
    }
    // TODO not returing correct multipart/related type(no boundary)
    return hasAttachments ? ctImpl : primaryCt;
}