Example usage for javax.mail.internet ContentType getParameter

List of usage examples for javax.mail.internet ContentType getParameter

Introduction

In this page you can find the example usage for javax.mail.internet ContentType getParameter.

Prototype

public String getParameter(String name) 

Source Link

Document

Return the specified parameter value.

Usage

From source file:rogerthat.topdesk.bizz.Util.java

static String getContent(HTTPResponse response) throws ParseException, UnsupportedEncodingException {
    String contentType = getHeader(response, "Content-Type");
    String characterSet = null;/* w  w  w  .j  av a  2  s  .c o  m*/
    if (contentType != null) {
        ContentType type = new ContentType(contentType);
        characterSet = type.getParameter("charset");
    }
    if (characterSet == null) {
        characterSet = "ASCII";
    }
    log.info("characterset: " + characterSet);
    return new String(response.getContent(), characterSet);
}

From source file:org.nuxeo.drive.operations.NuxeoDriveOperationHelper.java

public static void normalizeMimeTypeAndEncoding(Blob blob) throws ParseException {

    String mimeType = blob.getMimeType();
    if (!StringUtils.isEmpty(mimeType) && !"null".equals(mimeType)) {
        ContentType contentType = new ContentType(mimeType);
        blob.setMimeType(contentType.getBaseType());
        if (StringUtils.isEmpty(blob.getEncoding())) {
            String charset = contentType.getParameter("charset");
            if (!StringUtils.isEmpty(charset)) {
                blob.setEncoding(charset);
            }//from   w  ww  . ja v a2s .c o m
        }
    }
}

From source file:org.silverpeas.util.mail.EMLExtractor.java

