Example usage for io.netty.handler.codec.http HttpMethod HEAD

List of usage examples for io.netty.handler.codec.http HttpMethod HEAD

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpMethod HEAD.

Prototype

HttpMethod HEAD

To view the source code for io.netty.handler.codec.http HttpMethod HEAD.

Click Source Link

Document

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.

Usage

From source file:io.crate.protocols.http.HttpBlobHandler.java

License:Apache License

private boolean possibleRedirect(HttpRequest request, String index, String digest) {
    HttpMethod method = request.method();
    if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.HEAD)
            || (method.equals(HttpMethod.PUT) && HttpUtil.is100ContinueExpected(request))) {
        String redirectAddress;/*from   w w  w . j  a v  a2 s .  c  o  m*/
        try {
            redirectAddress = blobService.getRedirectAddress(index, digest);
        } catch (MissingHTTPEndpointException ex) {
            simpleResponse(HttpResponseStatus.BAD_GATEWAY);
            return true;
        }

        if (redirectAddress != null) {
            LOGGER.trace("redirectAddress: {}", redirectAddress);
            sendRedirect(activeScheme + redirectAddress);
            return true;
        }
    }
    return false;
}

From source file:io.crate.protocols.http.HttpBlobHandler.java

License:Apache License

private void handleBlobRequest(HttpRequest request, @Nullable HttpContent content) throws IOException {
    if (possibleRedirect(request, index, digest)) {
        reset();//  ww  w .j a  va 2 s .  co m
        return;
    }

    HttpMethod method = request.method();
    if (method.equals(HttpMethod.GET)) {
        get(request, index, digest);
        reset();
    } else if (method.equals(HttpMethod.HEAD)) {
        head(index, digest);
        reset();
    } else if (method.equals(HttpMethod.PUT)) {
        put(content, index, digest);
    } else if (method.equals(HttpMethod.DELETE)) {
        delete(index, digest);
        reset();
    } else {
        simpleResponse(HttpResponseStatus.METHOD_NOT_ALLOWED);
        reset();
    }
}

From source file:io.gatling.http.client.BasicHttpTest.java

License:Apache License

