List of usage examples for org.apache.http.util EntityUtils consume
public static void consume(HttpEntity httpEntity) throws IOException
From source file:fr.mael.microrss.xml.AtomFeedParser.java
@Override public List<Article> readArticles(Feed feed) throws Exception { List<Article> articles = new ArrayList<Article>(); JAXBContext jc = JAXBContext.newInstance("fr.mael.microrss.xml.atom"); Unmarshaller unmarshaller = jc.createUnmarshaller(); HttpGet get = new HttpGet(feed.getUrl()); HttpResponse response = client.execute(get); try {/*from w ww . j a v a2 s .c o m*/ Object o = unmarshaller.unmarshal(response.getEntity().getContent()); if (o instanceof JAXBElement) { JAXBElement element = (JAXBElement) o; FeedType feedType = (FeedType) element.getValue(); articles = readEntries(feedType.getAuthorOrCategoryOrContributor(), feed, feedType); } return articles; } catch (Exception e) { EntityUtils.consume(response.getEntity()); throw new Exception(e); } }
From source file:org.pentaho.di.core.util.HttpClientUtil.java
/** * * @param response the httpresponse for processing * @param charset the charset used for getting HttpEntity * @param decode determines if the result should be decoded or not * @return HttpEntity in String representation using provided charset * @throws IOException//from w ww. jav a 2 s .c om */ public static String responseToString(HttpResponse response, Charset charset, boolean decode) throws IOException { HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, charset); EntityUtils.consume(entity); if (decode) { result = URLDecoder.decode(result, StandardCharsets.UTF_8.name()); } return result; }
From source file:org.callimachusproject.client.HttpUriResponse.java
public String getRedirectLocation() throws IOException { int code = this.getStatusLine().getStatusCode(); if (code == 301 || code == 302 || code == 307 || code == 308) { Header location = this.getFirstHeader("Location"); if (location != null) { EntityUtils.consume(this.getEntity()); String value = location.getValue(); if (value.startsWith("/") || !value.contains(":")) { try { value = TermFactory.newInstance(systemId).resolve(value); } catch (IllegalArgumentException e) { return value; }/*from w ww. ja va 2 s. co m*/ } return value; } } return null; }
From source file:lh.api.showcase.server.util.HttpQueryUtils.java
public static String executeQuery(URI uri, ApiAuth apiAuth, HasProxySettings proxySetting, HttpClientFactory httpClientFactory, final int maxRetries) throws HttpErrorResponseException { //logger.info("uri: " + uri.toString()); AtomicInteger tryCounter = new AtomicInteger(0); while (true) { CloseableHttpClient httpclient = httpClientFactory.getHttpClient(proxySetting); HttpGet httpGet = new HttpGet(uri); httpGet.addHeader("Authorization", apiAuth.getAuthHeader()); httpGet.addHeader("Accept", "application/json"); //logger.info("auth: " + apiAuth.getAuthHeader()) ; //logger.info("query: " + httpGet.toString()); CloseableHttpResponse response = null; try {//www . j a v a 2s .co m response = httpclient.execute(httpGet); StatusLine status = response.getStatusLine(); BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity()); String json = IOUtils.toString(entity.getContent(), "UTF8"); EntityUtils.consume(entity); //logger.info("response: " + json); // check for errors if (status != null && status.getStatusCode() > 299) { if (status.getStatusCode() == 401) { // token has probably expired logger.info("Authentication Error. Token will be refreshed"); if (tryCounter.getAndIncrement() < maxRetries) { if (apiAuth.updateAccessToken()) { logger.info("Token successfully refreshed"); // we retry with the new token logger.info("Retry number " + tryCounter.get()); continue; } } } throw new HttpErrorResponseException(status.getStatusCode(), status.getReasonPhrase(), json); } return json; } catch (IOException e) { logger.severe(e.getMessage()); break; } finally { try { if (response != null) { response.close(); } } catch (IOException e) { logger.log(Level.SEVERE, e.getMessage()); } } } return null; }
From source file:org.eclipse.lyo.testsuite.server.trsutils.SendUtil.java
/** * /* ww w . j a v a 2s . c o m*/ * @param uri resource uri for creation factory * @param httpClient client used to post to the uri * @param httpContext http context to use for the call * @param contentType content type to be used in the creation * @param content content to be used in the creation * @throws SendException if an error occurs in posting to the uri */ public static String createResource(String uri, HttpClient httpClient, HttpContext httpContext, String contentType, String content) throws SendException { String createdResourceUri = ""; if (uri == null) throw new IllegalArgumentException(Messages.getServerString("send.util.uri.null")); //$NON-NLS-1$ if (httpClient == null) throw new IllegalArgumentException(Messages.getServerString("send.util.httpclient.null")); //$NON-NLS-1$ try { new URL(uri); // Make sure URL is valid HttpPost post = new HttpPost(uri); StringEntity entity = new StringEntity(content); post.setEntity(entity); post.setHeader(HttpConstants.ACCEPT, HttpConstants.CT_APPLICATION_RDF_XML);//$NON-NLS-1$ post.addHeader(HttpConstants.CONTENT_TYPE, contentType); post.addHeader(HttpConstants.CACHE_CONTROL, "max-age=0"); //$NON-NLS-1$ HttpResponse resp = httpClient.execute(post); try { if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { HttpErrorHandler.responseToException(resp); } createdResourceUri = resp.getFirstHeader(HttpConstants.LOCATION).getValue(); HttpResponseUtil.finalize(resp); } finally { try { if (entity != null) { EntityUtils.consume(entity); } } catch (IOException e) { // ignore } } } catch (Exception e) { String uriLocation = Messages.getServerString("send.util.uri.unidentifiable"); //$NON-NLS-1$ if (uri != null && !uri.isEmpty()) { uriLocation = uri; } throw new SendException(MessageFormat.format(Messages.getServerString("send.util.retrieve.error"), //$NON-NLS-1$ uriLocation)); } return createdResourceUri; }
From source file:pl.softech.gw.download.ResourceDownloader.java
public void download(String url, File dir, String fileName) throws IOException { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = null;/* ww w.j a va2s.com*/ BufferedInputStream in = null; InputStream ins = null; BufferedOutputStream out = null; try { StatusLine statusLine = response.getStatusLine(); entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { EntityUtils.consume(entity); throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } ins = entity.getContent(); long all = entity.getContentLength(); in = new BufferedInputStream(ins); out = new BufferedOutputStream(new FileOutputStream(new File(dir, fileName))); byte[] buffer = new byte[1024]; int cnt; while ((cnt = in.read(buffer)) >= 0) { out.write(buffer, 0, cnt); fireEvent(new BytesReceivedEvent(cnt, all, fileName)); } } catch (IOException e) { fireEvent(new DownloadErrorEvent(String.format("Error during downloading file %s", fileName), e)); throw e; } finally { if (ins != null) { ins.close(); } if (in != null) { in.close(); } if (out != null) { out.close(); } httpGet.releaseConnection(); httpclient.getConnectionManager().shutdown(); } }
From source file:cn.jachohx.crawler.fetcher.PageFetchResult.java
public void discardContentIfNotConsumed() { try {/*from w ww. j a v a2s .c o m*/ if (entity != null) { EntityUtils.consume(entity); } } catch (EOFException e) { // We can ignore this exception. It can happen on compressed streams // which are not // repeatable } catch (IOException e) { // We can ignore this exception. It can happen if the stream is // closed. } catch (Exception e) { e.printStackTrace(); } }
From source file:net.oebs.jalos.Client.java
public SubmitResponseObject submit(String postUrl) throws IOException { HttpPost httpPost = new HttpPost(this.serviceUrl + "/a/submit"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("url", postUrl)); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); ObjectMapper mapper = new ObjectMapper(); SubmitResponseObject sro = mapper.readValue(entity.getContent(), SubmitResponseObject.class); EntityUtils.consume(entity); response.close();/*from w ww. j av a 2 s . c o m*/ return sro; }
From source file:com.rackspace.api.clients.veracode.responses.AbstractXmlResponse.java
public AbstractXmlResponse(HttpResponse response) throws VeracodeApiException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try {/*from w w w.j a v a 2s . c om*/ builder = domFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException("Xml Configuration Issue parsing response", e); } HttpEntity responseEntity = extractResponse(response); try { if (response.getStatusLine().getStatusCode() == 200) { this.doc = builder.parse(responseEntity.getContent()); EntityUtils.consume(responseEntity); } else { String errorMessage = EntityUtils.toString(responseEntity); int errorCode = response.getStatusLine().getStatusCode(); throw new VeracodeApiException("Error Code: " + errorCode + " Error Message: " + errorMessage); } } catch (SAXException e) { throw new VeracodeApiException("Could not Parse Response.", e); } catch (IOException e) { throw new VeracodeApiException("Could not Parse Response.", e); } XPathFactory factory = XPathFactory.newInstance(); this.xpath = factory.newXPath(); }
From source file:com.github.ljtfreitas.restify.http.client.request.apache.httpclient.ApacheHttpClientResponse.java
@Override public void close() throws IOException { try {//from w ww .j a v a 2s. c o m EntityUtils.consume(entity); } finally { if (httpResponse instanceof Closeable) { ((Closeable) httpResponse).close(); } } }