use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project databus by linkedin.
the class TestResponseProcessors method testTargetSCNHappyPathNonChunked.
@Test
public void testTargetSCNHappyPathNonChunked() throws Exception {
TestAbstractQueue queue = new TestAbstractQueue();
TestConnectionStateMessage stateMsg = new TestConnectionStateMessage();
DefaultHttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
TestRemoteExceptionHandler remoteExHandler = new TestRemoteExceptionHandler();
Checkpoint cp = new Checkpoint();
cp.setConsumptionMode(DbusClientMode.BOOTSTRAP_SNAPSHOT);
BootstrapTargetScnHttpResponseProcessor processor = new BootstrapTargetScnHttpResponseProcessor(null, queue, stateMsg, cp, remoteExHandler, null);
ChannelBuffer buf = getScnResponse();
httpResponse.setContent(buf);
httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "1000");
processor.startResponse(httpResponse);
processor.finishResponse();
Assert.assertEquals("Error Handled", false, processor._errorHandled);
Assert.assertEquals("Processor Response State", AbstractHttpResponseProcessorDecorator.ResponseStatus.CHUNKS_FINISHED, processor._responseStatus);
Assert.assertEquals("Actor Queue Size", 1, queue.getMessages().size());
Assert.assertEquals("Expected ConnectionStateMessage", "TestConnectionStateMessage", queue.getMessages().get(0).getClass().getSimpleName());
TestConnectionStateMessage gotMsg = (TestConnectionStateMessage) (queue.getMessages().get(0));
System.out.println("Long Max is :" + Long.MAX_VALUE);
Assert.assertEquals("Expected ConnectionStateMessage State", TestConnectionStateMessage.State.TARGETSCN_RESPONSE_SUCCESS, gotMsg._state);
Assert.assertEquals("StartSCN Response Id Check", new Long(5678912), cp.getBootstrapTargetScn());
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project databus by linkedin.
the class TestResponseProcessors method testStreamExceptionAfterStartCase1.
@Test
public void testStreamExceptionAfterStartCase1() throws Exception {
TestAbstractQueue queue = new TestAbstractQueue();
TestConnectionStateMessage stateMsg = new TestConnectionStateMessage();
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
StreamHttpResponseProcessor processor = new StreamHttpResponseProcessor(null, queue, stateMsg, null);
ChannelBuffer buf = HeapChannelBufferFactory.getInstance().getBuffer(100);
HttpChunk httpChunk = new DefaultHttpChunk(buf);
HttpChunkTrailer httpChunkTrailer = new DefaultHttpChunkTrailer();
processor.startResponse(httpResponse);
processor.channelException(new Exception("dummy exception"));
processor.addChunk(httpChunk);
processor.addTrailer(httpChunkTrailer);
processor.finishResponse();
Assert.assertEquals("Error Handled", true, processor._errorHandled);
Assert.assertEquals("Processor Response State", AbstractHttpResponseProcessorDecorator.ResponseStatus.CHUNKS_FINISHED, processor._responseStatus);
Assert.assertEquals("Actor Queue Size", 1, queue.getMessages().size());
Assert.assertEquals("Expected ConnectionStateMessage", "TestConnectionStateMessage", queue.getMessages().get(0).getClass().getSimpleName());
TestConnectionStateMessage gotMsg = (TestConnectionStateMessage) (queue.getMessages().get(0));
Assert.assertEquals("Expected ConnectionStateMessage State", TestConnectionStateMessage.State.STREAM_RESPONSE_SUCCESS, gotMsg._state);
Assert.assertEquals("No More CHunks", true, stateMsg._channel.hasNoMoreChunks());
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project databus by linkedin.
the class TestResponseProcessors method testTargetSCNBootstrapTooOldException.
@Test
public void testTargetSCNBootstrapTooOldException() throws Exception {
TestAbstractQueue queue = new TestAbstractQueue();
TestConnectionStateMessage stateMsg = new TestConnectionStateMessage();
DefaultHttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
TestRemoteExceptionHandler remoteExHandler = new TestRemoteExceptionHandler();
remoteExHandler._exType = ExceptionType.BOOTSTRAP_TOO_OLD_EXCEPTION;
Checkpoint cp = new Checkpoint();
cp.setConsumptionMode(DbusClientMode.BOOTSTRAP_CATCHUP);
BootstrapTargetScnHttpResponseProcessor processor = new BootstrapTargetScnHttpResponseProcessor(null, queue, stateMsg, cp, remoteExHandler, null);
ChannelBuffer buf = getScnResponse();
HttpChunk httpChunk = new DefaultHttpChunk(buf);
HttpChunkTrailer httpChunkTrailer = new DefaultHttpChunkTrailer();
processor.startResponse(httpResponse);
processor.addChunk(httpChunk);
processor.addTrailer(httpChunkTrailer);
processor.finishResponse();
Assert.assertEquals("Error Handled", false, processor._errorHandled);
Assert.assertEquals("Processor Response State", AbstractHttpResponseProcessorDecorator.ResponseStatus.CHUNKS_FINISHED, processor._responseStatus);
Assert.assertEquals("Actor Queue Size", 1, queue.getMessages().size());
Assert.assertEquals("Expected ConnectionStateMessage", "TestConnectionStateMessage", queue.getMessages().get(0).getClass().getSimpleName());
TestConnectionStateMessage gotMsg = (TestConnectionStateMessage) (queue.getMessages().get(0));
Assert.assertEquals("Expected ConnectionStateMessage State", TestConnectionStateMessage.State.TARGETSCN_RESPONSE_ERROR, gotMsg._state);
Assert.assertEquals("No response", true, (null == stateMsg._cp));
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project databus by linkedin.
the class TestResponseProcessors method testStartSCNExceptionAfterFinish.
@Test
public void testStartSCNExceptionAfterFinish() throws Exception {
TestAbstractQueue queue = new TestAbstractQueue();
TestConnectionStateMessage stateMsg = new TestConnectionStateMessage();
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
TestRemoteExceptionHandler remoteExHandler = new TestRemoteExceptionHandler();
Checkpoint cp = new Checkpoint();
cp.setConsumptionMode(DbusClientMode.BOOTSTRAP_SNAPSHOT);
BootstrapStartScnHttpResponseProcessor processor = new BootstrapStartScnHttpResponseProcessor(null, queue, stateMsg, cp, remoteExHandler, null);
ChannelBuffer buf = getScnResponse();
HttpChunk httpChunk = new DefaultHttpChunk(buf);
HttpChunkTrailer httpChunkTrailer = new DefaultHttpChunkTrailer();
processor.startResponse(httpResponse);
processor.addChunk(httpChunk);
processor.addTrailer(httpChunkTrailer);
processor.finishResponse();
processor.channelException(new Exception("dummy exception"));
Assert.assertEquals("Error Handled", true, processor._errorHandled);
Assert.assertEquals("Processor Response State", AbstractHttpResponseProcessorDecorator.ResponseStatus.CHUNKS_EXCEPTION, processor._responseStatus);
Assert.assertEquals("Actor Queue Size", 1, queue.getMessages().size());
Assert.assertEquals("Expected ConnectionStateMessage", "TestConnectionStateMessage", queue.getMessages().get(0).getClass().getSimpleName());
TestConnectionStateMessage gotMsg = (TestConnectionStateMessage) (queue.getMessages().get(0));
Assert.assertEquals("Expected ConnectionStateMessage State", TestConnectionStateMessage.State.STARTSCN_RESPONSE_SUCCESS, gotMsg._state);
Assert.assertEquals("StartSCN Response Id Check", new Long(5678912), cp.getBootstrapStartScn());
}
use of org.jboss.netty.handler.codec.http.DefaultHttpResponse in project socket.io-netty by ibdknox.
the class WebSocketServerHandler method handleHttpRequest.
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
String reqURI = req.getUri();
if (reqURI.contains(POLLING_PATH)) {
String[] parts = reqURI.split("/");
String ID = parts.length > 3 ? parts[3] : "";
PollingIOClient client = (PollingIOClient) this.pollingClients.get(ID);
if (client == null) {
//new client
client = connectPoller(ctx);
client.Reconnect(ctx, req);
return;
}
if (req.getMethod() == GET) {
client.heartbeat();
client.Reconnect(ctx, req);
} else {
//we got a message
QueryStringDecoder decoder = new QueryStringDecoder("/?" + req.getContent().toString(CharsetUtil.UTF_8));
String message = decoder.getParameters().get("data").get(0);
handleMessage(client, message);
//make sure the connection is closed once we send a response
setKeepAlive(req, false);
//send a response that allows for cross domain access
HttpResponse resp = new DefaultHttpResponse(HTTP_1_1, OK);
resp.addHeader("Access-Control-Allow-Origin", "*");
sendHttpResponse(ctx, req, resp);
}
return;
}
// Serve the WebSocket handshake request.
String location = "";
if (reqURI.equals(WEBSOCKET_PATH)) {
location = getWebSocketLocation(req);
} else if (reqURI.equals(FLASHSOCKET_PATH)) {
location = getFlashSocketLocation(req);
}
if (location != "" && Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION)) && WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) {
// Create the WebSocket handshake response.
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
res.addHeader(Names.UPGRADE, WEBSOCKET);
res.addHeader(CONNECTION, Values.UPGRADE);
// Fill in the headers and contents depending on handshake method.
if (req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2)) {
// New handshake method with a challenge:
res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
}
// Calculate the answer of the challenge.
String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length());
int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length());
long c = req.getContent().readLong();
ChannelBuffer input = ChannelBuffers.buffer(16);
input.writeInt(a);
input.writeInt(b);
input.writeLong(c);
ChannelBuffer output = ChannelBuffers.wrappedBuffer(MessageDigest.getInstance("MD5").digest(input.array()));
res.setContent(output);
} else {
// Old handshake method with no challenge:
res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(WEBSOCKET_PROTOCOL, protocol);
}
}
// Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline();
p.remove("aggregator");
p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());
ctx.getChannel().write(res);
p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
connectSocket(ctx);
return;
}
// Send an error page otherwise.
sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
}
Aggregations