Example usage for org.apache.http.message BasicHeaderValueFormatter INSTANCE

List of usage examples for org.apache.http.message BasicHeaderValueFormatter INSTANCE

Introduction

In this page you can find the example usage for org.apache.http.message BasicHeaderValueFormatter INSTANCE.

Prototype

BasicHeaderValueFormatter INSTANCE

To view the source code for org.apache.http.message BasicHeaderValueFormatter INSTANCE.

Click Source Link

Usage

From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java

/**
 * This method takes in a ContentType and returns an equivalent ContentType, only overriding the
 * charset. This is needed because ContentType.withCharset(charset) does not correctly handle
 * headers with multiple parameters. Parsing a ContentType from a String works, calling
 * toString() to get the correct header value works, but there's no way from ContentType itself
 * to update a specific parameter in-place.
 *///from  w  w w .j a  v a 2 s . c o m
public static ContentType setCharset(ContentType contentType, Charset charset)
        throws ParseException, UnsupportedCharsetException {
    // Get the correct header value
    String contentTypeString = contentType.toString();

    // Parse the header manually the same way ContentType does it
    CharArrayBuffer buffer = new CharArrayBuffer(contentTypeString.length());
    buffer.append(contentTypeString);
    ParserCursor cursor = new ParserCursor(0, contentTypeString.length());
    HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(buffer, cursor);

    if (ArrayUtils.isNotEmpty(elements)) {
        String mimeType = elements[0].getName();
        NameValuePair[] params = elements[0].getParameters();
        List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
        boolean charsetFound = false;

        // Iterate through each parameter and override the charset if present
        if (ArrayUtils.isNotEmpty(params)) {
            for (NameValuePair nvp : params) {
                if (nvp.getName().equalsIgnoreCase("charset")) {
                    charsetFound = true;
                    nvp = new BasicNameValuePair(nvp.getName(), charset.name());
                }
                paramsList.add(nvp);
            }
        }

        // Add the charset at the end if it wasn't found before
        if (!charsetFound) {
            paramsList.add(new BasicNameValuePair("charset", charset.name()));
        }

        // Format the header the same way ContentType does it
        CharArrayBuffer newBuffer = new CharArrayBuffer(64);
        newBuffer.append(mimeType);
        newBuffer.append("; ");
        BasicHeaderValueFormatter.INSTANCE.formatParameters(newBuffer,
                paramsList.toArray(new NameValuePair[paramsList.size()]), false);
        // Once we have the correct string, let ContentType do the rest
        return ContentType.parse(newBuffer.toString());
    } else {
        throw new ParseException("Invalid content type: " + contentTypeString);
    }
}