From ded8373cb6b0dbdf44f66b763c9d92b022d42d0e Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Fri, 22 May 2015 16:04:31 +0200 Subject: [PATCH] =?UTF-8?q?move=20XmlRpcDelegatingHttpRequestHandler=20to?= =?UTF-8?q?=20SubServer=20=E2=80=94=20it=20used=20only=20here?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/org/jetbrains/io/BuiltInServer.java | 62 +++---------------- .../src/org/jetbrains/io/SubServer.java | 47 +++++++++++++- 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/platform/built-in-server/src/org/jetbrains/io/BuiltInServer.java b/platform/built-in-server/src/org/jetbrains/io/BuiltInServer.java index 981e193dde5c..00ee54626d05 100644 --- a/platform/built-in-server/src/org/jetbrains/io/BuiltInServer.java +++ b/platform/built-in-server/src/org/jetbrains/io/BuiltInServer.java @@ -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 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 handlers; - - public XmlRpcDelegatingHttpRequestHandler(Map 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); - } - } } \ No newline at end of file diff --git a/platform/built-in-server/src/org/jetbrains/io/SubServer.java b/platform/built-in-server/src/org/jetbrains/io/SubServer.java index afcb6d074e98..82f670ce4b5c 100644 --- a/platform/built-in-server/src/org/jetbrains/io/SubServer.java +++ b/platform/built-in-server/src/org/jetbrains/io/SubServer.java @@ -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 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 handlers; + + public XmlRpcDelegatingHttpRequestHandler(Map 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); + } + } } \ No newline at end of file