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

The following examples show how to use org.apache.http.HttpHost#getSchemeName() . 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: HttpContextUtils.java    From micrometer with Apache License 2.0 6 votes vote down vote up
static Tags generateTagsForRoute(HttpContext context) {
    String targetScheme = "UNKNOWN";
    String targetHost = "UNKNOWN";
    String targetPort = "UNKNOWN";
    Object routeAttribute = context.getAttribute("http.route");
    if (routeAttribute instanceof HttpRoute) {
        HttpHost host = ((HttpRoute) routeAttribute).getTargetHost();
        targetScheme = host.getSchemeName();
        targetHost = host.getHostName();
        targetPort = String.valueOf(host.getPort());
    }
    return Tags.of(
            "target.scheme", targetScheme,
            "target.host", targetHost,
            "target.port", targetPort
    );
}
 
Example 2
Source File: ExtendedConnectionOperator.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@Override
public void upgrade(ManagedHttpClientConnection connection, HttpHost host, HttpContext context) throws IOException {
  ConnectionSocketFactory socketFactory = getSocketFactory(host, HttpClientContext.adapt(context));

  if (!(socketFactory instanceof LayeredConnectionSocketFactory)) {
    throw new UnsupportedSchemeException(host.getSchemeName() +
        " protocol does not support connection upgrade");
  }

  LayeredConnectionSocketFactory layeredFactory = (LayeredConnectionSocketFactory) socketFactory;

  Socket socket = connection.getSocket();
  int port = this.schemePortResolver.resolve(host);
  socket = layeredFactory.createLayeredSocket(socket, host.getHostName(), port, context);

  connection.bind(socket);
}
 
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: 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 5
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 6
Source File: TestUtils.java    From esigate with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a mock {@link IncomingRequest}.
 * 
 * @param uri
 *            the uri
 * @return the {@link IncomingRequest}
 */
public static IncomingRequest.Builder createIncomingRequest(String uri) {
    HttpHost httpHost = UriUtils.extractHost(uri);
    String scheme = httpHost.getSchemeName();
    String host = httpHost.getHostName();
    int port = httpHost.getPort();
    RequestLine requestLine = new BasicRequestLine("GET", uri, HttpVersion.HTTP_1_1);
    IncomingRequest.Builder builder = IncomingRequest.builder(requestLine);
    builder.setContext(new ContainerRequestContext() {
    });
    // Remove default ports
    if (port == -1 || (port == Http.DEFAULT_HTTP_PORT && "http".equals(scheme))
            || (port == Http.DEFAULT_HTTPS_PORT && "https".equals(scheme))) {
        builder.addHeader("Host", host);
    } else {
        builder.addHeader("Host", host + ":" + port);
    }
    builder.setSession(new MockSession());
    return builder;
}
 
Example 7
Source File: Substitute_RestClient.java    From quarkus with Apache License 2.0 5 votes vote down vote up
protected HttpHost getKey(final HttpHost host) {
    if (host.getPort() <= 0) {
        final int port;
        try {
            port = schemePortResolver.resolve(host);
        } catch (final UnsupportedSchemeException ignore) {
            return host;
        }
        return new HttpHost(host.getHostName(), port, host.getSchemeName());
    } else {
        return host;
    }
}
 
Example 8
Source File: ExtendedConnectionOperator.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private ConnectionSocketFactory getSocketFactory(HttpHost host, HttpContext context) throws IOException {
  Lookup<ConnectionSocketFactory> registry = getSocketFactoryRegistry(context);
  ConnectionSocketFactory socketFactory = registry.lookup(host.getSchemeName());

  if (socketFactory == null) {
    throw new UnsupportedSchemeException(host.getSchemeName() + " protocol is not supported");
  }

  return socketFactory;
}
 
Example 9
Source File: AbstractRoutePlanner.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {
  Args.notNull(request, "Request");
  if (host == null) {
    throw new ProtocolException("Target host is not specified");
  }
  final HttpClientContext clientContext = HttpClientContext.adapt(context);
  final RequestConfig config = clientContext.getRequestConfig();
  int remotePort;
  if (host.getPort() <= 0) {
    try {
      remotePort = schemePortResolver.resolve(host);
    } catch (final UnsupportedSchemeException e) {
      throw new HttpException(e.getMessage());
    }
  } else
    remotePort = host.getPort();

  final Tuple<Inet4Address, Inet6Address> remoteAddresses = IpAddressTools.getRandomAddressesFromHost(host);
  final Tuple<InetAddress, InetAddress> addresses = determineAddressPair(remoteAddresses);

  final HttpHost target = new HttpHost(addresses.r, host.getHostName(), remotePort, host.getSchemeName());
  final HttpHost proxy = config.getProxy();
  final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
  clientContext.setAttribute(CHOSEN_IP_ATTRIBUTE, addresses.l);
  log.debug("Setting route context attribute to {}", addresses.l);
  if (proxy == null) {
    return new HttpRoute(target, addresses.l, secure);
  } else {
    return new HttpRoute(target, addresses.l, proxy, secure);
  }
}
 
Example 10
Source File: VespaHttpClientBuilder.java    From vespa with Apache License 2.0 5 votes vote down vote up
private HttpHost resolveTarget(HttpHost host) throws HttpException {
    try {
        String originalScheme = host.getSchemeName();
        String scheme = originalScheme.equalsIgnoreCase("http") ? "https" : originalScheme;
        int port = DefaultSchemePortResolver.INSTANCE.resolve(host);
        return new HttpHost(host.getHostName(), port, scheme);
    } catch (UnsupportedSchemeException e) {
        throw new HttpException(e.getMessage(), e);
    }
}