Package javax.mail.internet

Examples of javax.mail.internet.ContentType


  }

  private static String charsetFromMime(String mime) {
    String charset;
    try {
      ContentType parsedType = new ContentType(mime);
      charset = parsedType.getParameter("charset");
    } catch (ParseException e) {
      charset = null;
    }
    if (null == charset || "".equals(charset)) {
      return DATA_URI_DEFAULT_CHARSET;
View Full Code Here


  private static String getCharSet(URLConnection conn) {
    try {
      String contentType = conn.getContentType();
      if (contentType == null) { return ""; }
      String charset = new ContentType(contentType).getParameter("charset");
      return (charset == null) ? "" : charset;
    } catch (ParseException e) {
      return "";
    }
  }
View Full Code Here

        AttachmentPart ap = msg.createAttachmentPart(new StreamSource(inputStream), "text/xml");       
        msg.addAttachmentPart(ap);
        msg.saveChanges();
        assertNotNull(msg.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE));       
        String contentTypeValue = msg.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE)[0];
        ContentType contentType = new ContentType(contentTypeValue);
        assertNotNull("boundary parameter should exist in the content-type header", contentType.getParameter("boundary"));
        //start parameter is not checked, due to it is optional parameter, and seems RI will not add this value
        //assertNotNull("start parameter should exist in the content-type header", contentType.getParameter("start"));
        assertNotNull("type parameter should exist in the content-type header", contentType.getParameter("type"));
        assertEquals(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED, contentType.getBaseType());       
    }
View Full Code Here

        SOAPMessage msg = fac.createMessage();              
        InputStream inputStream = TestUtils.getTestFile("attach.xml");
        AttachmentPart ap = msg.createAttachmentPart(new StreamSource(inputStream), "text/xml");       
        msg.addAttachmentPart(ap);
        msg.saveChanges();
        ContentType contentType = new ContentType(msg.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE)[0]);
       
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        msg.writeTo(out);
        SOAPMessage msg2 = fac.createMessage(msg.getMimeHeaders(), new ByteArrayInputStream(out.toByteArray()));
        msg2.saveChanges();
        ContentType contentType2 = new ContentType(msg2.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE)[0]);

        assertEquals(contentType.getBaseType(), contentType2.getBaseType());
        assertEquals(contentType.getParameter("boundary"), contentType2.getParameter("boundary"));
        assertEquals(contentType.getParameter("type"), contentType2.getParameter("type"));
        //start parameter is not checked, due to it is an optional parameter, and seems RI will not add this value
        //assertEquals(contentType.getParameter("start"), contentType2.getParameter("start"));
    }
View Full Code Here

        AttachmentPart ap = msg.createAttachmentPart(new StreamSource(inputStream), "text/xml");       
        msg.addAttachmentPart(ap);
        msg.saveChanges();
       
        assertNotNull(msg.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE));
        ContentType contentType = new ContentType(msg.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE)[0]);       
        assertEquals(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED, contentType.getBaseType());
       
        msg.removeAllAttachments();
        msg.saveChanges();

        assertNotNull(msg.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE));
        contentType = new ContentType(msg.getMimeHeaders().getHeader(HTTPConstants.HEADER_CONTENT_TYPE)[0]);       
        assertEquals("text/xml", contentType.getBaseType());
    }
