Search in sources :

Example 1 with MutableHttpHeaders

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

the class HttpResponseEncoder method encodeBodyWithCodec.

private MutableHttpResponse<?> encodeBodyWithCodec(MutableHttpResponse<?> response, Object body, MediaTypeCodec codec, MediaType mediaType, ChannelHandlerContext context) {
    ByteBuf byteBuf = encodeBodyAsByteBuf(body, codec, context, response);
    int len = byteBuf.readableBytes();
    MutableHttpHeaders headers = response.getHeaders();
    if (!headers.contains(HttpHeaders.CONTENT_TYPE)) {
        headers.add(HttpHeaderNames.CONTENT_TYPE, mediaType);
    }
    headers.remove(HttpHeaders.CONTENT_LENGTH);
    headers.add(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(len));
    setBodyContent(response, byteBuf);
    return response;
}
Also used : MutableHttpHeaders(io.micronaut.http.MutableHttpHeaders) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with MutableHttpHeaders

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

the class FileTypeHandler method setDateHeader.

/**
 * @param response The Http response
 */
protected void setDateHeader(MutableHttpResponse response) {
    MutableHttpHeaders headers = response.getHeaders();
    LocalDateTime now = LocalDateTime.now();
    headers.date(now);
}
Also used : LocalDateTime(java.time.LocalDateTime) MutableHttpHeaders(io.micronaut.http.MutableHttpHeaders)

Example 3 with MutableHttpHeaders

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

the class FileTypeHandler method setDateAndCacheHeaders.

/**
 * @param response     The Http response
 * @param lastModified The last modified
 */
protected void setDateAndCacheHeaders(MutableHttpResponse response, long lastModified) {
    // Date header
    MutableHttpHeaders headers = response.getHeaders();
    LocalDateTime now = LocalDateTime.now();
    headers.date(now);
    // Add cache headers
    LocalDateTime cacheSeconds = now.plus(configuration.getCacheSeconds(), ChronoUnit.SECONDS);
    if (response.header(HttpHeaders.EXPIRES) == null) {
        headers.expires(cacheSeconds);
    }
    if (response.header(HttpHeaders.CACHE_CONTROL) == null) {
        NettyHttpServerConfiguration.FileTypeHandlerConfiguration.CacheControlConfiguration cacheConfig = configuration.getCacheControl();
        StringBuilder header = new StringBuilder(cacheConfig.getPublic() ? "public" : "private").append(", max-age=").append(configuration.getCacheSeconds());
        response.header(HttpHeaders.CACHE_CONTROL, header.toString());
    }
    if (response.header(HttpHeaders.LAST_MODIFIED) == null) {
        headers.lastModified(lastModified);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) MutableHttpHeaders(io.micronaut.http.MutableHttpHeaders)

Example 4 with MutableHttpHeaders

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

the class HeaderClientRequestBinder method bind.

@Override
public void bind(@NonNull MethodInvocationContext<Object, Object> context, @NonNull ClientRequestUriContext uriContext, @NonNull MutableHttpRequest<?> request) {
    List<AnnotationValue<Header>> headerAnnotations = context.getAnnotationValuesByType(Header.class);
    for (AnnotationValue<Header> headerAnnotation : headerAnnotations) {
        String headerName = headerAnnotation.stringValue("name").orElse(null);
        String headerValue = headerAnnotation.stringValue().orElse(null);
        MutableHttpHeaders headers = request.getHeaders();
        if (StringUtils.isNotEmpty(headerName) && StringUtils.isNotEmpty(headerValue) && !headers.contains(headerName)) {
            headers.set(headerName, headerValue);
        }
    }
}
Also used : Header(io.micronaut.http.annotation.Header) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) MutableHttpHeaders(io.micronaut.http.MutableHttpHeaders)

Example 5 with MutableHttpHeaders

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

the class NettyServerWebSocketUpgradeHandler method handleHandshake.

/**
 * Do the handshaking for WebSocket request.
 *
 * @param ctx           The channel handler context
 * @param req           The request
 * @param webSocketBean The web socket bean
 * @param response      The response
 * @return The channel future
 */
