forked from kevwil/aspen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-factor handler classes to possibly enable handlers coded in Ruby
- Loading branch information
Showing
7 changed files
with
96 additions
and
38 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
46 changes: 46 additions & 0 deletions
46
javalib/src/main/java/com/github/kevwil/aspen/AspenUpstreamHandlerBase.java
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 |
---|---|---|
@@ -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 ); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
javalib/src/main/java/com/github/kevwil/aspen/MultipleInstanceHandler.java
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 |
---|---|---|
@@ -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 | ||
{ | ||
} |
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
22 changes: 22 additions & 0 deletions
22
javalib/src/main/java/com/github/kevwil/aspen/SingleInstanceHandler.java
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 |
---|---|---|
@@ -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 | ||
{ | ||
} |
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