Search in sources :

Example 76 with HttpRequest

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

the class HttpServerMultiplexChannelHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
    // store request, as this channel handler is created per pipeline
    HttpRequest request = (HttpRequest) messageEvent.getMessage();
    LOG.debug("Message received: {}", request);
    HttpServerChannelHandler handler = getHandler(request);
    if (handler != null) {
        // store handler as attachment
        ctx.setAttachment(handler);
        handler.messageReceived(ctx, messageEvent);
    } else {
        // this resource is not found, so send empty response back
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
        response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
        response.headers().set(Exchange.CONTENT_LENGTH, 0);
        response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
        messageEvent.getChannel().write(response).syncUninterruptibly();
        // close the channel after send error message
        messageEvent.getChannel().close();
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) 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 77 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project cdap by caskdata.

the class HttpRequestHandler method messageReceived.

@Override
public void messageReceived(final ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    if (channelClosed) {
        return;
    }
    final Channel inboundChannel = event.getChannel();
    Object msg = event.getMessage();
    if (msg instanceof HttpChunk) {
        // This case below should never happen this would mean we get Chunks before HTTPMessage.
        if (chunkSender == null) {
            throw new HandlerException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Chunk received and event sender is null");
        }
        chunkSender.send(msg);
    } else if (msg instanceof HttpRequest) {
        // Discover and forward event.
        HttpRequest request = (HttpRequest) msg;
        request = applyProxyRules(request);
        // Suspend incoming traffic until connected to the outbound service.
        inboundChannel.setReadable(false);
        WrappedDiscoverable discoverable = getDiscoverable(request, (InetSocketAddress) inboundChannel.getLocalAddress());
        // If no event sender, make new connection, otherwise reuse existing one.
        MessageSender sender = discoveryLookup.get(discoverable);
        if (sender == null || !sender.isConnected()) {
            InetSocketAddress address = discoverable.getSocketAddress();
            ChannelFuture future = clientBootstrap.connect(address);
            final Channel outboundChannel = future.getChannel();
            outboundChannel.getPipeline().addAfter("request-encoder", "outbound-handler", new OutboundHandler(inboundChannel));
            if (Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload())) {
                SSLContext clientContext;
                try {
                    clientContext = SSLContext.getInstance("TLS");
                    clientContext.init(null, PermissiveTrustManagerFactory.getTrustManagers(), null);
                } catch (NoSuchAlgorithmException | KeyManagementException e) {
                    throw new RuntimeException("SSL is enabled for app-fabric but failed to create SSLContext in the router " + "client.", e);
                }
                SSLEngine engine = clientContext.createSSLEngine();
                engine.setUseClientMode(true);
                engine.setEnabledProtocols(new String[] { "TLSv1.2", "TLSv1.1", "TLSv1" });
                outboundChannel.getPipeline().addFirst("ssl", new SslHandler(engine));
                LOG.trace("Adding ssl handler to the pipeline.");
            }
            sender = new MessageSender(inboundChannel, future);
            discoveryLookup.put(discoverable, sender);
            // Remember the in-flight outbound channel
            inboundChannel.setAttachment(outboundChannel);
            outboundChannel.getCloseFuture().addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    inboundChannel.getPipeline().execute(new Runnable() {

                        @Override
                        public void run() {
                            // close the inbound channel as well if it carries the in-flight request
                            if (outboundChannel.equals(inboundChannel.getAttachment())) {
                                closeOnFlush(inboundChannel);
                            }
                        }
                    });
                }
            });
        } else {
            Channel outboundChannel = (Channel) inboundChannel.getAttachment();
            if (outboundChannel != null) {
                // Set outbound channel to be readable in case previous request has set it as non-readable
                outboundChannel.setReadable(true);
            }
        }
        // Send the message.
        sender.send(request);
        inboundChannel.setReadable(true);
        //Save the channelFuture for subsequent chunks
        if (request.isChunked()) {
            chunkSender = sender;
        }
    } else {
        super.messageReceived(ctx, event);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) HandlerException(co.cask.cdap.common.HandlerException) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) Channel(org.jboss.netty.channel.Channel) SSLContext(javax.net.ssl.SSLContext) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) SslHandler(org.jboss.netty.handler.ssl.SslHandler) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 78 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project cdap by caskdata.

