Java Code Examples for org.apache.http.HttpHost#getPort()

The following examples show how to use org.apache.http.HttpHost#getPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Elasticsearch6Configuration.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Parse Hosts String to list.
 *
 * <p>Hosts String format was given as following:
 *
 * <pre>
 *     connector.hosts = http://host_name:9092;http://host_name:9093
 * </pre>
 */
private static HttpHost validateAndParseHostsString(String host) {
	try {
		HttpHost httpHost = HttpHost.create(host);
		if (httpHost.getPort() < 0) {
			throw new ValidationException(String.format(
				"Could not parse host '%s' in option '%s'. It should follow the format 'http://host_name:port'. Missing port.",
				host,
				HOSTS_OPTION.key()));
		}

		if (httpHost.getSchemeName() == null) {
			throw new ValidationException(String.format(
				"Could not parse host '%s' in option '%s'. It should follow the format 'http://host_name:port'. Missing scheme.",
				host,
				HOSTS_OPTION.key()));
		}
		return httpHost;
	} catch (Exception e) {
		throw new ValidationException(String.format(
			"Could not parse host '%s' in option '%s'. It should follow the format 'http://host_name:port'.",
			host,
			HOSTS_OPTION.key()), e);
	}
}
 
Example 2
Source File: HttpRequestHelper.java    From esigate with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the target host as defined in the Host header or extracted from the request URI.
 * 
 * Usefull to generate Host header in a HttpRequest
 * 
 * @param request
 * @return the host formatted as host:port
 */
public static HttpHost getHost(HttpRequest request) {
    HttpHost httpHost = UriUtils.extractHost(request.getRequestLine().getUri());
    String scheme = httpHost.getSchemeName();
    String host = httpHost.getHostName();
    int port = httpHost.getPort();
    Header[] headers = request.getHeaders(HttpHeaders.HOST);
    if (headers != null && headers.length != 0) {
        String headerValue = headers[0].getValue();
        String[] splitted = headerValue.split(":");
        host = splitted[0];
        if (splitted.length > 1) {
            port = Integer.parseInt(splitted[1]);
        } else {
            port = -1;
        }
    }
    return new HttpHost(host, port, scheme);
}
 
Example 3
Source File: Elasticsearch7Configuration.java    From flink with Apache License 2.0 6 votes vote down vote up
private static HttpHost validateAndParseHostsString(String host) {
	try {
		HttpHost httpHost = HttpHost.create(host);
		if (httpHost.getPort() < 0) {
			throw new ValidationException(String.format(
				"Could not parse host '%s' in option '%s'. It should follow the format 'http://host_name:port'. Missing port.",
				host,
				HOSTS_OPTION.key()));
		}

		if (httpHost.getSchemeName() == null) {
			throw new ValidationException(String.format(
				"Could not parse host '%s' in option '%s'. It should follow the format 'http://host_name:port'. Missing scheme.",
				host,
				HOSTS_OPTION.key()));
		}
		return httpHost;
	} catch (Exception e) {
		throw new ValidationException(String.format(
			"Could not parse host '%s' in option '%s'. It should follow the format 'http://host_name:port'.",
			host,
			HOSTS_OPTION.key()), e);
	}
}
 
Example 4
Source File: WebRealmServiceTest.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
@Override
public void process( final HttpRequest request, final HttpContext context )
    throws HttpException, IOException
{
    AuthState authState = (AuthState) context.getAttribute( ClientContext.TARGET_AUTH_STATE );
    CredentialsProvider credsProvider = (CredentialsProvider) context
        .getAttribute( ClientContext.CREDS_PROVIDER );
    HttpHost targetHost = (HttpHost) context.getAttribute( ExecutionContext.HTTP_TARGET_HOST );

    // If not auth scheme has been initialized yet
    if( authState.getAuthScheme() == null )
    {
        AuthScope authScope = new AuthScope( targetHost.getHostName(),
                                             targetHost.getPort() );
        // Obtain credentials matching the target host
        Credentials creds = credsProvider.getCredentials( authScope );
        // If found, generate BasicScheme preemptively
        if( creds != null )
        {
            authState.setAuthScheme( new BasicScheme() );
            authState.setCredentials( creds );
        }
    }
}
 
