com.sun.jersey.core.header.InBoundHeaders Java Examples

The following examples show how to use com.sun.jersey.core.header.InBoundHeaders. 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: DcCoreContainerFilter.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * リクエストヘッダーの値をチェックする.
 * 現在は、Acceptヘッダーのみ(US-ASCII文字以外かどうか)をチェックする
 * @param request フィルター前リクエスト
 */
private void checkRequestHeader(ContainerRequest request) {
    // ヘッダーのキー名に全角文字が含まれる場合は、その文字を含めたキー名となるため、実際にはこの指定は無視される。
    // Jersey1.10では、Acceptヘッダーのキー名と値にUS-ASCII文字以外が含まれる場合に異常終了するため以下を対処
    // (Acceptを含む他のヘッダーにも同様の処理が行われるが、上記理由により動作上は問題ないと判断)
    // -キー名に含まれる場合は、その指定を無効(Accept:*/*)とする(Jerseryで組み込み済み)。
    // -値に含まれる場合は、400エラーとする。
    InBoundHeaders newHeaders = new InBoundHeaders();
    MultivaluedMap<String, String> headers = request.getRequestHeaders();
    for (String header : headers.keySet()) {
        if (header.contains(org.apache.http.HttpHeaders.ACCEPT)
                && !acceptHeaderValueRegex.matcher(header).matches()) {
            continue;
        } else {
            newHeaders.put(header, request.getRequestHeader(header));
        }
    }
    request.setHeaders(newHeaders);
    String acceptValue = request.getHeaderValue(org.apache.http.HttpHeaders.ACCEPT);
    if (acceptValue != null && !acceptHeaderValueRegex.matcher(acceptValue).matches()) {
        DcCoreException exception = DcCoreException.OData.BAD_REQUEST_HEADER_VALUE.params(
                org.apache.http.HttpHeaders.ACCEPT, acceptValue);
        throw exception;
    }
}
 
Example #2
Source File: NettyHandlerContainer.java    From recipes-rss with Apache License 2.0 5 votes vote down vote up
private InBoundHeaders getHeaders(HttpRequest request) {
	InBoundHeaders headers = new InBoundHeaders();
	for (String name : request.getHeaderNames()) {
		headers.put(name, request.getHeaders(name));
	}
	return headers;
}
 
Example #3
Source File: DcCoreContainerFilterTest.java    From io with Apache License 2.0 4 votes vote down vote up
/**
 * リクエストフィルタとしてメソッド/ヘッダオーバライドを実施していることを確認.
 * X-FORWARDED-PROTO、X-FORWARDED-HOSTヘッダでリクエストUri, Base UriのPROTO, HOST部が書き換わることを確認。
 * @throws URISyntaxException URISyntaxException
 */
