Search in sources :

Example 66 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project load-balancer by RestComm.

the class Server method createRequest.

private HttpRequest createRequest(String command) {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    ChannelBuffer buf = ChannelBuffers.copiedBuffer(getStringFromJson(command), Charset.forName("UTF-8"));
    request.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
    request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes());
    request.setContent(buf);
    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) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 67 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project graylog2-server by Graylog2.

the class HttpTransportHandlerTest method messageReceivedReturns404ForWrongPath.

@Test
public void messageReceivedReturns404ForWrongPath() throws Exception {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    httpRequest.headers().add("Host", "localhost");
    httpRequest.headers().add("Origin", "http://example.com");
    httpRequest.headers().add("Connection", "close");
    channel.offer(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.poll();
    assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.NOT_FOUND);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 68 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project graylog2-server by Graylog2.

the class HttpTransportHandlerTest method messageReceivedReturns405ForInvalidMethod.

@Test
public void messageReceivedReturns405ForInvalidMethod() throws Exception {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    httpRequest.headers().add("Host", "localhost");
    httpRequest.headers().add("Origin", "http://example.com");
    httpRequest.headers().add("Connection", "close");
    channel.offer(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.poll();
    assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.METHOD_NOT_ALLOWED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 69 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project graylog2-server by Graylog2.

the class HttpTransportHandlerTest method messageReceivedSuccessfullyProcessesOPTIONSRequest.

@Test
public void messageReceivedSuccessfullyProcessesOPTIONSRequest() throws Exception {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/gelf");
    httpRequest.headers().add("Host", "localhost");
    httpRequest.headers().add("Origin", "http://example.com");
    httpRequest.headers().add("Connection", "close");
    channel.offer(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.poll();
    assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.OK);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 70 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project hadoop by apache.

the class TestShuffleHandler method testMaxConnections.

/**
   * Validate the limit on number of shuffle connections.
   * 
   * @throws Exception exception
   */
@Test(timeout = 10000)
public void testMaxConnections() throws Exception {
    Configuration conf = new Configuration();
    conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
    conf.setInt(ShuffleHandler.MAX_SHUFFLE_CONNECTIONS, 3);
    ShuffleHandler shuffleHandler = new ShuffleHandler() {

        @Override
        protected Shuffle getShuffle(Configuration conf) {
            // replace the shuffle handler with one stubbed for testing
            return new Shuffle(conf) {

                @Override
                protected MapOutputInfo getMapOutputInfo(String mapId, int reduce, String jobId, String user) throws IOException {
                    // Do nothing.
                    return null;
                }

                @Override
                protected void populateHeaders(List<String> mapIds, String jobId, String user, int reduce, HttpRequest request, HttpResponse response, boolean keepAliveParam, Map<String, MapOutputInfo> infoMap) throws IOException {
                // Do nothing.
                }

                @Override
                protected void verifyRequest(String appid, ChannelHandlerContext ctx, HttpRequest request, HttpResponse response, URL requestUri) throws IOException {
                // Do nothing.
                }

                @Override
                protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch, String user, String mapId, int reduce, MapOutputInfo info) throws IOException {
                    // send a shuffle header and a lot of data down the channel
                    // to trigger a broken pipe
                    ShuffleHeader header = new ShuffleHeader("dummy_header", 5678, 5678, 1);
                    DataOutputBuffer dob = new DataOutputBuffer();
                    header.write(dob);
                    ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
                    dob = new DataOutputBuffer();
                    for (int i = 0; i < 100000; ++i) {
                        header.write(dob);
                    }
                    return ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
                }
            };
        }
    };
    shuffleHandler.init(conf);
    shuffleHandler.start();
    // setup connections
    int connAttempts = 3;
    HttpURLConnection[] conns = new HttpURLConnection[connAttempts];
    for (int i = 0; i < connAttempts; i++) {
        String URLstring = "http://127.0.0.1:" + shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY) + "/mapOutput?job=job_12345_1&reduce=1&map=attempt_12345_1_m_" + i + "_0";
        URL url = new URL(URLstring);
        conns[i] = (HttpURLConnection) url.openConnection();
        conns[i].setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
        conns[i].setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
    }
    // Try to open numerous connections
    for (int i = 0; i < connAttempts; i++) {
        conns[i].connect();
    }
    //Ensure first connections are okay
    conns[0].getInputStream();
    int rc = conns[0].getResponseCode();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, rc);
    conns[1].getInputStream();
    rc = conns[1].getResponseCode();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, rc);
    // This connection should be closed because it to above the limit
    try {
        rc = conns[2].getResponseCode();
        Assert.assertEquals("Expected a too-many-requests response code", ShuffleHandler.TOO_MANY_REQ_STATUS.getCode(), rc);
        long backoff = Long.valueOf(conns[2].getHeaderField(ShuffleHandler.RETRY_AFTER_HEADER));
        Assert.assertTrue("The backoff value cannot be negative.", backoff > 0);
        conns[2].getInputStream();
        Assert.fail("Expected an IOException");
    } catch (IOException ioe) {
        LOG.info("Expected - connection should not be open");
    } catch (NumberFormatException ne) {
        Assert.fail("Expected a numerical value for RETRY_AFTER header field");
    } catch (Exception e) {
        Assert.fail("Expected a IOException");
    }
    shuffleHandler.stop();
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) SocketChannel(org.jboss.netty.channel.socket.SocketChannel) Channel(org.jboss.netty.channel.Channel) AbstractChannel(org.jboss.netty.channel.AbstractChannel) ShuffleHeader(org.apache.hadoop.mapreduce.task.reduce.ShuffleHeader) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) IOException(java.io.IOException) URL(java.net.URL) EOFException(java.io.EOFException) IOException(java.io.IOException) ServiceStateException(org.apache.hadoop.service.ServiceStateException) HttpURLConnection(java.net.HttpURLConnection) DataOutputBuffer(org.apache.hadoop.io.DataOutputBuffer) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Test(org.junit.Test)

Aggregations

HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)136 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)69 Channel (org.jboss.netty.channel.Channel)47 Test (org.junit.Test)47 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)46 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)43 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 ChannelFuture (org.jboss.netty.channel.ChannelFuture)26 List (java.util.List)19 ArrayList (java.util.ArrayList)17 Map (java.util.Map)17 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)17 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)17 InetSocketAddress (java.net.InetSocketAddress)14 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)13 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)13 SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)12 SimpleTestHttpClient (com.linkedin.databus.core.test.netty.SimpleTestHttpClient)12 Configuration (org.apache.hadoop.conf.Configuration)12 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)12