Java Code Examples for com.sun.jersey.spi.container.ContainerRequest#setUris()

The following examples show how to use com.sun.jersey.spi.container.ContainerRequest#setUris() . 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: URITranslator.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
public void translate(ContainerRequest request) {
    String uri = request.getPath();
    List<PathSegment> segments = request.getPathSegments();
    String version = PathConstants.V1;

    if (!segments.isEmpty()) {
        version = segments.get(0).getPath();
    }

    for (Map.Entry<String, URITranslation> entry : uriTranslationMap.entrySet()) {
        String key = entry.getKey();
        if (uri.contains(key)) {
            String newPath = uriTranslationMap.get(key).translate(request.getPath());
            if (!newPath.equals(uri)) {
                request.setUris(request.getBaseUri(),
                    request.getBaseUriBuilder().path(version).path(newPath).build());
            }
        }
    }
}
 
Example 2
Source File: VersionFilter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
private ContainerRequest updateContainerRequest(ContainerRequest containerRequest, List<PathSegment> segments, String newVersion) {
    //add the new version
    UriBuilder builder = containerRequest.getBaseUriBuilder().path(newVersion);

    //add the rest of the request
    for (PathSegment segment : segments) {
        builder.path(segment.getPath());
    }

    if (containerRequest.getRequestUri().getQuery() != null &&
            !containerRequest.getRequestUri().getQuery().isEmpty()) {
        builder.replaceQuery(containerRequest.getRequestUri().getQuery());
    }

    containerRequest.getProperties().put(REQUESTED_PATH, containerRequest.getPath());
    containerRequest.setUris(containerRequest.getBaseUri(), builder.build());

    return containerRequest;
}
 
Example 3
Source File: RedirectResourceFilter.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * This method checks path of the incoming request, and
 * redirects following URIs:
 * <li>/controller -> SiteToSiteResource
 * @param containerRequest request to be modified
 * @return modified request
 */
@Override
public ContainerRequest filter(ContainerRequest containerRequest) {

    if(containerRequest.getPath().equals("controller")){
        UriBuilder builder = UriBuilder.fromUri(containerRequest.getBaseUri())
                .path(SiteToSiteResource.class)
                .replaceQuery(containerRequest.getRequestUri().getRawQuery());

        URI redirectTo = builder.build();
        containerRequest.setUris(containerRequest.getBaseUri(), redirectTo);
    }
    return containerRequest;
}
 
Example 4
Source File: ParamFilter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerRequest filter(final ContainerRequest request) {
  final MultivaluedMap<String, String> parameters = request.getQueryParameters();
  if (containsUpperCase(parameters.keySet())) {
    //rebuild URI
    final URI lower = rebuildQuery(request.getRequestUri(), parameters);
    request.setUris(request.getBaseUri(), lower);
  }
  return request;
}
 
Example 5
Source File: ParamFilter.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerRequest filter(final ContainerRequest request) {
  final MultivaluedMap<String, String> parameters = request.getQueryParameters();
  if (containsUpperCase(parameters.keySet())) {
    //rebuild URI
    final URI lower = rebuildQuery(request.getRequestUri(), parameters);
    request.setUris(request.getBaseUri(), lower);
  }
  return request;
}
 
Example 6
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 7
Source File: EndpointMutator.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
/**
 * Mutates the URI based on who the user is (provided in Authentication object) and what they're
 * requesting (provided in Container Request object).
 *
 * @param auth
 *            OAuth2Authentication object (contains principal for user).
 * @param request
 *            Container Request (contains path and query parameters).
 */
public void mutateURI(Authentication auth, ContainerRequest request) {

    /*
     * Don't mutate POSTs.
     */
    if (request.getMethod().equals(POST)) {
        return;
    }

    SLIPrincipal user = (SLIPrincipal) auth.getPrincipal();
    String clientId = ((OAuth2Authentication) auth).getClientAuthentication().getClientId();
    List<PathSegment> segments = sanitizePathSegments(request);
    String parameters = request.getRequestUri().getQuery();
    

    if (segments.size() == 0) {
        throw new NotFoundException();
    }

    if (usingVersionedApi(segments)) {
        if (!request.getProperties().containsKey(REQUESTED_PATH)) {
            request.getProperties().put(REQUESTED_PATH, request.getPath());
        }
        MutatedContainer mutated = uriMutator.mutate(segments, parameters, user, clientId);

        if (mutated != null && mutated.isModified()) {

            String version = getResourceVersion(segments, mutated);

            if (mutated.getHeaders() != null) {
                InBoundHeaders headers = new InBoundHeaders();
                headers.putAll(request.getRequestHeaders());
                for (String key : mutated.getHeaders().keySet()) {
                    headers.putSingle(key, mutated.getHeaders().get(key));
                }
                request.setHeaders(headers);
            }

            if (mutated.getPath() != null) {
                if (mutated.getQueryParameters() != null && !mutated.getQueryParameters().isEmpty()) {
                    LOG.info("URI Rewrite: {}?{} --> {}?{}", new Object[] { request.getPath(), parameters, mutated.getPath(),
                            mutated.getQueryParameters() });
                    request.setUris(request.getBaseUri(),
                            request.getBaseUriBuilder().path(version).path(mutated.getPath())
                                .replaceQuery(mutated.getQueryParameters()).build());
                } else {
                    LOG.info("URI Rewrite: {} --> {}", new Object[] { request.getPath(), mutated.getPath() });
                    request.setUris(request.getBaseUri(),
                            request.getBaseUriBuilder().path(version).path(mutated.getPath()).build());
                }
            }
        }
    }
}