Example usage for org.apache.http.entity ContentType toString

List of usage examples for org.apache.http.entity ContentType toString

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType toString.

Prototype

public String toString() 

Source Link

Usage

From source file:com.mirth.connect.server.userutil.HTTPUtil.java

/**
 * Serializes an HTTP request body into XML. Multipart requests will also automatically be
 * parsed into separate XML nodes.//from w w w.j av a2 s  . co m
 * 
 * @param httpBody
 *            The request body/payload string to parse.
 * @param contentType
 *            The MIME content type of the request.
 * @return The serialized XML string.
 * @throws MessagingException
 * @throws IOException
 * @throws DonkeyElementException
 * @throws ParserConfigurationException
 */
public static String httpBodyToXml(String httpBody, String contentType)
        throws MessagingException, IOException, DonkeyElementException, ParserConfigurationException {
    ContentType type = getContentType(contentType);
    Object content;

    if (type.getMimeType().startsWith(FileUploadBase.MULTIPART)) {
        content = new MimeMultipart(new ByteArrayDataSource(httpBody, type.toString()));
    } else {
        content = httpBody;
    }

    return HttpMessageConverter.contentToXml(content, type, true, null);
}

From source file:com.mirth.connect.server.userutil.HTTPUtil.java

/**
 * Serializes an HTTP request body into XML. Multipart requests will also automatically be
 * parsed into separate XML nodes./* w  w  w  .  j a  va 2  s .c  o  m*/
 * 
 * @param httpBody
 *            The request body/payload input stream to parse.
 * @param contentType
 *            The MIME content type of the request.
 * @return The serialized XML string.
 * @throws MessagingException
 * @throws IOException
 * @throws DonkeyElementException
 * @throws ParserConfigurationException
 */
public static String httpBodyToXml(InputStream httpBody, String contentType)
        throws MessagingException, IOException, DonkeyElementException, ParserConfigurationException {
    ContentType type = getContentType(contentType);
    Object content;

    if (type.getMimeType().startsWith(FileUploadBase.MULTIPART)) {
        content = new MimeMultipart(new ByteArrayDataSource(httpBody, type.toString()));
    } else {
        content = IOUtils.toString(httpBody,
                HttpMessageConverter.getDefaultHttpCharset(type.getCharset().name()));
    }

    return HttpMessageConverter.contentToXml(content, type, true, null);
}

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  2s  .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);
    }
}

From source file:org.apache.camel.component.olingo2.api.impl.AbstractFutureCallback.java

public static HttpStatusCodes checkStatus(HttpResponse response) throws ODataApplicationException {
    final StatusLine statusLine = response.getStatusLine();
    HttpStatusCodes httpStatusCode = HttpStatusCodes.fromStatusCode(statusLine.getStatusCode());
    if (400 <= httpStatusCode.getStatusCode() && httpStatusCode.getStatusCode() <= 599) {
        if (response.getEntity() != null) {
            try {
                final ContentType responseContentType = ContentType
                        .parse(response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());

                final String mimeType = responseContentType.getMimeType();
                if (ODATA_MIME_TYPE.matcher(mimeType).matches()) {
                    final ODataErrorContext errorContext = EntityProvider.readErrorDocument(
                            response.getEntity().getContent(), responseContentType.toString());
                    throw new ODataApplicationException(errorContext.getMessage(), errorContext.getLocale(),
                            httpStatusCode, errorContext.getErrorCode(), errorContext.getException());
                }/*from  w ww  .ja v  a2 s. c  o  m*/
            } catch (EntityProviderException e) {
                throw new ODataApplicationException(e.getMessage(), response.getLocale(), httpStatusCode, e);
            } catch (IOException e) {
                throw new ODataApplicationException(e.getMessage(), response.getLocale(), httpStatusCode, e);
            }
        }

        throw new ODataApplicationException(statusLine.getReasonPhrase(), response.getLocale(), httpStatusCode);
    }

    return httpStatusCode;
}

From source file:lucee.commons.net.http.httpclient.entity.ResourceHttpEntity.java

public ResourceHttpEntity(final Resource res, final ContentType contentType) {
    super();//from   ww w  .j  a  v a 2  s . c  o  m
    this.res = res;
    if (contentType != null)
        setContentType(contentType.toString());
    ct = contentType;
}

From source file:com.messagemedia.restapi.client.v1.internal.RestRequestBuilder.java

/**
 * Sets the content type for this request
 *
 * @param contentType the content type//from   w  ww .  jav a2  s  .co  m
 * @return this builder
 */
public RestRequestBuilder contentType(ContentType contentType) {
    this.headers.put(HTTP.CONTENT_TYPE, contentType.toString());
    return this;
}

From source file:com.haulmont.cuba.client.sys.fileupload.InputStreamProgressEntity.java

public InputStreamProgressEntity(InputStream content, ContentType contentType,
        @Nullable UploadProgressListener listener) {
    this.listener = listener;
    this.content = content;

    setContentType(contentType.toString());
}

From source file:nl.nn.adapterframework.http.mime.MultipartEntity.java

MultipartEntity(MultipartForm multipart, final ContentType contentType, final long contentLength) {
    super();//  ww w. j  ava 2s  .  c o  m
    this.multipart = multipart;
    this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType.toString());
    this.contentLength = contentLength;
}

From source file:org.fao.geonet.utils.nio.PathHttpEntity.java

public PathHttpEntity(final Path file, final ContentType contentType) {
    super();/*  w  w w . ja v a 2 s.  c  o m*/
    this.file = Args.notNull(file, "File");
    if (contentType != null) {
        setContentType(contentType.toString());
    }
}

From source file:onl.area51.httpd.util.PathEntity.java

@SuppressWarnings("OverridableMethodCallInConstructor")
public PathEntity(final Path file, final ContentType contentType) {
    super();//from   w ww  .  j  a v a2s  .  co  m
    this.file = Args.notNull(file, "Path");
    if (contentType != null) {
        setContentType(contentType.toString());
    }
}