Example usage for javax.mail.internet ContentType getBaseType

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

Introduction

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

Prototype

public String getBaseType() 

Source Link

Document

Return the MIME type string, without the parameters.

Usage

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);
            }// ww  w .  ja v a  2  s  .  c o m
        }
    }
}

From source file:org.trancecode.xproc.step.UnEscapeMarkupStepProcessor.java

private static ContentType getContentType(final String content_type, final Step inputStep) {
    try {//w w w  .java 2  s.c o  m
        final ContentType ct = new ContentType(content_type);
        if (!Steps.SUPPORTED_CONTENTTYPE.contains(ct.getBaseType())) {
            throw XProcExceptions.xc0051(inputStep.getLocation());
        }
        return ct;
    } catch (final ParseException e) {
        throw XProcExceptions.xc0051(inputStep.getLocation());
    }
}

From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RFC822ToTextConverter.java

protected static byte[] extractTextFromMessagePart(Part p) throws Exception {
    ContentType contentType = new ContentType(p.getContentType());
    String baseType = contentType.getBaseType();
    if (TXT_MT.equals(baseType)) {
        Object content = p.getContent();
        if (content instanceof String) {
            return ((String) content).getBytes();
        } else {/*from  w ww .  j  ava2 s .c om*/
            return null;
        }
    }
    ConversionService cs = Framework.getLocalService(ConversionService.class);

    String converterName = cs.getConverterName(baseType, TXT_MT);
    if (converterName == null) {
        return null;
    } else {
        BlobHolder result = cs.convert(converterName, new SimpleBlobHolder(new FileBlob(p.getInputStream())),
                null);
        return result.getBlob().getByteArray();
    }
}

From source file:org.silverpeas.components.mailinglist.service.job.MailProcessor.java

protected static String extractContentType(String contentType) {
    try {// w ww .j  a  v a 2  s .  co m
        ContentType type = new ContentType(contentType);
        return type.getBaseType();
    } catch (ParseException e) {
        SilverTrace.error("mailinglist", "MailProcessor.extractContentType()", "mailinglist.notification.error",
                e);
    }
    return contentType;
}

From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java

protected static String extractContentType(String contentType) {
    try {/*from   w ww. j  a va2  s .c om*/
        ContentType type = new ContentType(contentType);
        return type.getBaseType();
    } catch (ParseException e) {
        SilverTrace.error("mailingList", "MailProcessor.extractContentType()", "mailinglist.notification.error",
                e);
    }
    return contentType;
}

From source file:com.ctriposs.r2.message.rest.QueryTunnelUtil.java

/**
 * Helper function to create multi-part MIME
 *
 * @param entity         the body of a request
 * @param entityContentType content type of the body
 * @param query          a query part of a request
 *
 * @return a ByteString that represents a multi-part encoded entity that contains both
 *//*w w w  .  j  a v a  2s .com*/
private static MimeMultipart createMultiPartEntity(ByteString entity, String entityContentType, String query)
        throws MessagingException {
    MimeMultipart multi = new MimeMultipart(MIXED);

    // Create current entity with the associated type
    MimeBodyPart dataPart = new MimeBodyPart();

    ContentType contentType = new ContentType(entityContentType);
    dataPart.setContent(entity.copyBytes(), contentType.getBaseType());
    dataPart.setHeader(HEADER_CONTENT_TYPE, entityContentType);

    // Encode query params as form-urlencoded
    MimeBodyPart argPart = new MimeBodyPart();
    argPart.setContent(query, FORM_URL_ENCODED);
    argPart.setHeader(HEADER_CONTENT_TYPE, FORM_URL_ENCODED);

    multi.addBodyPart(argPart);
    multi.addBodyPart(dataPart);
    return multi;
}

From source file:org.trancecode.xproc.step.UnEscapeMarkupStepProcessor.java

private static String getUnEscapeContent(final String content, final String encoding,
        final ContentType contentType, final String charset) throws IOException {
    if (Steps.ENCODING_BASE64.equals(encoding)) {
        return Steps.getBase64Content(content, charset);
    } else if (MediaTypes.MEDIA_XML.equals(contentType.getBaseType())) {
        return StringEscapeUtils.unescapeHtml(content);
    } else if (MediaTypes.MEDIA_TYPE_HTML.equals(contentType.getBaseType())) {
        return StringEscapeUtils.unescapeXml(content);
    }/*from  ww  w.j  a  v  a2 s  .  com*/
    return "";
}

