Search in sources :

Example 1 with HttpHeaders

use of io.micronaut.http.HttpHeaders in project micronaut-core by micronaut-projects.

the class HttpRequestTest method testForEach2.

public void testForEach2() {
    final DefaultFullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.GET, "/test");
    nettyRequest.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    nettyRequest.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
    HttpRequest<?> request = new NettyHttpRequest(nettyRequest, new DetachedMockFactory().Mock(ChannelHandlerContext.class), ConversionService.SHARED, new HttpServerConfiguration());
    final HttpHeaders headers = request.getHeaders();
    headers.forEach((name, values) -> {
        assertEquals(HttpHeaders.CONTENT_TYPE, name);
        assertEquals(2, values.size());
        assertTrue(values.contains(MediaType.APPLICATION_JSON));
        assertTrue(values.contains(MediaType.APPLICATION_XML));
    });
    AtomicInteger integer = new AtomicInteger(0);
    headers.forEachValue((s, s2) -> integer.incrementAndGet());
    assertEquals(2, integer.get());
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerConfiguration(io.micronaut.http.server.HttpServerConfiguration) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) DetachedMockFactory(spock.mock.DetachedMockFactory)

Example 2 with HttpHeaders

use of io.micronaut.http.HttpHeaders in project micronaut-core by micronaut-projects.

the class CorsFilter method handleResponse.

/**
 * Handles a CORS response.
 *
 * @param request  The {@link HttpRequest} object
 * @param response The {@link MutableHttpResponse} object
 */
protected void handleResponse(HttpRequest<?> request, MutableHttpResponse<?> response) {
    HttpHeaders headers = request.getHeaders();
    Optional<String> originHeader = headers.getOrigin();
    originHeader.ifPresent(requestOrigin -> {
        Optional<CorsOriginConfiguration> optionalConfig = getConfiguration(requestOrigin);
        if (optionalConfig.isPresent()) {
            CorsOriginConfiguration config = optionalConfig.get();
            if (CorsUtil.isPreflightRequest(request)) {
                Optional<HttpMethod> result = headers.getFirst(ACCESS_CONTROL_REQUEST_METHOD, CONVERSION_CONTEXT_HTTP_METHOD);
                setAllowMethods(result.get(), response);
                Optional<List<String>> allowedHeaders = headers.get(ACCESS_CONTROL_REQUEST_HEADERS, ConversionContext.LIST_OF_STRING);
                allowedHeaders.ifPresent(val -> setAllowHeaders(val, response));
                setMaxAge(config.getMaxAge(), response);
            }
            setOrigin(requestOrigin, response);
            setVary(response);
            setExposeHeaders(config.getExposedHeaders(), response);
            setAllowCredentials(config, response);
        }
    });
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders) List(java.util.List) HttpMethod(io.micronaut.http.HttpMethod)

Example 3 with HttpHeaders

use of io.micronaut.http.HttpHeaders in project micronaut-security by micronaut-projects.

the class ClientCredentialsHeaderTokenPropagator method findToken.

/**
 * Search for a JWT token in a HTTP request.
 * @param request The request to look for the token in
 * @return if the JWT token is found it is returned, empty if not
 */
@Override
public Optional<String> findToken(@NonNull HttpRequest<?> request) {
    HttpHeaders headers = request.getHeaders();
    Optional<String> authorizationHeader = headers.findFirst(configuration.getHeaderName());
    return authorizationHeader.flatMap(this::extractTokenFromAuthorization);
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders)

Example 4 with HttpHeaders

use of io.micronaut.http.HttpHeaders in project micronaut-security by micronaut-projects.

the class HttpHeaderTokenPropagator method findToken.

/**
 * Search for a JWT token in a HTTP request.
 * @param request The request to look for the token in
 * @return if the JWT token is found it is returned, empty if not
 */
@Override
public Optional<String> findToken(HttpRequest<?> request) {
    HttpHeaders headers = request.getHeaders();
    Optional<String> authorizationHeader = headers.findFirst(configuration.getHeaderName());
    return authorizationHeader.flatMap(this::extractTokenFromAuthorization);
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders)

Example 5 with HttpHeaders

use of io.micronaut.http.HttpHeaders in project micronaut-security by micronaut-projects.

the class HttpHeaderTokenReader method findToken.

/**
 * Search for a JWT token in a HTTP request.
 * @param request The request to look for the token in
 * @return if the JWT token is found it is returned, empty if not
 */
@Override
public Optional<String> findToken(HttpRequest<?> request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking for bearer token in {} header", getHeaderName());
    }
    HttpHeaders headers = request.getHeaders();
    Optional<String> authorizationHeader = headers.findFirst(getHeaderName());
    return authorizationHeader.flatMap(this::extractTokenFromAuthorization);
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders)

Aggregations

HttpHeaders (io.micronaut.http.HttpHeaders)9 HttpServerConfiguration (io.micronaut.http.server.HttpServerConfiguration)3 HttpMethod (io.micronaut.http.HttpMethod)2 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 List (java.util.List)2 DetachedMockFactory (spock.mock.DetachedMockFactory)2 Publishers (io.micronaut.core.async.publisher.Publishers)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 ImmutableArgumentConversionContext (io.micronaut.core.convert.ImmutableArgumentConversionContext)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 HttpStatus (io.micronaut.http.HttpStatus)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Filter (io.micronaut.http.annotation.Filter)1 HttpServerFilter (io.micronaut.http.filter.HttpServerFilter)1 ServerFilterChain (io.micronaut.http.filter.ServerFilterChain)1