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

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

Introduction

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

Prototype

public static ContentType get(HttpEntity httpEntity) throws ParseException, UnsupportedCharsetException 

Source Link

Usage

From source file:org.ambraproject.wombat.service.remote.ReaderService.java

private static Charset getCharsetOrDefault(HttpEntity entity) {
    Charset charset = null;//from   w  w w  .jav  a 2s . c o m
    ContentType contentType = ContentType.get(Preconditions.checkNotNull(entity));
    if (contentType != null) {
        charset = contentType.getCharset(); // may be null
    }
    if (charset == null) {
        log.warn("Charset not specified in response header; defaulting to {}", DEFAULT_CHARSET.name());
        charset = DEFAULT_CHARSET;
    }
    return charset;
}

From source file:net.javacrumbs.restfire.httpcomponents.HttpComponentsResponse.java

private static Charset getCharset(HttpEntity entity) {
    Charset charset = null;//from   ww w  .j  a va 2 s. c om
    ContentType contentType = ContentType.get(entity);
    if (contentType != null) {
        charset = contentType.getCharset();
    }
    if (charset == null) {
        charset = HTTP.DEF_CONTENT_CHARSET;
    }
    return charset;
}

From source file:ch.cyberduck.core.hubic.HubicAuthenticationResponseHandler.java

@Override
public AuthenticationResponse handleResponse(final HttpResponse response) throws IOException {
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        Charset charset = HTTP.DEF_CONTENT_CHARSET;
        ContentType contentType = ContentType.get(response.getEntity());
        if (contentType != null) {
            if (contentType.getCharset() != null) {
                charset = contentType.getCharset();
            }//w  w  w  . j  a v a2 s.  c  o m
        }
        try {
            final JsonParser parser = new JsonParser();
            final JsonObject json = parser
                    .parse(new InputStreamReader(response.getEntity().getContent(), charset)).getAsJsonObject();
            final String token = json.getAsJsonPrimitive("token").getAsString();
            final String endpoint = json.getAsJsonPrimitive("endpoint").getAsString();
            return new AuthenticationResponse(response, token,
                    Collections.singleton(new Region(null, URI.create(endpoint), null, true)));
        } catch (JsonParseException e) {
            throw new IOException(e.getMessage(), e);
        }
    } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
            || response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN) {
        throw new AuthorizationException(new Response(response));
    }
    throw new GenericException(new Response(response));
}

From source file:org.esigate.HttpErrorPage.java

private static HttpEntity toMemoryEntity(HttpEntity httpEntity) {
    if (httpEntity == null) {
        return null;
    }/* w w  w.jav a  2  s  . co  m*/
    HttpEntity memoryEntity;
    try {
        byte[] content = EntityUtils.toByteArray(httpEntity);
        ByteArrayEntity byteArrayEntity = new ByteArrayEntity(content, ContentType.get(httpEntity));
        Header contentEncoding = httpEntity.getContentEncoding();
        if (contentEncoding != null) {
            byteArrayEntity.setContentEncoding(contentEncoding);
        }
        memoryEntity = byteArrayEntity;
    } catch (IOException e) {
        StringBuilderWriter out = new StringBuilderWriter(Parameters.DEFAULT_BUFFER_SIZE);
        PrintWriter pw = new PrintWriter(out);
        e.printStackTrace(pw);
        pw.close();
        memoryEntity = new StringEntity(out.toString(), ContentType.getOrDefault(httpEntity));
    }
    return memoryEntity;
}

From source file:com.qwazr.utils.http.HttpResponseHandler.java

public ContentType getContentType() {
    if (httpEntity == null)
        return null;
    return ContentType.get(httpEntity);
}

From source file:com.helger.httpclient.response.ResponseHandlerString.java

@Nullable
public String handleResponse(final HttpResponse aHttpResponse) throws IOException {
    // Convert to entity
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        return null;

    ContentType aContentType = ContentType.get(aEntity);
    if (aContentType == null)
        aContentType = m_aDefault;/*from   w  ww  .j  a v  a2s. co  m*/

    // Default to ISO-8859-1 internally
    final Charset aCharset = HttpClientHelper.getCharset(aContentType, m_aDefault.getCharset());

    return EntityUtils.toString(aEntity, aCharset);
}

From source file:ca.uhn.fhir.rest.client.apache.ApacheHttpResponse.java

@Override
public String getMimeType() {
    ContentType ct = ContentType.get(myResponse.getEntity());
    return ct != null ? ct.getMimeType() : null;
}

From source file:com.nominanuda.web.http.ServletHelperTest.java

@Test
public void testCopyRequest() throws Exception {
    final String msg = "mio";
    final String mediaType = CT_APPLICATION_OCTET_STREAM;
    Server server = startServer(10000, new AbstractHandler() {
        public void handle(String arg0, Request jettyReq, HttpServletRequest servletReq,
                HttpServletResponse arg3) throws IOException, ServletException {
            HttpRequest r = servletHelper.copyRequest(servletReq, false);
            asyncAssertEquals("bar", r.getFirstHeader("X-foo").getValue());
            asyncAssertEquals("PUT", r.getRequestLine().getMethod());
            HttpEntity e = ((HttpEntityEnclosingRequest) r).getEntity();
            asyncAssert(msg.getBytes("UTF-8").length == e.getContentLength(), "length");
            asyncAssert(e.getContentType().getValue().startsWith(mediaType));
            asyncAssertEquals(mediaType, ContentType.get(e).getMimeType());
            asyncAssertEquals(msg, EntityUtils.toString(e));
        }//w  w w.  java 2 s  . com
    });
    HttpClient c = buildClient(1);
    HttpPut req = new HttpPut("http://localhost:10000/foo/bar?a=b&a=");
    req.setEntity(new StringEntity(msg, ContentType.create(mediaType, CS_UTF_8)));
    req.addHeader("X-foo", "bar");
    c.execute(req);
    server.stop();
    dumpFailures(System.err);
    Assert.assertFalse(isFailed());
}

From source file:com.qwazr.utils.http.HttpUtils.java

/**
 * Check if the entity has the expected mime type. The charset is not
 * checked./*from  w w w .  j  a  v a  2  s. co  m*/
 *
 * @param response            The response to check
 * @param expectedContentType The expected content type
 * @return the entity from the response body
 * @throws ClientProtocolException if the response does not contains any entity
 */
public static HttpEntity checkIsEntity(HttpResponse response, ContentType expectedContentType)
        throws ClientProtocolException {
    if (response == null)
        throw new ClientProtocolException("No response");
    HttpEntity entity = response.getEntity();
    if (entity == null)
        throw new ClientProtocolException("Response does not contains any content entity");
    if (expectedContentType == null)
        return entity;
    ContentType contentType = ContentType.get(entity);
    if (contentType == null)
        throw new HttpResponseEntityException(response, "Unknown content type");
    if (!expectedContentType.getMimeType().equals(contentType.getMimeType()))
        throw new HttpResponseEntityException(response,
                StringUtils.fastConcat("Wrong content type: ", contentType.getMimeType()));
    return entity;
}