Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1594] Allow users to customize the Netty pipeline and thus add comp…
…ression
- Loading branch information
Showing
9 changed files
with
113 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,71 @@ | ||
package play.server; | ||
package play.server; | ||
|
||
import org.jboss.netty.channel.ChannelPipeline; | ||
import org.jboss.netty.channel.ChannelPipelineFactory; | ||
import org.jboss.netty.handler.codec.http.HttpChunkAggregator; | ||
import org.jboss.netty.handler.codec.http.HttpRequestDecoder; | ||
import org.jboss.netty.handler.codec.http.HttpResponseEncoder; | ||
import org.jboss.netty.channel.ChannelHandler; | ||
import play.Play; | ||
import play.Logger; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
|
||
import static org.jboss.netty.channel.Channels.pipeline; | ||
|
||
public class HttpServerPipelineFactory implements ChannelPipelineFactory { | ||
|
||
private String pipelineConfig = Play.configuration.getProperty("play.netty.pipeline", "play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.PlayHandler"); | ||
|
||
protected static Map<String, Class> classes = new HashMap<String, Class>(); | ||
|
||
public ChannelPipeline getPipeline() throws Exception { | ||
|
||
Integer max = Integer.valueOf(Play.configuration.getProperty("play.netty.maxContentLength", "-1")); | ||
|
||
ChannelPipeline pipeline = pipeline(); | ||
PlayHandler playHandler = new PlayHandler(); | ||
|
||
pipeline.addLast("flashPolicy", new FlashPolicyHandler()); | ||
pipeline.addLast("decoder", new HttpRequestDecoder()); | ||
pipeline.addLast("aggregator", new StreamChunkAggregator(max)); | ||
pipeline.addLast("encoder", new HttpResponseEncoder()); | ||
pipeline.addLast("chunkedWriter", playHandler.chunkedWriteHandler); | ||
pipeline.addLast("handler", playHandler); | ||
// Get all the pipeline. Give the user the opportunity to add their own | ||
String[] handlers = pipelineConfig.split(","); | ||
for (int i = 0; i < handlers.length - 1; i++) { | ||
String handler = handlers[i]; | ||
try { | ||
String name = getName(handler.trim()); | ||
ChannelHandler instance = getInstance(handler); | ||
if (instance != null) { | ||
pipeline.addLast(name, instance); | ||
Server.pipelines.put(name, instance); | ||
} | ||
} catch(Throwable e) { | ||
Logger.error(" error adding " + handler, e); | ||
} | ||
|
||
} | ||
|
||
// The last one is always the play handler | ||
String handler = handlers[handlers.length - 1]; | ||
ChannelHandler instance = getInstance(handler); | ||
if (instance != null) { | ||
pipeline.addLast("handler", instance); | ||
Server.pipelines.put("handler", instance); | ||
} | ||
|
||
return pipeline; | ||
} | ||
|
||
protected String getName(String name) { | ||
if (name.lastIndexOf(".") > 0) | ||
return name.substring(name.lastIndexOf(".") + 1); | ||
return name; | ||
} | ||
|
||
protected ChannelHandler getInstance(String name) throws Exception { | ||
|
||
Class clazz = classes.get(name); | ||
if (clazz == null) { | ||
clazz = Class.forName(name); | ||
classes.put(name, clazz); | ||
} | ||
if (ChannelHandler.class.isAssignableFrom(clazz)) | ||
return (ChannelHandler)clazz.newInstance(); | ||
return null; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters