Java Code Examples for javax.ws.rs.core.UriBuilder#host()

The following examples show how to use javax.ws.rs.core.UriBuilder#host() . 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: RestUtil.java    From cloud-odata-java with Apache License 2.0 6 votes vote down vote up
private static URI buildBaseUri(final HttpServletRequest request, final javax.ws.rs.core.UriInfo uriInfo, final List<PathSegment> precedingPathSegments) throws ODataException {
  try {
    UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
    for (final PathSegment ps : precedingPathSegments) {
      uriBuilder = uriBuilder.path(ps.getPath());
      for (final String key : ps.getMatrixParameters().keySet()) {
        final Object[] v = ps.getMatrixParameters().get(key).toArray();
        uriBuilder = uriBuilder.matrixParam(key, v);
      }
    }

    /*
     * workaround because of host name is cached by uriInfo
     */
    uriBuilder.host(request.getServerName());

    String uriString = uriBuilder.build().toString();
    if (!uriString.endsWith("/")) {
      uriString = uriString + "/";
    }

    return new URI(uriString);
  } catch (final URISyntaxException e) {
    throw new ODataException(e);
  }
}
 
Example 2
Source File: RestServer.java    From DataLink with Apache License 2.0 5 votes vote down vote up
/**
 * Get the URL to advertise to other workers and clients. This uses the default connector parseFrom the embedded Jetty
 * server, unless overrides for advertised hostname and/or port are provided via configs.
 */
public URI advertisedUrl() {
    UriBuilder builder = UriBuilder.fromUri(jettyServer.getURI());
    String advertisedHostname = config.getString(WorkerConfig.REST_ADVERTISED_HOST_NAME_CONFIG);
    if (advertisedHostname != null && !advertisedHostname.isEmpty())
        builder.host(advertisedHostname);
    Integer advertisedPort = config.getInt(WorkerConfig.REST_ADVERTISED_PORT_CONFIG);
    if (advertisedPort != null)
        builder.port(advertisedPort);
    else
        builder.port(config.getInt(WorkerConfig.REST_PORT_CONFIG));
    return builder.build();
}
 
Example 3
Source File: RestServer.java    From DataLink with Apache License 2.0 5 votes vote down vote up
/**
 * Get the URL to advertise to other workers and clients. This uses the default connector parseFrom the embedded Jetty
 * server, unless overrides for advertised hostname and/or port are provided via configs.
 */
public URI advertisedUrl() {
    UriBuilder builder = UriBuilder.fromUri(jettyServer.getURI());
    String advertisedHostname = System.getProperty(REST_ADVERTISED_HOST_NAME_CONFIG);
    if (advertisedHostname != null && !advertisedHostname.isEmpty())
        builder.host(advertisedHostname);
    Integer advertisedPort = Integer.getInteger(REST_ADVERTISED_PORT_CONFIG);
    if (advertisedPort != null) {
        builder.port(advertisedPort);
    }
    else {
        builder.port( REST_PORT_DEFAULT );
    }
    return builder.build();
}
 
Example 4
Source File: UriBuilderRequestFilter.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Override
public void filter( ContainerRequestContext ctx ) throws IOException {
    List<String> schemes = ctx.getHeaders().get(HttpHeaders.X_FORWARDED_PROTO);
    UriBuilder baseBuilder = ctx.getUriInfo().getBaseUriBuilder();
    UriBuilder requestBuilder = ctx.getUriInfo().getRequestUriBuilder();

    if (schemes != null && !schemes.isEmpty()) {
        String scheme = schemes.get(0);
        baseBuilder.scheme(scheme);
        requestBuilder.scheme(scheme);

        ctx.setRequestUri(baseBuilder.build(), requestBuilder.build());
    }

    List<String> hosts = ctx.getHeaders().get(HttpHeaders.X_FORWARDED_HOST);
    if (hosts != null && !hosts.isEmpty()) {
        String host = hosts.get(0);

        if (host.contains(":")) {
            // Forwarded host contains both host and port
            String [] parts = host.split(":");
            baseBuilder.host(parts[0]).port(Integer.parseInt(parts[1]));
            requestBuilder.host(parts[0]).port(Integer.parseInt(parts[1]));
        } else {
            baseBuilder.host(host);
            requestBuilder.host(host);
        }

        ctx.setRequestUri(baseBuilder.build(), requestBuilder.build());
    }
}
 
Example 5
Source File: RibbonJerseyClient.java    From dropwizard-consul with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws IllegalStateException if there are no available servers
 */
@Override
public WebTarget target(String uri) {
  final Server server = fetchServerOrThrow();
  final UriBuilder builder = UriBuilder.fromUri(uri);
  builder.scheme(server.getScheme());
  builder.host(server.getHost());
  builder.port(server.getPort());
  return delegate.target(builder);
}
 
Example 6
Source File: RibbonJerseyClient.java    From dropwizard-consul with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws IllegalStateException if there are no available servers
 */
