use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.
the class AbstractNettyHttpConnection method createEmptyRequest.
/**
* Creates an empty HttpRequest object with pre-filled standard headers
* @param uriString the request URL
* @return the request object
*/
protected HttpRequest createEmptyRequest(String uriString) {
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriString);
request.setHeader(HttpHeaders.Names.HOST, _server.getAddress().toString());
request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
String hostHdr = DbusHttpUtils.getLocalHostName();
String svcHdr = DbusConstants.getServiceIdentifier();
if (!hostHdr.isEmpty()) {
request.setHeader(DatabusHttpHeaders.DBUS_CLIENT_HOST_HDR, hostHdr);
}
if (!svcHdr.isEmpty()) {
request.setHeader(DatabusHttpHeaders.DBUS_CLIENT_SERVICE_HDR, svcHdr);
}
return request;
}
use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.
the class DummyHttpRequestHandler method testWriteTwoChunks.
@Test
public void testWriteTwoChunks() {
LOG.info("Start: Testing headers with one chunk");
setupClient();
String chunk1 = "hello";
String chunk2 = "bye";
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
chunks.add(chunk1.getBytes(Charset.defaultCharset()));
chunks.add(chunk2.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>();
footers.put("footer1", "1value");
footers.put("footer2", "2value");
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"));
assertEquals("Checking footer1 value", "1value", response.getHeader("footer1"));
assertEquals("Checking footer2 value", "2value", response.getHeader("footer2"));
byte[] responseBody = _responseHandler.getReceivedBytes();
assertEquals("response length", chunk1.getBytes(Charset.defaultCharset()).length + chunk2.getBytes(Charset.defaultCharset()).length, responseBody.length);
byte[] fullBody = new byte[chunk1.getBytes(Charset.defaultCharset()).length + chunk2.getBytes(Charset.defaultCharset()).length];
System.arraycopy(chunk1.getBytes(Charset.defaultCharset()), 0, fullBody, 0, chunk1.getBytes(Charset.defaultCharset()).length);
System.arraycopy(chunk2.getBytes(Charset.defaultCharset()), 0, fullBody, chunk1.getBytes(Charset.defaultCharset()).length, chunk2.getBytes(Charset.defaultCharset()).length);
assertTrue("response content", Arrays.equals(fullBody, responseBody));
LOG.info("Done: Testing headers with one chunk");
}
use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.
the class DummyHttpRequestHandler method testWriteEmptyResponse.
@Test
public void testWriteEmptyResponse() {
LOG.info("Start: Testing empty response");
setupClient();
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
HashMap<String, String> headers = new HashMap<String, String>();
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());
byte[] responseBody = _responseHandler.getReceivedBytes();
assertTrue("empty response", null == responseBody || responseBody.length == 0);
LOG.info("Done: Testing empty response");
}
use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.
the class TestRelayCommandsLocal method doTestSources2Command.
private void doTestSources2Command() throws Exception {
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/sources");
SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
ByteArrayInputStream in = new ByteArrayInputStream(respHandler.getReceivedBytes());
ObjectMapper objMapper = new ObjectMapper();
List<IdNamePair> res = objMapper.readValue(in, new TypeReference<List<IdNamePair>>() {
});
assertNotNull("no result", res);
if (LOG.isDebugEnabled()) {
LOG.debug("/sources response:" + new String(respHandler.getReceivedBytes()));
}
HashSet<IdNamePair> origSet = new HashSet<IdNamePair>(_staticConfig.getSourceIds());
HashSet<IdNamePair> resSet = new HashSet<IdNamePair>(res);
Assert.assertEquals(origSet, resSet);
}
use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.
the class TestRelayCommandsLocal method prepareTestOneDataStreamCommand.
private void prepareTestOneDataStreamCommand() throws Exception {
//generate an event
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/genDataEvents/start?src_ids=100&fromScn=10&eventsPerSec=1&duration=10");
SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
String respString = new String(respHandler.getReceivedBytes());
LOG.debug("Response string:" + respString);
ByteArrayInputStream in = new ByteArrayInputStream(respHandler.getReceivedBytes());
ObjectMapper objMapper = new ObjectMapper();
Map<String, String> genRes = objMapper.readValue(in, new TypeReference<Map<String, String>>() {
});
HttpResponse respObj = respHandler.getResponse();
assertNull("/genDataEvents returned unexpected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
assertEquals("event-generation failed to start", "true", genRes.get("genDataEventsStarted"));
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
}
}
Aggregations