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:de.l3s.boilerpipe.sax.HTMLFetcher.java

public static HTMLDocument fetch(final String url) throws IOException {
    //DefaultHttpClient httpclient = new DefaultHttpClient();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet request = new HttpGet(url.toString());
    request.setHeader("User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36");
    request.setHeader("Referer", "http://www.google.com");

    HttpResponse response = httpclient.execute(request);
    HttpEntity entity = response.getEntity();
    //System.out.println("Response Code: " +
    //response.getStatusLine().getStatusCode());
    ContentType contentType = ContentType.getOrDefault(entity);
    Charset charset = contentType.getCharset();
    if (charset == null) {
        charset = Charset.forName("gb2312");
    }/*w  w  w. ja  v a  2  s .c o m*/

    BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent(), charset));

    StringBuilder builder = new StringBuilder();
    String aux = "";
    Charset cs = Charset.forName("utf8");
    boolean charsetFlag = false;
    while ((aux = rd.readLine()) != null) {
        if (aux != null && !charsetFlag && (aux.contains("http-equiv") || !aux.contains("src"))) {
            Matcher m = PAT_CHARSET_REX.matcher(aux);
            if (m.find()) {
                final String cName = m.group(1);
                charsetFlag = true;
                try {
                    cs = Charset.forName(cName);
                    break;
                } catch (UnsupportedCharsetException e) {
                    // keep default
                }
            }
        }
        //builder.append(aux);
        //System.out.println(builder.toString());
    }

    HttpGet request2 = new HttpGet(url.toString());
    request2.setHeader("User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36");
    request2.setHeader("Referer", "http://www.google.com");

    HttpResponse response2 = httpclient.execute(request2);
    HttpEntity entity2 = response2.getEntity();
    contentType = ContentType.getOrDefault(entity2);
    charset = contentType.getCharset();
    if (charset == null)
        charset = cs;
    //if(charset.name().toLowerCase().equals("gb2312"))
    //   charset = Charset.forName("gbk");
    BufferedReader rd2 = new BufferedReader(new InputStreamReader(entity2.getContent(), charset));
    while ((aux = rd2.readLine()) != null) {
        builder.append(aux);
        //System.out.println(builder.toString());
    }

    String text = builder.toString();
    //System.out.println(text);
    rd.close();
    rd2.close();
    return new HTMLDocument(text, cs); //sometimes cs not equal to charset
}

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

@Override
public XmlObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();

    if (status < 200 || status >= 300) {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }//from  w  w  w . j a  v a2s . c  om

    HttpEntity entity = response.getEntity();

    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }

    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    dbfac.setIgnoringElementContentWhitespace(true);
    dbfac.setCoalescing(true);
    dbfac.setIgnoringComments(true);
    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.UTF_8;
        }
        return XmlObject.fromDocument(docBuilder.parse(entity.getContent(), charset.name()));
    } catch (ParserConfigurationException ex) {
        throw new IllegalStateException(ex);
    } catch (SAXException ex) {
        throw new ClientProtocolException("Malformed XML document", ex);
    }
}

From source file:au.csiro.casda.sodalint.Validator.java

/**
 * Retrieve the content from an address using a GET request.
 *  /*www  .java 2 s.c o  m*/
 * @param address The address to be queried.
 * @return The content, or null if no content could be read.
 * @throws HttpResponseException If a non 200 response code is returned.
 * @throws UnsupportedEncodingException If the content does not have an XML format. 
 * @throws IOException If the content could not be read.
 */
protected String getXmlContentFromUrl(String address)
        throws HttpResponseException, UnsupportedEncodingException, IOException {
    Response response = Request.Get(address).execute();
    HttpResponse httpResponse = response.returnResponse();
    StatusLine statusLine = httpResponse.getStatusLine();
    final int statusCodeOk = 200;
    if (statusLine.getStatusCode() != statusCodeOk) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    HttpEntity entity = httpResponse.getEntity();
    if (entity == null) {
        return null;
    }
    ContentType contentType = ContentType.getOrDefault(entity);
    if (!ContentType.APPLICATION_XML.getMimeType().equals(contentType.getMimeType())
            && !ContentType.TEXT_XML.getMimeType().equals(contentType.getMimeType())) {
        throw new UnsupportedEncodingException(contentType.toString());
    }
    String content = readTextContent(entity);
    if (StringUtils.isBlank(content)) {
        return null;
    }

    return content;
}

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

