List of usage examples for org.apache.http.client.methods HttpUriRequest abort
void abort() throws UnsupportedOperationException;
From source file:com.arcbees.vcs.AbstractVcsApi.java
protected <T> T processResponse(HttpClientWrapper httpClient, HttpUriRequest request, Credentials credentials, Gson gson, Class<T> clazz) throws IOException { try {// w ww. ja va 2 s . co m HttpResponse httpResponse = doExecuteRequest(httpClient, request, credentials); HttpEntity entity = httpResponse.getEntity(); if (entity == null) { throw new IOException( "Failed to complete request. Empty response. Status: " + httpResponse.getStatusLine()); } try { return readEntity(clazz, entity, gson); } finally { EntityUtils.consumeQuietly(entity); } } finally { request.abort(); } }
From source file:com.jgoetsch.eventtrader.source.ApeStreamingMsgSource.java
private JSONArray executeRequest(HttpClient client, HttpUriRequest request) { try {/* ww w. j av a 2 s.c o m*/ HttpResponse rsp = client.execute(request); HttpEntity entity = rsp.getEntity(); if (rsp.getStatusLine().getStatusCode() >= 400 || entity == null) { log.error("HTTP request to " + request.getURI() + " failed with status " + rsp.getStatusLine()); return null; } else { return (JSONArray) JSONValue.parse(new BufferedReader(new InputStreamReader(entity.getContent()))); } } catch (Exception e) { log.warn("Exception reading or parsing message source", e); return null; } finally { request.abort(); } }
From source file:org.commonjava.maven.galley.transport.htcli.internal.util.TransferResponseUtils.java
public static boolean handleUnsuccessfulResponse(final HttpUriRequest request, final CloseableHttpResponse response, HttpLocation location, final String url, final boolean graceful404) throws TransferException { final Logger logger = LoggerFactory.getLogger(TransferResponseUtils.class); final StatusLine line = response.getStatusLine(); InputStream in = null;/*from w ww . j ava 2s .c om*/ HttpEntity entity = null; try { entity = response.getEntity(); final int sc = line.getStatusCode(); boolean contentMissing = (sc == HttpStatus.SC_NOT_FOUND || sc == HttpStatus.SC_GONE); if (graceful404 && contentMissing) { return false; } else { ByteArrayOutputStream out = null; if (entity != null) { in = entity.getContent(); out = new ByteArrayOutputStream(); copy(in, out); } if (NON_SERVER_GATEWAY_ERRORS.contains(sc) || (sc > 499 && sc < 599)) { throw new BadGatewayException(location, url, sc, "HTTP request failed: %s%s", line, (out == null ? "" : "\n\n" + new String(out.toByteArray()))); } else if (contentMissing) { throw new TransferException("HTTP request failed: %s\nURL: %s%s", line, url, (out == null ? "" : "\n\n" + new String(out.toByteArray()))); } else { throw new TransferLocationException(location, "HTTP request failed: %s%s", line, (out == null ? "" : "\n\n" + new String(out.toByteArray()))); } } } catch (final IOException e) { request.abort(); throw new TransferLocationException(location, "Error reading body of unsuccessful request.\nStatus: %s.\nURL: %s.\nReason: %s", e, line, url, e.getMessage()); } finally { IOUtils.closeQuietly(in); if (entity != null) { try { EntityUtils.consume(entity); } catch (final IOException e) { logger.debug("Failed to consume entity: " + e.getMessage(), e); } } } }
From source file:org.codelibs.fess.crawler.client.http.HcHttpClient.java
protected void closeResources(final HttpUriRequest httpRequest, ResponseData responseData) { IOUtils.closeQuietly(responseData);//from w w w . j a v a 2 s . co m httpRequest.abort(); }
From source file:org.iglootools.hchelpers.java.HttpClientTemplate.java
/** * Executes the given {@link HttpUriRequest}, considers the given list of * {@link HttpErrorHandler} and returns the result of * {@link HttpResponseCallback#doWithHttpResponse(HttpResponse)} * * @param httpUriRequest//from w ww.j ava 2s .c om * @param httpResponseCallback * @param httpErrorHandlers * @return */ public <T> T execute(HttpUriRequest httpUriRequest, HttpResponseCallback<T> httpResponseCallback, Iterable<HttpErrorHandler> httpErrorHandlers) { try { final HttpResponse httpResponse = this.httpClient.execute(httpUriRequest); logger.debug("Received Status: {}", httpResponse.getStatusLine()); HttpErrorHandler httpErrorHandler = findHttpErrorHandlerApplyingToResponse(httpErrorHandlers, httpResponse); if (httpErrorHandler == null) { T o = httpResponseCallback.doWithHttpResponse(httpResponse); consumeContent(httpResponse); return o; } else { httpErrorHandler.handle(httpResponse); } } catch (ClientProtocolException e) { httpUriRequest.abort(); throw new RuntimeException(e); } catch (IOException e) { httpUriRequest.abort(); throw new RuntimeException(e); } catch (RuntimeException e) { httpUriRequest.abort(); throw e; } catch (Exception e) { httpUriRequest.abort(); throw new RuntimeException(e); } finally { } throw new RuntimeException("Should never happen : programming error"); }
From source file:com.netflix.loadbalancer.PingUrl.java
public boolean isAlive(Server server) { String urlStr = ""; if (isSecure) { urlStr = "https://"; } else {/*from w w w .ja v a2 s. c om*/ urlStr = "http://"; } urlStr += server.id; urlStr += getPingAppendString(); boolean isAlive = false; HttpClient httpClient = new DefaultHttpClient(); HttpUriRequest getRequest = new HttpGet(urlStr); String content = null; try { HttpResponse response = httpClient.execute(getRequest); content = EntityUtils.toString(response.getEntity()); isAlive = (response.getStatusLine().getStatusCode() == 200); if (getExpectedContent() != null) { LOGGER.debug("content:" + content); if (content == null) { isAlive = false; } else { if (content.equals(getExpectedContent())) { isAlive = true; } else { isAlive = false; } } } } catch (IOException e) { e.printStackTrace(); } finally { // Release the connection. getRequest.abort(); } return isAlive; }
From source file:com.jgoetsch.eventtrader.source.HttpStreamingMsgSource.java
protected boolean executeRequest(HttpClient client, HttpUriRequest request, MsgParser msgParser) { HttpEntity entity = null;/*from w w w.ja v a2s .c o m*/ try { HttpResponse rsp = client.execute(request); entity = rsp.getEntity(); if (rsp.getStatusLine().getStatusCode() >= 400 || entity == null) { log.warn("HTTP request to " + request.getURI() + " failed with status " + rsp.getStatusLine()); // if 400 level error don't keep trying return (rsp.getStatusLine().getStatusCode() >= 500); } else { log.info("Connected to streaming source " + request.getURI().getHost() + ", waiting for messages"); return msgParser.parseContent(entity.getContent(), -1, entity.getContentType().getValue(), this); } } catch (SocketTimeoutException e) { return true; } catch (Exception e) { log.warn("Exception reading or parsing message source", e); return false; } finally { request.abort(); } }
From source file:it.staiger.jmeter.protocol.http.sampler.HTTPHC4DynamicFilePost.java
@Override public boolean interrupt() { HttpUriRequest request = currentRequest; if (request != null) { currentRequest = null; // don't try twice try {/*from w w w .j av a 2 s . c o m*/ request.abort(); } catch (UnsupportedOperationException e) { log.warn("Could not abort pending request", e); } } return request != null; }