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

The following examples show how to use javax.ws.rs.core.UriBuilder#scheme() . 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: ArchiveLogCollection.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * コンストラクタ.
 * @param cell コレクションが属するCellオブジェクト
 * @param uriInfo コレクションのURL情報
 */
public ArchiveLogCollection(Cell cell, UriInfo uriInfo) {
    // archiveコレクションの作成日と更新日はセルの作成日とする
    // ただし更新日についてはアーカイブログファイルが作成されている場合、そのファイルの最新日が「createFileInformation」で設定される
    this.created = cell.getPublished();
    this.updated = cell.getPublished();

    // archiveコレクションのURLを生成
    StringBuilder urlSb = new StringBuilder();
    UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
    uriBuilder.scheme(DcCoreConfig.getUnitScheme());
    urlSb.append(uriBuilder.build().toASCIIString());
    urlSb.append(uriInfo.getPath());
    this.url = urlSb.toString();

    StringBuilder archiveDirName = EventUtils.getEventLogDir(cell.getId(), cell.getOwner()).append("archive");
    this.directoryPath = archiveDirName.toString();
}
 
Example 2
Source File: LoadBalancerRequestFilter.java    From ameba with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void filter(ContainerRequestContext ctx) throws IOException {

    String scheme = getValue(ctx.getHeaders(), "x-forwarded-proto");
    String port = getValue(ctx.getHeaders(), "x-forwarded-port");
    if (scheme == null && port == null)
        return;

    UriBuilder baseBuilder = ctx.getUriInfo().getBaseUriBuilder();
    UriBuilder requestBuilder = ctx.getUriInfo().getRequestUriBuilder();
    if (scheme != null) {
        baseBuilder.scheme(scheme);
        requestBuilder.scheme(scheme);
        baseBuilder.port(443);
        requestBuilder.port(443);
    }

    if (port != null) {
        int nPort = Integer.parseInt(port);
        baseBuilder.port(nPort);
        requestBuilder.port(nPort);
    }

    ctx.setRequestUri(baseBuilder.build(), requestBuilder.build());
}
 
Example 3
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 4
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 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(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 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(UriBuilder uriBuilder) {
  final Server server = fetchServerOrThrow();
  uriBuilder.scheme(server.getScheme());
  uriBuilder.host(server.getHost());
  uriBuilder.port(server.getPort());
  return delegate.target(uriBuilder);
}
 
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(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 8
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 9
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 10
Source File: UriBuilderRequestFilter.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
private void processProtocolHeader(ContainerRequestContext ctx, UriBuilder baseBuilder, UriBuilder requestBuilder) {
    String protoHeaderValue = getFirstHeaderValueOrNull(ctx, HttpHeaders.X_FORWARDED_PROTO);
    if (protoHeaderValue != null) {
        baseBuilder.scheme(protoHeaderValue);
        requestBuilder.scheme(protoHeaderValue);
        ctx.setRequestUri(baseBuilder.build(), requestBuilder.build());
    }
}
 
Example 11
Source File: CellEsImpl.java    From io with Apache License 2.0 5 votes vote down vote up
private static String getBaseUri(final UriInfo uriInfo, String cellName) {
    // URLを生成してSet
    StringBuilder urlSb = new StringBuilder();
    UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
    uriBuilder.scheme(DcCoreConfig.getUnitScheme());
    urlSb.append(uriBuilder.build().toASCIIString());
    urlSb.append(cellName);
    urlSb.append("/");
    return urlSb.toString();
}
 
Example 12
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 13
Source File: RestUtil.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static URI buildBaseUri(final UriInfo uriInfo, final HttpServletRequest request,
    final List<PathSegment> precedingPathSegments) throws ODataException {
  try {
    String path = uriInfo.getBaseUri().getPath();
    UriBuilder uriBuilder = UriBuilder.fromUri(path);
    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()).port(request.getServerPort());
    uriBuilder.scheme(request.getScheme());

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

    return new URI(uriString);
  } catch (final URISyntaxException e) {
    throw new ODataException(e);
  }
}
 
Example 14
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;
}
 
Example 15
Source File: ContentViewerController.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * @param request request
 * @return Get the content request context based on the specified request
 */
@SuppressWarnings("deprecation")
private ContentRequestContext getContentRequest(final HttpServletRequest request) {
    final String ref = request.getParameter("ref");
    final String clientId = request.getParameter("clientId");

    final UriBuilder refUriBuilder = UriBuilder.fromUri(ref);

    // base the data ref on the request parameter but ensure the scheme is based off the incoming request...
    // this is necessary for scenario's where the NiFi instance is behind a proxy running a different scheme
    refUriBuilder.scheme(request.getScheme());

    // If there is path context from a proxy, remove it since this request will be used inside the cluster
    final String proxyContextPath = getFirstHeaderValue(request, PROXY_CONTEXT_PATH_HTTP_HEADER, FORWARDED_CONTEXT_HTTP_HEADER, FORWARDED_PREFIX_HTTP_HEADER);
    if (StringUtils.isNotBlank(proxyContextPath)) {
        refUriBuilder.replacePath(StringUtils.substringAfter(UriBuilder.fromUri(ref).build().getPath(), proxyContextPath));
    }

    final URI refUri = refUriBuilder.build();

    final String query = refUri.getQuery();

    String rawClusterNodeId = null;
    if (query != null) {
        final String[] queryParameters = query.split("&");

        for (int i = 0; i < queryParameters.length; i++) {
            if (queryParameters[0].startsWith("clusterNodeId=")) {
                rawClusterNodeId = StringUtils.substringAfterLast(queryParameters[0], "clusterNodeId=");
            }
        }
    }
    final String clusterNodeId = rawClusterNodeId;

    return new ContentRequestContext() {
        @Override
        public String getDataUri() {
            return refUri.toString();
        }

        @Override
        public String getClusterNodeId() {
            return clusterNodeId;
        }

        @Override
        public String getClientId() {
            return clientId;
        }

        @Override
        public String getProxiedEntitiesChain() {
            return null;
        }
    };
}