use of org.jboss.netty.handler.codec.http.HttpChunk in project databus by linkedin.
the class HttpRequestHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
/*NettyStats nettyStats = _configManager.getNettyStats();
CallCompletion callCompletion = nettyStats.isEnabled() ?
nettyStats.getRequestHandler_messageRecieved().startCall() :
null;*/
try {
if (!readingChunks) {
request = (HttpRequest) e.getMessage();
ctx.sendUpstream(e);
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
String queryPath = queryStringDecoder.getPath();
int slashPos = queryPath.indexOf('/', 1);
if (slashPos < 0) {
slashPos = queryPath.length();
}
String cmdName = queryPath.substring(1, slashPos);
ServerContainer.RuntimeConfig config = _serverContainer.getContainerRuntimeConfigMgr().getReadOnlyConfig();
if (LOG.isDebugEnabled()) {
LOG.debug("Got command: " + cmdName);
}
dbusRequest = new DatabusRequest(cmdName, request.getMethod(), e.getRemoteAddress(), config);
if (LOG.isDebugEnabled()) {
LOG.debug("Starting processing command [" + dbusRequest.getId() + "] " + dbusRequest.getName());
}
Properties requestProps = dbusRequest.getParams();
if (slashPos < queryPath.length()) {
requestProps.put(DatabusRequest.PATH_PARAM_NAME, queryPath.substring(slashPos + 1));
}
for (Map.Entry<String, String> h : request.getHeaders()) {
handleHttpHeader(h);
}
Map<String, List<String>> params = queryStringDecoder.getParameters();
if (!params.isEmpty()) {
for (Entry<String, List<String>> p : params.entrySet()) {
String key = p.getKey();
List<String> vals = p.getValue();
if (vals.size() == 1) {
requestProps.put(key, vals.get(0));
} else {
requestProps.put(key, vals);
}
for (String val : vals) {
LOG.trace("PARAM: " + key + " = " + val);
}
}
}
if (request.isChunked()) {
if (null != _readTimeoutHandler) {
readingChunks = true;
_readTimeoutHandler.start(ctx.getPipeline().getContext(_readTimeoutHandler));
}
} else {
ChannelBuffer content = request.getContent();
handleRequestContentChunk(content);
writeResponse(ctx, e);
}
} else if (e.getMessage() instanceof HttpChunk) {
HttpChunk chunk = (HttpChunk) e.getMessage();
if (chunk.isLast()) {
readingChunks = false;
LOG.trace("END OF CONTENT");
HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
for (Map.Entry<String, String> h : trailer.getHeaders()) {
handleHttpHeader(h);
}
writeResponse(ctx, e);
} else {
ChannelBuffer content = chunk.getContent();
handleRequestContentChunk(content);
}
}
ctx.sendUpstream(e);
// FIXME DDS-305: Rework the netty stats collector to use event-based stats aggregation
/*if (null != callCompletion)
{
callCompletion.endCall();
}*/
} catch (Exception ex) {
LOG.error("HttpRequestHandler.messageReceived error", ex);
// FIXME DDS-305: Rework the netty stats collector to use event-based stats aggregation
/*if (null != callCompletion)
{
callCompletion.endCallWithError(ex);
}*/
}
}
use of org.jboss.netty.handler.codec.http.HttpChunk in project databus by linkedin.
the class MockServerChannelHandler method printMessage.
// auxiliary method to print messages
private void printMessage(String prefix, MessageEvent e) {
Object msgO = e.getMessage();
String resp;
if (msgO instanceof HttpRequest) {
HttpRequest msgReq = (HttpRequest) msgO;
// Matcher result = pattern.matcher(msgReq.getUri());
resp = msgReq.getUri();
} else if (msgO instanceof HttpResponse) {
HttpResponse msgReq = (HttpResponse) msgO;
resp = msgReq.toString();
} else if (msgO instanceof HttpChunk) {
HttpChunk msgReq = (HttpChunk) msgO;
resp = msgReq.toString();
} else {
ChannelBuffer msg = (ChannelBuffer) msgO;
byte[] bytes = new byte[msg.capacity()];
msg.readBytes(bytes);
msg.setIndex(0, bytes.length);
StringBuilder out = new StringBuilder("MSG: ").append(e.getChannel().getRemoteAddress());
out.append("\nMESSAGE length=").append(bytes.length).append("\n").append(new String(bytes));
resp = out.toString();
}
LOG.debug(prefix + resp);
}
use of org.jboss.netty.handler.codec.http.HttpChunk in project databus by linkedin.
the class TestHttpResponseProcessor method testHappyPathChunking.
@Test
public void testHappyPathChunking() throws DatabusException {
Logger log = Logger.getLogger("GenericHttpResponseHandler.testHappyPathChunking");
final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(KeepAliveType.KEEP_ALIVE);
responseHandler.getLog().setLevel(_logLevel);
TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
TestConnectListener connectListener = new TestConnectListener(log);
TestSendRequestListener requestListener = new TestSendRequestListener(log);
TestCloseListener closeListener = new TestCloseListener(log);
responseHandler.setConnectionListener(connectListener);
Channel channel = createClientBootstrap(responseHandler);
SocketAddress clientAddr = channel.getLocalAddress();
try {
setListeners(responseHandler, respProcessor, requestListener, closeListener);
channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
// It seems that there is a race condition between the writeFuture succeeding
// and the writeComplete message getting to the handler. Make sure that the
// writeComplete has got to the handler before we do anything else with
// the channel.
final GenericHttpResponseHandler handler = getResponseHandler(channel);
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return handler._messageState.hasSentRequest();
}
}, "request sent", 1000, log);
HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
resp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
sendServerResponse(clientAddr, resp, 1000);
HttpChunk chunk1 = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer("chunk1".getBytes(Charset.defaultCharset())));
sendServerResponse(clientAddr, chunk1, 1000);
HttpChunk chunk2 = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer("chunk2".getBytes(Charset.defaultCharset())));
sendServerResponse(clientAddr, chunk2, 1000);
sendServerResponse(clientAddr, new DefaultHttpChunkTrailer(), 1000);
final List<String> callbacks = respProcessor.getCallbacks();
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return callbacks.size() == 5;
}
}, "waiting for response processed", 1000, null);
final List<String> connectCallbacks = connectListener.getCallbacks();
final List<String> requestCallbacks = requestListener.getCallbacks();
final List<String> closeCallbacks = closeListener.getCallbacks();
stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
Assert.assertEquals(callbacks.get(0), "startResponse");
Assert.assertEquals(callbacks.get(1), "addChunk");
Assert.assertEquals(callbacks.get(2), "addChunk");
Assert.assertEquals(callbacks.get(3), "addTrailer");
Assert.assertEquals(callbacks.get(4), "finishResponse");
// make sure that no new callbacks have showed up
Assert.assertEquals(callbacks.size(), 5);
} finally {
channel.close();
}
}
use of org.jboss.netty.handler.codec.http.HttpChunk in project databus by linkedin.
the class DummyRemoteExceptionHandler method runHappyPathRegister.
private void runHappyPathRegister(final Logger log, TestingConnectionCallback callback, DummyRemoteExceptionHandler remoteExceptionHandler, final NettyHttpDatabusRelayConnection conn, TestResponseProcessors.TestConnectionStateMessage msg, SocketAddress clientAddr, SimpleObjectCaptureHandler objCapture, HttpResponse sourcesResp) throws JsonGenerationException, JsonMappingException, IOException {
HttpRequest msgReq;
HttpChunk body;
objCapture.clear();
conn.requestRegister("1", msg);
// verify server gets the /register request
msgReq = captureRequest(objCapture);
Assert.assertTrue(msgReq.getUri().startsWith("/register"));
// send back some response
RegisterResponseEntry entry = new RegisterResponseEntry(1L, (short) 1, SOURCE1_SCHEMA_STR);
String responseStr = NettyTestUtils.generateRegisterResponse(entry);
body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(responseStr.getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(_dummyServer, clientAddr, sourcesResp, body);
waitForCallback(callback, TestResponseProcessors.TestConnectionStateMessage.State.REGISTER_SUCCESS, log);
Assert.assertNull(remoteExceptionHandler.getLastException());
}
use of org.jboss.netty.handler.codec.http.HttpChunk in project databus by linkedin.
the class DummyRemoteExceptionHandler method runHappyPathSources.
private HttpResponse runHappyPathSources(final Logger log, TestingConnectionCallback callback, DummyRemoteExceptionHandler remoteExceptionHandler, SocketAddress clientAddr, SimpleObjectCaptureHandler objCapture) {
Assert.assertTrue(objCapture.waitForMessage(1000, 0));
Object msgObj = objCapture.getMessages().get(0);
Assert.assertTrue(msgObj instanceof HttpRequest);
HttpRequest msgReq = (HttpRequest) msgObj;
// now has "?protocolVersion=X" appended
Assert.assertTrue(msgReq.getUri().startsWith("/sources"));
callback.clearLastMsg();
objCapture.clear();
// send back some response
HttpResponse sourcesResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
sourcesResp.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
sourcesResp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
HttpChunk body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer("[{\"id\":1,\"name\":\"test.source1\"}]".getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(_dummyServer, clientAddr, sourcesResp, body);
waitForCallback(callback, TestResponseProcessors.TestConnectionStateMessage.State.SOURCES_SUCCESS, log);
Assert.assertNull(remoteExceptionHandler.getLastException());
callback.clearLastMsg();
objCapture.clear();
return sourcesResp;
}
Aggregations