Search in sources :

Example 6 with HttpResponseStatus

use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project voldemort by voldemort.

the class CoordinatorAdminRequestHandler method handlePut.

private HttpResponse handlePut(Map<String, Properties> configsToPut) {
    logger.info("Handling a Http POST Admin request");
    String response = storeClientConfigs.putConfigs(configsToPut);
    HttpResponseStatus responseStatus;
    if (response.contains(StoreClientConfigService.ERROR_MESSAGE_PARAM_KEY)) {
        responseStatus = BAD_REQUEST;
    } else {
        responseStatus = OK;
    }
    return sendResponse(responseStatus, response);
}
Also used : HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus)

Example 7 with HttpResponseStatus

use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project crate by crate.

the class HttpBlobHandler method writeToFile.

protected void writeToFile(ChannelBuffer input, boolean last, final boolean continueExpected) throws IOException {
    if (digestBlob == null) {
        throw new IllegalStateException("digestBlob is null in writeToFile");
    }
    RemoteDigestBlob.Status status = digestBlob.addContent(input, last);
    HttpResponseStatus exitStatus = null;
    switch(status) {
        case FULL:
            exitStatus = HttpResponseStatus.CREATED;
            break;
        case PARTIAL:
            // tell the client to continue
            if (continueExpected) {
                write(ctx, succeededFuture(ctx.getChannel()), CONTINUE.duplicate());
            }
            return;
        case MISMATCH:
            exitStatus = HttpResponseStatus.BAD_REQUEST;
            break;
        case EXISTS:
            exitStatus = HttpResponseStatus.CONFLICT;
            break;
        case FAILED:
            exitStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
            break;
    }
    assert exitStatus != null : "exitStatus should not be null";
    LOGGER.trace("writeToFile exit status http:{} blob: {}", exitStatus, status);
    simpleResponse(exitStatus);
}
Also used : RemoteDigestBlob(io.crate.blob.RemoteDigestBlob) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus)

Example 8 with HttpResponseStatus

use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project crate by crate.

the class HttpBlobHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    Throwable ex = e.getCause();
    if (ex instanceof ClosedChannelException) {
        LOGGER.trace("channel closed: {}", ex.toString());
        return;
    } else if (ex instanceof IOException) {
        String message = ex.getMessage();
        if (message != null && message.contains("Connection reset by peer")) {
            LOGGER.debug(message);
        } else {
            LOGGER.warn(message, e);
        }
        return;
    }
    HttpResponseStatus status;
    String body = null;
    if (ex instanceof DigestMismatchException || ex instanceof BlobsDisabledException || ex instanceof IllegalArgumentException) {
        status = HttpResponseStatus.BAD_REQUEST;
        body = String.format(Locale.ENGLISH, "Invalid request sent: %s", ex.getMessage());
    } else if (ex instanceof DigestNotFoundException || ex instanceof IndexNotFoundException) {
        status = HttpResponseStatus.NOT_FOUND;
    } else if (ex instanceof EsRejectedExecutionException) {
        status = TOO_MANY_REQUESTS;
        body = String.format(Locale.ENGLISH, "Rejected execution: %s", ex.getMessage());
    } else {
        status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
        body = String.format(Locale.ENGLISH, "Unhandled exception: %s", ex);
    }
    if (body != null) {
        LOGGER.debug(body);
    }
    simpleResponse(status, body);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) DigestNotFoundException(io.crate.blob.exceptions.DigestNotFoundException) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) IOException(java.io.IOException) BlobsDisabledException(io.crate.blob.v2.BlobsDisabledException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 9 with HttpResponseStatus

use of org.jboss.netty.handler.codec.http.HttpResponseStatus in project socket.io-netty by ibdknox.

the class WebSocketServerHandler method handleHttpRequest.

private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
    String reqURI = req.getUri();
    if (reqURI.contains(POLLING_PATH)) {
        String[] parts = reqURI.split("/");
        String ID = parts.length > 3 ? parts[3] : "";
        PollingIOClient client = (PollingIOClient) this.pollingClients.get(ID);
        if (client == null) {
            //new client
            client = connectPoller(ctx);
            client.Reconnect(ctx, req);
            return;
        }
        if (req.getMethod() == GET) {
            client.heartbeat();
            client.Reconnect(ctx, req);
        } else {
            //we got a message
            QueryStringDecoder decoder = new QueryStringDecoder("/?" + req.getContent().toString(CharsetUtil.UTF_8));
            String message = decoder.getParameters().get("data").get(0);
            handleMessage(client, message);
            //make sure the connection is closed once we send a response
            setKeepAlive(req, false);
            //send a response that allows for cross domain access
            HttpResponse resp = new DefaultHttpResponse(HTTP_1_1, OK);
            resp.addHeader("Access-Control-Allow-Origin", "*");
            sendHttpResponse(ctx, req, resp);
        }
        return;
    }
    // Serve the WebSocket handshake request.
    String location = "";
    if (reqURI.equals(WEBSOCKET_PATH)) {
        location = getWebSocketLocation(req);
    } else if (reqURI.equals(FLASHSOCKET_PATH)) {
        location = getFlashSocketLocation(req);
    }
    if (location != "" && 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);
        // Fill in the headers and contents depending on handshake method.
        if (req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2)) {
            // New handshake method with a challenge:
            res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
            res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));
            String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);
            if (protocol != null) {
                res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
            }
            // Calculate the answer of the challenge.
            String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
            String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
            int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length());
            int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length());
            long c = req.getContent().readLong();
            ChannelBuffer input = ChannelBuffers.buffer(16);
            input.writeInt(a);
            input.writeInt(b);
            input.writeLong(c);
            ChannelBuffer output = ChannelBuffers.wrappedBuffer(MessageDigest.getInstance("MD5").digest(input.array()));
            res.setContent(output);
        } else {
            // Old handshake method with no challenge:
            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());
        connectSocket(ctx);
        return;
    }
    // Send an error page otherwise.
    sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
}
Also used : HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) WebSocketFrameEncoder(org.jboss.netty.handler.codec.http.websocket.WebSocketFrameEncoder) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) WebSocketFrameDecoder(org.jboss.netty.handler.codec.http.websocket.WebSocketFrameDecoder)

