Java Code Examples for org.apache.http.client.utils.URIBuilder#setHost()

The following examples show how to use org.apache.http.client.utils.URIBuilder#setHost() . 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: HttpSinkProvider.java    From ambari-metrics with Apache License 2.0 6 votes vote down vote up
public HttpSinkProvider() {
  Configuration config;
  try {
    config = conf.getMetricsConf();
  } catch (Exception e) {
    throw new ExceptionInInitializerError("Unable to read configuration for sink.");
  }
  String protocol = config.get("timeline.metrics.service.external.http.sink.protocol", "http");
  String host = config.get("timeline.metrics.service.external.http.sink.host", "localhost");
  String port = config.get("timeline.metrics.service.external.http.sink.port", "6189");

  if (protocol.contains("https")) {
    loadTruststore(
      config.getTrimmed("timeline.metrics.service.external.http.sink.truststore.path"),
      config.getTrimmed("timeline.metrics.service.external.http.sink.truststore.type"),
      config.getTrimmed("timeline.metrics.service.external.http.sink.truststore.password")
    );
  }

  URIBuilder uriBuilder = new URIBuilder();
  uriBuilder.setScheme(protocol);
  uriBuilder.setHost(host);
  uriBuilder.setPort(Integer.parseInt(port));
  connectUrl = uriBuilder.toString();
}
 
Example 2
Source File: ClientDnsOverwrite.java    From mxisd with GNU Affero General Public License v3.0 6 votes vote down vote up
public URIBuilder transform(URI initial) {
    URIBuilder builder = new URIBuilder(initial);
    Entry mapping = mappings.get(initial.getHost());
    if (mapping == null) {
        throw new InternalServerError("No DNS client override for " + initial.getHost());
    }

    try {
        URL target = new URL(mapping.getValue());
        builder.setScheme(target.getProtocol());
        builder.setHost(target.getHost());
        if (target.getPort() != -1) {
            builder.setPort(target.getPort());
        }

        return builder;
    } catch (MalformedURLException e) {
        log.warn("Skipping DNS overwrite entry {} due to invalid value [{}]: {}", mapping.getName(), mapping.getValue(), e.getMessage());
        throw new ConfigurationException("Invalid DNS overwrite entry in homeserver client: " + mapping.getName(), e.getMessage());
    }
}
 
Example 3
Source File: SearchTheMovieDbProxyResource.java    From movieapp-dialog with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the URI from the URI parameter hash
 * <p>
 * This will append each of the {key, value} pairs in uriParamsHash to the URI.
 * 
 * @param uriParamsHash the hashtable containing parameters to be added to the URI for making the call to TMDB
 * @return URI for making the HTTP call to TMDB API
 * @throws URISyntaxException if the uri being built is in the incorrect format
 */
private static URI buildUriStringFromParamsHash(Hashtable<String, String> uriParamsHash, String path) throws URISyntaxException {
    URIBuilder urib = new URIBuilder();
    urib.setScheme("http"); //$NON-NLS-1$
    urib.setHost(TMDB_BASE_URL);
    urib.setPath(path);
    urib.addParameter("api_key", themoviedbapikey); //$NON-NLS-1$
    if (uriParamsHash != null) {
        Set<String> keys = uriParamsHash.keySet();
        for (String key : keys) {
            urib.addParameter(key, uriParamsHash.get(key));
        }
    }
    return urib.build();
}
 
Example 4
Source File: DigitalOceanClient.java    From digitalocean-api-java with MIT License 5 votes vote down vote up
private URI createUri(ApiRequest request) {
  URIBuilder ub = new URIBuilder();
  ub.setScheme(HTTPS_SCHEME);
  ub.setHost(apiHost);
  ub.setPath(createPath(request));

  if (null != request.getPageNo()) {
    ub.setParameter(PARAM_PAGE_NO, request.getPageNo().toString());
  }

  if (RequestMethod.GET == request.getMethod()) {
    if (null == request.getPerPage()) {
      ub.setParameter(PARAM_PER_PAGE, String.valueOf(DEFAULT_PAGE_SIZE)); // As per DO
      // documentation
    } else {
      ub.setParameter(PARAM_PER_PAGE, request.getPerPage().toString());
    }
  }

  if (null != request.getQueryParams()) {
    for (Map.Entry<String, String> entry : request.getQueryParams().entrySet()) {
      ub.setParameter(entry.getKey(), entry.getValue());
    }
  }

  URI uri = null;
  try {
    uri = ub.build();
  } catch (URISyntaxException use) {
    log.error(use.getMessage(), use);
  }

  return uri;
}
 