@Nullable
public IJson handleResponse(final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        throw new ClientProtocolException("Response contains no content");

    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = HttpClientHelper.getCharset(aContentType);

    if (m_bDebugMode) {
        // Read all in String
        final String sJson = StringHelper.trim(EntityUtils.toString(aEntity, aCharset));

        if (LOGGER.isInfoEnabled())
            LOGGER.info("Got JSON: <" + sJson + ">");

        final IJson ret = JsonReader.readFromString(sJson);
        if (ret == null)
            throw new IllegalArgumentException("Failed to parse as JSON: " + sJson);
        return ret;
    }// www .  j a v a  2  s.c  o m

    // Read via reader to avoid duplication in memory
    final Reader aReader = new InputStreamReader(aEntity.getContent(), aCharset);
    return JsonReader.readFromReader(aReader);
}

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

@Nullable
public Document handleResponse(final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        throw new ClientProtocolException("Response contains no content");

    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = HttpClientHelper.getCharset(aContentType);

    if (m_bDebugMode) {
        // Read all in String
        final String sXML = EntityUtils.toString(aEntity, aCharset);

        if (LOGGER.isInfoEnabled())
            LOGGER.info("Got XML: <" + sXML + ">");

        Document ret = null;//from  w  w  w.j  a  v a  2  s.  co  m
        try {
            ret = DOMReader.readXMLDOM(sXML);
        } catch (final SAXException ex) {
            // Ignore
        }
        if (ret == null)
            throw new IllegalArgumentException("Failed to parse as XML: " + sXML);
        return ret;
    }

    // Read via reader to avoid duplication in memory
    final Reader aReader = new InputStreamReader(aEntity.getContent(), aCharset);
    try {
        return DOMReader.readXMLDOM(aReader);
    } catch (final SAXException ex) {
        throw new IllegalArgumentException("Failed to parse as XML", ex);
    }
}

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

@Nullable
public IMicroDocument handleResponse(final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        throw new ClientProtocolException("Response contains no content");

    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = HttpClientHelper.getCharset(aContentType);

    if (m_bDebugMode) {
        // Read all in String
        final String sXML = EntityUtils.toString(aEntity, aCharset);

        if (LOGGER.isInfoEnabled())
            LOGGER.info("Got XML: <" + sXML + ">");

        final IMicroDocument ret = MicroReader.readMicroXML(sXML);
        if (ret == null)
            throw new IllegalArgumentException("Failed to parse as XML: " + sXML);
        return ret;
    }//from   w ww.  j  a  v  a 2 s.  co  m

    // Read via reader to avoid duplication in memory
    final Reader aReader = new InputStreamReader(aEntity.getContent(), aCharset);
    return MicroReader.readMicroXML(aReader);
}

From source file:com.zhuc.nupay.mcm.app.jbei.HttpApi.java

public String post(String url, List<NameValuePair> params) throws HttpApiException {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(url);
    //        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    //        nvps.add(new BasicNameValuePair("username", "admin"));
    //        nvps.add(new BasicNameValuePair("password", "admin"));
    //        // post?

    CloseableHttpResponse response = null;
    try {/*from   w  ww  .j  a  va 2s  .  c o  m*/
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            // ??
            ContentType contentType = ContentType.getOrDefault(entity);
            Charset charset = contentType.getCharset();
            InputStream is = entity.getContent();
            // inputstreamreader???
            BufferedReader br = new BufferedReader(new InputStreamReader(is, charset));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            //System.out.println(":->"+sb.toString());

            return sb.toString();
        } else {
            throw new HttpApiException("Empty Response from server");
        }

    } catch (IOException ex) {
        Logger.getLogger(HttpApi.class.getName()).log(Level.SEVERE, null, ex);

        throw new HttpApiException("Network Error", ex);

    } finally {
        try {
            if (response != null)
                response.close();
        } catch (IOException ex) {
            Logger.getLogger(HttpApi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:org.esigate.HttpErrorPage.java

private static HttpEntity toMemoryEntity(HttpEntity httpEntity) {
    if (httpEntity == null) {
        return null;
    }/*from   www .  jav  a2 s  . c o 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:co.com.soinsoftware.altablero.utils.HttpRequest.java

@Override
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        final ContentType contentType = ContentType.getOrDefault(entity);
        if (contentType.getMimeType().equals(ContentType.APPLICATION_OCTET_STREAM.getMimeType())) {
            InputStream inputStream = null;
            byte[] zipInBytes = EntityUtils.toByteArray(entity);
            if (zipInBytes != null) {
                inputStream = new ByteArrayInputStream(zipInBytes);
            }/*from  w w  w.  ja va  2s .com*/
            return inputStream;
        }
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {
        throw new ClientProtocolException(EXCEPTION_MESSAGE + status);
    }
}

From source file:edu.uci.ics.crawler4j.crawler.Page.java

/** * Loads the content of this page from a fetched * HttpEntity. */
public void load(HttpEntity entity) throws Exception {
    contentType = null;//from   w  w w  .ja va  2 s.  c o  m
    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);
}