Search in sources :

Example 66 with DefaultHttpRequest

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;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest)

Example 67 with DefaultHttpRequest

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");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HashMap(java.util.HashMap) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) Channel(org.jboss.netty.channel.Channel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.testng.annotations.Test)

Example 68 with DefaultHttpRequest

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");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HashMap(java.util.HashMap) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) Channel(org.jboss.netty.channel.Channel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.testng.annotations.Test)

Example 69 with DefaultHttpRequest

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);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleHttpResponseHandler(com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) List(java.util.List) IdNamePair(com.linkedin.databus.core.util.IdNamePair) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HashSet(java.util.HashSet)

Example 70 with DefaultHttpRequest

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) {
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleHttpResponseHandler(com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)128 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)101 Test (org.junit.Test)53 Channel (org.jboss.netty.channel.Channel)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)33 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)25 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)24 Test (org.testng.annotations.Test)23 HttpMethod (org.jboss.netty.handler.codec.http.HttpMethod)21 ChannelFuture (org.jboss.netty.channel.ChannelFuture)19 SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)12 SimpleTestHttpClient (com.linkedin.databus.core.test.netty.SimpleTestHttpClient)12 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)11 InetSocketAddress (java.net.InetSocketAddress)11 Logger (org.apache.log4j.Logger)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)10 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 SocketAddress (java.net.SocketAddress)9