Example 5
Source File: KieClient.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getBaseUrl() {
    URIBuilder uri = new URIBuilder();

    uri.setScheme(schema);
    uri.setHost(hostname);
    uri.setPort(port);
    return RequestHelper.appendPath(uri.toString(), webapp);
}
 
Example 6
Source File: Utilities.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
private static URIBuilder getUriBuilder(@NotNull String protocol, @NotNull String dcaHost, @NotNull String dcaPort) {
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setHost(dcaHost);
    uriBuilder.setPort(Integer.valueOf(dcaPort));
    uriBuilder.setScheme(protocol);
    return uriBuilder;
}
 
Example 7
Source File: HttpUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
public static URIBuilder getUriBuilder(@NotNull final String region) {
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setHost(IAAS + "." + region + "." + OCI_HOST);
    uriBuilder.setScheme(HTTPS);
    return uriBuilder;
}
 
Example 8
Source File: HttpUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
public static URIBuilder getUriBuilder(NutanixCommonInputs nutanixCommonInputs) {
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setHost(nutanixCommonInputs.getHostname());
    uriBuilder.setPort(Integer.parseInt(nutanixCommonInputs.getPort()));
    uriBuilder.setScheme(HTTPS);
    return uriBuilder;
}
 
Example 9
Source File: HttpUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
public static URIBuilder getUriBuilder(String url) {
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setHost(url);
    uriBuilder.setScheme(HTTPS);
    return uriBuilder;
}
 
Example 10
Source File: HttpUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
public static URIBuilder getUriBuilder() {
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setHost(GRAPH_HOST);
    uriBuilder.setScheme(HTTPS);
    return uriBuilder;
}
 
Example 11
Source File: HttpUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
public static URIBuilder getUriBuilder() {
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setHost(TERRAFORM_HOST);
    uriBuilder.setScheme(HTTPS);
    return uriBuilder;
}
 
Example 12
Source File: RiakHttpClient.java    From streams with Apache License 2.0 5 votes vote down vote up
public void start() throws Exception {
  Objects.nonNull(config);
  assert(config.getScheme().startsWith("http"));
  URIBuilder uriBuilder = new URIBuilder();
  uriBuilder.setScheme(config.getScheme());
  uriBuilder.setHost(config.getHosts().get(0));
  uriBuilder.setPort(config.getPort().intValue());
  baseURI = uriBuilder.build();
  client = HttpClients.createDefault();
}
 
Example 13
Source File: HTTPUtil.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Remove selected fields from a URI producing a new URI
 *
 * @param uri the uri to convert
 * @param excludes the parts to exclude
 * @return The new URI instance
 */
static URI uriExclude(final URI uri, URIPart... excludes) {
  URIBuilder urib = new URIBuilder();
  EnumSet<URIPart> set = EnumSet.of(excludes[0], excludes);
  for (URIPart part : URIPart.values()) {
    if (set.contains(part))
      continue;
    switch (part) {
      case SCHEME:
        urib.setScheme(uri.getScheme());
        break;
      case USERINFO:
        urib.setUserInfo(uri.getRawUserInfo());
        break;
      case HOST:
        urib.setHost(uri.getHost());
        break;
      case PORT:
        urib.setPort(uri.getPort());
        break;
      case PATH:
        urib.setPath(uri.getPath());
        break;
      case QUERY:
        urib.setCustomQuery(uri.getQuery());
        break;
      case FRAGMENT:
        urib.setFragment(uri.getFragment());
        break;
    }
  }
  try {
    return urib.build();
  } catch (URISyntaxException e) {
    throw new IllegalArgumentException(e.getMessage());
  }
}
 
