@Override @Deprecated public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) { setCancellable(new Cancellable() { @Override public boolean cancel() { try { releaseTrigger.abortConnection(); return true; } catch (final IOException ex) { return false; } } }); }
@Test(expected=RequestAbortedException.class) public void testExecAbortedPriorToConnectionSetup() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpClientContext context = new HttpClientContext(); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test")); Mockito.when(managedConn.isOpen()).thenReturn(Boolean.FALSE); Mockito.when(execAware.isAborted()).thenReturn(Boolean.FALSE, Boolean.TRUE); try { mainClientExec.execute(route, request, context, execAware); } catch (final IOException ex) { Mockito.verify(connRequest, Mockito.times(1)).get(0, TimeUnit.MILLISECONDS); Mockito.verify(execAware, Mockito.times(2)).setCancellable(Mockito.<Cancellable>any()); Mockito.verify(connManager, Mockito.never()).connect( Mockito.same(managedConn), Mockito.<HttpRoute>any(), Mockito.anyInt(), Mockito.<HttpContext>any()); throw ex; } }
@Deprecated public void setConnectionRequest(final ClientConnectionRequest connRequest) { setCancellable(new Cancellable() { public boolean cancel() { connRequest.abortRequest(); return true; } }); }
@Deprecated public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) { setCancellable(new Cancellable() { public boolean cancel() { try { releaseTrigger.abortConnection(); return true; } catch (final IOException ex) { return false; } } }); }
public void abort() { if (this.aborted.compareAndSet(false, true)) { final Cancellable cancellable = this.cancellableRef.getAndSet(null); if (cancellable != null) { cancellable.cancel(); } } }
/** * Resets internal state of the request making it reusable. * * @since 4.2 */ public void reset() { final Cancellable cancellable = this.cancellableRef.getAndSet(null); if (cancellable != null) { cancellable.cancel(); } this.aborted.set(false); }
@Override @Deprecated public void setConnectionRequest(final ClientConnectionRequest connRequest) { setCancellable(new Cancellable() { @Override public boolean cancel() { connRequest.abortRequest(); return true; } }); }
@Override public void abort() { if (this.aborted.compareAndSet(false, true)) { final Cancellable cancellable = this.cancellableRef.getAndSet(null); if (cancellable != null) { cancellable.cancel(); } } }
/** * @since 4.2 */ @Override public void setCancellable(final Cancellable cancellable) { if (!this.aborted.get()) { this.cancellableRef.set(cancellable); } }
@Test public void testExecRequestNonPersistentConnection() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpClientContext context = new HttpClientContext(); final RequestConfig config = RequestConfig.custom() .setConnectTimeout(123) .setSocketTimeout(234) .setConnectionRequestTimeout(345) .build(); context.setRequestConfig(config); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test")); final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); Mockito.when(requestExecutor.execute( Mockito.same(request), Mockito.<HttpClientConnection>any(), Mockito.<HttpClientContext>any())).thenReturn(response); final CloseableHttpResponse finalResponse = mainClientExec.execute( route, request, context, execAware); Mockito.verify(connManager).requestConnection(route, null); Mockito.verify(connRequest).get(345, TimeUnit.MILLISECONDS); Mockito.verify(execAware, Mockito.times(1)).setCancellable(connRequest); Mockito.verify(execAware, Mockito.times(2)).setCancellable(Mockito.<Cancellable>any()); Mockito.verify(connManager).connect(managedConn, route, 123, context); Mockito.verify(connManager).routeComplete(managedConn, route, context); Mockito.verify(managedConn).setSocketTimeout(234); Mockito.verify(requestExecutor, Mockito.times(1)).execute(request, managedConn, context); Mockito.verify(managedConn, Mockito.times(1)).close(); Mockito.verify(connManager).releaseConnection(managedConn, null, 0, TimeUnit.MILLISECONDS); Assert.assertNotNull(context.getTargetAuthState()); Assert.assertNotNull(context.getProxyAuthState()); Assert.assertSame(managedConn, context.getConnection()); Assert.assertNull(context.getUserToken()); Assert.assertNotNull(finalResponse); Assert.assertTrue(finalResponse instanceof HttpResponseProxy); }
@Test public void testExecRequestNonPersistentConnection() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpClientContext context = new HttpClientContext(); final RequestConfig config = RequestConfig.custom() .setConnectTimeout(123) .setSocketTimeout(234) .setConnectionRequestTimeout(345) .build(); context.setRequestConfig(config); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test")); final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); Mockito.when(requestExecutor.execute( Mockito.same(request), Mockito.<HttpClientConnection>any(), Mockito.<HttpClientContext>any())).thenReturn(response); final CloseableHttpResponse finalResponse = minimalClientExec.execute( route, request, context, execAware); Mockito.verify(connManager).requestConnection(route, null); Mockito.verify(connRequest).get(345, TimeUnit.MILLISECONDS); Mockito.verify(execAware, Mockito.times(1)).setCancellable(connRequest); Mockito.verify(execAware, Mockito.times(2)).setCancellable(Mockito.<Cancellable>any()); Mockito.verify(connManager).connect(managedConn, route, 123, context); Mockito.verify(connManager).routeComplete(managedConn, route, context); Mockito.verify(managedConn).setSocketTimeout(234); Mockito.verify(requestExecutor, Mockito.times(1)).execute(request, managedConn, context); Mockito.verify(managedConn, Mockito.times(1)).close(); Mockito.verify(connManager).releaseConnection(managedConn, null, 0, TimeUnit.MILLISECONDS); Assert.assertSame(managedConn, context.getConnection()); Assert.assertNotNull(finalResponse); Assert.assertTrue(finalResponse instanceof HttpResponseProxy); }
@Override public void setCancellable(final Cancellable cancellable) { try { if(!releaseTriggerLatch.await(1, TimeUnit.SECONDS)) { throw new RuntimeException("Waited too long..."); } } catch(final InterruptedException ie) { throw new RuntimeException(ie); } super.setCancellable(cancellable); }
InternalHttpRequest(final String method, final URI requestURI) { Args.notBlank(method, "Method"); Args.notNull(requestURI, "Request URI"); this.method = method; this.uri = requestURI; this.aborted = new AtomicBoolean(false); this.cancellableRef = new AtomicReference<Cancellable>(null); }
@Override public void abort() throws UnsupportedOperationException { if (this.aborted.compareAndSet(false, true)) { final Cancellable cancellable = this.cancellableRef.getAndSet(null); if (cancellable != null) { cancellable.cancel(); } } }
protected AbstractExecutionAwareRequest() { super(); this.aborted = new AtomicBoolean(false); this.cancellableRef = new AtomicReference<Cancellable>(null); }
/** * @since 4.2 */ public void setCancellable(final Cancellable cancellable) { if (!this.aborted.get()) { this.cancellableRef.set(cancellable); } }
@Override public void setCancellable(final Cancellable cancellable) { if (!this.aborted.get()) { this.cancellableRef.set(cancellable); } }
@SuppressWarnings( "deprecation" ) @Override protected void service( final HttpServletRequest servletRequest, final HttpServletResponse servletResponse ) throws ServletException, IOException { final String proxyRequestUri = rewriteUrlFromRequest( servletRequest ); final HttpRequest proxyRequest = newProxyRequest( servletRequest, proxyRequestUri ); copyRequestHeaders( servletRequest, proxyRequest ); setXForwardedForHeader( servletRequest, proxyRequest ); proxyPrepared( proxyRequest ); try { final HttpResponse proxyResponse = _client.execute( URIUtils.extractHost( _targetUri ), proxyRequest ); // Process the response final int statusCode = proxyResponse.getStatusLine().getStatusCode(); if ( doResponseRedirectOrNotModifiedLogic( servletRequest, servletResponse, proxyResponse, statusCode ) ) { //just to be sure, but is probably a no-op EntityUtils.consume( proxyResponse.getEntity() ); return; } // Pass the response code. This method with the "reason phrase" is deprecated but it's the only way to pass the // reason along too. servletResponse.setStatus( statusCode, proxyResponse.getStatusLine().getReasonPhrase() ); copyResponseHeaders( proxyResponse, servletResponse ); // Send the content to the client copyResponseEntity( proxyResponse, servletResponse ); } catch ( final Exception e ) { //abort request, according to best practice with HttpClient if ( proxyRequest instanceof Cancellable ) { final Cancellable cancellable = (Cancellable) proxyRequest; cancellable.cancel(); } handleError( e ); } }
/** * Sets {@link Cancellable} for the ongoing operation. */ void setCancellable(Cancellable cancellable);