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:org.asynchttpclient.Dsl.java

License:Open Source License

public static RequestBuilder head(String url) {
    return request(HttpMethod.HEAD.name(), url);
}

From source file:org.cloudfoundry.dependency.resource.http.HttpCheckAction.java

License:Apache License

private Mono<Instant> requestLastModified() {
    return this.httpClient.request(HttpMethod.HEAD, getUri(), request -> request).flatMap(
            response -> Mono.just(response.responseHeaders().getTimeMillis(HttpHeaderNames.LAST_MODIFIED)))
            .map(Instant::ofEpochMilli);
}

From source file:org.elasticsearch.hadoop.http.netty4.Netty4HttpRequest.java

License:Apache License

@Override
public Method method() {
    HttpMethod httpMethod = request.method();
    if (httpMethod == HttpMethod.GET)
        return Method.GET;

    if (httpMethod == HttpMethod.POST)
        return Method.POST;

    if (httpMethod == HttpMethod.PUT)
        return Method.PUT;

    if (httpMethod == HttpMethod.DELETE)
        return Method.DELETE;

    if (httpMethod == HttpMethod.HEAD) {
        return Method.HEAD;
    }/*from www. j a  v a2 s.  c  om*/

    if (httpMethod == HttpMethod.OPTIONS) {
        return Method.OPTIONS;
    }

    return Method.GET;
}

From source file:org.elasticsearch.http.nio.HttpReadWriteHandlerTests.java

License:Apache License

private NioHttpRequest prepareHandlerForResponse(HttpReadWriteHandler handler) throws IOException {
    HttpMethod method = randomBoolean() ? HttpMethod.GET : HttpMethod.HEAD;
    HttpVersion version = randomBoolean() ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;
    String uri = "http://localhost:9090/" + randomAlphaOfLength(8);

    io.netty.handler.codec.http.HttpRequest request = new DefaultFullHttpRequest(version, method, uri);
    ByteBuf buf = requestEncoder.encode(request);
    try {/*  w w w. j  a v  a  2s  .c  o  m*/
        handler.consumeReads(toChannelBuffer(buf));
    } finally {
        buf.release();
    }

    ArgumentCaptor<NioHttpRequest> requestCaptor = ArgumentCaptor.forClass(NioHttpRequest.class);
    verify(transport, atLeastOnce()).incomingRequest(requestCaptor.capture(), any(HttpChannel.class));

    NioHttpRequest nioHttpRequest = requestCaptor.getValue();
    assertNotNull(nioHttpRequest);
    assertEquals(method.name(), nioHttpRequest.method().name());
    if (version == HttpVersion.HTTP_1_1) {
        assertEquals(HttpRequest.HttpVersion.HTTP_1_1, nioHttpRequest.protocolVersion());
    } else {
        assertEquals(HttpRequest.HttpVersion.HTTP_1_0, nioHttpRequest.protocolVersion());
    }
    assertEquals(nioHttpRequest.uri(), uri);
    return nioHttpRequest;
}

From source file:org.elasticsearch.http.nio.NioHttpChannel.java

License:Apache License