Example 14
Source File: URIUtils.java    From elucidate-server with MIT License 5 votes vote down vote up
public static String buildBaseUrl(String baseScheme, String baseHost, int basePort, String basePath) {
    URIBuilder builder = new URIBuilder();
    builder.setScheme(baseScheme);
    builder.setHost(baseHost);

    if (!DEFAULT_PORTS.containsKey(baseScheme.toLowerCase()) || DEFAULT_PORTS.get(baseScheme) != basePort) {
        builder.setPort(basePort);
    }

    builder.setPath(basePath);
    return builder.toString();
}
 
Example 15
Source File: RequestBuilder.java    From snowflake-ingest-java with Apache License 2.0 5 votes vote down vote up
/**
 * Given a request UUID, construct a URIBuilder for the common parts
 * of any Ingest Service request
 *
 * @param requestId the UUID with which we want to label this request
 * @return a URI builder we can use to finish build the URI
 */
private URIBuilder makeBaseURI(UUID requestId)
{
  //We can't make a request with no id
  if (requestId == null)
  {
    LOGGER.error("RequestId is null!");
    throw new IllegalArgumentException();
  }

  //construct the builder object
  URIBuilder builder = new URIBuilder();

  //set the scheme
  builder.setScheme(scheme);

  //set the host name
  builder.setHost(host);

  //set the port name
  builder.setPort(port);

  //set the request id
  builder.setParameter(REQUEST_ID, requestId.toString());

  return builder;
}
 
Example 16
Source File: AbstractHttpSmsReceiver.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
/**
 * 创建URI
 *
 * @return
 */
public URI buildURIByConfig() throws URISyntaxException {
    URIBuilder builder = new URIBuilder();
    builder.setHost(config.getHost());
    builder.setPort(config.getPort());
    builder.setPath(config.getPath());
    builder.setScheme(config.getProtocol());
    builder.setCharset(Charset.forName(config.getCharset()));
    return builder.build();
}
 
Example 17
Source File: AbstractHttpSmsSender.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
/**
 * 创建URI
 *
 * @return
 */
public URI buildURIByConfig() throws URISyntaxException {
    URIBuilder builder = new URIBuilder();
    builder.setHost(config.getHost());
    builder.setPort(config.getPort());
    builder.setPath(config.getPath());
    builder.setScheme(config.getProtocol());
    builder.setCharset(Charset.forName(config.getCharset()));
    return builder.build();
}
 
Example 18
Source File: DataRemote.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
protected URI replacePath(String absPath) {
	URIBuilder ub = new URIBuilder();
	ub.setScheme(uri.getScheme());
	ub.setUserInfo(uri.getUserInfo());
	ub.setHost(uri.getHost());
	ub.setPort(uri.getPort());
	ub.setPath(absPath);
	try {
		return ub.build();
	} catch (URISyntaxException e) {
		throw new RuntimeException("Error building URI from: '" + uri + "' and '" + absPath + "'");
	}
}
 
Example 19
Source File: RemoteHttpService.java    From datawave with Apache License 2.0 5 votes vote down vote up
protected URIBuilder buildURI() throws TextParseException {
    final String host = serviceHost();
    final int port = servicePort();
    URIBuilder builder = new URIBuilder();
    builder.setScheme(serviceScheme());
    if (useSrvDns()) {
        List<LookupResult> results = dnsSrvResolver.resolve(host);
        if (results != null && !results.isEmpty()) {
            LookupResult result = results.get(0);
            builder.setHost(result.host());
            builder.setPort(result.port());
            // Consul sends the hostname back in its own namespace. Although the A record is included in the
            // "ADDITIONAL SECTION", Spotify SRV lookup doesn't translate, so we need to do the lookup manually.
            if (result.host().endsWith(".consul.")) {
                Record[] newResults = new Lookup(result.host(), Type.A, DClass.IN).run();
                if (newResults != null && newResults.length > 0) {
                    builder.setHost(newResults[0].rdataToString());
                } else {
                    throw new IllegalArgumentException("Unable to resolve service host " + host + " -> " + result.host() + " -> ???");
                }
            }
        } else {
            throw new IllegalArgumentException("Unable to resolve service host: " + host);
        }
    } else {
        builder.setHost(host);
        builder.setPort(port);
    }
    return builder;
}
 
