Example usage for io.netty.handler.codec.http HttpResponseStatus equals

List of usage examples for io.netty.handler.codec.http HttpResponseStatus equals

Introduction

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

Prototype

@Override
public boolean equals(Object o) 

Source Link

Document

Equality of HttpResponseStatus only depends on #code() .

Usage

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpBlobStore.java

License:Open Source License

private boolean cacheMiss(HttpResponseStatus status) {
    // Supporting NO_CONTENT for nginx webdav compatibility.
    return status.equals(HttpResponseStatus.NOT_FOUND) || status.equals(HttpResponseStatus.NO_CONTENT);
}

From source file:com.schibsted.triathlon.main.TriathlonRouterTest.java

License:Apache License

Matcher<HttpResponseStatus> matchResponseStatus(final HttpResponseStatus status) {
    return new TypeSafeMatcher<HttpResponseStatus>() {
        public boolean matchesSafely(HttpResponseStatus item) {
            return status.equals(item);
        }/*from   www  . j  av  a 2 s.c o m*/

        public void describeTo(Description description) {
            description.appendText("a response status " + status);
        }
    };
}

From source file:org.asynchttpclient.providers.netty.handler.Protocol.java

License:Open Source License

protected boolean handleRedirectAndExit(Request request, NettyResponseFuture<?> future, HttpResponse response,
        final Channel channel) throws Exception {

    io.netty.handler.codec.http.HttpResponseStatus status = response.getStatus();
    boolean redirectEnabled = request.isRedirectOverrideSet() ? request.isRedirectEnabled()
            : config.isRedirectEnabled();
    boolean isRedirectStatus = status.equals(MOVED_PERMANENTLY) || status.equals(FOUND)
            || status.equals(SEE_OTHER) || status.equals(TEMPORARY_REDIRECT);

    if (redirectEnabled && isRedirectStatus) {
        if (future.incrementAndGetCurrentRedirectCount() >= config.getMaxRedirects()) {
            throw new MaxRedirectException("Maximum redirect reached: " + config.getMaxRedirects());

        } else {/*from w  ww.  ja va  2 s . c  o m*/
            // We must allow 401 handling again.
            future.getAndSetAuth(false);

            String location = response.headers().get(HttpHeaders.Names.LOCATION);
            URI uri = AsyncHttpProviderUtils.getRedirectUri(future.getURI(), location);

            if (!uri.toString().equals(future.getURI().toString())) {
                final RequestBuilder requestBuilder = new RequestBuilder(future.getRequest());
                if (config.isRemoveQueryParamOnRedirect()) {
                    requestBuilder.setQueryParameters(null);
                }

                // FIXME why not do that for 301 and 307 too?
                // FIXME I think condition is wrong
                if ((status.equals(FOUND) || status.equals(SEE_OTHER))
                        && !(status.equals(FOUND) && config.isStrict302Handling())) {
                    requestBuilder.setMethod(HttpMethod.GET.name());
                }

                // in case of a redirect from HTTP to HTTPS, future attributes might change
                final boolean initialConnectionKeepAlive = future.isKeepAlive();
                final String initialPoolKey = channels.getPoolKey(future);

                future.setURI(uri);
                String newUrl = uri.toString();
                if (request.getURI().getScheme().startsWith(WEBSOCKET)) {
                    newUrl = newUrl.replaceFirst(HTTP, WEBSOCKET);
                }

                logger.debug("Redirecting to {}", newUrl);

                if (future.getHttpHeaders().contains(HttpHeaders.Names.SET_COOKIE2)) {
                    for (String cookieStr : future.getHttpHeaders().getAll(HttpHeaders.Names.SET_COOKIE2)) {
                        Cookie c = CookieDecoder.decode(cookieStr, timeConverter);
                        if (c != null) {
                            requestBuilder.addOrReplaceCookie(c);
                        }
                    }
                } else if (future.getHttpHeaders().contains(HttpHeaders.Names.SET_COOKIE)) {
                    for (String cookieStr : future.getHttpHeaders().getAll(HttpHeaders.Names.SET_COOKIE)) {
                        Cookie c = CookieDecoder.decode(cookieStr, timeConverter);
                        if (c != null) {
                            requestBuilder.addOrReplaceCookie(c);
                        }
                    }
                }

                Callback callback = new Callback(future) {
                    public void call() throws Exception {
                        if (!(initialConnectionKeepAlive && channel.isActive()
                                && channels.offerToPool(initialPoolKey, channel))) {
                            channels.finishChannel(channel);
                        }
                    }
                };

                if (HttpHeaders.isTransferEncodingChunked(response)) {
                    // We must make sure there is no bytes left before
                    // executing the next request.
                    // FIXME investigate this
                    Channels.setDefaultAttribute(channel, callback);
                } else {
                    // FIXME don't understand: this offers the connection to the pool, or even closes it, while the
                    // request has not been sent, right?
                    callback.call();
                }

                Request redirectRequest = requestBuilder.setUrl(newUrl).build();
                // FIXME why not reuse the channel is same host?
                requestSender.sendNextRequest(redirectRequest, future);
                return true;
            }
        }
    }
    return false;
}

