Example usage for io.vertx.core.http HttpMethod HEAD

List of usage examples for io.vertx.core.http HttpMethod HEAD

Introduction

In this page you can find the example usage for io.vertx.core.http HttpMethod HEAD.

Prototype

HttpMethod HEAD

To view the source code for io.vertx.core.http HttpMethod HEAD.

Click Source Link

Usage

From source file:com.groupon.vertx.utils.HealthcheckHandler.java

License:Apache License

protected void processHeartBeatResponse(Boolean exists, HttpServerRequest request, long startTime) {
    HttpResponseStatus status;/*from  www .  jav  a2s  . co  m*/
    final boolean includeBody = !request.method().equals(HttpMethod.HEAD);

    if (exists) {
        status = HttpResponseStatus.OK;
    } else {
        status = HttpResponseStatus.SERVICE_UNAVAILABLE;
    }

    setCommonHttpResponse(request, status);

    String responseBody = status.reasonPhrase();
    if (includeBody) {
        request.response().end(responseBody);
    } else {
        request.response().putHeader(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(responseBody.length()));
        request.response().end();
    }

    long totalTime = System.currentTimeMillis() - startTime;
    LOG.debug("handle", "healthcheckResponse", new String[] { "method", "status", "totalTime" },
            request.method(), status.code(), totalTime);
}

From source file:com.groupon.vertx.utils.HealthcheckHandler.java

License:Apache License

protected void processExceptionResponse(HttpServerRequest request, Exception ex, long startTime) {
    HttpResponseStatus status = HttpResponseStatus.SERVICE_UNAVAILABLE;
    final boolean includeBody = !request.method().equals(HttpMethod.HEAD);
    String responseBody = status.reasonPhrase() + ": " + ex.getMessage();

    setCommonHttpResponse(request, status);

    if (includeBody) {
        request.response().end(responseBody);
    } else {/*w w w  .  j  a v a  2  s .  c  om*/
        request.response().putHeader(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(responseBody.length()));
        request.response().end();
    }

    long totalTime = System.currentTimeMillis() - startTime;
    LOG.debug("handle", "healthcheckResponse", new String[] { "method", "status", "totalTime" },
            request.method(), status.code(), totalTime);
}

From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java

License:Open Source License

@Override
public void handle(RoutingContext context) {
    HttpServerRequest request = context.request();
    if (request.method() != HttpMethod.GET && request.method() != HttpMethod.HEAD) {
        if (log.isTraceEnabled())
            log.trace("Not GET or HEAD so ignoring request");
        context.next();/* ww  w . j a v  a2 s  .  co m*/
    } else {
        String path = context.normalisedPath();
        // if the normalized path is null it cannot be resolved
        if (path == null) {
            log.warn("Invalid path: " + context.request().path() + " so returning 404");
            context.fail(NOT_FOUND.code());
            return;
        }

        // only root is known for sure to be a directory. all other directories must be identified as such.
        if (!directoryListing && "/".equals(path)) {
            path = indexPage;
        }

        // can be called recursive for index pages
        sendStatic(context, path);

    }
}

From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java

License:Open Source License

private void sendFile(RoutingContext context, String file, FileProps fileProps) {
    HttpServerRequest request = context.request();

    Long offset = null;/*from   ww w .j  a  v a2  s. c  o m*/
    Long end = null;
    MultiMap headers = null;

    if (rangeSupport) {
        // check if the client is making a range request
        String range = request.getHeader("Range");
        // end byte is length - 1
        end = fileProps.size() - 1;

        if (range != null) {
            Matcher m = RANGE.matcher(range);
            if (m.matches()) {
                try {
                    String part = m.group(1);
                    // offset cannot be empty
                    offset = Long.parseLong(part);
                    // offset must fall inside the limits of the file
                    if (offset < 0 || offset >= fileProps.size()) {
                        throw new IndexOutOfBoundsException();
                    }
                    // length can be empty
                    part = m.group(2);
                    if (part != null && part.length() > 0) {
                        // ranges are inclusive
                        end = Long.parseLong(part);
                        // offset must fall inside the limits of the file
                        if (end < offset || end >= fileProps.size()) {
                            throw new IndexOutOfBoundsException();
                        }
                    }
                } catch (NumberFormatException | IndexOutOfBoundsException e) {
                    context.fail(REQUESTED_RANGE_NOT_SATISFIABLE.code());
                    return;
                }
            }
        }

        // notify client we support range requests
        headers = request.response().headers();
        headers.set("Accept-Ranges", "bytes");
        // send the content length even for HEAD requests
        headers.set("Content-Length", Long.toString(end + 1 - (offset == null ? 0 : offset)));
    }

    writeCacheHeaders(request, fileProps);

    if (request.method() == HttpMethod.HEAD) {
        request.response().end();
    } else {
        if (rangeSupport && offset != null) {
            // must return content range
            headers.set("Content-Range", "bytes " + offset + "-" + end + "/" + fileProps.size());
            // return a partial response
            request.response().setStatusCode(PARTIAL_CONTENT.code());

            // Wrap the sendFile operation into a TCCL switch, so the file resolver would find the file from the set
            // classloader (if any).
            final Long finalOffset = offset;
            final Long finalEnd = end;
            wrapInTCCLSwitch(() -> request.response().sendFile(file, finalOffset, finalEnd + 1, res2 -> {
                if (res2.failed()) {
                    context.fail(res2.cause());
                }
            }), null);
        } else {
            // Wrap the sendFile operation into a TCCL switch, so the file resolver would find the file from the set
            // classloader (if any).
            wrapInTCCLSwitch(() -> request.response().sendFile(file, res2 -> {
                if (res2.failed()) {
                    context.fail(res2.cause());
                }
            }), null);
        }
    }
}

From source file:io.gravitee.gateway.http.vertx.VertxHttpClient.java

License:Apache License

private HttpMethod convert(io.gravitee.common.http.HttpMethod httpMethod) {
    switch (httpMethod) {
    case CONNECT:
        return HttpMethod.CONNECT;
    case DELETE:/*from   ww  w.ja  v a 2  s  .c o  m*/
        return HttpMethod.DELETE;
    case GET:
        return HttpMethod.GET;
    case HEAD:
        return HttpMethod.HEAD;
    case OPTIONS:
        return HttpMethod.OPTIONS;
    case PATCH:
        return HttpMethod.PATCH;
    case POST:
        return HttpMethod.POST;
    case PUT:
        return HttpMethod.PUT;
    case TRACE:
        return HttpMethod.TRACE;
    }

    return null;
}

From source file:org.etourdot.vertx.marklogic.http.impl.request.DefaultMarklogicRequest.java

License:Open Source License

@Override
public MarkLogicRequest head(String uri) {
    return method(uri, HttpMethod.HEAD);
}