Example 20
Source File: ComponentContainer.java    From Selenium-Foundation with Apache License 2.0 4 votes vote down vote up
/**
 * Get the URL defined by the specified {@link PageUrl} annotation.
 * <p>
 * <b>NOTES</b>: <ul>
 *     <li>If the {@code pageUrl} argument is {@code null} or the {@code value} element of the specified
 *         {@link PageUrl} annotation is unspecified, this method returns {@code null}.
 *     <li>If {@code scheme} of the specified {@code pageUrl} argument is unspecified or set to {@code http/https},
 *         the specified {@code targetUri} is overlaid by the elements of the {@link PageUrl} annotation to
 *         produce the fully-qualified <b>HTTP</b> target page URL.<ul>
 *         <li>If the {@code value} element specifies an absolute path, this path is returned as-is.</li>
 *         <li>If the {@code value} element specifies a relative path, this is appended to the path specified by
 *             {@code targetUri} to resolve the page URL.</li>
 *         <li>If the {@code scheme} element is specified, its value overrides the scheme of {@code targetUri}.
 *             If the value of the {@code scheme} element is empty, the scheme of {@code targetUri} is set to
 *             {@code null}.</li>
 *         <li>If the {@code userInfo} element is specified, its value overrides the userInfo of {@code targetUrl}.
 *             If the value of the {@code userInfo} element is empty, the userInfo of {@code targetUri} is set to
 *             {@code null}.</li>
 *         <li>If the {@code host} element is specified, its value overrides the host of {@code targetUrl}. If the
 *             value of the {@code host} element is empty, the host of {@code targetUri} is set to {@code null}.
 *             </li>
 *         <li>If the {@code port} element is specified, its value overrides the port of {@code targetUri}. If the
 *             value of the {@code port} element is empty, the port of {@code targetUri} is set to <b>-1</b>.</li>
 *     </ul></li>
 *     <li>For <b>HTTP</b> URLs that require query parameters, these parameters must be included in the
 *         {@code value} element of the specified {@link PageUrl} annotation. The {@code params} element of the
 *         annotation is only used for pattern-based landing page verification.</li>
 *     <li>If {@code scheme} of the specified {@code pageUrl} is set to {@code file}, the value of the
 *         {@code targetUri} argument is ignored. The only element of the {@link PageUrl} annotation that
 *         is used to produce the fully-qualified <b>FILE</b> target page URL is {@code value}. The value of the
 *         {@code value} element specifies the relative path of a file within your project's resources, which is
 *         resolved via {@link ClassLoader#getResource}.</li>
 * </ul>
 * 
 * @param pageUrl page URL annotation
 * @param targetUri target URI
 * @return defined page URL as a string (may be 'null')
 */
@SuppressWarnings({"squid:S3776", "squid:MethodCyclomaticComplexity"})
public static String getPageUrl(final PageUrl pageUrl, final URI targetUri) {
    if (pageUrl == null || PLACEHOLDER.equals(pageUrl.value())) {
        return null;
    }
    
    String result = null;
    String scheme = pageUrl.scheme();
    String path = pageUrl.value();
    
    if ("file".equals(scheme)) {
        result = Thread.currentThread().getContextClassLoader().getResource(path).toString();
    } else {
        String userInfo = pageUrl.userInfo();
        String host = pageUrl.host();
        String port = pageUrl.port();
        
        URIBuilder builder = new URIBuilder(targetUri);
        
        if (!path.isEmpty()) {
            URI pathUri = URI.create(path);
            if (pathUri.isAbsolute()) {
                return pathUri.toString();
            } else {
                builder.setPath(URI.create(LOOPBACK + builder.getPath() + "/").resolve("./" + path).getPath());
            }
        }
        
        if (!PLACEHOLDER.equals(scheme)) {
            builder.setScheme(scheme.isEmpty() ? null : scheme);
        }
        
        if (!PLACEHOLDER.equals(userInfo)) {
            builder.setUserInfo(userInfo.isEmpty() ? null : userInfo);
        }
        
        if (!PLACEHOLDER.equals(host)) {
            builder.setHost(host.isEmpty() ? null : host);
        }
        
        if (!PLACEHOLDER.equals(port)) {
            builder.setPort(port.isEmpty() ? -1 : Integer.parseInt(port));
        }
        
        result = builder.toString();
    }
    
    return result;
}