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;
}
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);
}
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);
}
}
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);
}
}
}
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());
}
}
Aggregations