use of org.jboss.netty.handler.codec.http.HttpChunk 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.HttpChunk 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.HttpChunk in project voldemort by voldemort.
the class CoordinatorAdminRequestHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
if (!readingChunks) {
HttpRequest request = this.request = (HttpRequest) messageEvent.getMessage();
String requestURI = this.request.getUri();
if (logger.isDebugEnabled()) {
logger.debug("Admin Request URI: " + requestURI);
}
if (request.isChunked()) {
readingChunks = true;
} else {
String[] requestUriSegments = requestURI.split("/");
HttpResponse response;
if (requestUriSegments.length > 0 && requestUriSegments[1].equals(STORE_OPS_NAMESPACE)) {
List<String> storeList = Lists.newArrayList();
if (requestUriSegments.length > 2) {
String csvStoreList = requestUriSegments[2];
String[] storeArray = csvStoreList.split(",");
storeList = Lists.newArrayList(storeArray);
}
HttpMethod httpMethod = request.getMethod();
if (httpMethod.equals(HttpMethod.GET)) {
response = handleGet(storeList);
} else if (httpMethod.equals(HttpMethod.POST)) {
Map<String, Properties> configsToPut = Maps.newHashMap();
ChannelBuffer content = this.request.getContent();
if (content != null) {
byte[] postBody = new byte[content.capacity()];
content.readBytes(postBody);
configsToPut = ClientConfigUtil.readMultipleClientConfigAvro(new String(postBody));
}
response = handlePut(configsToPut);
} else if (httpMethod.equals(HttpMethod.DELETE)) {
if (storeList.size() == 0) {
response = handleBadRequest("Cannot delete config for all stores. Please specify at least one store name.");
} else {
response = handleDelete(storeList);
}
} else {
// Bad HTTP method
response = handleBadRequest("Unsupported HTTP method. Only GET, POST and DELETE are supported.");
}
} else {
// Bad namespace
response = handleBadRequest("Unsupported namespace. Only /" + STORE_OPS_NAMESPACE + "/ is supported.");
}
messageEvent.getChannel().write(response);
}
} else {
HttpChunk chunk = (HttpChunk) messageEvent.getMessage();
if (chunk.isLast()) {
readingChunks = false;
}
}
}
use of org.jboss.netty.handler.codec.http.HttpChunk in project load-balancer by RestComm.
the class HttpServerRequestHandler method writeResponse.
@SuppressWarnings("deprecation")
private void writeResponse(MessageEvent e, HttpResponseStatus status, String responseString) {
// Convert the response content to a ChannelBuffer.
if (chunk)
for (int i = 0; i < 1000; i++) responseString += "HOW MUCH IS THE FISH";
ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseString, Charset.forName("UTF-8"));
// Decide whether to close the connection or not.
boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION));
// Build the response object.
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
if (!chunk)
response.setContent(buf);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
if (chunk)
response.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, "chunked");
if (!chunk)
if (!close) {
// There's no need to add 'Content-Length' header
// if this is the last response.
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
}
String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
if (cookieString != null) {
CookieDecoder cookieDecoder = new CookieDecoder();
Set<Cookie> cookies = cookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
CookieEncoder cookieEncoder = new CookieEncoder(true);
for (Cookie cookie : cookies) {
cookieEncoder.addCookie(cookie);
}
response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
}
}
// Write the response.
ChannelFuture future = e.getChannel().write(response);
if (chunk) {
while (buf.readableBytes() > 0) {
int maxBytes = 1000;
if (buf.readableBytes() < 1000)
maxBytes = buf.readableBytes();
HttpChunk currChunk = new DefaultHttpChunk(buf.readBytes(maxBytes));
future = e.getChannel().write(currChunk);
}
HttpChunk currChunk = new DefaultHttpChunk(buf);
future = e.getChannel().write(currChunk);
}
// Close the connection after the write operation is done if necessary.
if (close) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
use of org.jboss.netty.handler.codec.http.HttpChunk in project load-balancer by RestComm.
the class HttpServerRequestHandler method handle.
public void handle(ChannelHandlerContext ctx, MessageEvent e) throws IOException {
if (!readingChunks) {
request = (HttpRequest) e.getMessage();
requests.add(request.getUri());
if (request.isChunked())
readingChunks = true;
else {
requestCount.incrementAndGet();
try {
String response = createResponseFromQueryParams(new URI(request.getUri()));
if (!badServer)
writeResponse(e, HttpResponseStatus.OK, response);
else
writeResponse(e, HttpResponseStatus.BAD_REQUEST, response);
} catch (Exception ex) {
}
}
} else {
HttpChunk chunk = (HttpChunk) e.getMessage();
if (chunk.isLast())
readingChunks = false;
}
}
Aggregations