Skip to content

Commit

Permalink
re-factor handler classes to possibly enable handlers coded in Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwil committed Dec 21, 2009
1 parent 2fb3414 commit b1a6080
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 38 deletions.
2 changes: 0 additions & 2 deletions javalib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,11 @@
<groupId>org.jruby</groupId>
<artifactId>jruby</artifactId>
<version>1.4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.1.5.GA</version>
<scope>compile</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.kevwil.aspen;

import org.jboss.netty.channel.*;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import org.jboss.netty.handler.codec.http.*;
import org.jboss.netty.buffer.ChannelBuffers;

/**
* Netty callback handler base class with common exception handling
*
* @author kevwil
* @since Dec. 21, 2009
*/
public class AspenUpstreamHandlerBase
extends SimpleChannelUpstreamHandler
{
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored"})
@Override
public void exceptionCaught( final ChannelHandlerContext ctx, final ExceptionEvent e )
throws Exception
{
Channel ch = e.getChannel();
Throwable cause = e.getCause();
if( cause instanceof TooLongFrameException )
{
sendError( ctx, HttpResponseStatus.BAD_REQUEST );
return;
}
cause.printStackTrace();
if( ch.isConnected() )
{
sendError( ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR );
}
}

private void sendError( ChannelHandlerContext ctx, HttpResponseStatus status )
{
HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, status );
response.setHeader(
HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8" );
response.setContent( ChannelBuffers.copiedBuffer(
"Failure: " + status.toString() + "\r\n", "UTF-8") );
// Close the connection as soon as the error message is sent.
ctx.getChannel().write( response ).addListener( ChannelFutureListener.CLOSE );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.kevwil.aspen;

import org.jboss.netty.channel.ChannelPipelineCoverage;

/**
* Non-thread-safe Netty callback handler
*
* "one" means you must create a new instance of
* this handler type for each new channel. It means
* the member variables of the handler instance can not be
* shared at all, and violating this contract will
* lead the handler to a race condition.
*
* You need to override the UpstreamHandler method you want to handle.
*
* @author kevwil
* @since Dec. 21, 2009
*/
@ChannelPipelineCoverage("one")
public class MultipleInstanceHandler
extends AspenUpstreamHandlerBase
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
* @author kevwil
* @since Jun 25, 2009
*/
@ChannelPipelineCoverage("one")
public class NettyServerHandler
extends SimpleChannelUpstreamHandler
extends MultipleInstanceHandler
{
private RackProxy _rack;

Expand All @@ -31,34 +30,4 @@ public void messageReceived( final ChannelHandlerContext ctx, final MessageEvent
// write the response out
e.getChannel().write( response ).addListener( ChannelFutureListener.CLOSE );
}

@SuppressWarnings({"ThrowableResultOfMethodCallIgnored"})
@Override
public void exceptionCaught( final ChannelHandlerContext ctx, final ExceptionEvent e )
throws Exception
{
Channel ch = e.getChannel();
Throwable cause = e.getCause();
if( cause instanceof TooLongFrameException )
{
sendError( ctx, HttpResponseStatus.BAD_REQUEST );
return;
}
cause.printStackTrace();
if( ch.isConnected() )
{
sendError( ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR );
}
}

private void sendError( ChannelHandlerContext ctx, HttpResponseStatus status )
{
HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, status );
response.setHeader(
HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8" );
response.setContent( ChannelBuffers.copiedBuffer(
"Failure: " + status.toString() + "\r\n", "UTF-8") );
// Close the connection as soon as the error message is sent.
ctx.getChannel().write( response ).addListener( ChannelFutureListener.CLOSE );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class RackUtil
{
private RackUtil(){}

public static ChannelHandlerContext buildChannelHandlerContext( final String server, final String port )
public static ChannelHandlerContext buildDummyChannelHandlerContext( final String server, final String port )
{
int p = 80;
if( port != null && port.length() > 0 )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.kevwil.aspen;

import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

/**
* thread-safe Netty callback handler
*
* By using this base class, you assert that member variables
* are designed to be OK to share among multiple channels (or
* there's nothing to share).
*
* You need to override the UpstreamHandler method you want to handle.
*
* @author kevwil
* @since Dec. 21, 2009
*/
@ChannelPipelineCoverage("all")
public class SingleInstanceHandler
extends AspenUpstreamHandlerBase
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void setUp()
{
server = "localhost";
port = "8080";
ctx = RackUtil.buildChannelHandlerContext( server, port );
ctx = RackUtil.buildDummyChannelHandlerContext( server, port );
r = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, "http://"+server+":"+port+"/" );
env = RubyHash.newHash( ruby );
}
Expand All @@ -50,7 +50,7 @@ public void shouldCreateDummyChannelHandlerContextWithLocalServerAddress() throw
@Test
public void shouldUsePort80() throws Exception
{
ctx = RackUtil.buildChannelHandlerContext( server, null );
ctx = RackUtil.buildDummyChannelHandlerContext( server, null );
assertNotNull( ctx );
assertNotNull( ctx.getChannel() );
assertNotNull( ctx.getChannel().getLocalAddress() );
Expand Down Expand Up @@ -88,7 +88,7 @@ public void shouldParseUriFromHostHeader() throws Exception
@Test
public void shouldParseServerPortWhenNoneGiven() throws Exception
{
ctx = RackUtil.buildChannelHandlerContext( server, null );
ctx = RackUtil.buildDummyChannelHandlerContext( server, null );
r = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, "http://"+server+"/" );
RackUtil.doUriRelated( ctx, r, env );

Expand Down

0 comments on commit b1a6080

Please sign in to comment.