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

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

Introduction

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

Prototype

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

Source Link

Usage

From source file:interoperabilite.webservice.fluent.FluentResponseHandling.java

public static void main(String[] args) throws Exception {
    Document result = Request.Get("http://somehost/content").execute()
            .handleResponse(new ResponseHandler<Document>() {

                @Override//from  w  w w  .j a  va 2s.c  o  m
                public Document handleResponse(final HttpResponse response) throws IOException {
                    StatusLine statusLine = response.getStatusLine();
                    HttpEntity entity = response.getEntity();
                    if (statusLine.getStatusCode() >= 300) {
                        throw new HttpResponseException(statusLine.getStatusCode(),
                                statusLine.getReasonPhrase());
                    }
                    if (entity == null) {
                        throw new ClientProtocolException("Response contains no content");
                    }
                    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                    try {
                        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                        ContentType contentType = ContentType.getOrDefault(entity);
                        if (!contentType.equals(ContentType.APPLICATION_XML)) {
                            throw new ClientProtocolException("Unexpected content type:" + contentType);
                        }
                        Charset charset = contentType.getCharset();
                        if (charset == null) {
                            charset = Consts.ISO_8859_1;
                        }
                        return docBuilder.parse(entity.getContent(), charset.name());
                    } catch (ParserConfigurationException ex) {
                        throw new IllegalStateException(ex);
                    } catch (SAXException ex) {
                        throw new ClientProtocolException("Malformed XML document", ex);
                    }
                }

            });
    // Do something useful with the result
    System.out.println(result);
}

From source file:io.aos.protocol.http.httpcommon.FluentResponseHandling.java

public static void main(String... args) throws Exception {
    Document result = Request.Get("http://somehost/content").execute()
            .handleResponse(new ResponseHandler<Document>() {

                public Document handleResponse(final HttpResponse response) throws IOException {
                    StatusLine statusLine = response.getStatusLine();
                    HttpEntity entity = response.getEntity();
                    if (statusLine.getStatusCode() >= 300) {
                        throw new HttpResponseException(statusLine.getStatusCode(),
                                statusLine.getReasonPhrase());
                    }//from w  w w  .  j a  va 2 s.  com
                    if (entity == null) {
                        throw new ClientProtocolException("Response contains no content");
                    }
                    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                    try {
                        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                        ContentType contentType = ContentType.getOrDefault(entity);
                        if (!contentType.equals(ContentType.APPLICATION_XML)) {
                            throw new ClientProtocolException("Unexpected content type:" + contentType);
                        }
                        Charset charset = contentType.getCharset();
                        if (charset == null) {
                            charset = HTTP.DEF_CONTENT_CHARSET;
                        }
                        return docBuilder.parse(entity.getContent(), charset.name());
                    } catch (ParserConfigurationException ex) {
                        throw new IllegalStateException(ex);
                    } catch (SAXException ex) {
                        throw new ClientProtocolException("Malformed XML document", ex);
                    }
                }

            });
    // Do something useful with the result
    System.out.println(result);
}

From source file:com.mvn.aircraft.ApiTest.java

@Test
public static void testMimeType(String restURL, String expectedMimeType)
        throws ClientProtocolException, IOException {

    HttpUriRequest request = new HttpGet(restURL);
    HttpResponse httpResponse = (HttpResponse) HttpClientBuilder.create().build().execute(request);

    Assert.assertEquals(expectedMimeType,
            ContentType.getOrDefault((HttpEntity) httpResponse.getStatus()).getMimeType());
}

From source file:com.wudaosoft.net.httpclient.StringResponseHandler.java

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        Charset charset = ContentType.getOrDefault(entity).getCharset();
        if (charset == null) {
            charset = Consts.UTF_8;// w w  w .j a v  a2s  .  com
        }
        return entity != null ? EntityUtils.toString(entity, charset) : "";
    } else {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }
}

From source file:com.wudaosoft.net.httpclient.JsonResponseHandler.java

@Override
public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        Charset charset = ContentType.getOrDefault(entity).getCharset();
        if (charset == null) {
            charset = Consts.UTF_8;/* w ww.java2s . c  o  m*/
        }
        try {
            return entity != null ? JSON.parseObject(EntityUtils.toString(entity, charset)) : new JSONObject();
        } catch (JSONException e) {
            throw new ClientProtocolException("Json format error: " + e.getMessage());
        }
    } else {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }
}

From source file:org.eclipse.emf.ecp.emf2web.json.JSONResponseHandler.java

@Override
public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    final StatusLine statusLine = response.getStatusLine();

    final HttpEntity entity = response.getEntity();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }/*ww  w.  ja v a  2 s.c om*/
    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }

    final Gson gson = new GsonBuilder().create();
    final ContentType contentType = ContentType.getOrDefault(entity);
    final Charset charset = contentType.getCharset();

    final Reader reader = new InputStreamReader(entity.getContent(), charset);

    return gson.fromJson(reader, new TypeToken<T>() {
    }.getType());
}

From source file:com.meplato.store2.ApacheHttpResponse.java

/**
 * Instantiates a new instance of ApacheHttpResponse,
 * then close the response.//from  www.  j  av a 2 s .c o m
 *
 * @param response the HTTP response from the ApacheHttpClient.
 * @throws ServiceException if e.g. serialization of the response fails.
 */
public ApacheHttpResponse(CloseableHttpResponse response) throws ServiceException {
    this.statusCode = response.getStatusLine().getStatusCode();

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        try {
            ContentType contentType = ContentType.getOrDefault(entity);
            Charset charset = contentType.getCharset();
            this.body = EntityUtils.toString(entity, charset);
        } catch (IOException e) {
            EntityUtils.consumeQuietly(entity);
            throw new ServiceException("Error deserializing data", null, e);
        } finally {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
    } else {
        this.body = null;
    }
}

From source file:org.apache.hadoop.gateway.shell.BasicResponse.java

public String getContentType() {
    return ContentType.getOrDefault(response.getEntity()).getMimeType();
}

From source file:com.nanocrawler.data.Page.java

public void load(HttpEntity entity) throws Exception {
    contentType = null;//from   w ww.  java  2s . com
    Header type = entity.getContentType();
    if (type != null) {
        contentType = type.getValue();
    }

    contentEncoding = null;
    Header encoding = entity.getContentEncoding();
    if (encoding != null) {
        contentEncoding = encoding.getValue();
    }

    Charset charset = ContentType.getOrDefault(entity).getCharset();
    if (charset != null) {
        contentCharset = charset.displayName();
    }

    contentData = EntityUtils.toByteArray(entity);
}

From source file:org.apache.hadoop.gateway.shell.BasicResponse.java

public String getContentEncoding() {
    return ContentType.getOrDefault(response.getEntity()).getCharset().name();
}