From source file:org.eclipse.milo.opcua.stack.client.transport.http.OpcClientHttpCodec.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, HttpResponse httpResponse, List<Object> out) throws Exception {

    logger.trace("channelRead0: " + httpResponse);

    UaTransportRequest transportRequest = ctx.channel().attr(KEY_PENDING_REQUEST).getAndSet(null);

    if (httpResponse instanceof FullHttpResponse) {
        String contentType = httpResponse.headers().get(HttpHeaderNames.CONTENT_TYPE);

        FullHttpResponse fullHttpResponse = (FullHttpResponse) httpResponse;
        ByteBuf content = fullHttpResponse.content();

        UaResponseMessage responseMessage;

        switch (transportProfile) {
        case HTTPS_UABINARY: {
            if (!UABINARY_CONTENT_TYPE.equalsIgnoreCase(contentType)) {
                throw new UaException(StatusCodes.Bad_DecodingError, "unexpected content-type: " + contentType);
            }/*www.  j  a  v  a2  s .  co m*/

            OpcUaBinaryStreamDecoder decoder = new OpcUaBinaryStreamDecoder(content);
            responseMessage = (UaResponseMessage) decoder.readMessage(null);
            break;
        }

        case HTTPS_UAXML: {
            // TODO extract document from SOAP message body
            throw new UaException(StatusCodes.Bad_InternalError,
                    "no decoder for transport: " + transportProfile);
        }

        default:
            throw new UaException(StatusCodes.Bad_InternalError,
                    "no decoder for transport: " + transportProfile);
        }

        transportRequest.getFuture().complete(responseMessage);
    } else {
        HttpResponseStatus status = httpResponse.status();

        if (status.equals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE)) {
            transportRequest.getFuture()
                    .completeExceptionally(new UaException(StatusCodes.Bad_ResponseTooLarge));
        } else {
            transportRequest.getFuture().completeExceptionally(new UaException(StatusCodes.Bad_UnexpectedError,
                    String.format("%s: %s", status.code(), status.reasonPhrase())));
        }
    }
}

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

License:Apache License

private void runExpectHeaderTest(final Settings settings, final String expectation, final int contentLength,
        final HttpResponseStatus expectedStatus) throws InterruptedException {
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
        @Override/*from w  ww .ja  v a 2  s  .  c o m*/
        public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
            channel.sendResponse(
                    new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
        }

        @Override
        public void dispatchBadRequest(RestRequest request, RestChannel channel, ThreadContext threadContext,
                Throwable cause) {
            throw new AssertionError();
        }
    };
    try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays,
            pageRecycler, threadPool, xContentRegistry(), dispatcher)) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (NioHttpClient client = new NioHttpClient()) {
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
                    "/");
            request.headers().set(HttpHeaderNames.EXPECT, expectation);
            HttpUtil.setContentLength(request, contentLength);

            final FullHttpResponse response = client.post(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(expectedStatus));
                if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) {
                    final FullHttpRequest continuationRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
                            HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER);
                    final FullHttpResponse continuationResponse = client.post(remoteAddress.address(),
                            continuationRequest);
                    try {
                        assertThat(continuationResponse.status(), is(HttpResponseStatus.OK));
                        assertThat(new String(ByteBufUtil.getBytes(continuationResponse.content()),
                                StandardCharsets.UTF_8), is("done"));
                    } finally {
                        continuationResponse.release();
                    }
                }
            } finally {
                response.release();
            }
        }
    }
}

From source file:org.thingsplode.synapse.proxy.handlers.HttpResponse2ResponseDecoder.java

License:Apache License

private boolean marshalledBodyExpected(HttpResponseStatus status) {
    return (status.equals(HttpResponseStatus.ACCEPTED)) || (status.equals(HttpResponseStatus.OK))
            || (status.equals(HttpResponseStatus.CREATED));
}