move XmlRpcDelegatingHttpRequestHandler to SubServer — it used only here

This commit is contained in:
Vladimir Krivosheev
2015-05-26 14:28:06 +02:00
parent d479e8f030
commit ded8373cb6
2 changed files with 56 additions and 53 deletions
@@ -15,7 +15,6 @@
*/
package org.jetbrains.io;
import com.intellij.ide.XmlRpcServer;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
@@ -24,11 +23,7 @@ import com.intellij.util.net.NetUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.QueryStringDecoder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.ide.CustomPortServerManager;
import org.jetbrains.ide.PooledThreadExecutor;
@@ -36,7 +31,6 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Map;
public class BuiltInServer implements Disposable {
static final Logger LOG = Logger.getInstance(BuiltInServer.class);
@@ -59,7 +53,8 @@ public class BuiltInServer implements Disposable {
public static BuiltInServer start(int workerCount, int firstPort, int portsCount, boolean tryAnyPort) throws Throwable {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(workerCount, PooledThreadExecutor.INSTANCE);
ChannelRegistrar channelRegistrar = new ChannelRegistrar();
ServerBootstrap bootstrap = createServerBootstrap(eventLoopGroup, channelRegistrar, null);
ServerBootstrap bootstrap = NettyUtil.nioServerBootstrap(eventLoopGroup);
configureChildHandler(bootstrap, channelRegistrar);
int port = bind(firstPort, portsCount, tryAnyPort, bootstrap, channelRegistrar);
BuiltInServer server = new BuiltInServer(eventLoopGroup, port);
bindCustomPorts(server);
@@ -70,32 +65,14 @@ public class BuiltInServer implements Disposable {
return port;
}
@NotNull
static ServerBootstrap createServerBootstrap(@NotNull EventLoopGroup eventLoopGroup,
@NotNull final ChannelRegistrar channelRegistrar,
@Nullable Map<String, Object> xmlRpcHandlers) {
ServerBootstrap bootstrap = NettyUtil.nioServerBootstrap(eventLoopGroup);
if (xmlRpcHandlers == null) {
final PortUnificationServerHandler portUnificationServerHandler = new PortUnificationServerHandler();
bootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(channelRegistrar, portUnificationServerHandler);
}
});
}
else {
final XmlRpcDelegatingHttpRequestHandler handler = new XmlRpcDelegatingHttpRequestHandler(xmlRpcHandlers);
bootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(channelRegistrar);
NettyUtil.addHttpServerCodec(channel.pipeline());
channel.pipeline().addLast(handler);
}
});
}
return bootstrap;
static void configureChildHandler(@NotNull ServerBootstrap bootstrap, @NotNull final ChannelRegistrar channelRegistrar) {
final PortUnificationServerHandler portUnificationServerHandler = new PortUnificationServerHandler();
bootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(channelRegistrar, portUnificationServerHandler);
}
});
}
private static void bindCustomPorts(@NotNull BuiltInServer server) {
@@ -164,23 +141,4 @@ public class BuiltInServer implements Disposable {
public static void replaceDefaultHandler(@NotNull ChannelHandlerContext context, @NotNull ChannelHandler channelHandler) {
context.pipeline().replace(DelegatingHttpRequestHandler.class, "replacedDefaultHandler", channelHandler);
}
@ChannelHandler.Sharable
private static final class XmlRpcDelegatingHttpRequestHandler extends DelegatingHttpRequestHandlerBase {
private final Map<String, Object> handlers;
public XmlRpcDelegatingHttpRequestHandler(Map<String, Object> handlers) {
this.handlers = handlers;
}
@Override
protected boolean process(@NotNull ChannelHandlerContext context, @NotNull FullHttpRequest request, @NotNull QueryStringDecoder urlDecoder) throws IOException {
if (handlers.isEmpty()) {
// not yet initialized, for example, P2PTransport could add handlers after we bound.
return false;
}
return request.method() == HttpMethod.POST && XmlRpcServer.SERVICE.getInstance().process(urlDecoder.path(), request, context, handlers);
}
}
}
@@ -15,14 +15,24 @@
*/
package org.jetbrains.io;
import com.intellij.ide.XmlRpcServer;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.net.NetUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.QueryStringDecoder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.ide.CustomPortServerManager;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Map;
final class SubServer implements CustomPortServerManager.CustomPortService, Disposable {
private ChannelRegistrar channelRegistrar;
@@ -47,7 +57,23 @@ final class SubServer implements CustomPortServerManager.CustomPortService, Disp
channelRegistrar = new ChannelRegistrar();
}
ServerBootstrap bootstrap = BuiltInServer.createServerBootstrap(server.eventLoopGroup, channelRegistrar, user.createXmlRpcHandlers());
ServerBootstrap bootstrap = NettyUtil.nioServerBootstrap(server.eventLoopGroup);
Map<String, Object> xmlRpcHandlers = user.createXmlRpcHandlers();
if (xmlRpcHandlers == null) {
BuiltInServer.configureChildHandler(bootstrap, channelRegistrar);
}
else {
final XmlRpcDelegatingHttpRequestHandler handler = new XmlRpcDelegatingHttpRequestHandler(xmlRpcHandlers);
bootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(channelRegistrar);
NettyUtil.addHttpServerCodec(channel.pipeline());
channel.pipeline().addLast(handler);
}
});
}
try {
bootstrap.localAddress(user.isAvailableExternally() ? new InetSocketAddress(port) : new InetSocketAddress(NetUtils.getLoopbackAddress(), port));
channelRegistrar.add(bootstrap.bind().syncUninterruptibly().channel());
@@ -86,4 +112,23 @@ final class SubServer implements CustomPortServerManager.CustomPortService, Disp
stop();
user.setManager(null);
}
@ChannelHandler.Sharable
private static final class XmlRpcDelegatingHttpRequestHandler extends DelegatingHttpRequestHandlerBase {
private final Map<String, Object> handlers;
public XmlRpcDelegatingHttpRequestHandler(Map<String, Object> handlers) {
this.handlers = handlers;
}
@Override
protected boolean process(@NotNull ChannelHandlerContext context, @NotNull FullHttpRequest request, @NotNull QueryStringDecoder urlDecoder) throws IOException {
if (handlers.isEmpty()) {
// not yet initialized, for example, P2PTransport could add handlers after we bound.
return false;
}
return request.method() == HttpMethod.POST && XmlRpcServer.SERVICE.getInstance().process(urlDecoder.path(), request, context, handlers);
}
}
}