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

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

Introduction

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

Prototype

public String getMimeType() 

Source Link

Usage

From source file:ste.web.http.velocity.BugFreeVelocityHandler.java

private String mime(final ContentType type) {
    return type.getMimeType();
}

From source file:io.cloudslang.content.httpclient.build.ContentTypeBuilderTest.java

@Test
public void buildContentTypeWithRequestCharacterSet() {
    ContentType contentType = new ContentTypeBuilder().setContentType("application/json; charset=UTF-8")
            .setRequestCharacterSet(Consts.ISO_8859_1.name()).buildContentType();

    assertEquals(APPLICATION_JSON_CONTENT_TYPE, contentType.getMimeType().toString());
    assertEquals(Consts.ISO_8859_1.name(), contentType.getCharset().name());
}

From source file:fr.ippon.wip.http.hc.TransformerResponseInterceptor.java

/**
 * If httpResponse must be transformed, creates an instance of
 * WIPTransformer, executes WIPTransformer#transform on the response content
 * and updates the response entity accordingly.
 * //from   w w w  .  j  av a2s . c  o m
 * @param httpResponse
 * @param context
 * @throws HttpException
 * @throws IOException
 */
public void process(HttpResponse httpResponse, HttpContext context) throws HttpException, IOException {
    PortletRequest portletRequest = HttpClientResourceManager.getInstance().getCurrentPortletRequest();
    PortletResponse portletResponse = HttpClientResourceManager.getInstance().getCurrentPortletResponse();
    WIPConfiguration config = WIPUtil.getConfiguration(portletRequest);
    RequestBuilder request = HttpClientResourceManager.getInstance().getCurrentRequest();

    if (httpResponse == null) {
        // No response -> no transformation
        LOG.warning("No response to transform.");
        return;
    }

    HttpEntity entity = httpResponse.getEntity();
    if (entity == null) {
        // No entity -> no transformation
        return;
    }

    ContentType contentType = ContentType.getOrDefault(entity);
    String mimeType = contentType.getMimeType();

    String actualURL;
    RedirectLocations redirectLocations = (RedirectLocations) context
            .getAttribute("http.protocol.redirect-locations");
    if (redirectLocations != null)
        actualURL = Iterables.getLast(redirectLocations.getAll()).toString();
    else if (context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS) == CacheResponseStatus.CACHE_HIT) {
        actualURL = request.getRequestedURL();
    } else {
        HttpRequest actualRequest = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost actualHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        actualURL = actualHost.toURI() + actualRequest.getRequestLine().getUri();
    }

    // Check if actual URI must be transformed
    if (!config.isProxyURI(actualURL))
        return;

    // a builder for creating a WIPTransformer instance
    TransformerBuilder transformerBuilder = new TransformerBuilder().setActualURL(actualURL)
            .setMimeType(mimeType).setPortletRequest(portletRequest).setPortletResponse(portletResponse)
            .setResourceType(request.getResourceType()).setXmlReaderPool(xmlReaderPool);

    // Creates an instance of Transformer depending on ResourceType and
    // MimeType
    int status = transformerBuilder.build();
    if (status == TransformerBuilder.STATUS_NO_TRANSFORMATION)
        return;

    WIPTransformer transformer = transformerBuilder.getTransformer();
    // Call WIPTransformer#transform method and update the response Entity
    // object
    try {
        String content = EntityUtils.toString(entity);
        String transformedContent = ((AbstractTransformer) transformer).transform(content);

        StringEntity transformedEntity;
        if (contentType.getCharset() != null) {
            transformedEntity = new StringEntity(transformedContent, contentType);
        } else {
            transformedEntity = new StringEntity(transformedContent);
        }
        transformedEntity.setContentType(contentType.toString());
        httpResponse.setEntity(transformedEntity);

    } catch (SAXException e) {
        LOG.log(Level.SEVERE, "Could not transform HTML", e);
        throw new IllegalArgumentException(e);
    } catch (TransformerException e) {
        LOG.log(Level.SEVERE, "Could not transform HTML", e);
        throw new IllegalArgumentException(e);
    }
}

From source file:net.ychron.unirestinst.request.body.MultipartBody.java

public MultipartBody field(String name, byte[] bytes, ContentType contentType, String fileName) {
    return field(name, new ByteArrayBody(bytes, contentType, fileName), true, contentType.getMimeType());
}

From source file:net.ychron.unirestinst.request.body.MultipartBody.java

public MultipartBody field(String name, InputStream stream, ContentType contentType, String fileName) {
    return field(name, new InputStreamBody(stream, contentType, fileName), true, contentType.getMimeType());
}

From source file:org.robertburrelldonkin.template4couchdb.rest.HttpClientRestClientTest.java

@Test
public void testPostMediaType() throws Exception {
    ArgumentCaptor<HttpPost> argument = ArgumentCaptor.forClass(HttpPost.class);
    this.subject.post(URL, documentMarshaller, document, responseUnmarshaller);
    verify(client).execute(argument.capture(), eq(handler));
    final ContentType contentType = ContentType.get(argument.getValue().getEntity());
    assertThat(contentType.getMimeType(), is("application/json"));
    assertThat(contentType.getCharset(), is(Consts.UTF_8));
}

From source file:org.springframework.boot.cli.command.init.InitializrService.java

/**
 * Loads the service capabilities of the service at the specified URL. If the service
 * supports generating a textual representation of the capabilities, it is returned,
 * otherwise {@link InitializrServiceMetadata} is returned.
 * @param serviceUrl to url of the initializer service
 * @return the service capabilities (as a String) or the
 * {@link InitializrServiceMetadata} describing the service
 * @throws IOException if the service capabilities cannot be loaded
 *//*from w w w .j  a v a  2s  .co  m*/
public Object loadServiceCapabilities(String serviceUrl) throws IOException {
    HttpGet request = new HttpGet(serviceUrl);
    request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, ACCEPT_SERVICE_CAPABILITIES));
    CloseableHttpResponse httpResponse = execute(request, serviceUrl, "retrieve help");
    validateResponse(httpResponse, serviceUrl);
    HttpEntity httpEntity = httpResponse.getEntity();
    ContentType contentType = ContentType.getOrDefault(httpEntity);
    if (contentType.getMimeType().equals("text/plain")) {
        return getContent(httpEntity);
    }
    return parseJsonMetadata(httpEntity);
}

From source file:com.joyent.manta.http.ContentTypeLookupTest.java

public void canFindDefault() {
    MantaHttpHeaders headers = new MantaHttpHeaders(EXAMPLE_HEADERS);
    ContentType troff = ContentType.create("application/x-troff");
    ContentType jsonStream = ContentType.create("application/x-json-stream");

    Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(null, troff).getMimeType(),
            troff.getMimeType());
    Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, troff).getMimeType(),
            troff.getMimeType());//from   w ww.  ja v a 2  s .  c  o m
    headers.put("Content-Type", "application/x-json-stream; type=directory");
    Assert.assertEquals(ContentTypeLookup.findOrDefaultContentType(headers, troff).getMimeType(),
            jsonStream.getMimeType());
}

From source file:org.apache.hadoop.gateway.dispatch.DefaultDispatch.java

private String getInboundResponseContentType(final HttpEntity entity) {
    String fullContentType = null;
    if (entity != null) {
        ContentType entityContentType = ContentType.get(entity);
        if (entityContentType != null) {
            if (entityContentType.getCharset() == null) {
                final String entityMimeType = entityContentType.getMimeType();
                final String defaultCharset = MimeTypes.getDefaultCharsetForMimeType(entityMimeType);
                if (defaultCharset != null) {
                    LOG.usingDefaultCharsetForEntity(entityMimeType, defaultCharset);
                    entityContentType = entityContentType.withCharset(defaultCharset);
                }/*  ww  w  . j  a  v a2  s .co m*/
            } else {
                LOG.usingExplicitCharsetForEntity(entityContentType.getMimeType(),
                        entityContentType.getCharset());
            }
            fullContentType = entityContentType.toString();
        }
    }
    if (fullContentType == null) {
        LOG.unknownResponseEntityContentType();
    } else {
        LOG.inboundResponseEntityContentType(fullContentType);
    }
    return fullContentType;
}

From source file:org.esigate.servlet.impl.ResponseCapturingWrapper.java

@Override
public void setContentType(String type) {
    ContentType parsedContentType = ContentType.parse(type);
    this.contentType = parsedContentType.getMimeType();
    if (parsedContentType.getCharset() != null) {
        this.characterEncoding = parsedContentType.getCharset().name();
    }/*from   w w  w.  j a  va2s  .c  o m*/
    updateContentTypeHeader();
}