Example usage for org.apache.http.conn.routing HttpRoutePlanner determineRoute

List of usage examples for org.apache.http.conn.routing HttpRoutePlanner determineRoute

Introduction

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

Prototype

public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException;

Source Link

Document

Determines the route for a request.

Usage

From source file:com.eviware.soapui.support.components.BrowserComponent.java

public static void updateJXBrowserProxy() {
    ProxyConfig proxyConf = BrowserServices.getInstance().getProxyConfig();
    if (proxyConf == null)
        return;//w ww .j av  a  2  s  .  c o  m

    try {
        proxyConf.setAutoDetectForNetwork(false);

        if (!proxyAuthenticationInitialized) {
            proxyConf.setAuthenticationHandler(ServerType.HTTP, new AuthenticationHandler() {
                @Override
                public ProxyServerLogin authenticationRequired(ServerType arg0) {
                    Settings settings = SoapUI.getSettings();
                    PropertyExpansionContext context = null;

                    String proxyUsername = PropertyExpander.expandProperties(context,
                            settings.getString(ProxySettings.USERNAME, null));
                    String proxyPassword = PropertyExpander.expandProperties(context,
                            settings.getString(ProxySettings.PASSWORD, null));

                    return new ProxyServerLogin(proxyUsername, proxyPassword);
                }
            });

            proxyAuthenticationInitialized = true;
        }

        if (ProxyUtils.isProxyEnabled()) {
            if (ProxyUtils.isAutoProxy()) {
                HttpRoutePlanner routePlanner = HttpClientSupport.getHttpClient().getRoutePlanner();
                HttpRoute httpRoute = routePlanner.determineRoute(new HttpHost("soapui.org"),
                        new HttpGet("http://soapui.org"), null);

                HttpHost proxyHost = httpRoute.getProxyHost();

                if (proxyHost != null) {
                    proxyConf.setProxy(ServerType.HTTP,
                            new ProxyServer(proxyHost.getHostName(), proxyHost.getPort()));
                } else {
                    proxyConf.setDirectConnection();
                }
            } else {
                Settings settings = SoapUI.getSettings();
                PropertyExpansionContext context = null;

                // check system properties first
                String proxyHost = System.getProperty("http.proxyHost");
                String proxyPort = System.getProperty("http.proxyPort");

                if (proxyHost == null)
                    proxyHost = PropertyExpander.expandProperties(context,
                            settings.getString(ProxySettings.HOST, ""));

                if (proxyPort == null)
                    proxyPort = PropertyExpander.expandProperties(context,
                            settings.getString(ProxySettings.PORT, ""));

                proxyConf.setProxy(ServerType.HTTP, new ProxyServer(proxyHost, Integer.parseInt(proxyPort)));
                // check excludes
                proxyConf.setExceptions(PropertyExpander.expandProperties(context,
                        settings.getString(ProxySettings.EXCLUDES, "")));
            }
        } else {
            proxyConf.setDirectConnection();
        }
    } catch (Exception e) {
        //ignore
    }
}

From source file:org.sonatype.nexus.error.reporting.NexusPRConnectorTest.java

@Test
public void testNonProxyHosts() throws HttpException {
    final String host = "host";
    final int port = 1234;

    when(proxySettings.isEnabled()).thenReturn(true);
    when(proxySettings.getHostname()).thenReturn(host);
    when(proxySettings.getPort()).thenReturn(port);
    when(proxySettings.getNonProxyHosts()).thenReturn(Sets.newHashSet(".*"));

    final DefaultHttpClient client = (DefaultHttpClient) underTest.client();
    assertThat(ConnRouteParams.getDefaultProxy(client.getParams()), notNullValue());

    final HttpRoutePlanner planner = client.getRoutePlanner();
    final HttpRequest request = mock(HttpRequest.class);
    when(request.getParams()).thenReturn(mock(HttpParams.class));

    planner.determineRoute(new HttpHost("host"), request, mock(HttpContext.class));

    ArgumentCaptor<HttpParams> captor = ArgumentCaptor.forClass(HttpParams.class);

    verify(request).setParams(captor.capture());
    assertThat(ConnRouteParams.getDefaultProxy(captor.getValue()), nullValue());
}

From source file:org.apache.http.impl.client.AbstractHttpClient.java

@Override
protected final CloseableHttpResponse doExecute(final HttpHost target, final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException {

    Args.notNull(request, "HTTP request");
    // a null target may be acceptable, this depends on the route planner
    // a null context is acceptable, default context created below

    HttpContext execContext = null;
    RequestDirector director = null;//  w ww .  j  a v  a 2s.  c o m
    HttpRoutePlanner routePlanner = null;
    ConnectionBackoffStrategy connectionBackoffStrategy = null;
    BackoffManager backoffManager = null;

    // Initialize the request execution context making copies of
    // all shared objects that are potentially threading unsafe.
    synchronized (this) {

        final HttpContext defaultContext = createHttpContext();
        if (context == null) {
            execContext = defaultContext;
        } else {
            execContext = new DefaultedHttpContext(context, defaultContext);
        }
        final HttpParams params = determineParams(request);
        final RequestConfig config = HttpClientParamConfig.getRequestConfig(params);
        execContext.setAttribute(ClientContext.REQUEST_CONFIG, config);

        // Create a director for this request
        director = createClientRequestDirector(getRequestExecutor(), getConnectionManager(),
                getConnectionReuseStrategy(), getConnectionKeepAliveStrategy(), getRoutePlanner(),
                getProtocolProcessor(), getHttpRequestRetryHandler(), getRedirectStrategy(),
                getTargetAuthenticationStrategy(), getProxyAuthenticationStrategy(), getUserTokenHandler(),
                params);
        routePlanner = getRoutePlanner();
        connectionBackoffStrategy = getConnectionBackoffStrategy();
        backoffManager = getBackoffManager();
    }

    try {
        if (connectionBackoffStrategy != null && backoffManager != null) {
            final HttpHost targetForRoute = (target != null) ? target
                    : (HttpHost) determineParams(request).getParameter(ClientPNames.DEFAULT_HOST);
            final HttpRoute route = routePlanner.determineRoute(targetForRoute, request, execContext);

            final CloseableHttpResponse out;
            try {
                out = CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext));
            } catch (final RuntimeException re) {
                if (connectionBackoffStrategy.shouldBackoff(re)) {
                    backoffManager.backOff(route);
                }
                throw re;
            } catch (final Exception e) {
                if (connectionBackoffStrategy.shouldBackoff(e)) {
                    backoffManager.backOff(route);
                }
                if (e instanceof HttpException) {
                    throw (HttpException) e;
                }
                if (e instanceof IOException) {
                    throw (IOException) e;
                }
                throw new UndeclaredThrowableException(e);
            }
            if (connectionBackoffStrategy.shouldBackoff(out)) {
                backoffManager.backOff(route);
            } else {
                backoffManager.probe(route);
            }
            return out;
        } else {
            return CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext));
        }
    } catch (final HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}