@Override
public WebTarget target(URI uri) {
  final Server server = fetchServerOrThrow();
  final UriBuilder builder = UriBuilder.fromUri(uri);
  builder.scheme(server.getScheme());
  builder.host(server.getHost());
  builder.port(server.getPort());
  return delegate.target(builder);
}
 
Example 7
Source File: RibbonJerseyClient.java    From dropwizard-consul with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws IllegalStateException if there are no available servers
 */
@Override
public WebTarget target(UriBuilder uriBuilder) {
  final Server server = fetchServerOrThrow();
  uriBuilder.scheme(server.getScheme());
  uriBuilder.host(server.getHost());
  uriBuilder.port(server.getPort());
  return delegate.target(uriBuilder);
}
 
Example 8
Source File: RibbonJerseyClient.java    From dropwizard-consul with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws IllegalStateException if there are no available servers
 */
@Override
public WebTarget target(Link link) {
  final Server server = fetchServerOrThrow();
  final UriBuilder builder = UriBuilder.fromLink(link);
  builder.scheme(server.getScheme());
  builder.host(server.getHost());
  builder.port(server.getPort());
  return delegate.target(builder);
}
 
Example 9
Source File: ConsulAdvertiser.java    From dropwizard-consul with Apache License 2.0 5 votes vote down vote up
/**
 * Return the health check URL for the service
 *
 * @param applicationScheme Scheme the server is listening on
 * @return health check URL
 */
protected String getHealthCheckUrl(String applicationScheme) {
  final UriBuilder builder = UriBuilder.fromPath(environment.getAdminContext().getContextPath());
  builder.path("healthcheck");
  builder.scheme(applicationScheme);
  if (serviceAddress.get() == null) {
    builder.host("127.0.0.1");
  } else {
    builder.host(serviceAddress.get());
  }
  builder.port(serviceAdminPort.get());
  return builder.build().toString();
}
 
Example 10
Source File: UriBuilderRequestFilter.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Override
public void filter( ContainerRequestContext ctx ) throws IOException {
    List<String> schemes = ctx.getHeaders().get(HttpHeaders.X_FORWARDED_PROTO);
    UriBuilder baseBuilder = ctx.getUriInfo().getBaseUriBuilder();
    UriBuilder requestBuilder = ctx.getUriInfo().getRequestUriBuilder();

    if (schemes != null && !schemes.isEmpty()) {
        String scheme = schemes.get(0);
        baseBuilder.scheme(scheme);
        requestBuilder.scheme(scheme);

        ctx.setRequestUri(baseBuilder.build(), requestBuilder.build());
    }

    List<String> hosts = ctx.getHeaders().get(HttpHeaders.X_FORWARDED_HOST);
    if (hosts != null && !hosts.isEmpty()) {
        String host = hosts.get(0);

        if (host.contains(":")) {
            // Forwarded host contains both host and port
            String [] parts = host.split(":");
            baseBuilder.host(parts[0]).port(Integer.parseInt(parts[1]));
            requestBuilder.host(parts[0]).port(Integer.parseInt(parts[1]));
        } else {
            baseBuilder.host(host);
            requestBuilder.host(host);
        }

        ctx.setRequestUri(baseBuilder.build(), requestBuilder.build());
    }
}
 
Example 11
Source File: DcCoreContainerFilter.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * Uriのオーバーライド処理.
 * @param request 加工するリクエスト
 */
private void uriOverride(final ContainerRequest request) {
    String xForwardedProto = request.getHeaderValue(DcCoreUtils.HttpHeaders.X_FORWARDED_PROTO);
    String xForwardedHost = request.getHeaderValue(DcCoreUtils.HttpHeaders.X_FORWARDED_HOST);
    String xForwardedPath = request.getHeaderValue(DcCoreUtils.HttpHeaders.X_FORWARDED_PATH);

    UriBuilder bub = request.getBaseUriBuilder();
    UriBuilder rub = request.getRequestUriBuilder();

    if (xForwardedProto != null) {
        bub.scheme(xForwardedProto);
        rub.scheme(xForwardedProto);
    }
    if (xForwardedHost != null) {
        bub.host(xForwardedHost);
        rub.host(xForwardedHost);
    }
    if (xForwardedPath != null) {
        bub.replacePath("/");
        // クエリを含んでいる場合は、クエリを削除してリクエストパスに設定する
        if (xForwardedPath.contains("?")) {
            xForwardedPath = xForwardedPath.substring(0, xForwardedPath.indexOf("?"));
        }
        rub.replacePath(xForwardedPath);
    }
    request.setUris(bub.build(), rub.build());
}
 
Example 12
Source File: KeycloakUriInfo.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private UriBuilder initUriBuilder(UriBuilder b) {
    b.scheme(scheme);
    b.host(hostname);
    b.port(port);
    b.replacePath(contextPath);
    return b;
}