Example 5
Source File: ApiProxyServlet.java    From onboard with Apache License 2.0 5 votes vote down vote up
/** Copy request headers from the servlet client to the proxy request. */
protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) {
    // Get an Enumeration of all of the header names sent by the client
    Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames();
    while (enumerationOfHeaderNames.hasMoreElements()) {
        String headerName = (String) enumerationOfHeaderNames.nextElement();
        // Instead the content-length is effectively set via InputStreamEntity
        if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH))
            continue;
        if (hopByHopHeaders.containsHeader(headerName))
            continue;

        Enumeration headers = servletRequest.getHeaders(headerName);
        while (headers.hasMoreElements()) {// sometimes more than one value
            String headerValue = (String) headers.nextElement();
            // In case the proxy host is running multiple virtual servers,
            // rewrite the Host header to ensure that we get content from
            // the correct virtual server
            if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) {
                HttpHost host = getTargetHost(servletRequest);
                headerValue = host.getHostName();
                if (host.getPort() != -1)
                    headerValue += ":" + host.getPort();
            } else if (headerName.equalsIgnoreCase(org.apache.http.cookie.SM.COOKIE)) {
                headerValue = getRealCookie(headerValue);
            }
            proxyRequest.addHeader(headerName, headerValue);
        }
    }
}
 
Example 6
Source File: RemoteSystemClient.java    From ghwatch with Apache License 2.0 5 votes vote down vote up
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
  AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
  CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
  HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

  if (authState.getAuthScheme() == null) {
    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
    Credentials creds = credsProvider.getCredentials(authScope);
    if (creds != null) {
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }
  }
}
 
Example 7
Source File: DispatchService.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void build() throws URISyntaxException {

        URI uri = MDSInterface2.getRoot(this);
        mHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        AuthScope authScope = new AuthScope(mHost.getHostName(), mHost.getPort());
        Credentials creds = new UsernamePasswordCredentials("username", "password");
        credsProvider.setCredentials(authScope, creds);

        mClient = new DefaultHttpClient();
        ((AbstractHttpClient) mClient).getCredentialsProvider().setCredentials(
                authScope, creds);
    }
 
Example 8
Source File: HttpAsyncRequestExecutorInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    HttpContext context = CONTEXT_LOCAL.get();
    CONTEXT_LOCAL.remove();
    if (context == null) {
        return;
    }
    final ContextCarrier contextCarrier = new ContextCarrier();
    HttpRequestWrapper requestWrapper = (HttpRequestWrapper) context.getAttribute(HttpClientContext.HTTP_REQUEST);
    HttpHost httpHost = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST);

    RequestLine requestLine = requestWrapper.getRequestLine();
    String uri = requestLine.getUri();
    String operationName = uri.startsWith("http") ? new URL(uri).getPath() : uri;
    int port = httpHost.getPort();
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, httpHost.getHostName() + ":" + (port == -1 ? 80 : port));
    span.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
    Tags.URL.set(span, requestWrapper.getOriginal().getRequestLine().getUri());
    Tags.HTTP.METHOD.set(span, requestLine.getMethod());
    SpanLayer.asHttp(span);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        requestWrapper.setHeader(next.getHeadKey(), next.getHeadValue());
    }
}
 
Example 9
Source File: PreemtiveAuthorizationHttpRequestInterceptor.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
            ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
 
Example 10
Source File: PreemtiveAuthorizationHttpRequestInterceptor.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
            ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
 
Example 11
Source File: PreemtiveAuthorizationHttpRequestInterceptor.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
            ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
 
Example 12
Source File: CommonsDataLoader.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Define the Credentials
 *
 * @param httpClientBuilder
 * @param url
 * @return {@link HttpClientBuilder}
 */