From source file:com.linkedin.r2.message.rest.QueryTunnelUtil.java

/**
 * Helper function to create multi-part MIME
 *
 * @param entity         the body of a request
 * @param entityContentType content type of the body
 * @param query          a query part of a request
 *
 * @return a ByteString that represents a multi-part encoded entity that contains both
 */// ww w  .j  a  v  a  2  s .  co  m
private static MimeMultipart createMultiPartEntity(final ByteString entity, final String entityContentType,
        String query) throws MessagingException {
    MimeMultipart multi = new MimeMultipart(MIXED);

    // Create current entity with the associated type
    MimeBodyPart dataPart = new MimeBodyPart();

    ContentType contentType = new ContentType(entityContentType);
    if (MULTIPART.equals(contentType.getBaseType())) {
        MimeMultipart nested = new MimeMultipart(new DataSource() {
            @Override
            public InputStream getInputStream() throws IOException {
                return entity.asInputStream();
            }

            @Override
            public OutputStream getOutputStream() throws IOException {
                return null;
            }

            @Override
            public String getContentType() {
                return entityContentType;
            }

            @Override
            public String getName() {
                return null;
            }
        });
        dataPart.setContent(nested, contentType.getBaseType());

    } else {
        dataPart.setContent(entity.copyBytes(), contentType.getBaseType());
    }
    dataPart.setHeader(HEADER_CONTENT_TYPE, entityContentType);

    // Encode query params as form-urlencoded
    MimeBodyPart argPart = new MimeBodyPart();
    argPart.setContent(query, FORM_URL_ENCODED);
    argPart.setHeader(HEADER_CONTENT_TYPE, FORM_URL_ENCODED);

    multi.addBodyPart(argPart);
    multi.addBodyPart(dataPart);
    return multi;
}

From source file:crawl.SphinxWrapper.java

private static Document makeDocument(Page page) {
    String url = page.toURL();/*from w  ww .j  a va 2  s. c  om*/
    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.nuxeo.cm.mail.actionpipe.ExtractMessageInformation.java

@SuppressWarnings("unchecked")
protected static void getAttachmentParts(Part p, String defaultFilename, MimetypeRegistry mimeService,
        ExecutionContext context) throws MessagingException, IOException {
    String filename = getFilename(p, defaultFilename);
    List<Blob> blobs = (List<Blob>) context.get(ATTACHMENTS_KEY);

    if (!p.isMimeType("multipart/*")) {
        String disp = p.getDisposition();
        // no disposition => consider it may be body
        if (disp == null && !context.containsKey(BODY_KEY)) {
            // this will need to be parsed
            context.put(BODY_KEY, p.getContent());
        } else if (disp != null
                && (disp.equalsIgnoreCase(Part.ATTACHMENT) || (disp.equalsIgnoreCase(Part.INLINE)
                        && !(p.isMimeType("text/*") || p.isMimeType("image/*"))))) {
            log.debug("Saving attachment to file " + filename);
            Blob blob = Blobs.createBlob(p.getInputStream());
            String mime = DEFAULT_BINARY_MIMETYPE;
            try {
                if (mimeService != null) {
                    ContentType contentType = new ContentType(p.getContentType());
                    mime = mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob,
                            contentType.getBaseType());
                }// ww  w.  j  av  a 2s . c  o  m
            } catch (MimetypeDetectionException e) {
                log.error(e);
            }
            blob.setMimeType(mime);

            blob.setFilename(filename);

            blobs.add(blob);

            // for debug
            // File f = new File(filename);
            // ((MimeBodyPart) p).saveFile(f);

            log.debug("---------------------------");

        } else {
            log.debug(String.format("Ignoring part with type %s", p.getContentType()));
        }
    }

    if (p.isMimeType("multipart/*")) {
        log.debug("This is a Multipart");
        log.debug("---------------------------");
        Multipart mp = (Multipart) p.getContent();

        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
            getAttachmentParts(mp.getBodyPart(i), defaultFilename, mimeService, context);
        }
    } else if (p.isMimeType(MESSAGE_RFC822_MIMETYPE)) {
        log.debug("This is a Nested Message");
        log.debug("---------------------------");
        getAttachmentParts((Part) p.getContent(), defaultFilename, mimeService, context);
    }

}