private static String getFileName(Part part) throws MessagingException {
    String fileName = part.getFileName();
    if (!StringUtil.isDefined(fileName)) {
        try {/*from   w ww  .  ja v  a  2 s  . c om*/
            ContentType type = new ContentType(part.getContentType());
            fileName = type.getParameter("name");
        } catch (ParseException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    }
    if (StringUtil.isDefined(fileName) && fileName.startsWith("=?") && fileName.endsWith("?=")) {
        try {
            fileName = MimeUtility.decodeText(part.getFileName());
        } catch (UnsupportedEncodingException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    }
    return fileName;
}

From source file:org.apache.axis.attachments.MimeUtils.java

/**
 * Determine as efficiently as possible the content length for attachments in a mail Multipart.
 * @param mp is the multipart to be serarched.
 * @return the actual length.//from   ww  w .  ja  va  2 s. c  o  m
 *
 * @throws javax.mail.MessagingException
 * @throws java.io.IOException
 */
public static long getContentLength(javax.mail.Multipart mp)
        throws javax.mail.MessagingException, java.io.IOException {

    int totalParts = mp.getCount();
    long totalContentLength = 0;

    for (int i = 0; i < totalParts; ++i) {
        javax.mail.internet.MimeBodyPart bp = (javax.mail.internet.MimeBodyPart) mp.getBodyPart(i);

        totalContentLength += getContentLength(bp);
    }

    String ctype = mp.getContentType();
    javax.mail.internet.ContentType ct = new javax.mail.internet.ContentType(ctype);
    String boundaryStr = ct.getParameter("boundary");
    int boundaryStrLen = boundaryStr.length() + 4; // must add two for -- prefix and another two for crlf

    // there is one more boundary than parts
    // each parts data must have crlf after it.
    // last boundary has an additional --crlf
    return totalContentLength + boundaryStrLen * (totalParts + 1) + 2 * totalParts + +4;
}

From source file:crawl.SphinxWrapper.java

private static Document makeDocument(Page page) {
    String url = page.toURL();//from  w  w w .j  av  a 2s. c o  m
    FeatureMap params = Factory.newFeatureMap();

    Document doc = null;

    String docName = shortenUrl(url).replaceAll("[^\\p{ASCII}]", "_") + "_" + Gate.genSym();

    /* Take advantage of the MIME type from the server when
     * constructing the GATE document.      */
    String contentTypeStr = page.getContentType();
    String originalMimeType = null;

    if (contentTypeStr != null) {
        try {
            ContentType contentType = new ContentType(contentTypeStr);
            String mimeType = contentType.getBaseType();
            String encoding = contentType.getParameter("charset");

            // get the content as bytes, and convert it to string using the correct
            // encoding (thanks to Christian Wartena for patch)
            byte[] bContent = page.getContentBytes();
            String sContent = new String(bContent, Charset.forName(encoding));
            params.put(Document.DOCUMENT_STRING_CONTENT_PARAMETER_NAME, sContent);

            if (mimeType != null) {
                if (convertXmlTypes) {
                    originalMimeType = mimeType;
                    mimeType = convertMimeType(mimeType);
                    if (!originalMimeType.equals(mimeType)) {
                        System.out.println("   convert " + originalMimeType + " -> " + mimeType);
                    }
                }
                params.put(Document.DOCUMENT_MIME_TYPE_PARAMETER_NAME, mimeType);
            }

            if (encoding != null) {
                params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, encoding);

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    try {
        doc = (Document) Factory.createResource(DocumentImpl.class.getName(), params, null, docName);
        FeatureMap docFeatures = doc.getFeatures();

        Integer originalLength = page.getLength();
        docFeatures.put("originalLength", originalLength);

        /* Use the Last-Modified HTTP header if available.  */
        long lastModified = page.getLastModified();
        Date date;
        if (lastModified > 0L) {
            date = new Date(lastModified);
        } else {
            date = new Date();
        }
        docFeatures.put("Date", date);

        if (originalMimeType != null) {
            docFeatures.put("originalMimeType", originalMimeType);
        }

        doc.setSourceUrl(page.getURL());
        docFeatures.put("gate.SourceURL", url);
    } catch (ResourceInstantiationException e) {
        System.err.println("WARNING: could not intantiate document " + docName);
        e.printStackTrace();
    }

    return doc;
}

From source file:org.esxx.Parsers.java

public static String getParameter(ContentType ct, String name, String def) {
    String value = ct.getParameter(name);
    return value == null ? def : value;
}

From source file:com.cubusmail.mail.text.MessageTextUtil.java

/**
 * Reads the string out of part's input stream. On first try the input
 * stream retrieved by <code>javax.mail.Part.getInputStream()</code> is
 * used. If an I/O error occurs (<code>java.io.IOException</code>) then the
 * next try is with part's raw input stream. If everything fails an empty
 * string is returned.//from ww w .  j ava  2 s . co  m
 * 
 * @param p
 *            - the <code>javax.mail.Part</code> object
 * @param ct
 *            - the part's content type
 * @return the string read from part's input stream or the empty string ""
 *         if everything failed
 * @throws MessagingException
 *             - if an error occurs in part's getter methods
 */
public static String readPart(final Part p) throws MessagingException {

    String contentType = p.getContentType();
    ContentType type = new ContentType(contentType);

    /*
     * Use specified charset if available else use default one
     */
    String charset = type.getParameter("charset");
    if (null == charset || charset.equalsIgnoreCase(CubusConstants.US_ASCII)) {
        charset = CubusConstants.DEFAULT_CHARSET;
    }
    try {
        return readStream(p.getInputStream(), charset);
    } catch (final IOException e) {
        /*
         * Try to get data from raw input stream
         */
        final InputStream inStream;
        if (p instanceof MimeBodyPart) {
            final MimeBodyPart mpb = (MimeBodyPart) p;
            inStream = mpb.getRawInputStream();
        } else if (p instanceof MimeMessage) {
            final MimeMessage mm = (MimeMessage) p;
            inStream = mm.getRawInputStream();
        } else {
            inStream = null;
        }
        if (inStream == null) {
            /*
             * Neither a MimeBodyPart nor a MimeMessage
             */
            return "";
        }
        try {
            return readStream(inStream, charset);
        } catch (final IOException e1) {
            log.error(e1.getLocalizedMessage(), e1);
            return e1.getLocalizedMessage();
            // return STR_EMPTY;
        } finally {
            try {
                inStream.close();
            } catch (final IOException e1) {
                log.error(e1.getLocalizedMessage(), e1);
            }
        }
    }
}

From source file:com.adaptris.core.http.ContentTypeProviderImpl.java

private boolean hasCharset(String mimeType) {
    boolean result = false;
    try {/*  w  w  w  .j  av a2s. c o  m*/
        ContentType ct = new ContentType(mimeType);
        String charset = ct.getParameter("charset");
        result = !isBlank(charset);
    } catch (ParseException e) {
        // couldn't parse, hasn't got a charset.
        result = false;
    }
    return result;
}

From source file:org.apache.axis2.format.TextMessageBuilderAdapter.java

public OMElement processDocument(Reader reader, String contentType, MessageContext messageContext)
        throws AxisFault {
    String charset;/*from   w  ww  .j a  v a2s .c  om*/
    try {
        ContentType ct = new ContentType(contentType);
        charset = ct.getParameter("charset");
    } catch (ParseException ex) {
        charset = null;
    }
    if (charset == null) {
        charset = MessageContext.DEFAULT_CHAR_SET_ENCODING;
    }
    messageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charset);
    return processDocument(new ReaderInputStream(reader, charset), contentType, messageContext);
}

From source file:com.google.caja.plugin.DataUriFetcher.java

private String charsetFromMime(String mime) {
    String charset;/* w w  w . j  a va  2  s  .  co  m*/
    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;
    } else {
        return charset;
    }
}