private HttpClientBuilder configCredentials(HttpClientBuilder httpClientBuilder, final String url) {

	final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
	for (final Map.Entry<HttpHost, UsernamePasswordCredentials> entry : authenticationMap.entrySet()) {

		final HttpHost httpHost = entry.getKey();
		final UsernamePasswordCredentials usernamePasswordCredentials = entry.getValue();
		final AuthScope authscope = new AuthScope(httpHost.getHostName(), httpHost.getPort());
		credentialsProvider.setCredentials(authscope, usernamePasswordCredentials);
	}
	httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
	httpClientBuilder = configureProxy(httpClientBuilder, credentialsProvider, url);
	return httpClientBuilder;
}
 
Example 13
Source File: PreemtiveAuthorizationHttpRequestInterceptor.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
            ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }
    }
}
 
Example 14
Source File: DefaultClientExchangeHandlerImplStartMethodInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private NameIntValuePair<String> getHost(final Object target) {
    if (target instanceof RequestProducerGetter) {
        final HttpAsyncRequestProducer producer = ((RequestProducerGetter) target)._$PINPOINT$_getRequestProducer();
        final HttpHost httpHost = producer.getTarget();
        if (httpHost != null) {
            return new NameIntValuePair<String>(httpHost.getHostName(), httpHost.getPort());
        }
    }
    return new NameIntValuePair<String>(null, -1);
}
 
Example 15
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the CONNECT request for tunnelling.
 * Called by {@link #createTunnelToTarget createTunnelToTarget}.
 *
 * @param route     the route to establish
 * @param context   the context for request execution
 *
 * @return  the CONNECT request for tunnelling
 */
protected HttpRequest createConnectRequest(HttpRoute route,
                                           HttpContext context) {
    // see RFC 2817, section 5.2 and
    // INTERNET-DRAFT: Tunneling TCP based protocols through
    // Web proxy servers

    HttpHost target = route.getTargetHost();

    String host = target.getHostName();
    int port = target.getPort();
    if (port < 0) {
        Scheme scheme = connManager.getSchemeRegistry().
            getScheme(target.getSchemeName());
        port = scheme.getDefaultPort();
    }

    StringBuilder buffer = new StringBuilder(host.length() + 6);
    buffer.append(host);
    buffer.append(':');
    buffer.append(Integer.toString(port));

    String authority = buffer.toString();
    ProtocolVersion ver = HttpProtocolParams.getVersion(params);
    HttpRequest req = new BasicHttpRequest("CONNECT", authority, ver);

    return req;
}
 
Example 16
Source File: HTTPAuthUtil.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static public AuthScope hostToAuthScope(HttpHost host) {
  return new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
}
 
Example 17
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 4 votes vote down vote up
private void updateAuthState(
        final AuthState authState,
        final HttpHost host,
        final CredentialsProvider credsProvider) {

    if (!authState.isValid()) {
        return;
    }

    String hostname = host.getHostName();
    int port = host.getPort();
    if (port < 0) {
        Scheme scheme = connManager.getSchemeRegistry().getScheme(host);
        port = scheme.getDefaultPort();
    }

    AuthScheme authScheme = authState.getAuthScheme();
    AuthScope authScope = new AuthScope(
            hostname,
            port,
            authScheme.getRealm(),
            authScheme.getSchemeName());

    if (DEBUG) {
    	Logger.debug("Authentication scope: {}", authScope);
    }
    Credentials creds = authState.getCredentials();
    if (creds == null) {
        creds = credsProvider.getCredentials(authScope);
        if (DEBUG) {
            if (creds != null) {
            	Logger.debug("Found credentials");
            } else {
            	Logger.debug("Credentials not found");
            }
        }
    } else {
        if (authScheme.isComplete()) {
        	if (DEBUG) {
        		Logger.debug("Authentication failed");
        	}
            creds = null;
        }
    }
    authState.setAuthScope(authScope);
    authState.setCredentials(creds);
}
 
Example 18
Source File: TestTwitterSocket.java    From albert with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	  OAuthConsumer consumer = new CommonsHttpOAuthConsumer(
				Constants.ConsumerKey,
				Constants.ConsumerSecret);
	  consumer.setTokenWithSecret(Constants.AccessToken, Constants.AccessSecret);
	  
	  
    HttpParams params = new BasicHttpParams();
    // HTTP 协议的版本,1.1/1.0/0.9
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    // 字符集
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    // 伪装的浏览器类型
    // IE7 是 
    // Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)
    //
    // Firefox3.03
    // Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
    //
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, true);
    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());
    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
    HttpContext context = new BasicHttpContext(null);
    HttpHost host = new HttpHost("127.0.0.1", 1080);
    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
    try {
      String[] targets = { "https://www.twitter.com"};
      for (int i = 0; i < targets.length; i++) {
        if (!conn.isOpen()) {
          Socket socket = new Socket(host.getHostName(), host.getPort());
          conn.bind(socket, params);
        }
        
	  
        BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
//        consumer.sign(request);
        
        System.out.println(">> Request URI: " + request.getRequestLine().getUri());
        context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
        request.setParams(params);
        httpexecutor.preProcess(request, httpproc, context);
        HttpResponse response = httpexecutor.execute(request, conn, context);
        response.setParams(params);
        httpexecutor.postProcess(response, httpproc, context);
        // 返回码
        System.out.println("<< Response: " + response.getStatusLine());
        // 返回的文件头信息
//        Header[] hs = response.getAllHeaders();
//        for (Header h : hs) {
//          System.out.println(h.getName() + ":" + h.getValue());
//        }
        // 输出主体信息
//        System.out.println(EntityUtils.toString(response.getEntity()));
        
        HttpEntity entry = response.getEntity();
        StringBuffer sb = new StringBuffer();
  	  if(entry != null)
  	  {
  	    InputStreamReader is = new InputStreamReader(entry.getContent());
  	    BufferedReader br = new BufferedReader(is);
  	    String str = null;
  	    while((str = br.readLine()) != null)
  	    {
  	     sb.append(str.trim());
  	    }
  	    br.close();
  	  }
  	  System.out.println(sb.toString());
  	  
        System.out.println("==============");
        if (!connStrategy.keepAlive(response, context)) {
          conn.close();
        } else {
          System.out.println("Connection kept alive...");
        }
      }
    } finally {
      conn.close();
    }
  }
 