@Override
public void sendResponse(RestResponse response) {
    // if the response object was created upstream, then use it;
    // otherwise, create a new one
    ByteBuf buffer = ByteBufUtils.toByteBuf(response.content());
    final FullHttpResponse resp;
    if (HttpMethod.HEAD.equals(nettyRequest.method())) {
        resp = newResponse(Unpooled.EMPTY_BUFFER);
    } else {/*from www  .ja  va 2 s .c o  m*/
        resp = newResponse(buffer);
    }
    resp.setStatus(getStatus(response.status()));

    String opaque = nettyRequest.headers().get("X-Opaque-Id");
    if (opaque != null) {
        setHeaderField(resp, "X-Opaque-Id", opaque);
    }

    // Add all custom headers
    addCustomHeaders(resp, response.getHeaders());
    addCustomHeaders(resp, threadContext.getResponseHeaders());

    ArrayList<Releasable> toClose = new ArrayList<>(3);

    boolean success = false;
    try {
        // If our response doesn't specify a content-type header, set one
        setHeaderField(resp, HttpHeaderNames.CONTENT_TYPE.toString(), response.contentType(), false);
        // If our response has no content-length, calculate and set one
        setHeaderField(resp, HttpHeaderNames.CONTENT_LENGTH.toString(), String.valueOf(buffer.readableBytes()),
                false);

        addCookies(resp);

        BytesReference content = response.content();
        if (content instanceof Releasable) {
            toClose.add((Releasable) content);
        }
        BytesStreamOutput bytesStreamOutput = bytesOutputOrNull();
        if (bytesStreamOutput instanceof ReleasableBytesStreamOutput) {
            toClose.add((Releasable) bytesStreamOutput);
        }

        if (isCloseConnection()) {
            toClose.add(nioChannel::close);
        }

        BiConsumer<Void, Exception> listener = (aVoid, ex) -> Releasables.close(toClose);
        nioChannel.getContext().sendMessage(new NioHttpResponse(sequence, resp), listener);
        success = true;
    } finally {
        if (success == false) {
            Releasables.close(toClose);
        }
    }
}

From source file:org.elasticsearch.http.nio.NioHttpRequest.java

License:Apache License

@Override
public RestRequest.Method method() {
    HttpMethod httpMethod = request.method();
    if (httpMethod == HttpMethod.GET)
        return RestRequest.Method.GET;

    if (httpMethod == HttpMethod.POST)
        return RestRequest.Method.POST;

    if (httpMethod == HttpMethod.PUT)
        return RestRequest.Method.PUT;

    if (httpMethod == HttpMethod.DELETE)
        return RestRequest.Method.DELETE;

    if (httpMethod == HttpMethod.HEAD) {
        return RestRequest.Method.HEAD;
    }//from  www .jav  a 2s  .  co  m

    if (httpMethod == HttpMethod.OPTIONS) {
        return RestRequest.Method.OPTIONS;
    }

    if (httpMethod == HttpMethod.PATCH) {
        return RestRequest.Method.PATCH;
    }

    if (httpMethod == HttpMethod.TRACE) {
        return RestRequest.Method.TRACE;
    }

    if (httpMethod == HttpMethod.CONNECT) {
        return RestRequest.Method.CONNECT;
    }

    throw new IllegalArgumentException("Unexpected http method: " + httpMethod);
}

From source file:org.glassfish.jersey.netty.httpserver.NettyHttp2ResponseWriter.java

License:Open Source License

@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext)
        throws ContainerException {

    String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
    int statusCode = responseContext.getStatus();

    HttpResponseStatus status = reasonPhrase == null ? HttpResponseStatus.valueOf(statusCode)
            : new HttpResponseStatus(statusCode, reasonPhrase);

    DefaultHttp2Headers response = new DefaultHttp2Headers();
    response.status(Integer.toString(responseContext.getStatus()));

    for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
        response.add(e.getKey().toLowerCase(), e.getValue());
    }/*  w  ww . ja  v  a 2  s . c o m*/

    response.set(HttpHeaderNames.CONTENT_LENGTH, Long.toString(contentLength));

    ctx.writeAndFlush(new DefaultHttp2HeadersFrame(response));

    if (!headersFrame.headers().method().equals(HttpMethod.HEAD.asciiName())
            && (contentLength > 0 || contentLength == -1)) {

        return new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                write(new byte[] { (byte) b });
            }

            @Override
            public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {

                ByteBuf buffer = ctx.alloc().buffer(len);
                buffer.writeBytes(b, off, len);

                ctx.writeAndFlush(new DefaultHttp2DataFrame(buffer, false));
            }

            @Override
            public void flush() throws IOException {
                ctx.flush();
            }

            @Override
            public void close() throws IOException {
                ctx.write(new DefaultHttp2DataFrame(true)).addListener(NettyResponseWriter.FLUSH_FUTURE);
            }
        };

    } else {
        ctx.writeAndFlush(new DefaultHttp2DataFrame(true));
        return null;
    }
}

