Search in sources :

Example 46 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.

the class TestRelayCommandsLocal method testNoDataStreamCommand.

@Test
public void testNoDataStreamCommand() throws Exception {
    LOG.debug("\n\nstarting testNoDataStreamCommand()\n");
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/stream?sources=100&size=1000&output=json&checkPoint={\"windowScn\":-1,\"windowOffset\":-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));
    assertEquals("expected to get empty response", 0, respHandler.getReceivedBytes().length);
    HttpResponse respObj = respHandler.getResponse();
    assertNull("/stream returned unexpected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    assertNull("/stream returned unexpected error with cause", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CAUSE_CLASS_HEADER));
}
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) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) Test(org.testng.annotations.Test)

Example 47 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project bagheera by mozilla-metrics.

the class RootResponse method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    if (msg instanceof HttpMessage) {
        HttpRequest request = (HttpRequest) msg;
        if (ROOT_PATH.equals(request.getUri()) || request.getUri().isEmpty()) {
            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
            ChannelFuture future = e.getChannel().write(response);
            future.addListener(ChannelFutureListener.CLOSE);
        } else {
            Channels.fireMessageReceived(ctx, request, e.getRemoteAddress());
        }
    } else {
        ctx.sendUpstream(e);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpMessage(org.jboss.netty.handler.codec.http.HttpMessage)

Example 48 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project bagheera by mozilla-metrics.

the class SubmissionHandler method handleOptions.

private void handleOptions(MessageEvent e, BagheeraHttpRequest request) {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "POST,PUT,DELETE");
    response.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
    ChannelFuture future = e.getChannel().write(response);
    future.addListener(ChannelFutureListener.CLOSE);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Example 49 with HttpResponse

use of org.jboss.netty.handler.codec.http.HttpResponse in project NabAlive by jcheype.

the class TtsController method init.

@PostConstruct
void init() {
    restHandler.get(new Route("/tts/:apikey/:voice") {

        @Override
        public void handle(Request request, final Response response, Map<String, String> map) throws Exception {
            String text = StringEscapeUtils.escapeXml(checkNotNull(request.getParam("text")));
            String voice = checkNotNull(map.get("voice"));
            if (!text.startsWith("<s>")) {
                text = "<s>" + text + "</s>";
            }
            final String key = text + "|" + voice;
            byte[] bytes = ttsCache.asMap().get(key);
            if (bytes != null) {
                response.write(ChannelBuffers.copiedBuffer(bytes));
                return;
            }
            asyncHttpClient.preparePost(frenchTtsUrl).setBody(text).execute(new AsyncCompletionHandler<com.ning.http.client.Response>() {

                @Override
                public com.ning.http.client.Response onCompleted(com.ning.http.client.Response asyncResponse) throws Exception {
                    InputStream inputStream = asyncResponse.getResponseBodyAsStream();
                    byte[] bytes = ByteStreams.toByteArray(inputStream);
                    ttsCache.asMap().put(key, bytes);
                    response.write(bytes);
                    return asyncResponse;
                }

                @Override
                public void onThrowable(Throwable t) {
                    logger.error("TTS error", t);
                    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                    response.write(httpResponse);
                }
            });
        }
    }).get(new Route("/tts/:voice") {

        @Override
        public void handle(Request request, final Response response, Map<String, String> map) throws Exception {
            String text = StringEscapeUtils.escapeXml(checkNotNull(request.getParam("text")));
            String voice = checkNotNull(map.get("voice"));
            if (!text.startsWith("<s>")) {
                text = "<s>" + text + "</s>";
            }
            final String key = text + "|" + voice;
            byte[] bytes = ttsCache.asMap().get(key);
            if (bytes != null) {
                response.write(ChannelBuffers.copiedBuffer(bytes));
                return;
            }
            asyncHttpClient.preparePost(frenchTtsUrl).setBody(text).execute(new AsyncCompletionHandler<com.ning.http.client.Response>() {

                @Override
                public com.ning.http.client.Response onCompleted(com.ning.http.client.Response asyncResponse) throws Exception {
                    InputStream inputStream = asyncResponse.getResponseBodyAsStream();
                    byte[] bytes = ByteStreams.toByteArray(inputStream);
                    ttsCache.asMap().put(key, bytes);
                    response.write(bytes);
                    return asyncResponse;
                }

                @Override
                public void onThrowable(Throwable t) {
                    logger.error("TTS error", t);
                    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                    response.write(httpResponse);
                }
            });
        }
    });
}
Also used : InputStream(java.io.InputStream) Request(com.nabalive.framework.web.Request) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Response(com.nabalive.framework.web.Response) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) AsyncCompletionHandler(com.ning.http.client.AsyncCompletionHandler) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Map(java.util.Map) Route(com.nabalive.framework.web.Route) PostConstruct(javax.annotation.PostConstruct)

