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:mitm.common.mail.MimeUtils.java

/**
 * Parses the contentType and returns the Java Charset found in the content type. Null if charset 
 * parameter is not found.//from www . java 2 s .co m
 */
public static String getCharsetFromContentType(String contentType) throws MimeTypeParseException {
    MimeType mimeType = new MimeType(contentType);

    String charset = mimeType.getParameter("charset");

    return StringUtils.isNotEmpty(charset) ? MimeUtility.javaCharset(charset) : null;
}

From source file:org.echocat.jomon.net.http.HttpUtils.java

@Nullable
public static Charset findCharSetOf(@Nullable MimeType mimeType) throws IOException {
    final String charsetName = mimeType != null ? mimeType.getParameter("charset") : null;
    try {/*from w w w.  ja v a  2s .  c om*/
        return charsetName != null ? forName(charsetName) : null;
    } catch (final UnsupportedCharsetException e) {
        throw new IOException("In contentType '" + mimeType + "' was an unsupported charset provided.", e);
    }
}

From source file:com.github.restdriver.serverdriver.http.RequestBody.java

private ContentType createContentType(String contentType) {
    try {/*from  w  ww .  j  a  v a 2 s. c o m*/

        MimeType mimeType = new MimeType(contentType);

        String mediaType = mimeType.getBaseType();
        String charset = defaultString(mimeType.getParameter("charset"), DEFAULT_CONTENT_ENCODING);

        return ContentType.create(mediaType, charset);

    } catch (MimeTypeParseException e) {
        throw new IllegalArgumentException("Invalid content type: " + contentType, e);
    }
}

From source file:org.apache.abdera2.test.client.MultipartRelatedEntityTest.java

License:asdf

@Test
public void testMultimediaRelatedContentType() throws Exception {
    MimeType type = new MimeType("Multipart/Related;boundary=\"35245352345sdfg\"");
    assertTrue(MimeTypeHelper.isMatch("Multipart/Related", type.toString()));
    assertEquals("35245352345sdfg", type.getParameter("boundary"));
}

From source file:com.googlecode.ddom.saaj.SOAPMessageTest.java

@Validated
@Test//  w w w .  ja  va 2 s.  c o  m
public void testAddAttachmentPart() throws Exception {
    SOAPMessage message = getFactory().createMessage();
    AttachmentPart attachment = message.createAttachmentPart();
    message.addAttachmentPart(attachment);
    Iterator it = message.getAttachments();
    assertTrue(it.hasNext());
    assertSame(attachment, it.next());
    assertFalse(it.hasNext());
    // Check that the content type automatically changes to SwA
    if (message.saveRequired()) {
        message.saveChanges();
    }
    String[] contentTypeArray = message.getMimeHeaders().getHeader("Content-Type");
    assertNotNull(contentTypeArray);
    assertEquals(1, contentTypeArray.length);
    MimeType contentType = new MimeType(contentTypeArray[0]);
    assertEquals("multipart/related", contentType.getBaseType());
    assertEquals(messageSet.getVersion().getContentType(), contentType.getParameter("type"));
    assertNotNull(contentType.getParameter("boundary"));
}

From source file:ddf.content.provider.filesystem.FileSystemProvider.java

private String getMimeType(MimeType mimeType) {
    LOGGER.trace("ENTERING: getMimeType");

    String mimeTypeStr = mimeType.getBaseType();
    String mimeTypeIdValue = mimeType.getParameter(ID_PARAMETER);
    if (!StringUtils.isEmpty(mimeTypeIdValue)) {
        mimeTypeStr += ";id=" + mimeTypeIdValue;
    }/*from  w  w  w.  j ava 2s  .c  om*/

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("mimeTypeStr = " + mimeTypeStr);
    }

    LOGGER.trace("EXITING: getMimeType");

    return mimeTypeStr;
}

From source file:ddf.mime.mapper.MimeTypeToTransformerMapperImpl.java

