List of usage examples for org.apache.http.conn.routing HttpRoute HttpRoute
public HttpRoute(final HttpHost target, final InetAddress local, final HttpHost proxy, final boolean secure)
From source file:httpclient.conn.ManagerConnectProxy.java
/** * Main entry point to this example.// w ww .j ava 2 s . c o m * * @param args ignored */ public final static void main(String[] args) throws Exception { // make sure to use a proxy that supports CONNECT final HttpHost target = new HttpHost("issues.apache.org", 443, "https"); final HttpHost proxy = new HttpHost("127.0.0.1", 8666, "http"); setup(); // some general setup ClientConnectionManager clcm = createManager(); HttpRequest req = createRequest(target); HttpContext ctx = createContext(); System.out.println("preparing route to " + target + " via " + proxy); HttpRoute route = new HttpRoute(target, null, proxy, supportedSchemes.getScheme(target).isLayered()); System.out.println("requesting connection for " + route); ClientConnectionRequest connRequest = clcm.requestConnection(route, null); ManagedClientConnection conn = connRequest.getConnection(0, null); try { System.out.println("opening connection"); conn.open(route, ctx, getParams()); HttpRequest connect = createConnect(target); System.out.println("opening tunnel to " + target); conn.sendRequestHeader(connect); // there is no request entity conn.flush(); System.out.println("receiving confirmation for tunnel"); HttpResponse connected = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); printResponseHeader(connected); System.out.println("----------------------------------------"); int status = connected.getStatusLine().getStatusCode(); if ((status < 200) || (status > 299)) { System.out.println("unexpected status code " + status); System.exit(1); } System.out.println("receiving response body (ignored)"); conn.receiveResponseEntity(connected); conn.tunnelTarget(false, getParams()); System.out.println("layering secure connection"); conn.layerProtocol(ctx, getParams()); // finally we have the secure connection and can send the request System.out.println("sending request"); conn.sendRequestHeader(req); // there is no request entity conn.flush(); System.out.println("receiving response header"); HttpResponse rsp = conn.receiveResponseHeader(); System.out.println("----------------------------------------"); printResponseHeader(rsp); System.out.println("----------------------------------------"); System.out.println("closing connection"); conn.close(); } finally { if (conn.isOpen()) { System.out.println("shutting down connection"); try { conn.shutdown(); } catch (Exception x) { System.out.println("problem during shutdown"); x.printStackTrace(System.out); } } System.out.println("releasing connection"); clcm.releaseConnection(conn, -1, null); } }
From source file:com.puppetlabs.geppetto.injectable.eclipse.impl.ProxiedRoutePlanner.java
@Override public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException { if (request == null) { throw new IllegalStateException("Request must not be null."); }//from w w w.j a va2 s . c o m // If we have a forced route, we can do without a target. HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams()); if (route != null) return route; // If we get here, there is no forced route. // So we need a target to compute a route. if (target == null) { throw new IllegalStateException("Target host must not be null."); } final InetAddress local = ConnRouteParams.getLocalAddress(request.getParams()); final HttpHost proxy = ConnRouteParams.getDefaultProxy(request.getParams()); final Scheme schm; try { schm = schemeRegistry.getScheme(target.getSchemeName()); } catch (IllegalStateException ex) { throw new HttpException(ex.getMessage()); } // as it is typically used for TLS/SSL, we assume that // a layered scheme implies a secure connection final boolean secure = schm.isLayered(); if (proxy != null) return new HttpRoute(target, local, proxy, secure); IProxyData[] select = Activator.getInstance().getProxyService().select(URI.create(target.toURI())); for (IProxyData proxyData : select) if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) { HttpHost proxyHost = new HttpHost(proxyData.getHost(), proxyData.getPort()); return new HttpRoute(target, null, proxyHost, secure); } return new HttpRoute(target, local, secure); }
From source file:org.wso2.carbon.http2.transport.Http2TransportSender.java
/** * Handels requests come from Axis2 Engine * * @param msgCtx/*from ww w . j a va 2 s . c o m*/ * @param targetEPR * @param trpOut * @throws AxisFault */ public void sendMessage(MessageContext msgCtx, String targetEPR, OutTransportInfo trpOut) throws AxisFault { try { if (targetEPR.toLowerCase().contains("http2://")) { targetEPR = targetEPR.replaceFirst("http2://", "http://"); } else if (targetEPR.toLowerCase().contains("https2://")) { targetEPR = targetEPR.replaceFirst("https2://", "https://"); } URI uri = new URI(targetEPR); String scheme = uri.getScheme() != null ? uri.getScheme() : "http"; String hostname = uri.getHost(); int port = uri.getPort(); if (port == -1) { // use default if ("http".equals(scheme)) { port = 80; } else if ("https".equals(scheme)) { port = 443; } } HttpHost target = new HttpHost(hostname, port, scheme); boolean secure = "https".equals(target.getSchemeName()); HttpHost proxy = proxyConfig.selectProxy(target); msgCtx.setProperty(PassThroughConstants.PROXY_PROFILE_TARGET_HOST, target.getHostName()); HttpRoute route; if (proxy != null) { route = new HttpRoute(target, null, proxy, secure); } else { route = new HttpRoute(target, null, secure); } Http2TargetRequestUtil util = new Http2TargetRequestUtil(targetConfiguration, route); msgCtx.setProperty(Http2Constants.PASSTHROUGH_TARGET, util); if (msgCtx.getProperty(PassThroughConstants.PASS_THROUGH_PIPE) == null) { Pipe pipe = new Pipe(targetConfiguration.getBufferFactory().getBuffer(), "Test", targetConfiguration); msgCtx.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe); msgCtx.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE); } Http2ClientHandler clientHandler = connectionFactory.getChannelHandler(target); String tenantDomain = (msgCtx.getProperty(MultitenantConstants.TENANT_DOMAIN) == null) ? null : (String) msgCtx.getProperty(MultitenantConstants.TENANT_DOMAIN); String dispatchSequence = (msgCtx.getProperty(Http2Constants.HTTP2_DISPATCH_SEQUENCE) == null) ? null : (String) msgCtx.getProperty(Http2Constants.HTTP2_DISPATCH_SEQUENCE); String errorSequence = (msgCtx.getProperty(Http2Constants.HTTP2_ERROR_SEQUENCE) == null) ? null : (String) msgCtx.getProperty(Http2Constants.HTTP2_ERROR_SEQUENCE); InboundResponseSender responseSender = (msgCtx .getProperty(InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER) == null) ? null : (InboundResponseSender) msgCtx .getProperty(InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER); boolean serverPushEnabled = (msgCtx .getProperty(Http2Constants.HTTP2_PUSH_PROMISE_REQEUST_ENABLED) == null) ? false : (boolean) msgCtx.getProperty(Http2Constants.HTTP2_PUSH_PROMISE_REQEUST_ENABLED); clientHandler.setResponseReceiver(tenantDomain, dispatchSequence, errorSequence, responseSender, targetConfiguration, serverPushEnabled); clientHandler.channelWrite(msgCtx); } catch (URISyntaxException e) { log.error("Error parsing the http2 endpoint url"); throw new AxisFault(e.getMessage()); } }
From source file:com.alu.e3.gateway.loadbalancer.E3HttpClientConfigurer.java
/** * Converts a TargetHost to an HttpRoute * @param targetHost the target host to transform * @return HttpRoute HTTP route// w w w . j a v a 2s .c om * @throws MalformedURLException */ private HttpRoute getHttpRoute(TargetHost targetHost) throws MalformedURLException { HttpRoute route = null; URL url = new URL(targetHost.getUrl()); boolean secured = url.getProtocol().equalsIgnoreCase(HTTP_SECURED_PROTOCOL); if (forwardProxy != null) { Integer proxyPort = Integer.parseInt(forwardProxy.getProxyPort()); String proxyHost = forwardProxy.getProxyHost(); route = new HttpRoute(new HttpHost(url.getHost(), url.getPort()), null, new HttpHost(proxyHost, proxyPort), secured); } else { route = new HttpRoute(new HttpHost(url.getHost(), url.getPort()), null, secured); } return route; }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@Test @SuppressWarnings("deprecation") public void testConnectionManagerAndProxy() throws Exception { HttpParams params = new BasicHttpParams(); Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80); SchemeRegistry sr = new SchemeRegistry(); sr.register(http);/*from w ww.j a v a 2 s .c o m*/ ClientConnectionManager cm = new ThreadSafeClientConnManager(params, sr); DefaultHttpClient httpClient = new DefaultHttpClient(cm, params); // ? HTTP? String proxyHost = "proxy.wdf.sap.corp"; int proxyPort = 8080; httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials("", "")); HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy); // ? HTTP? HttpRoutePlanner routePlanner = new HttpRoutePlanner() { public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException { return new HttpRoute(target, null, new HttpHost("proxy.wdf.sap.corp", 8080), true); } }; // httpClient.setRoutePlanner(routePlanner); String[] urisToGet = { "http://119.29.234.42/", // "http://119.29.234.42/solr", // "http://119.29.234.42/jenkins", // "http://119.29.234.42/jenkins/manage", // "http://119.29.234.42/jenkins/credential-store", // }; // ?URI GetThread[] threads = new GetThread[urisToGet.length]; for (int i = 0; i < threads.length; i++) { HttpGet httpget = new HttpGet(urisToGet[i]); threads[i] = new GetThread(httpClient, httpget); } // for (int j = 0; j < threads.length; j++) { threads[j].start(); } // ? for (int j = 0; j < threads.length; j++) { threads[j].join(); } cm.closeIdleConnections(40, TimeUnit.SECONDS); cm.closeExpiredConnections(); }
From source file:org.apache.http.impl.conn.TestPoolingHttpClientConnectionManager.java
@Test public void testProxyConnectAndUpgrade() throws Exception { final HttpHost target = new HttpHost("somehost", -1, "https"); final HttpHost proxy = new HttpHost("someproxy", 8080); final InetAddress remote = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 }); final InetAddress local = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }); final HttpRoute route = new HttpRoute(target, local, proxy, true); final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS); entry.markRouteComplete();//from w ww .ja va 2 s .c o m Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE); Mockito.when(conn.isOpen()).thenReturn(true); Mockito.when(future.isCancelled()).thenReturn(false); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(conn1); final ConnectionSocketFactory plainsf = Mockito.mock(ConnectionSocketFactory.class); final LayeredConnectionSocketFactory sslsf = Mockito.mock(LayeredConnectionSocketFactory.class); final Socket socket = Mockito.mock(Socket.class); final HttpClientContext context = HttpClientContext.create(); final SocketConfig sconfig = SocketConfig.custom().build(); final ConnectionConfig cconfig = ConnectionConfig.custom().build(); mgr.setDefaultSocketConfig(sconfig); mgr.setDefaultConnectionConfig(cconfig); Mockito.when(dnsResolver.resolve("someproxy")).thenReturn(new InetAddress[] { remote }); Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8080); Mockito.when(schemePortResolver.resolve(target)).thenReturn(8443); Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainsf); Mockito.when(socketFactoryRegistry.lookup("https")).thenReturn(sslsf); Mockito.when(plainsf.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(plainsf.connectSocket(Mockito.anyInt(), Mockito.eq(socket), Mockito.<HttpHost>any(), Mockito.<InetSocketAddress>any(), Mockito.<InetSocketAddress>any(), Mockito.<HttpContext>any())) .thenReturn(socket); mgr.connect(conn1, route, 123, context); Mockito.verify(dnsResolver, Mockito.times(1)).resolve("someproxy"); Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(proxy); Mockito.verify(plainsf, Mockito.times(1)).createSocket(context); Mockito.verify(plainsf, Mockito.times(1)).connectSocket(123, socket, proxy, new InetSocketAddress(remote, 8080), new InetSocketAddress(local, 0), context); Mockito.when(conn.getSocket()).thenReturn(socket); mgr.upgrade(conn1, route, context); Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(target); Mockito.verify(sslsf, Mockito.times(1)).createLayeredSocket(socket, "somehost", 8443, context); mgr.routeComplete(conn1, route, context); }
From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java
@Test public void testInternalConnFactoryCreateViaProxy() throws Exception { final ConfigData configData = new ConfigData(); final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory(configData, connFactory);/*from w w w . j ava 2s . c o m*/ final HttpHost target = new HttpHost("somehost", 80); final HttpHost proxy = new HttpHost("someproxy", 8888); final HttpRoute route = new HttpRoute(target, null, proxy, false); final ConnectionConfig config = ConnectionConfig.custom().build(); configData.setConnectionConfig(proxy, config); internalConnFactory.create(route, iosession); Mockito.verify(connFactory).create(iosession, config); }
From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java
@Test public void testResolveRemoteAddressViaProxy() throws Exception { final InternalAddressResolver addressResolver = new InternalAddressResolver(schemePortResolver, dnsResolver);//from www. j a v a2s . com final HttpHost target = new HttpHost("somehost", 80); final HttpHost proxy = new HttpHost("someproxy"); final HttpRoute route = new HttpRoute(target, null, proxy, false); Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8888); final byte[] ip = new byte[] { 10, 0, 0, 10 }; Mockito.when(dnsResolver.resolve("someproxy")) .thenReturn(new InetAddress[] { InetAddress.getByAddress(ip) }); final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveRemoteAddress(route); Assert.assertNotNull(address); Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress()); Assert.assertEquals(8888, address.getPort()); }