List of usage examples for org.apache.http.client.methods HttpUriRequest hashCode
@HotSpotIntrinsicCandidate public native int hashCode();
From source file:net.yacy.cora.protocol.http.HTTPClient.java
private void storeConnectionInfo(final HttpUriRequest httpUriRequest) { final int port = httpUriRequest.getURI().getPort(); final String thost = httpUriRequest.getURI().getHost(); //assert thost != null : "uri = " + httpUriRequest.getURI().toString(); ConnectionInfo.addConnection(/*from ww w . j ava2 s .co m*/ new ConnectionInfo(httpUriRequest.getURI().getScheme(), port == -1 ? thost : thost + ":" + port, httpUriRequest.getMethod() + " " + httpUriRequest.getURI().getPath(), httpUriRequest.hashCode(), System.currentTimeMillis(), this.upbytes)); }
From source file:net.yacy.cora.protocol.http.HTTPClient.java
private byte[] getContentBytes(final HttpUriRequest httpUriRequest, final int maxBytes, final boolean concurrent) throws IOException { byte[] content = null; try {// w w w .j a va 2s.c o m execute(httpUriRequest, concurrent); if (this.httpResponse == null) return null; // get the response body final HttpEntity httpEntity = this.httpResponse.getEntity(); if (httpEntity != null) { if (getStatusCode() == HttpStatus.SC_OK) { if (maxBytes >= 0 && httpEntity.getContentLength() > maxBytes) { /* When anticipated content length is already known and exceed the specified limit : * throw an exception and abort the connection, consistently with getByteArray() implementation * Otherwise returning null and consuming fully the entity can be very long on large resources */ throw new IOException( "Content to download exceed maximum value of " + Formatter.bytesToString(maxBytes)); } content = getByteArray(httpEntity, maxBytes); } // Ensures that the entity content is fully consumed and the content stream, if exists, is closed. EntityUtils.consume(httpEntity); } } catch (final IOException e) { httpUriRequest.abort(); throw e; } finally { if (this.httpResponse != null) this.httpResponse.close(); ConnectionInfo.removeConnection(httpUriRequest.hashCode()); } return content; }
From source file:net.yacy.cora.protocol.http.HTTPClient.java
private void execute(final HttpUriRequest httpUriRequest, final boolean concurrent) throws IOException { final HttpClientContext context = HttpClientContext.create(); context.setRequestConfig(reqConfBuilder.build()); if (this.host != null) context.setTargetHost(new HttpHost(this.host)); setHeaders(httpUriRequest);/*from w w w . ja va2 s .co m*/ // statistics storeConnectionInfo(httpUriRequest); // execute the method; some asserts confirm that that the request can be send with Content-Length and is therefore not terminated by EOF if (httpUriRequest instanceof HttpEntityEnclosingRequest) { final HttpEntityEnclosingRequest hrequest = (HttpEntityEnclosingRequest) httpUriRequest; final HttpEntity entity = hrequest.getEntity(); assert entity != null; //assert !entity.isChunked(); //assert entity.getContentLength() >= 0; assert !hrequest.expectContinue(); } final String initialThreadName = Thread.currentThread().getName(); Thread.currentThread().setName("HTTPClient-" + httpUriRequest.getURI()); final long time = System.currentTimeMillis(); try { if (concurrent) { FutureTask<CloseableHttpResponse> t = new FutureTask<CloseableHttpResponse>( new Callable<CloseableHttpResponse>() { @Override public CloseableHttpResponse call() throws ClientProtocolException, IOException { final CloseableHttpClient client = clientBuilder.build(); CloseableHttpResponse response = client.execute(httpUriRequest, context); return response; } }); executor.execute(t); try { this.httpResponse = t.get(this.timeout, TimeUnit.MILLISECONDS); } catch (ExecutionException e) { throw e.getCause(); } catch (Throwable e) { } try { t.cancel(true); } catch (Throwable e) { } if (this.httpResponse == null) throw new IOException("timout to client after " + this.timeout + "ms" + " for url " + httpUriRequest.getURI().toString()); } else { final CloseableHttpClient client = clientBuilder.build(); this.httpResponse = client.execute(httpUriRequest, context); } this.httpResponse.setHeader(HeaderFramework.RESPONSE_TIME_MILLIS, Long.toString(System.currentTimeMillis() - time)); } catch (final Throwable e) { ConnectionInfo.removeConnection(httpUriRequest.hashCode()); httpUriRequest.abort(); if (this.httpResponse != null) this.httpResponse.close(); //e.printStackTrace(); throw new IOException( "Client can't execute: " + (e.getCause() == null ? e.getMessage() : e.getCause().getMessage()) + " duration=" + Long.toString(System.currentTimeMillis() - time) + " for url " + httpUriRequest.getURI().toString()); } finally { /* Restore the thread initial name */ Thread.currentThread().setName(initialThreadName); } }