Example 50 with HttpResponse

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

the class TestShuffleHandler method testKeepAlive.

@Test(timeout = 10000)
public void testKeepAlive() throws Exception {
    final ArrayList<Throwable> failures = new ArrayList<Throwable>(1);
    Configuration conf = new Configuration();
    conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
    conf.setBoolean(ShuffleHandler.SHUFFLE_CONNECTION_KEEP_ALIVE_ENABLED, true);
    // try setting to -ve keep alive timeout.
    conf.setInt(ShuffleHandler.SHUFFLE_CONNECTION_KEEP_ALIVE_TIME_OUT, -100);
    ShuffleHandler shuffleHandler = new ShuffleHandler() {

        @Override
        protected Shuffle getShuffle(final 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 {
                    return null;
                }

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

                @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 {
                    // Send some dummy data (populate content length details)
                    ShuffleHeader header = new ShuffleHeader("attempt_12345_1_m_1_0", 5678, 5678, 1);
                    DataOutputBuffer dob = new DataOutputBuffer();
                    header.write(dob);
                    dob = new DataOutputBuffer();
                    for (int i = 0; i < 100000; ++i) {
                        header.write(dob);
                    }
                    long contentLength = dob.getLength();
                    // disable connectinKeepAliveEnabled if keepAliveParam is available
                    if (keepAliveParam) {
                        connectionKeepAliveEnabled = false;
                    }
                    super.setResponseHeaders(response, keepAliveParam, contentLength);
                }

                @Override
                protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch, String user, String mapId, int reduce, MapOutputInfo info) throws IOException {
                    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
                    // send a shuffle header and a lot of data down the channel
                    // to trigger a broken pipe
                    ShuffleHeader header = new ShuffleHeader("attempt_12345_1_m_1_0", 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()));
                }

                @Override
                protected void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
                    if (failures.size() == 0) {
                        failures.add(new Error());
                        ctx.getChannel().close();
                    }
                }

                @Override
                protected void sendError(ChannelHandlerContext ctx, String message, HttpResponseStatus status) {
                    if (failures.size() == 0) {
                        failures.add(new Error());
                        ctx.getChannel().close();
                    }
                }
            };
        }
    };
    shuffleHandler.init(conf);
    shuffleHandler.start();
    String shuffleBaseURL = "http://127.0.0.1:" + shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY);
    URL url = new URL(shuffleBaseURL + "/mapOutput?job=job_12345_1&reduce=1&" + "map=attempt_12345_1_m_1_0");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
    conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
    conn.connect();
    DataInputStream input = new DataInputStream(conn.getInputStream());
    Assert.assertEquals(HttpHeader.KEEP_ALIVE.asString(), conn.getHeaderField(HttpHeader.CONNECTION.asString()));
    Assert.assertEquals("timeout=1", conn.getHeaderField(HttpHeader.KEEP_ALIVE.asString()));
    Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
    ShuffleHeader header = new ShuffleHeader();
    header.readFields(input);
    input.close();
    // For keepAlive via URL
    url = new URL(shuffleBaseURL + "/mapOutput?job=job_12345_1&reduce=1&" + "map=attempt_12345_1_m_1_0&keepAlive=true");
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
    conn.setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
    conn.connect();
    input = new DataInputStream(conn.getInputStream());
    Assert.assertEquals(HttpHeader.KEEP_ALIVE.asString(), conn.getHeaderField(HttpHeader.CONNECTION.asString()));
    Assert.assertEquals("timeout=1", conn.getHeaderField(HttpHeader.KEEP_ALIVE.asString()));
    Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
    header = new ShuffleHeader();
    header.readFields(input);
    input.close();
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) ShuffleHeader(org.apache.hadoop.mapreduce.task.reduce.ShuffleHeader) SocketChannel(org.jboss.netty.channel.socket.SocketChannel) Channel(org.jboss.netty.channel.Channel) AbstractChannel(org.jboss.netty.channel.AbstractChannel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) DataInputStream(java.io.DataInputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DataOutputBuffer(org.apache.hadoop.io.DataOutputBuffer) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Test(org.junit.Test)

Aggregations

HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)143 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)111 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)61 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)53 Test (org.testng.annotations.Test)51 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)47 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)37 Channel (org.jboss.netty.channel.Channel)34 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)30 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)28 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 InetSocketAddress (java.net.InetSocketAddress)25 ChannelFuture (org.jboss.netty.channel.ChannelFuture)25 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)25 Checkpoint (com.linkedin.databus.core.Checkpoint)24 SocketAddress (java.net.SocketAddress)23 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)20 Test (org.junit.Test)20 Logger (org.apache.log4j.Logger)16 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)16