Package org.jboss.netty.handler.codec.http

Examples of org.jboss.netty.handler.codec.http.DefaultHttpResponse


    private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) {
        // Allow only GET methods.
        if (req.getMethod() != GET) {
            sendHttpResponse(
                    ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
            return;
        }

        // Serve the WebSocket handshake request.
        if (req.getUri().equals(WEBSOCKET_PATH) &&
            Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION)) &&
            WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) {

            // Create the WebSocket handshake response.
            HttpResponse res = new DefaultHttpResponse(
                    HTTP_1_1,
                    new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
            res.addHeader(Names.UPGRADE, WEBSOCKET);
            res.addHeader(CONNECTION, Values.UPGRADE);
            res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
            res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req));
            String protocol = req.getHeader(WEBSOCKET_PROTOCOL);
            if (protocol != null) {
                res.addHeader(WEBSOCKET_PROTOCOL, protocol);
            }

            // Upgrade the connection and send the handshake response.
            ChannelPipeline p = ctx.getChannel().getPipeline();
            p.remove("aggregator");
            p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());

            ctx.getChannel().write(res);
            p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
            ctx.getChannel().write(new DefaultWebSocketFrame("Welcome!"));
            return;
        }

        // Send an error page otherwise.
        sendHttpResponse(
                ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
    }
View Full Code Here


        ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();
        responseContent.writeBytes(keysOutputStream.toByteArray());

        // Create the Response object
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);

        // Set the right headers
        response.setHeader(CONTENT_TYPE, "multipart/binary");
        response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");

        // Copy the data into the payload
        response.setContent(responseContent);
        response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());

        // Write the response to the Netty Channel
        if(logger.isDebugEnabled()) {
            String keyStr = getKeysHexString(this.versionedResponses.keySet());
            debugLog("GET_ALL",
View Full Code Here

        }
        ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();
        responseContent.writeBytes(outputStream.toByteArray());

        // Create the Response object
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);

        // Set the right headers
        response.setHeader(CONTENT_TYPE, "multipart/binary");
        response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
        response.setHeader(CONTENT_LOCATION, contentLocationKey);

        // Copy the data into the payload
        response.setContent(responseContent);
        response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());

        // Write the response to the Netty Channel
        if(logger.isDebugEnabled()) {
            String keyStr = RestUtils.getKeyHexString(this.key);
            debugLog("GET",
View Full Code Here

    @Override
    public void sendResponse(StoreStats performanceStats,
                             boolean isFromLocalZone,
                             long startTimeInMs) {
        // Create the Response object
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, CREATED);

        // Set the right headers
        response.setHeader(CONTENT_LENGTH, 0);

        if(this.successfulPutVC != null) {
            numVectorClockEntries += successfulPutVC.getVersionMap().size();
            String serializedVC = RestUtils.getSerializedVectorClock(successfulPutVC);
            response.setHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, serializedVC);
        }

        // Write the response to the Netty Channel
        if(logger.isDebugEnabled()) {
            String keyStr = RestUtils.getKeyHexString(key);
View Full Code Here

    public static void writeErrorResponse(MessageEvent messageEvent,
                                          HttpResponseStatus status,
                                          String message) {

        // Create the Response object
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
        response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
        response.setContent(ChannelBuffers.copiedBuffer("Failure: " + status.toString() + ". "
                                                        + message + "\r\n", CharsetUtil.UTF_8));
        response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());

        // Write the response to the Netty Channel
        messageEvent.getChannel().write(response);
    }
View Full Code Here

        byte[] responseContent = eTags.getBytes();
        ChannelBuffer responseContentBuffer = ChannelBuffers.dynamicBuffer(responseContent.length);
        responseContentBuffer.writeBytes(responseContent);

        // Create the Response object
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);

        // Set the right headers
        response.setHeader(CONTENT_TYPE, "binary");
        response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
        response.setHeader(CONTENT_LOCATION, contentLocationKey);

        // Copy the data into the payload
        response.setContent(responseContentBuffer);
        response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());

        // Write the response to the Netty Channel
        this.messageEvent.getChannel().write(response);

    }
View Full Code Here

    @Override
    public void sendResponse(StoreStats performanceStats,
                             boolean isFromLocalZone,
                             long startTimeInMs) {
        // Create the Response object
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NO_CONTENT);

        // Set the right headers
        response.setHeader(CONTENT_LENGTH, "0");

        // Write the response to the Netty Channel
        if(logger.isDebugEnabled()) {
            String keyStr = RestUtils.getKeyHexString(key);
            debugLog("DELETE", this.storeName, keyStr, startTimeInMs, System.currentTimeMillis(), 0);
View Full Code Here

                            }

                            @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);
                            }
                        });

                    }
View Full Code Here

                        else {
                            byte[] bytes = ByteStreams.toByteArray(getClass().getResourceAsStream("/app/default.png"));
                            buffer = ChannelBuffers.copiedBuffer(bytes);
                        }

                        DefaultHttpResponse defaultHttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
                        defaultHttpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "image/png");
                        defaultHttpResponse.setContent(buffer);

                        response.write(defaultHttpResponse);
                    }
                });
View Full Code Here

        final ChannelBuffer buffer = ChannelBuffers.copiedBuffer(data);
        write(buffer);
    }

    public void write(ChannelBuffer buffer) {
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        response.setHeader(HttpHeaders.Names.SERVER, "nuvoos server");

        setContentLength(response, buffer.readableBytes());
        response.setContent(buffer);

        if (isKeepAlive(request)) {
            response.setHeader(HttpHeaders.Names.CONNECTION, "Keep-Alive");
        }

        final ChannelFuture future = ctx.getChannel().write(response);
        if (!isKeepAlive(request)) {
            future.addListener(ChannelFutureListener.CLOSE);
View Full Code Here

TOP

Related Classes of org.jboss.netty.handler.codec.http.DefaultHttpResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.