Example usage for javax.activation MimeType getParameter

List of usage examples for javax.activation MimeType getParameter

Introduction

In this page you can find the example usage for javax.activation MimeType getParameter.

Prototype

public String getParameter(String name) 

Source Link

Document

Retrieve the value associated with the given name, or null if there is no current association.

Usage

From source file:org.mule.transformer.types.SimpleDataType.java

public SimpleDataType(Class<?> type, String mimeType) {
    this.type = type;
    if (mimeType == null) {
        this.mimeType = ANY_MIME_TYPE;
    } else {/* w  ww  .j av  a2s  . c  om*/
        try {
            MimeType mt = new MimeType(mimeType);
            this.mimeType = mt.getPrimaryType() + "/" + mt.getSubType();
            if (mt.getParameter("charset") != null) {
                encoding = mt.getParameter("charset");
            }
        } catch (MimeTypeParseException e) {
            //TODO, this should really get thrown
            throw new MuleRuntimeException(e);
        }
    }
}

From source file:org.xcmis.restatom.collections.CmisObjectCollection.java

/**
 * Get content stream from entry./*from   www.j  a v  a  2 s .  c  om*/
 *
 * @param entry source entry
 * @param request request context
 * @return content stream as <code>ContentStream</code> or null if there is
 *         no 'content' in entry
 * @throws IOException if any i/o error occurs
 * @throws ResponseContextException other errors
 */
protected ContentStream getContentStream(Entry entry, RequestContext request)
        throws IOException, ResponseContextException {
    ContentStream contentStream = null;
    ContentTypeElement cmisContent = entry.getExtension(AtomCMIS.CONTENT);
    if (cmisContent != null) {
        CmisContentType content = cmisContent.getContent();

        InputStream is = content.getBase64();
        contentStream = new BaseContentStream(is, is.available(), null,
                org.xcmis.spi.utils.MimeType.fromString(content.getMediatype()));
    } else {
        Content content = entry.getContentElement();
        if (content != null) {
            final IRI src = content.getSrc();
            if (src != null) {
                if (src.equals(new IRI(getContentLink(getId(request), request)))) {
                    // If 'src' attribute provides URI is the same to current
                    // object (document). This may happen when client does 'check-in'
                    // or 'check-out' operation.
                } else {
                    URL url = null;
                    try {
                        url = src.toURL();
                    } catch (URISyntaxException e) {
                        String msg = "Invalid src attribute: " + src;
                        throw new ResponseContextException(createErrorResponse(msg, 400));
                    }
                    // HTTP only
                    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
                    httpConnection.setRequestMethod("GET");
                    httpConnection.setDoOutput(false);
                    httpConnection.setDoInput(true);
                    int status = httpConnection.getResponseCode();
                    if (200 == status) {
                        contentStream = new BaseContentStream(new HttpConnectionStream(httpConnection), null,
                                org.xcmis.spi.utils.MimeType
                                        .fromString(httpConnection.getHeaderField("Content-Type")));
                    } else {
                        httpConnection.disconnect();
                        String msg = "Unable get content from URI : " + src.toString() + ". Response status is "
                                + status;
                        throw new ResponseContextException(createErrorResponse(msg, 500));
                    }
                }
            } else {
                Type contentType = content.getContentType();
                org.xcmis.spi.utils.MimeType mediaType;
                if (contentType == Type.XML || contentType == Type.XHTML || contentType == Type.HTML
                        || contentType == Type.TEXT) {
                    switch (contentType) {
                    case XHTML:
                        mediaType = new org.xcmis.spi.utils.MimeType("application", "xhtml+xml");
                        break;
                    case HTML:
                        mediaType = new org.xcmis.spi.utils.MimeType("text", "html");
                        break;
                    case TEXT:
                        mediaType = new org.xcmis.spi.utils.MimeType("text", "plain");
                        break;
                    case XML:
                        mediaType = convertMimeType(content.getMimeType());
                        break;
                    default:
                        // Must never happen.
                        mediaType = new org.xcmis.spi.utils.MimeType();
                    }

                    byte[] data;
                    // XXX CMISSpaces sends XML content as Base64 encoded but
                    // Abdera waits for plain text.
                    // Done just for research work. Find good solution to fix this.
                    if (SPACES_AIR_SPECIFIC_REFERER.equalsIgnoreCase(request.getHeader("referer"))) {
                        data = Base64.decodeBase64(content.getText().getBytes());
                    } else {
                        String charset = mediaType.getParameter(CmisConstants.CHARSET);
                        if (charset == null) {
                            // workaround
                            mediaType.getParameters().put(CmisConstants.CHARSET, "UTF-8");
                        }
                        data = content.getValue().getBytes(charset == null ? "UTF-8" : charset);

                    }

                    contentStream = new BaseContentStream(data, null, mediaType);
                } else {
                    contentStream = new BaseContentStream(content.getDataHandler().getInputStream(), null,
                            convertMimeType(content.getMimeType()));
                }
            }
        }
    }
    return contentStream;
}