Example usage for javax.activation MimeTypeParameterList MimeTypeParameterList

List of usage examples for javax.activation MimeTypeParameterList MimeTypeParameterList

Introduction

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

Prototype

public MimeTypeParameterList(String parameterList) throws MimeTypeParseException 

Source Link

Document

Constructs a new MimeTypeParameterList with the passed in data.

Usage

From source file:org.mulgara.resolver.http.HttpContent.java

/**
 * Read the mime type. Should only be done if the Mime type is not already available
 * as this will close the connection.//from  w  ww  . j  a v a  2s.  c om
 * @return The MimeType for the URL.
 * @throws NotModifiedException if the content validates against the cache
 */
@SuppressWarnings("unchecked")
private MimeType readMimeType(HttpMethod method) throws NotModifiedException {
    MimeType result = null;
    String contentType = null;

    try {
        // obtain connection and retrieve the headers
        Header header = method.getResponseHeader("Content-Type");
        if (header != null) {
            contentType = header.getValue();
            // find the parameter separator so we can protect against bad params
            int sep = contentType.indexOf(';');
            // no params, just create the MimeType
            if (sep < 0)
                result = new MimeType(contentType);
            else {
                // create the MimeType from the type/subtype
                result = new MimeType(contentType.substring(0, sep));
                // parse parameters separately and set the result accordingly
                try {
                    MimeTypeParameterList params = new MimeTypeParameterList(contentType.substring(sep + 1));
                    Enumeration<String> names = (Enumeration<String>) params.getNames();
                    while (names.hasMoreElements()) {
                        String name = names.nextElement();
                        result.setParameter(name, params.get(name));
                    }
                } catch (MimeTypeParseException e) {
                    logger.warn("Ignoring bad parameters in '" + contentType.substring(sep + 1)
                            + "' from the content type for " + httpUri);
                }
            }
            if (logger.isInfoEnabled()) {
                logger.info("Obtain content type " + result + "  from " + httpUri);
            }
        }
    } catch (java.lang.IllegalStateException e) {
        logger.info("Unable to obtain content type for " + httpUri);
    } catch (MimeTypeParseException e) {
        logger.warn("Unexpected parameters before ; in '" + contentType + "' as a content type for " + httpUri);
    }
    return result;
}