Example 10 with HttpResponseStatus

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

the class SubmissionHandler method handlePost.

private void handlePost(MessageEvent e, BagheeraHttpRequest request) {
    HttpResponseStatus status = BAD_REQUEST;
    ChannelBuffer content = request.getContent();
    String remoteIpAddress = HttpUtil.getRemoteAddr(request, ((InetSocketAddress) e.getChannel().getRemoteAddress()).getAddress().getHostAddress());
    if (content.readable() && content.readableBytes() > 0) {
        BagheeraMessage.Builder templateBuilder = BagheeraMessage.newBuilder();
        setMessageFields(request, e, templateBuilder, System.currentTimeMillis(), false);
        BagheeraMessage template = templateBuilder.buildPartial();
        BagheeraMessage.Builder storeBuilder = BagheeraMessage.newBuilder(template);
        storeBuilder.setPayload(ByteString.copyFrom(content.toByteBuffer()));
        storeBuilder.setId(request.getId());
        producer.send(storeBuilder.build());
        if (request.containsHeader(HEADER_OBSOLETE_DOCUMENT)) {
            handleObsoleteDocuments(request, remoteIpAddress, request.getHeaders(HEADER_OBSOLETE_DOCUMENT), template);
        } else {
            LOG.info("IP " + remoteIpAddress + " " + request.getNamespace() + " HTTP_PUT " + request.getId());
        }
        status = CREATED;
    }
    updateRequestMetrics(request.getNamespace(), request.getMethod().getName(), content.readableBytes());
    writeResponse(status, e, request.getNamespace(), URI.create(request.getId()).toString());
}
Also used : HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) InetSocketAddress(java.net.InetSocketAddress) BagheeraMessage(com.mozilla.bagheera.BagheeraProto.BagheeraMessage) ByteString(com.google.protobuf.ByteString) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

HttpResponseStatus (org.jboss.netty.handler.codec.http.HttpResponseStatus)11 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)4 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)4 DataInputStream (java.io.DataInputStream)3 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Configuration (org.apache.hadoop.conf.Configuration)3 DataOutputBuffer (org.apache.hadoop.io.DataOutputBuffer)3 ShuffleHeader (org.apache.hadoop.mapreduce.task.reduce.ShuffleHeader)3 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)3 AbstractChannel (org.jboss.netty.channel.AbstractChannel)3 Channel (org.jboss.netty.channel.Channel)3 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)3 SocketChannel (org.jboss.netty.channel.socket.SocketChannel)3 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)3 Test (org.junit.Test)3 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2