@Test
public void testFilterContainerRequest() throws URISyntaxException {
    // 被テストオブジェクトを準備
    DcCoreContainerFilter containerFilter = new DcCoreContainerFilter();
    // ContainerRequiestを準備
    WebApplication wa = mock(WebApplication.class);
    InBoundHeaders headers = new InBoundHeaders();
    // メソッドオーバーライド
    headers.add(DcCoreUtils.HttpHeaders.X_HTTP_METHOD_OVERRIDE, HttpMethod.OPTIONS);
    // ヘッダオーバーライド
    String authzValue = "Bearer tokenstring";
    String acceptValue = "text/html";
    String contentTypeValue = "application/xml";
    headers.add(DcCoreUtils.HttpHeaders.X_OVERRIDE, HttpHeaders.AUTHORIZATION + ": " + authzValue);
    headers.add(HttpHeaders.ACCEPT, contentTypeValue);
    headers.add(DcCoreUtils.HttpHeaders.X_OVERRIDE, HttpHeaders.ACCEPT + ": " + acceptValue);
    headers.add(HttpHeaders.CONTENT_TYPE, contentTypeValue);

    // X-FORWARDED-* 系のヘッダ設定
    String scheme = "https";
    String host = "example.org";
    headers.add(DcCoreUtils.HttpHeaders.X_FORWARDED_PROTO, scheme);
    headers.add(DcCoreUtils.HttpHeaders.X_FORWARDED_HOST, host);

    ContainerRequest request = new ContainerRequest(wa, HttpMethod.POST,
            new URI("http://dc1.example.com/hoge"),
            new URI("http://dc1.example.com/hoge/hoho"),
            headers, null);

    // HttpServletRequestのmockを準備
    HttpServletRequest mockServletRequest = mock(HttpServletRequest.class);
    when(mockServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://dc1.example.com"));

    ServletContext mockServletContext = mock(ServletContext.class);
    when(mockServletContext.getContextPath()).thenReturn("");
    when(mockServletRequest.getServletContext()).thenReturn(mockServletContext);
    containerFilter.setHttpServletRequest(mockServletRequest);

    // 被テスト処理の実行
    ContainerRequest filteredRequest = containerFilter.filter(request);

    // 結果の検証。
    Assert.assertEquals(HttpMethod.OPTIONS, filteredRequest.getMethod());
    Assert.assertEquals(authzValue, filteredRequest.getHeaderValue(HttpHeaders.AUTHORIZATION));
    Assert.assertEquals(acceptValue, filteredRequest.getHeaderValue(HttpHeaders.ACCEPT));

    Assert.assertEquals(contentTypeValue, filteredRequest.getHeaderValue(HttpHeaders.CONTENT_TYPE));
    Assert.assertEquals(scheme, filteredRequest.getRequestUri().getScheme());
    Assert.assertEquals(host, filteredRequest.getRequestUri().getHost());
}
 
Example #4
Source File: OptionsMethodTest.java    From io with Apache License 2.0 4 votes vote down vote up
/**
 * 認証なしのOPTIONSメソッドがリクエストされた場合にpersoniumで受け付けている全メソッドが返却されること.
 * @throws URISyntaxException URISyntaxException
 */
@Test
public void 認証なしのOPTIONSメソッドがリクエストされた場合にpersoniumで受け付けている全メソッドが返却されること() throws URISyntaxException {
    // 被テストオブジェクトを準備
    DcCoreContainerFilter containerFilter = new DcCoreContainerFilter();
    // ContainerRequiestを準備
    WebApplication wa = mock(WebApplication.class);
    InBoundHeaders headers = new InBoundHeaders();
    // X-FORWARDED-* 系のヘッダ設定
    String scheme = "https";
    String host = "example.org";
    headers.add(DcCoreUtils.HttpHeaders.X_FORWARDED_PROTO, scheme);
    headers.add(DcCoreUtils.HttpHeaders.X_FORWARDED_HOST, host);
    ContainerRequest request = new ContainerRequest(wa, HttpMethod.OPTIONS,
            new URI("http://dc1.example.com/hoge"),
            new URI("http://dc1.example.com/hoge/hoho"),
            headers, null);
    // HttpServletRequestのmockを準備
    HttpServletRequest mockServletRequest = mock(HttpServletRequest.class);
    when(mockServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://dc1.example.com"));
    ServletContext mockServletContext = mock(ServletContext.class);
    when(mockServletContext.getContextPath()).thenReturn("");
    when(mockServletRequest.getServletContext()).thenReturn(mockServletContext);
    containerFilter.setHttpServletRequest(mockServletRequest);
    try {
        containerFilter.filter(request);
    } catch (WebApplicationException e) {
        Response response = e.getResponse();
        assertEquals(response.getStatus(), HttpStatus.SC_OK);
        MultivaluedMap<String, Object> meta = response.getMetadata();
        List<Object> values = meta.get("Access-Control-Allow-Methods");
        assertEquals(values.size(), 1);
        String value = (String) values.get(0);
        String[] methods = value.split(",");
        Map<String, String> masterMethods = new HashMap<String, String>();
        masterMethods.put(HttpMethod.OPTIONS, "");
        masterMethods.put(HttpMethod.GET, "");
        masterMethods.put(HttpMethod.POST, "");
        masterMethods.put(HttpMethod.PUT, "");
        masterMethods.put(HttpMethod.DELETE, "");
        masterMethods.put(HttpMethod.HEAD, "");
        masterMethods.put(com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MERGE, "");
        masterMethods.put(com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MKCOL, "");
        masterMethods.put(com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MOVE, "");
        masterMethods.put(com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPFIND, "");
        masterMethods.put(com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPPATCH, "");
        masterMethods.put(com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.ACL, "");
        for (String method : methods) {
            if (method.trim() == "") {
                continue;
            }
            String m = masterMethods.remove(method.trim());
            if (m == null) {
                fail("Method " + method + " is not defined.");
            }
        }
        if (!masterMethods.isEmpty()) {
            fail("UnExcpected Error.");
        }
    }
}
 
Example #5
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());
                }
            }
        }
    }
}