the class HttpStatusRequestHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    Object msg = event.getMessage();
    if (msg instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) msg;
        // be served by the router itself without it talking to any downstream services
        if (request.getUri().equals(Constants.EndPoints.STATUS)) {
            String statusString = Constants.Monitor.STATUS_OK;
            ChannelBuffer responseContent = ChannelBuffers.wrappedBuffer(Charsets.UTF_8.encode(statusString));
            HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
            httpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, responseContent.readableBytes());
            httpResponse.setContent(responseContent);
            ChannelFuture writeFuture = Channels.future(event.getChannel());
            Channels.write(ctx, writeFuture, httpResponse);
            writeFuture.addListener(ChannelFutureListener.CLOSE);
            return;
        }
    }
    super.messageReceived(ctx, event);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) 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) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 79 with HttpRequest

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

the class TestShuffleHandler method testGetMapOutputInfo.

@Test(timeout = 100000)
public void testGetMapOutputInfo() throws Exception {
    final ArrayList<Throwable> failures = new ArrayList<Throwable>(1);
    Configuration conf = new Configuration();
    conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
    conf.setInt(ShuffleHandler.MAX_SHUFFLE_CONNECTIONS, 3);
    conf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, "simple");
    UserGroupInformation.setConfiguration(conf);
    File absLogDir = new File("target", TestShuffleHandler.class.getSimpleName() + "LocDir").getAbsoluteFile();
    conf.set(YarnConfiguration.NM_LOCAL_DIRS, absLogDir.getAbsolutePath());
    ApplicationId appId = ApplicationId.newInstance(12345, 1);
    String appAttemptId = "attempt_12345_1_m_1_0";
    String user = "randomUser";
    String reducerId = "0";
    List<File> fileMap = new ArrayList<File>();
    createShuffleHandlerFiles(absLogDir, user, appId.toString(), appAttemptId, conf, fileMap);
    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 void populateHeaders(List<String> mapIds, String outputBaseStr, String dagId, String user, Range reduceRange, HttpResponse response, boolean keepAliveParam, Map<String, MapOutputInfo> infoMap) throws IOException {
                    // Only set response headers and skip everything else
                    // send some dummy value for content-length
                    super.setResponseHeaders(response, keepAliveParam, 100);
                }

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

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

                @Override
                protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch, String user, String mapId, Range reduceRange, MapOutputInfo info) throws IOException {
                    // send a shuffle header
                    ShuffleHeader header = new ShuffleHeader("attempt_12345_1_m_1_0", 5678, 5678, 1);
                    DataOutputBuffer dob = new DataOutputBuffer();
                    header.write(dob);
                    return ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
                }
            };
        }
    };
    shuffleHandler.init(conf);
    try {
        shuffleHandler.start();
        DataOutputBuffer outputBuffer = new DataOutputBuffer();
        outputBuffer.reset();
        Token<JobTokenIdentifier> jt = new Token<JobTokenIdentifier>("identifier".getBytes(), "password".getBytes(), new Text(user), new Text("shuffleService"));
        jt.write(outputBuffer);
        shuffleHandler.initializeApplication(new ApplicationInitializationContext(user, appId, ByteBuffer.wrap(outputBuffer.getData(), 0, outputBuffer.getLength())));
        URL url = new URL("http://127.0.0.1:" + shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY) + "/mapOutput?job=job_12345_0001&dag=1&reduce=" + reducerId + "&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();
        try {
            DataInputStream is = new DataInputStream(conn.getInputStream());
            ShuffleHeader header = new ShuffleHeader();
            header.readFields(is);
            is.close();
        } catch (EOFException e) {
        // ignore
        }
        Assert.assertEquals("sendError called due to shuffle error", 0, failures.size());
    } finally {
        shuffleHandler.stop();
        FileUtil.fullyDelete(absLogDir);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) ArrayList(java.util.ArrayList) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) Token(org.apache.hadoop.security.token.Token) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) DataOutputBuffer(org.apache.hadoop.io.DataOutputBuffer) EOFException(java.io.EOFException) List(java.util.List) ArrayList(java.util.ArrayList) ApplicationInitializationContext(org.apache.hadoop.yarn.server.api.ApplicationInitializationContext) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) SocketChannel(org.jboss.netty.channel.socket.SocketChannel) Channel(org.jboss.netty.channel.Channel) AbstractChannel(org.jboss.netty.channel.AbstractChannel) ShuffleHeader(org.apache.tez.runtime.library.common.shuffle.orderedgrouped.ShuffleHeader) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) JobTokenIdentifier(org.apache.tez.common.security.JobTokenIdentifier) Text(org.apache.hadoop.io.Text) DataInputStream(java.io.DataInputStream) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) File(java.io.File) Map(java.util.Map) Test(org.junit.Test)

