ratpack.server.internal.NettyHandlerAdapter Maven / Gradle / Ivy
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ratpack.server.internal;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.http.*;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.event.internal.DefaultEventController;
import ratpack.exec.ExecControl;
import ratpack.exec.ExecController;
import ratpack.file.internal.ActivationBackedMimeTypes;
import ratpack.file.internal.ShouldCompressPredicate;
import ratpack.func.Action;
import ratpack.func.Pair;
import ratpack.handling.Handler;
import ratpack.handling.Handlers;
import ratpack.handling.RequestOutcome;
import ratpack.handling.direct.DirectChannelAccess;
import ratpack.handling.direct.internal.DefaultDirectChannelAccess;
import ratpack.handling.internal.ChainHandler;
import ratpack.handling.internal.DefaultContext;
import ratpack.handling.internal.DescribingHandler;
import ratpack.handling.internal.DescribingHandlers;
import ratpack.http.MutableHeaders;
import ratpack.http.Request;
import ratpack.http.Response;
import ratpack.http.internal.*;
import ratpack.registry.Registry;
import ratpack.render.internal.DefaultRenderController;
import ratpack.server.ServerConfig;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicBoolean;
@ChannelHandler.Sharable
public class NettyHandlerAdapter extends SimpleChannelInboundHandler {
private static final AttributeKey RESPONSE_TRANSMITTER_ATTRIBUTE_KEY = AttributeKey.valueOf(DefaultResponseTransmitter.class.getName());
private static final AttributeKey> CHANNEL_SUBSCRIBER_ATTRIBUTE_KEY = AttributeKey.valueOf("ratpack.subscriber");
private final static Logger LOGGER = LoggerFactory.getLogger(NettyHandlerAdapter.class);
private final Handler[] handlers;
private final DefaultContext.ApplicationConstants applicationConstants;
private final ExecController execController;
private final Predicate> shouldCompress;
private final Registry rootRegistry;
private final boolean addResponseTimeHeader;
private final ExecControl execControl;
public NettyHandlerAdapter(ServerConfig serverConfig, Registry registry, Handler handler) throws Exception {
super(false);
this.handlers = ChainHandler.unpack(handler);
this.rootRegistry = registry;
this.addResponseTimeHeader = serverConfig.isTimeResponses();
this.applicationConstants = new DefaultContext.ApplicationConstants(this.rootRegistry, new DefaultRenderController(), Handlers.notFound());
this.execController = registry.get(ExecController.class);
this.execControl = execController.getControl();
if (serverConfig.isCompressResponses()) {
ImmutableSet blacklist = serverConfig.getCompressionMimeTypeBlackList();
this.shouldCompress = new ShouldCompressPredicate(
serverConfig.getCompressionMinSize(),
serverConfig.getCompressionMimeTypeWhiteList(),
blacklist.isEmpty() ? ActivationBackedMimeTypes.getDefaultExcludedMimeTypes() : blacklist
);
} else {
this.shouldCompress = Predicates.alwaysFalse();
}
}
@Override
public void channelRead(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
if (!(msg instanceof FullHttpRequest)) {
Action
© 2015 - 2025 Weber Informatics LLC | Privacy Policy