Example 19
Source File: HtmlUnitSSLConnectionSocketFactory.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Connect via socket.
 * @param connectTimeout the timeout
 * @param socket the socket
 * @param host the host
 * @param remoteAddress the remote address
 * @param localAddress the local address
 * @param context the context
 * @return the created/connected socket
 * @throws IOException in case of problems
 */
@Override
public Socket connectSocket(
        final int connectTimeout,
        final Socket socket,
        final HttpHost host,
        final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress,
        final HttpContext context) throws IOException {
    final HttpHost socksProxy = SocksConnectionSocketFactory.getSocksProxy(context);
    if (socksProxy != null) {
        final Socket underlying = SocksConnectionSocketFactory.createSocketWithSocksProxy(socksProxy);
        underlying.setReuseAddress(true);

        final SocketAddress socksProxyAddress = new InetSocketAddress(socksProxy.getHostName(),
                socksProxy.getPort());
        try {
            //underlying.setSoTimeout(soTimeout);
            underlying.connect(remoteAddress, connectTimeout);
        }
        catch (final SocketTimeoutException ex) {
            throw new ConnectTimeoutException("Connect to " + socksProxyAddress + " timed out");
        }

        final Socket sslSocket = getSSLSocketFactory().createSocket(underlying, socksProxy.getHostName(),
                socksProxy.getPort(), true);
        configureSocket((SSLSocket) sslSocket, context);
        return sslSocket;
    }
    try {
        return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
    }
    catch (final IOException e) {
        if (useInsecureSSL_ && "handshake alert:  unrecognized_name".equals(e.getMessage())) {
            setEmptyHostname(host);

            return super.connectSocket(connectTimeout,
                    createSocket(context),
                    host, remoteAddress, localAddress, context);
        }
        throw e;
    }
}
 
Example 20
Source File: SocksConnectionSocketFactory.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
static Socket createSocketWithSocksProxy(final HttpHost socksProxy) {
    final InetSocketAddress address = new InetSocketAddress(socksProxy.getHostName(), socksProxy.getPort());
    final Proxy proxy = new Proxy(Proxy.Type.SOCKS, address);
    return new Socket(proxy);
}