Example 80 with HttpRequest

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

the class TestShuffleHandler method testSendMapCount.

@Test(timeout = 4000)
public void testSendMapCount() throws Exception {
    final List<ShuffleHandler.ReduceMapFileCount> listenerList = new ArrayList<ShuffleHandler.ReduceMapFileCount>();
    final ChannelHandlerContext mockCtx = mock(ChannelHandlerContext.class);
    final MessageEvent mockEvt = mock(MessageEvent.class);
    final Channel mockCh = mock(AbstractChannel.class);
    final ChannelPipeline mockPipeline = Mockito.mock(ChannelPipeline.class);
    // Mock HttpRequest and ChannelFuture
    final HttpRequest mockHttpRequest = createMockHttpRequest();
    final ChannelFuture mockFuture = createMockChannelFuture(mockCh, listenerList);
    final ShuffleHandler.TimeoutHandler timerHandler = new ShuffleHandler.TimeoutHandler();
    // Mock Netty Channel Context and Channel behavior
    Mockito.doReturn(mockCh).when(mockCtx).getChannel();
    Mockito.when(mockCh.getPipeline()).thenReturn(mockPipeline);
    Mockito.when(mockPipeline.get(Mockito.any(String.class))).thenReturn(timerHandler);
    when(mockCtx.getChannel()).thenReturn(mockCh);
    Mockito.doReturn(mockFuture).when(mockCh).write(Mockito.any(Object.class));
    when(mockCh.write(Object.class)).thenReturn(mockFuture);
    // Mock MessageEvent behavior
    Mockito.doReturn(mockCh).when(mockEvt).getChannel();
    when(mockEvt.getChannel()).thenReturn(mockCh);
    Mockito.doReturn(mockHttpRequest).when(mockEvt).getMessage();
    final ShuffleHandler sh = new MockShuffleHandler();
    Configuration conf = new Configuration();
    // The Shuffle handler port associated with the service is bound to but not used.
    conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
    sh.init(conf);
    sh.start();
    int maxOpenFiles = conf.getInt(ShuffleHandler.SHUFFLE_MAX_SESSION_OPEN_FILES, ShuffleHandler.DEFAULT_SHUFFLE_MAX_SESSION_OPEN_FILES);
    sh.getShuffle(conf).messageReceived(mockCtx, mockEvt);
    assertTrue("Number of Open files should not exceed the configured " + "value!-Not Expected", listenerList.size() <= maxOpenFiles);
    while (!listenerList.isEmpty()) {
        listenerList.remove(0).operationComplete(mockFuture);
        assertTrue("Number of Open files should not exceed the configured " + "value!-Not Expected", listenerList.size() <= maxOpenFiles);
    }
    sh.close();
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) MessageEvent(org.jboss.netty.channel.MessageEvent) SocketChannel(org.jboss.netty.channel.socket.SocketChannel) Channel(org.jboss.netty.channel.Channel) AbstractChannel(org.jboss.netty.channel.AbstractChannel) ArrayList(java.util.ArrayList) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) 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