@Override
public <T> List<T> findMatches(Class<T> clazz, MimeType userMimeType) {
    BundleContext bundleContext = getContext();
    ServiceReference[] refs = null;/*w w w .  j  av  a 2  s  . c  om*/
    List<T> list = new ArrayList<T>();

    if (bundleContext == null) {
        LOGGER.debug("Cannot find matches, bundle context is null.");
        return list;
    }
    if (clazz == null) {
        LOGGER.warn("Cannot find matches, service argument is null.");
        throw new IllegalArgumentException("Invalid argument supplied, null service argument");
    }

    /*
     * Extract the services using the bundle context.
     */
    try {
        refs = bundleContext.getServiceReferences(clazz.getName(), null);
    } catch (InvalidSyntaxException e) {
        LOGGER.warn("Invalid filter syntax ", e);
        throw new IllegalArgumentException("Invalid syntax supplied: " + userMimeType.toString());
    }

    // If no InputTransformers found, return empty list
    if (refs == null) {
        LOGGER.debug("No {} services found - return empty list", clazz.getName());
        return list;
    }

    /*
     * Sort the list of service references based in it's Comparable interface.
     */
    Arrays.sort(refs, Collections.reverseOrder());

    /*
     * If the mime type is null return the whole list of service references
     */
    if (userMimeType == null) {
        if (refs.length > 0) {
            for (ServiceReference ref : refs) {
                Object service = (bundleContext.getService(ref));
                T typedService = clazz.cast(service);
                list.add(typedService);
            }
        }
        return list;
    }

    String userIdValue = userMimeType.getParameter(MimeTypeToTransformerMapper.ID_KEY);
    List<T> strictlyMatching = new ArrayList<T>();

    for (ServiceReference ref : refs) {

        List<String> mimeTypesServicePropertyList = getServiceMimeTypesList(ref);

        String serviceId = getServiceId(ref);

        for (String mimeTypeRawEntry : mimeTypesServicePropertyList) {

            MimeType mimeTypeEntry = constructMimeType(mimeTypeRawEntry);

            if (mimeTypeEntry != null
                    && StringUtils.equals(mimeTypeEntry.getBaseType(), userMimeType.getBaseType())
                    && (userIdValue == null || StringUtils.equals(userIdValue, serviceId))) {

                try {
                    Object service = bundleContext.getService(ref);
                    T typedService = clazz.cast(service);
                    strictlyMatching.add(typedService);
                    break; // found exact mimetype, no need to continue within
                    // the same service

                } catch (ClassCastException cce) {
                    LOGGER.debug("Caught illegal cast to transformer type. ", cce);
                }
            }
        }
    }

    return strictlyMatching;
}

From source file:org.apache.abdera.protocol.client.AbstractClientResponse.java

/**
 * Get the character set encoding specified by the server in the Content-Type header
 *//*from  w  ww  . ja  v  a 2  s .com*/
public String getCharacterEncoding() {
    String charset = null;
    try {
        MimeType mt = getContentType();
        if (mt != null)
            charset = mt.getParameter("charset");
    } catch (Exception e) {
    }
    return charset;
}

From source file:org.apache.james.transport.matchers.HasMimeTypeParameter.java

private boolean mimeTypeContainsParameter(MimeType mimeType, String name, String value) {
    return Optional.ofNullable(mimeType.getParameter(name)).map(value::equals).orElse(false);
}

From source file:org.codice.ddf.catalog.ui.metacard.impl.BaseLocator.java

private boolean filterByMimeType(Set<MimeType> serviceMimeTypes, Map<String, Object> serviceProperties,
        MimeType targetMimeType) {

    Set<String> serviceBaseTypes = serviceMimeTypes.stream().map(MimeType::getBaseType)
            .collect(Collectors.toSet());

    String targetServiceId = targetMimeType.getParameter(Constants.SERVICE_ID);

    String targetBaseType = targetMimeType.getBaseType();

    String serviceId = getServiceId(serviceProperties);

    return serviceBaseTypes.contains(targetBaseType)
            && (targetServiceId == null || StringUtils.equals(targetServiceId, serviceId));
}