View Full Code Here

     * @param attachments the set of attachments to be used to substitute xop:Include elements
     * @throws SOAPException
     */
    public SOAPPartImpl(SOAPMessageImpl parentSoapMsg, InputStream inputStream,
                        MimeHeaders mimeHeaders, Attachments attachments) throws SOAPException {
        ContentType contentType = null;
        if (mimeHeaders == null) {
            //TODO : read string from constants
            this.mimeHeaders = new MimeHeaders();
            this.mimeHeaders.addHeader("Content-ID", IDGenerator.generateID());
            this.mimeHeaders.addHeader("content-type", HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML);
        } else {
            String contentTypes[] = mimeHeaders.getHeader(HTTPConstants.HEADER_CONTENT_TYPE);
            if (contentTypes != null && contentTypes.length > 0) {
                try {
                    contentType = new ContentType(contentTypes[0]);
                } catch (ParseException ex) {
                    throw new SOAPException("Invalid content type '" + contentTypes[0] + "'");
                }
            }
            this.mimeHeaders = SAAJUtil.copyMimeHeaders(mimeHeaders);
        }

        soapMessage = parentSoapMsg;

        String charset;
        boolean isMTOM;
        String soapEnvelopeNamespaceURI;
        SOAPFactory soapFactory;
        if (contentType == null) {
            charset = null;
            isMTOM = false;
            soapFactory = new SOAP11Factory();
            soapEnvelopeNamespaceURI = null;
        } else {
            String baseType = contentType.getBaseType().toLowerCase();
            String soapContentType;
            if (baseType.equals(MTOMConstants.MTOM_TYPE)) {
                isMTOM = true;
                String typeParam = contentType.getParameter("type");
                if (typeParam == null) {
                    throw new SOAPException("Missing 'type' parameter in XOP content type");
                } else {
                    soapContentType = typeParam.toLowerCase();
                }
            } else {
                isMTOM = false;
                soapContentType = baseType;
            }
           
            if (soapContentType.equals(HTTPConstants.MEDIA_TYPE_TEXT_XML)) {
                soapEnvelopeNamespaceURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
                soapFactory = new SOAP11Factory();
            } else if (soapContentType.equals(HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML)) {
                soapEnvelopeNamespaceURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI;
                soapFactory = new SOAP12Factory();
            } else {
                throw new SOAPException("Unrecognized content type '" + soapContentType + "'");
            }
           
            charset = contentType.getParameter("charset");
        }
       
        XMLStreamReader streamReader;
        try {
            if (charset != null) {
View Full Code Here

     * @throws SOAPException if there was a problem saving changes to this message.
     */
    public void saveChanges() throws SOAPException {
        try {
            String contentTypeValue = getSingleHeaderValue(HTTPConstants.HEADER_CONTENT_TYPE);
            ContentType contentType = null;
            if (isEmptyString(contentTypeValue)) {
                if (attachmentParts.size() > 0) {
                    contentTypeValue = HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED;
                } else {
                    contentTypeValue = getBaseType();
                }
            }
            contentType = new ContentType(contentTypeValue);
           
            //Use configures the baseType with multipart/related while no attachment exists or all the attachments are removed
            if(contentType.getBaseType().equals(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED) && attachmentParts.size() == 0) {
                contentType = new ContentType(getBaseType());
            }
          
            //If it is of multipart/related, initialize those required values in the content-type, including boundary etc.
            if (contentType.getBaseType().equals(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED)) {
               
                //Configure boundary
                String boundaryParam = contentType.getParameter("boundary");
                if (isEmptyString(boundaryParam)) {
                    contentType.setParameter("boundary", UIDGenerator.generateMimeBoundary());
                }

                //Configure start content id, always get it from soapPart in case it is changed
                String soapPartContentId = soapPart.getContentId();
                if (isEmptyString(soapPartContentId)) {
                    soapPartContentId = "<" + UIDGenerator.generateContentId() + ">";
                    soapPart.setContentId(soapPartContentId);
                }
                contentType.setParameter("start", soapPartContentId);
               
                //Configure contentId for each attachments
                for(AttachmentPart attachmentPart : attachmentParts) {
                    if(isEmptyString(attachmentPart.getContentId())) {
                        attachmentPart.setContentId("<" + UIDGenerator.generateContentId() + ">");
                    }
                }
               
                //Configure type               
                contentType.setParameter("type", getBaseType());
               
                //Configure charset
                String soapPartContentTypeValue = getSingleHeaderValue(soapPart.getMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE));
                ContentType soapPartContentType = null;
                if (isEmptyString(soapPartContentTypeValue)) {
                    soapPartContentType = new ContentType(soapPartContentTypeValue);
                } else {
                    soapPartContentType = new ContentType(getBaseType());
                }               
                setCharsetParameter(soapPartContentType);
            } else {
                //Configure charset
                setCharsetParameter(contentType);
View Full Code Here

           
            SOAPEnvelope envelope = ((SOAPEnvelopeImpl) soapPart.getEnvelope()).getOMEnvelope();
            if (attachmentParts.isEmpty()) {
                envelope.serialize(out, format);
            } else {
                ContentType contentType = new ContentType(getSingleHeaderValue(HTTPConstants.HEADER_CONTENT_TYPE));
                String boundary = contentType.getParameter("boundary");
                if(isEmptyString(boundary)) {
                    boundary = UIDGenerator.generateMimeBoundary();
                    contentType.setParameter("boundary", boundary);
                }
                format.setMimeBoundary(boundary);

                String rootContentId = soapPart.getContentId();
                if(isEmptyString(rootContentId)) {
                    rootContentId = "<" + UIDGenerator.generateContentId() + ">";
                    soapPart.setContentId(rootContentId);
                }               
                contentType.setParameter("start", rootContentId);
                if ((rootContentId.indexOf("<") > -1) & (rootContentId.indexOf(">") > -1)) {
                    rootContentId = rootContentId.substring(1, (rootContentId.length() - 1));
                }
                format.setRootContentId(rootContentId);

                format.setSOAP11(((SOAPEnvelopeImpl) soapPart.getEnvelope()).getOMFactory() instanceof SOAP11Factory);
               
                //Double save the content-type in case anything is updated
                mimeHeaders.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType.toString());

                OMMultipartWriter mpw = new OMMultipartWriter(out, format);
                OutputStream rootPartOutputStream = mpw.writeRootPart();
                envelope.serialize(rootPartOutputStream);
                rootPartOutputStream.close();
View Full Code Here

        } else {
            this.fileStorageThreshold = 1;
        }
        attachmentsMap = new TreeMap();
        try {
            contentType = new ContentType(contentTypeString);
        } catch (ParseException e) {
            throw new OMException(
                    "Invalid Content Type Field in the Mime Message"
                    , e);
        }
View Full Code Here

        } else {
            this.fileStorageThreshold = 1;
        }
        attachmentsMap = new TreeMap();
        try {
            contentType = new ContentType(contentTypeString);
        } catch (ParseException e) {
            throw new OMException(
                    "Invalid Content Type Field in the Mime Message"
                    , e);
        }
View Full Code Here

TOP

Related Classes of javax.mail.internet.ContentType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.