use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
//HttpRequest request = (HttpRequest) e.getMessage();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
ChunkedBodyWritableByteChannel writeChannel = new ChunkedBodyWritableByteChannel(e.getChannel(), response);
for (String key : _headers.keySet()) {
writeChannel.setMetadata(key, _headers.get(key));
}
for (byte[] chunk : _responseChunks) {
writeChannel.write(ByteBuffer.wrap(chunk));
}
writeChannel.setResponseCode(_responseCode);
for (String key : _footers.keySet()) {
writeChannel.setMetadata(key, _footers.get(key));
}
writeChannel.close();
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method testWriteOneChunk.
@Test
public void testWriteOneChunk() {
LOG.info("Start: Testing headers with one chunk");
setupClient();
String chunk1 = "hello";
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
chunks.add(chunk1.getBytes(Charset.defaultCharset()));
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("header1", "value1");
headers.put("header2", "value2");
HashMap<String, String> footers = new HashMap<String, String>();
setupServer(HttpResponseStatus.OK, chunks, headers, footers);
ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", connectFuture.isSuccess());
HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
Channel requestChannel = connectFuture.getChannel();
ChannelFuture writeFuture = requestChannel.write(request);
writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", writeFuture.isSuccess());
HttpResponse response = _responseHandler.getResponse();
assertEquals("response code", Integer.toString(HttpResponseStatus.OK.getCode()), response.getHeader(ChunkedBodyWritableByteChannel.RESPONSE_CODE_FOOTER_NAME));
assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
byte[] responseBody = _responseHandler.getReceivedBytes();
assertEquals("response length", chunk1.getBytes(Charset.defaultCharset()).length, responseBody.length);
assertTrue("response content", Arrays.equals(chunk1.getBytes(Charset.defaultCharset()), responseBody));
LOG.info("Done: Testing headers with one chunk");
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method testWriteHeadersEmptyBody.
@Test
public void testWriteHeadersEmptyBody() {
LOG.info("Start: Testing headers with empty body");
setupClient();
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("header1", "value1");
headers.put("header2", "value2");
HashMap<String, String> footers = new HashMap<String, String>();
setupServer(HttpResponseStatus.OK, chunks, headers, footers);
ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", connectFuture.isSuccess());
HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
Channel requestChannel = connectFuture.getChannel();
ChannelFuture writeFuture = requestChannel.write(request);
writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", writeFuture.isSuccess());
HttpResponse response = _responseHandler.getResponse();
assertEquals("response code", HttpResponseStatus.OK, response.getStatus());
assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
assertEquals("Checking header3 value", null, response.getHeader("header3"));
byte[] responseBody = _responseHandler.getReceivedBytes();
assertTrue("empty response", null == responseBody || responseBody.length == 0);
LOG.info("Done: Testing headers with empty body");
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class TestRelayCommandsLocal method testRegisterV4CommandLessThanHappyPaths.
@Test
public void testRegisterV4CommandLessThanHappyPaths() throws Exception {
LOG.debug("\n\nstarting testRegisterV4CommandLessThanHappyPaths()\n");
// protocolVersion < 2
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=1");
SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
HttpResponse respObj = respHandler.getResponse();
assertNotNull("/register failed to return expected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
// protocolVersion > 4
httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=5");
respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
respObj = respHandler.getResponse();
assertNotNull("/register failed to return expected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
// protocolVersion == 2: this is a happy path, but explicitly specifying the version is
// unusual in this case (default = version 2 or 3, which are identical for /register), so
// check for expected response
httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=2");
respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
respObj = respHandler.getResponse();
assertNull("/register v2 returned unexpected error: " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER), respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
String registerResponseProtocolVersionStr = respObj.getHeader(DatabusHttpHeaders.DBUS_CLIENT_RELAY_PROTOCOL_VERSION_HDR);
assertNotNull("/register protocol-version response header not present", registerResponseProtocolVersionStr);
assertEquals("client-relay protocol response version mismatch", "2", registerResponseProtocolVersionStr);
byte[] respBytes = respHandler.getReceivedBytes();
if (LOG.isDebugEnabled()) {
LOG.debug("/register response: " + new String(respBytes));
}
ByteArrayInputStream in = new ByteArrayInputStream(respBytes);
ObjectMapper objMapper = new ObjectMapper();
List<RegisterResponseEntry> sourceSchemasList = null;
try {
sourceSchemasList = objMapper.readValue(in, new TypeReference<List<RegisterResponseEntry>>() {
});
} catch (JsonMappingException jmex) {
Assert.fail("ObjectMapper failed unexpectedly");
}
assertNotNull("missing source schemas in response", sourceSchemasList);
assertEquals("expected one source schema", 1, sourceSchemasList.size());
RegisterResponseEntry rre = sourceSchemasList.get(0);
assertEquals("unexpected source id", 4002, rre.getId());
Schema resSchema = Schema.parse(rre.getSchema());
assertEquals("unexpected source-schema name for source id 4002", "test4.source2_v1", resSchema.getFullName());
// protocolVersion == 3: as with v2 above; just do a quick sanity check
httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=3");
respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
respObj = respHandler.getResponse();
assertNull("/register v3 returned unexpected error: " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER), respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
registerResponseProtocolVersionStr = respObj.getHeader(DatabusHttpHeaders.DBUS_CLIENT_RELAY_PROTOCOL_VERSION_HDR);
assertNotNull("/register protocol-version response header not present", registerResponseProtocolVersionStr);
assertEquals("client-relay protocol response version mismatch", "3", registerResponseProtocolVersionStr);
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class TestRelayCommandsLocal method doTestOneDataStreamCommand.
private void doTestOneDataStreamCommand() throws Exception {
//try to read it
Checkpoint cp = Checkpoint.createFlexibleCheckpoint();
String streamRequest = "/stream?sources=100&size=100000&output=json&checkPoint=" + cp.toString();
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, streamRequest);
SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
HttpResponse respObj = respHandler.getResponse();
assertNull("/stream returned unexpected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
if (LOG.isDebugEnabled()) {
LOG.debug("/stream response:" + new String(respHandler.getReceivedBytes()));
}
ObjectMapper objMapper = new ObjectMapper();
ByteArrayInputStream in = new ByteArrayInputStream(respHandler.getReceivedBytes());
objMapper.readValue(in, new TypeReference<Map<String, String>>() {
});
}
Aggregations