List of usage examples for org.apache.http.entity ContentType getOrDefault
public static ContentType getOrDefault(HttpEntity httpEntity) throws ParseException, UnsupportedCharsetException
From source file:fr.ippon.wip.http.hc.HttpClientExecutor.java
private Response createResponse(HttpResponse httpResponse, byte[] responseBody, String url, boolean portalUrlComputed) throws IOException { // Create Response object from HttpResponse ContentType contentType = ContentType.getOrDefault(httpResponse.getEntity()); Charset charset = contentType.getCharset(); String mimeType = httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(); int statusCode = httpResponse.getStatusLine().getStatusCode(); return new Response(responseBody, charset, mimeType, url, statusCode, portalUrlComputed); }
From source file:com.autonomousturk.crawler.Page.java
/** * Loads the content of this page from a fetched * HttpEntity./*from w w w . j ava 2s . co m*/ */ public void load(HttpEntity entity) throws Exception { contentType = null; 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:me.ixfan.wechatkit.token.AccessTokenResponseHandler.java
/** * Response handler of WeChat access_token request. * * @param response/*from w w w .jav a 2 s.c o m*/ * @return * @throws IOException */ @Override public AccessToken handleResponse(HttpResponse response) throws IOException { StatusLine status = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (status.getStatusCode() >= 300) { throw new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()); } if (null == entity) { logger.error("Exception occurred while obtaining access_token, there's no data in the response."); throw new ClientProtocolException("Empty HTTP response"); } ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); if (null == charset) { charset = StandardCharsets.UTF_8; } Reader reader = new InputStreamReader(entity.getContent(), charset); JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject(); logger.debug("obtained access_token: " + jsonObject.toString()); Map<String, String> keyValues = new HashMap<>(); for (Map.Entry<String, JsonElement> element : jsonObject.entrySet()) { keyValues.put(element.getKey(), element.getValue().getAsString()); } AccessToken accessToken = new AccessToken(); if (null != keyValues.get("access_token")) { accessToken.setAccessToken(keyValues.get("access_token")); accessToken.setExpiresIn(Long.parseLong(keyValues.get("expires_in"))); } else { logger.error("Obtaining access_token failed, errcode:{}, errmsg:{}", keyValues.get("errcode"), keyValues.get("errmsg")); } return accessToken; }
From source file:com.meplato.store2.ApacheHttpResponse.java
/** * Instantiates a new instance of ApacheHttpResponse. * * @param response the HTTP response from the ApacheHttpClient. * @throws ServiceException if e.g. serialization of the response fails. *///from www . j av a2 s . com public ApacheHttpResponse(HttpResponse 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) { throw new ServiceException("Error deserializing data", null, e); } finally { } } else { this.body = null; } }
From source file:org.aliuge.crawler.page.Page.java
/** * Loads the content of this page from a fetched HttpEntity. *//*from www . ja v a 2 s. co m*/ public void load(HttpEntity entity) throws Exception { contentType = null; 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); // headcharsetmeta? if (null == contentCharset || "" == contentCharset) { int length = contentData.length; if (length > 2048) { length = 2048; } String html = new String(contentData, 0, 2048); contentCharset = matchCharset(html.toLowerCase()); } }
From source file:com.dougtest.restTest.basicRestTests.java
/** * This test case is used to verify that if given a bad URL the REST API will return an "Not found error" message * @throws ClientProtocolException/*from w ww . j a v a 2s . c o m*/ * @throws IOException */ @Test public void givenUrlDoesNotExist() throws ClientProtocolException, IOException { final String jsonMimeType = "application/xml"; final HttpUriRequest request = new HttpGet(badurl); final HttpResponse response = HttpClientBuilder.create().build().execute(request); final String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_NOT_FOUND)); assertEquals(jsonMimeType, mimeType); }
From source file:nl.nn.adapterframework.http.HttpResponseHandler.java
public ContentType getContentType() { Header contentTypeHeader = this.getFirstHeader(HttpHeaders.CONTENT_TYPE); ContentType contentType;//from w w w .ja v a2 s .c o m if (contentTypeHeader != null) { contentType = ContentType.parse(contentTypeHeader.getValue()); } else { contentType = ContentType.getOrDefault(httpEntity); } return contentType; }
From source file:crawler.Page.java
/** * Loads the content of this page from a fetched HttpEntity. * * @param entity HttpEntity// w w w . j a v a 2s. c o m * @param maxBytes The maximum number of bytes to read * @throws Exception when load fails */ public void load(HttpEntity entity, int maxBytes) throws Exception { contentType = null; 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 = toByteArray(entity, maxBytes); }
From source file:project.latex.balloon.writer.HttpDataWriter.java
void sendPostRequest(String rawString) throws IOException { String jsonString = getJsonStringFromRawData(rawString); CloseableHttpClient httpclient = HttpClients.createDefault(); StringEntity entity = new StringEntity(jsonString, ContentType.create("plain/text", Consts.UTF_8)); HttpPost httppost = new HttpPost(receiverUrl); httppost.addHeader("content-type", "application/json"); httppost.setEntity(entity);//from w w w . j a v a 2s . co m ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, 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"); } ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset)); StringBuilder stringBuilder = new StringBuilder(); String line = reader.readLine(); while (line != null) { stringBuilder.append(line); line = reader.readLine(); } return stringBuilder.toString(); } }; String responseString = httpclient.execute(httppost, responseHandler); logger.info(responseString); }
From source file:me.ixfan.wechatkit.util.JsonResponseHandler.java
@Override public JsonObject handleResponse(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 ww.j a v a 2s . co m if (entity == null) { throw new ClientProtocolException("Response contains no content"); } ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); if (null == charset) { charset = StandardCharsets.UTF_8; } Reader reader = new InputStreamReader(entity.getContent(), charset); return new JsonParser().parse(reader).getAsJsonObject(); }