From source file:org.glassfish.jersey.netty.httpserver.NettyResponseWriter.java

License:Open Source License

@Override
public synchronized OutputStream writeResponseStatusAndHeaders(long contentLength,
        ContainerResponse responseContext) throws ContainerException {

    if (responseWritten) {
        LOGGER.log(Level.FINE, "Response already written.");
        return null;
    }//from  w w  w  .  j a v a 2  s  . co m

    responseWritten = true;

    String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
    int statusCode = responseContext.getStatus();

    HttpResponseStatus status = reasonPhrase == null ? HttpResponseStatus.valueOf(statusCode)
            : new HttpResponseStatus(statusCode, reasonPhrase);

    DefaultHttpResponse response;
    if (contentLength == 0) {
        response = new DefaultFullHttpResponse(req.protocolVersion(), status);
    } else {
        response = new DefaultHttpResponse(req.protocolVersion(), status);
    }

    for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
        response.headers().add(e.getKey(), e.getValue());
    }

    if (contentLength == -1) {
        HttpUtil.setTransferEncodingChunked(response, true);
    } else {
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, contentLength);
    }

    if (HttpUtil.isKeepAlive(req)) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    ctx.writeAndFlush(response);

    if (req.method() != HttpMethod.HEAD && (contentLength > 0 || contentLength == -1)) {

        JerseyChunkedInput jerseyChunkedInput = new JerseyChunkedInput(ctx.channel());

        if (HttpUtil.isTransferEncodingChunked(response)) {
            ctx.write(new HttpChunkedInput(jerseyChunkedInput)).addListener(FLUSH_FUTURE);
        } else {
            ctx.write(new HttpChunkedInput(jerseyChunkedInput)).addListener(FLUSH_FUTURE);
        }
        return jerseyChunkedInput;

    } else {
        ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        return null;
    }
}

From source file:org.ireland.jnetty.http.HttpServletRequestImpl.java

License:Open Source License

public boolean isHead() {
    return HttpMethod.HEAD == _httpMethod;
}

From source file:org.restexpress.RestExpressTest.java

License:Apache License

@Test
public void shouldCallAltMethods() throws ClientProtocolException, IOException {
    int port = nextPort();
    String testUrl = createUrl(TEST_URL_PATTERN, port);
    RestExpress re = new RestExpress();
    NoopController controller = new NoopController();
    re.uri(TEST_PATH, controller).method(HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.PATCH);
    re.bind(port);/* w  w w .ja  v a2 s. c  o m*/

    waitForStartup();

    HttpGet get = new HttpGet(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(get);
        assertEquals(405, response.getStatusLine().getStatusCode());
    } finally {
        get.releaseConnection();
    }

    HttpOptions options = new HttpOptions(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(options);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(0, controller.delete);
        assertEquals(0, controller.read);
        assertEquals(0, controller.create);
        assertEquals(0, controller.update);
        assertEquals(1, controller.options);
        assertEquals(0, controller.head);
        assertEquals(0, controller.patch);
    } finally {
        options.releaseConnection();
    }

    HttpHead head = new HttpHead(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(head);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(0, controller.delete);
        assertEquals(0, controller.read);
        assertEquals(0, controller.create);
        assertEquals(0, controller.update);
        assertEquals(1, controller.options);
        assertEquals(1, controller.head);
        assertEquals(0, controller.patch);
    } finally {
        head.releaseConnection();
    }

    HttpPatch patch = new HttpPatch(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(patch);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(0, controller.delete);
        assertEquals(0, controller.read);
        assertEquals(0, controller.create);
        assertEquals(0, controller.update);
        assertEquals(1, controller.options);
        assertEquals(1, controller.head);
        assertEquals(1, controller.patch);
    } finally {
        patch.releaseConnection();
    }

    re.shutdown(true);
}