org.restexpress.pipeline.PipelineBuilder Maven / Gradle / Ivy
/*
* Copyright 2010, eCollege, Inc. All rights reserved.
*/
package org.restexpress.pipeline;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpContentCompressor;
import org.jboss.netty.handler.codec.http.HttpContentDecompressor;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.ssl.SslHandler;
import org.jboss.netty.handler.stream.ChunkedWriteHandler;
/**
* Provides a tiny DSL to define the pipeline features.
*
* @author toddf
* @since Aug 27, 2010
*/
public class PipelineBuilder
implements ChannelPipelineFactory
{
// SECTION: CONSTANTS
private static final int DEFAULT_MAX_CONTENT_LENGTH = 20480;
// SECTION: INSTANCE VARIABLES
private List requestHandlers = new ArrayList();
private ExecutionHandler executionHandler = null;
private int maxContentLength = DEFAULT_MAX_CONTENT_LENGTH;
private SSLContext sslContext = null;
// SECTION: CONSTRUCTORS
public PipelineBuilder()
{
super();
}
// SECTION: BUILDER METHODS
public PipelineBuilder setExecutionHandler(ExecutionHandler handler)
{
this.executionHandler = handler;
return this;
}
public PipelineBuilder addRequestHandler(ChannelHandler handler)
{
if (!requestHandlers.contains(handler))
{
requestHandlers.add(handler);
}
return this;
}
/**
* Set the maximum length of the aggregated (chunked) content. If the length of the
* aggregated content exceeds this value, a TooLongFrameException will be raised during
* the request, which can be mapped in the RestExpress server to return a
* BadRequestException, if desired.
*
* @param value
* @return this PipelineBuilder for method chaining.
*/
public PipelineBuilder setMaxContentLength(int value)
{
this.maxContentLength = value;
return this;
}
public PipelineBuilder setSSLContext(SSLContext sslContext)
{
this.sslContext = sslContext;
return this;
}
public SSLContext getSSLContext()
{
return sslContext;
}
// SECTION: CHANNEL PIPELINE FACTORY
@Override
public ChannelPipeline getPipeline()
throws Exception
{
ChannelPipeline pipeline = Channels.pipeline();
if (null != sslContext)
{
SSLEngine sslEngine = sslContext.createSSLEngine();
sslEngine.setUseClientMode(false);
SslHandler sslHandler = new SslHandler(sslEngine);
pipeline.addLast("ssl", sslHandler);
}
// Upstream handlers
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(maxContentLength));
pipeline.addLast("inflater", new HttpContentDecompressor());
// Downstream handlers
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkWriter", new ChunkedWriteHandler());
pipeline.addLast("deflater", new HttpContentCompressor());
if (executionHandler != null)
{
pipeline.addLast("executionHandler", executionHandler);
}
for (ChannelHandler handler : requestHandlers)
{
pipeline.addLast(handler.getClass().getSimpleName(), handler);
}
return pipeline;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy