Example usage for org.apache.http.conn.routing HttpRoute HttpRoute

List of usage examples for org.apache.http.conn.routing HttpRoute HttpRoute

Introduction

In this page you can find the example usage for org.apache.http.conn.routing HttpRoute HttpRoute.

Prototype

public HttpRoute(final HttpHost target) 

Source Link

Document

Creates a new direct insecure route.

Usage

From source file:org.apache.http.impl.conn.TestPoolingHttpClientConnectionManager.java

@Test(expected = ConnectionPoolTimeoutException.class)
public void testLeaseFutureTimeout() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);

    Mockito.when(future.isCancelled()).thenReturn(Boolean.TRUE);
    Mockito.when(future.get(1, TimeUnit.SECONDS)).thenThrow(new TimeoutException());
    Mockito.when(pool.lease(route, null, null)).thenReturn(future);

    final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
    connRequest1.get(1, TimeUnit.SECONDS);
}

From source file:org.apache.http.impl.conn.TestPoolingHttpClientConnectionManager.java

@Test
public void testReleaseReusable() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);

    final CPoolEntry entry = Mockito
            .spy(new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS));
    entry.markRouteComplete();/*w  w  w  . j  ava  2 s .  c  o m*/

    Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE);
    Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry);
    Mockito.when(pool.lease(route, null, null)).thenReturn(future);
    Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);

    final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
    final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS);
    Assert.assertNotNull(conn1);
    Assert.assertTrue(conn1.isOpen());

    mgr.releaseConnection(conn1, "some state", 0, TimeUnit.MILLISECONDS);

    Mockito.verify(pool).release(entry, true);
    Mockito.verify(entry).setState("some state");
    Mockito.verify(entry).updateExpiry(Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));
}

From source file:org.apache.http.impl.conn.TestPoolingHttpClientConnectionManager.java

@Test
public void testReleaseNonReusable() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);

    final CPoolEntry entry = Mockito
            .spy(new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS));
    entry.markRouteComplete();// w w w . j  a v a  2 s . co m

    Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE);
    Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry);
    Mockito.when(pool.lease(route, null, null)).thenReturn(future);
    Mockito.when(conn.isOpen()).thenReturn(Boolean.FALSE);

    final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
    final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS);
    Assert.assertNotNull(conn1);
    Assert.assertFalse(conn1.isOpen());

    mgr.releaseConnection(conn1, "some state", 0, TimeUnit.MILLISECONDS);

    Mockito.verify(pool).release(entry, false);
    Mockito.verify(entry, Mockito.never()).setState(Mockito.anyObject());
    Mockito.verify(entry, Mockito.never()).updateExpiry(Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));
}

From source file:org.apache.http.impl.nio.client.MinimalClientExchangeHandlerImpl.java

public void start() throws HttpException, IOException {
    final HttpHost target = this.requestProducer.getTarget();
    final HttpRequest original = this.requestProducer.generateRequest();

    if (original instanceof HttpExecutionAware) {
        ((HttpExecutionAware) original).setCancellable(this);
    }//from ww  w.  j av  a 2 s  .  com
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + getId() + "] start execution");
    }

    if (original instanceof Configurable) {
        final RequestConfig config = ((Configurable) original).getConfig();
        if (config != null) {
            this.localContext.setRequestConfig(config);
        }
    }

    final HttpRequestWrapper request = HttpRequestWrapper.wrap(original);
    final HttpRoute route = new HttpRoute(target);
    setCurrentRequest(request);
    setRoute(route);

    this.localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
    this.localContext.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, target);
    this.localContext.setAttribute(HttpClientContext.HTTP_ROUTE, route);

    this.httpProcessor.process(request, this.localContext);

    requestConnection();
}

From source file:org.apache.http.impl.nio.client.PipeliningClientExchangeHandlerImpl.java

public void start() throws HttpException, IOException {
    if (this.log.isDebugEnabled()) {
        this.log.debug("[exchange: " + getId() + "] start execution");
    }/*  w ww  .ja  va  2s.  c o m*/

    final HttpRoute route = new HttpRoute(this.target);
    setRoute(route);

    this.localContext.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
    this.localContext.setAttribute(HttpClientContext.HTTP_ROUTE, route);

    requestConnection();
}

From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java

@Test
public void testRequestReleaseConnection() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);
    final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L,
            TimeUnit.MILLISECONDS, connCallback);
    Assert.assertNotNull(future);// w  w  w. j a va 2  s .  c o  m

    Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L),
            Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture());
    final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
    final Log log = Mockito.mock(Log.class);
    final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
    poolentry.markRouteComplete();
    callaback.completed(poolentry);

    Assert.assertTrue(future.isDone());
    final NHttpClientConnection managedConn = future.get();
    Mockito.verify(connCallback).completed(Matchers.<NHttpClientConnection>any());

    Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
    connman.releaseConnection(managedConn, "new state", 5, TimeUnit.SECONDS);

    Mockito.verify(pool).release(poolentry, true);
    Assert.assertEquals("new state", poolentry.getState());
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(poolentry.getUpdated());
    cal.add(Calendar.SECOND, 5);
    Assert.assertEquals(cal.getTimeInMillis(), poolentry.getExpiry());
}

From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java

@Test
public void testReleaseConnectionIncompleteRoute() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);
    final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L,
            TimeUnit.MILLISECONDS, connCallback);
    Assert.assertNotNull(future);//from  w w w.  j  a va  2 s  . c  om

    Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L),
            Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture());
    final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
    final Log log = Mockito.mock(Log.class);
    final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
    callaback.completed(poolentry);

    Assert.assertTrue(future.isDone());
    final NHttpClientConnection managedConn = future.get();
    Mockito.verify(connCallback).completed(Matchers.<NHttpClientConnection>any());

    Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
    connman.releaseConnection(managedConn, "new state", 5, TimeUnit.SECONDS);

    Mockito.verify(pool).release(poolentry, false);
}

From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java

@Test
public void testRequestConnectionFutureCancelled() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);
    final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L,
            TimeUnit.MILLISECONDS, null);
    Assert.assertNotNull(future);//from   w  ww. ja va 2s .  c  o  m
    future.cancel(true);

    Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L),
            Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture());
    final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
    final Log log = Mockito.mock(Log.class);
    final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
    poolentry.markRouteComplete();
    callaback.completed(poolentry);

    Mockito.verify(pool).release(poolentry, true);
}

From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java

@Test(expected = ExecutionException.class)
public void testRequestConnectionFailed() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);
    final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L,
            TimeUnit.MILLISECONDS, null);
    Assert.assertNotNull(future);//from w ww .j  a v  a2s . co  m

    Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L),
            Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture());
    final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
    callaback.failed(new Exception());

    Assert.assertTrue(future.isDone());
    future.get();
}

From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java

@Test
public void testRequestConnectionCancelled() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);
    final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L,
            TimeUnit.MILLISECONDS, null);
    Assert.assertNotNull(future);/*w  w w  .  j a va  2  s .  c  om*/

    Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L),
            Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture());
    final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
    callaback.cancelled();

    Assert.assertTrue(future.isDone());
    Assert.assertTrue(future.isCancelled());
    Assert.assertNull(future.get());
}