@Test
void testHeadHasEmptyBody() throws Throwable {
    withClient().run(client -> withServer(server).run(server -> {
        server.enqueueOk();/*  ww w.  j av a  2s . co  m*/
        Request request = new RequestBuilder(HttpMethod.HEAD, Uri.create(getTargetUrl())).build();
        client.test(request, 0, new TestListener() {
            @Override
            public void onComplete0() {
                assertEquals(200, status.code());
                assertNull(chunks);
            }
        }).get(TIMEOUT_SECONDS, SECONDS);
    }));
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientImpl.java

License:Apache License

protected boolean shouldFollowRedirectForRequest(HttpClientConfig config, HttpClientRequest<I> request) {
    HttpClientConfig.RedirectsHandling redirectsHandling = config.getFollowRedirect();

    switch (redirectsHandling) {
    case Enable:/*w  ww  .  j a  v  a2 s .  c o  m*/
        return true;
    case Disable:
        return false;
    case Undefined:
        return request.getMethod() == HttpMethod.HEAD || request.getMethod() == HttpMethod.GET;
    default:
        return false;
    }
}

From source file:io.urmia.api.handler.RestApiHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {

    if (msg instanceof HttpRequest) {

        if (requestState != RequestState.EXPECT_REQUEST) {
            sendError(ctx, ERROR_BAD_REQUEST);
            return;
        }//from ww w  . j  a va  2 s  .  c o  m

        request = (HttpRequest) msg;

        log.info("received HttpRequest: {} {}", request.getMethod(), request.getUri());

        final ObjectRequest r;

        try {
            r = new ObjectRequest(request);
        } catch (ExceptionInInitializerError e) {
            log.warn("unable to parse the request into a command: {}", request.getUri());
            return;
        }

        objectRequest = r;
        httpRequest = request;

        if (HttpMethod.PUT.equals(request.getMethod())) {
            if (objectRequest.isNotDirectory()) // make dir request
                setupProxyToPUT(ctx, objectRequest);

            requestState = RequestState.EXPECT_CONTENT_OR_LAST;
        } else {
            requestState = RequestState.EXPECT_LAST;
            ctx.read();
        }

        return;
    }

    if (objectRequest == null) {
        String uri = request == null ? "" : request.getUri();
        log.warn("not expecting an empty objectRequest. parse error maybe: {}", uri);
        ByteBuf body = request == null || request.getMethod().equals(HttpMethod.HEAD) ? null
                : errorBody("ResourceNotFoundError", uri + " does not exist");
        sendError(ctx, HttpResponseStatus.NOT_FOUND, body);
        return;
    }

    if (msg instanceof HttpContent) {

        if (requestState != RequestState.EXPECT_LAST && requestState != RequestState.EXPECT_CONTENT_OR_LAST) {
            log.warn("not expecting LAST or CONTENT, requestState: {}", requestState);
            sendError(ctx, HttpResponseStatus.NOT_EXTENDED);
            return;
        }

        final boolean last = msg instanceof LastHttpContent;
        final boolean emptyLast = last && msg == LastHttpContent.EMPTY_LAST_CONTENT;

        if (proxyMode && !emptyLast) // todo: the emptyLast was added for mln
            ctx.fireChannelRead(msg);

        // example of reading only if at the end
        if (last) {

            log.debug("received LastHttpContent: {}", msg);
            requestState = RequestState.EXPECT_REQUEST;

            final HttpRequest request = httpRequest;

            if (HttpMethod.HEAD.equals(request.getMethod())) {
                handleHEAD(ctx, objectRequest);
                return;
            }

            if (HttpMethod.GET.equals(request.getMethod())) {
                handleGET(ctx, objectRequest);
                return;
            }

            if (HttpMethod.DELETE.equals(request.getMethod())) {
                handleDELETE(ctx, objectRequest);
                return;
            }

            if (HttpMethod.PUT.equals(request.getMethod())) {
                if (proxyMode)
                    log.info("finished file upload: {}", objectRequest);
                else
                    handlePUTmkdir(ctx, objectRequest);
                return;
            }

            log.warn("unknown request: {}", request);
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        }

        return;
    }

    log.warn("unexpected msg type: {}", msg);
    sendError(ctx, HttpResponseStatus.BAD_REQUEST);
}

From source file:io.urmia.st.StorageServerHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {

    if (msg instanceof HttpRequest) {

        requestStartMS = System.currentTimeMillis();

        request = (HttpRequest) msg;//ww  w  . j  a va 2  s  .c  o  m

        log.info("received HttpRequest: {} {}", request.getMethod(), request.getUri());

        // todo: what's this?
        //if (is100ContinueExpected(request)) {
        //    send100Continue(ctx);
        //}

        if (HttpMethod.PUT.equals(request.getMethod())) {
            // start doing the fs put. next msg is not a LastHttpContent
            handlePUT(ctx, request);
            //return;
        }

        ctx.read();
        return;
    }

    if (msg instanceof HttpContent) {

        // New chunk is received
        HttpContent chunk = (HttpContent) msg;

        //log.info("chunk {} of size: {}", chunk, chunk.content().readableBytes());

        if (fileChannel != null) {
            writeToFile(chunk.content());
        }

        // example of reading only if at the end
        if (chunk instanceof LastHttpContent) {

            log.trace("received LastHttpContent: {}", chunk);

            if (HttpMethod.HEAD.equals(request.getMethod())) {
                handleHEAD(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.GET.equals(request.getMethod())) {
                handleGET(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.DELETE.equals(request.getMethod())) {
                handleDELETE(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.PUT.equals(request.getMethod())) {
                // TODO: reset() if exception catch or timeout (i.e. no LastHttpContent)
                writeResponse(ctx, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK),
                        true); // close the connection after upload (mput) done
                reset();
                access.success(ctx, "PUT", uri, requestStartMS);
                ctx.read();
                return;
            }

            log.warn("unknown request: {}", request);
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        }

        ctx.read();
        return;
    }

    log.warn("unknown msg type: {}", msg);
}

From source file:io.vertx.core.http.impl.HttpUtils.java

License:Open Source License

static HttpMethod toNettyHttpMethod(io.vertx.core.http.HttpMethod method, String rawMethod) {
    switch (method) {
    case CONNECT: {
        return HttpMethod.CONNECT;
    }/*w  ww. j a v  a 2  s .  co  m*/
    case GET: {
        return HttpMethod.GET;
    }
    case PUT: {
        return HttpMethod.PUT;
    }
    case POST: {
        return HttpMethod.POST;
    }
    case DELETE: {
        return HttpMethod.DELETE;
    }
    case HEAD: {
        return HttpMethod.HEAD;
    }
    case OPTIONS: {
        return HttpMethod.OPTIONS;
    }
    case TRACE: {
        return HttpMethod.TRACE;
    }
    case PATCH: {
        return HttpMethod.PATCH;
    }
    default: {
        return HttpMethod.valueOf(rawMethod);
    }
    }
}

From source file:org.apache.hyracks.http.server.AbstractServlet.java

License:Apache License

@Override
public void handle(IServletRequest request, IServletResponse response) {
    try {//from   w  ww. j a v  a  2 s. c om
        final HttpMethod method = request.getHttpRequest().method();
        if (HttpMethod.GET.equals(method)) {
            get(request, response);
        } else if (HttpMethod.HEAD.equals(method)) {
            head(request, response);
        } else if (HttpMethod.POST.equals(method)) {
            post(request, response);
        } else if (HttpMethod.PUT.equals(method)) {
            put(request, response);
        } else if (HttpMethod.DELETE.equals(method)) {
            delete(request, response);
        } else if (HttpMethod.OPTIONS.equals(method)) {
            options(request, response);
        } else {
            notAllowed(method, response);
        }
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Unhandled exception", e);
        response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    } catch (Throwable th) { //NOSONAR Just logging and then throwing again
        try {
            LOGGER.log(Level.WARNING, "Unhandled throwable", th);
        } catch (Throwable loggingFailure) {// NOSONAR... swallow logging failure
        }
        throw th;
    }
}

From source file:org.apache.hyracks.http.server.AbstractServlet.java

License:Apache License

@SuppressWarnings("squid:S1172")
protected void head(IServletRequest request, IServletResponse response) throws Exception {
    // designed to be extended but an error in standard case
    notAllowed(HttpMethod.HEAD, response);
}

From source file:org.apache.tajo.storage.http.ExampleHttpServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext context, FullHttpRequest request) throws Exception {

    if (request.getMethod().equals(HttpMethod.HEAD)) {

        processHead(context, request);/*from  w  ww .j  a  va 2  s  . com*/

    } else if (request.getMethod().equals(HttpMethod.GET)) {

        processGet(context, request);

    } else {
        // error
        String msg = "Not supported method: " + request.getMethod();
        LOG.error(msg);
        context.writeAndFlush(getBadRequest(msg));
    }
}