protected ChannelFuture handleHandshake(ChannelHandlerContext ctx, NettyHttpRequest req, WebSocketBean<?> webSocketBean, MutableHttpResponse<?> response) {
    int maxFramePayloadLength = webSocketBean.messageMethod().map(m -> m.intValue(OnMessage.class, "maxPayloadLength").orElse(65536)).orElse(65536);
    String subprotocols = webSocketBean.getBeanDefinition().stringValue(ServerWebSocket.class, "subprotocols").filter(s -> !StringUtils.isEmpty(s)).orElse(null);
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(ctx, req), subprotocols, true, maxFramePayloadLength);
    handshaker = wsFactory.newHandshaker(req.getNativeRequest());
    MutableHttpHeaders headers = response.getHeaders();
    io.netty.handler.codec.http.HttpHeaders nettyHeaders;
    if (headers instanceof NettyHttpHeaders) {
        nettyHeaders = ((NettyHttpHeaders) headers).getNettyHeaders();
    } else {
        nettyHeaders = new DefaultHttpHeaders();
        for (Map.Entry<String, List<String>> entry : headers) {
            nettyHeaders.add(entry.getKey(), entry.getValue());
        }
    }
    Channel channel = ctx.channel();
    if (handshaker == null) {
        return WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
    } else {
        return handshaker.handshake(channel, req.getNativeRequest(), nettyHeaders, channel.newPromise());
    }
}
Also used : RouteExecutor(io.micronaut.http.server.RouteExecutor) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) LoggerFactory(org.slf4j.LoggerFactory) AsciiString(io.netty.util.AsciiString) Internal(io.micronaut.core.annotation.Internal) CloseReason(io.micronaut.websocket.CloseReason) OnOpen(io.micronaut.websocket.annotation.OnOpen) HttpStatus(io.micronaut.http.HttpStatus) Map(java.util.Map) HttpResponse(io.micronaut.http.HttpResponse) RequestBinderRegistry(io.micronaut.http.bind.RequestBinderRegistry) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) MutableHttpHeaders(io.micronaut.http.MutableHttpHeaders) ChannelPipeline(io.netty.channel.ChannelPipeline) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SslHandler(io.netty.handler.ssl.SslHandler) Optional(java.util.Optional) HttpAttributes(io.micronaut.http.HttpAttributes) WebSocketSessionRepository(io.micronaut.http.netty.websocket.WebSocketSessionRepository) WebSocketBean(io.micronaut.websocket.context.WebSocketBean) OnMessage(io.micronaut.websocket.annotation.OnMessage) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) WebSocketBeanRegistry(io.micronaut.websocket.context.WebSocketBeanRegistry) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NettyHttpHeaders(io.micronaut.http.netty.NettyHttpHeaders) UriRouteMatch(io.micronaut.web.router.UriRouteMatch) Schedulers(reactor.core.scheduler.Schedulers) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) HttpRequest(io.micronaut.http.HttpRequest) ServerRequestContext(io.micronaut.http.context.ServerRequestContext) MediaTypeCodecRegistry(io.micronaut.http.codec.MediaTypeCodecRegistry) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) HttpMethod(io.micronaut.http.HttpMethod) Logger(org.slf4j.Logger) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) Flux(reactor.core.publisher.Flux) NettyEmbeddedServices(io.micronaut.http.server.netty.NettyEmbeddedServices) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) ServerWebSocket(io.micronaut.websocket.annotation.ServerWebSocket) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) ChannelPipelineCustomizer(io.micronaut.http.netty.channel.ChannelPipelineCustomizer) ChannelHandler(io.netty.channel.ChannelHandler) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Router(io.micronaut.web.router.Router) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Channel(io.netty.channel.Channel) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) OnMessage(io.micronaut.websocket.annotation.OnMessage) AsciiString(io.netty.util.AsciiString) NettyHttpHeaders(io.micronaut.http.netty.NettyHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) MutableHttpHeaders(io.micronaut.http.MutableHttpHeaders) List(java.util.List) Map(java.util.Map)

Aggregations

MutableHttpHeaders (io.micronaut.http.MutableHttpHeaders)7 Internal (io.micronaut.core.annotation.Internal)2 NonNull (io.micronaut.core.annotation.NonNull)2 StringUtils (io.micronaut.core.util.StringUtils)2 HttpResponse (io.micronaut.http.HttpResponse)2 HttpStatus (io.micronaut.http.HttpStatus)2 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)2 RequestBinderRegistry (io.micronaut.http.bind.RequestBinderRegistry)2 MediaTypeCodecRegistry (io.micronaut.http.codec.MediaTypeCodecRegistry)2 ServerRequestContext (io.micronaut.http.context.ServerRequestContext)2 NettyHttpHeaders (io.micronaut.http.netty.NettyHttpHeaders)2 ChannelPipelineCustomizer (io.micronaut.http.netty.channel.ChannelPipelineCustomizer)2 OnMessage (io.micronaut.websocket.annotation.OnMessage)2 WebSocketBean (io.micronaut.websocket.context.WebSocketBean)2 WebSocketBeanRegistry (io.micronaut.websocket.context.WebSocketBeanRegistry)2 SslHandler (io.netty.handler.ssl.SslHandler)2 LocalDateTime (java.time.LocalDateTime)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Consumer (java.util.function.Consumer)2 Publisher (org.reactivestreams.Publisher)2