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

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

Introduction

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

Prototype

public static HttpMethod valueOf(String name) 

Source Link

Document

Returns the HttpMethod represented by the specified name.

Usage

From source file:com.titilink.camel.rest.client.CamelClient.java

License:LGPL

/**
 * ??GET POST PUT DELETE/*  w w w.java 2  s.  c o m*/
 *
 * @param uri               
 * @param reqBuffer         ?
 * @param additionalHeaders header????token 
 * @param challengeResponse challengeResponse
 * @param msTimeout         
 * @return RestResponse
 * @since v1.0.3
 */
public static RestResponse handleDefault(String method, URI uri, byte[] reqBuffer,
        Map<String, String> additionalHeaders, ChallengeResponse challengeResponse, long msTimeout) {
    return handle(HttpMethod.valueOf(method.toUpperCase()), uri, reqBuffer, additionalHeaders,
            challengeResponse, true, msTimeout);
}

From source file:com.twitter.http2.HttpStreamDecoder.java

License:Apache License

private StreamedHttpRequest createHttpRequest(HttpHeadersFrame httpHeadersFrame) throws Exception {
    // Create the first line of the request from the name/value pairs
    HttpMethod method = HttpMethod.valueOf(httpHeadersFrame.headers().get(":method"));
    String url = httpHeadersFrame.headers().get(":path");

    httpHeadersFrame.headers().remove(":method");
    httpHeadersFrame.headers().remove(":path");

    StreamedHttpRequest request = new StreamedHttpRequest(HttpVersion.HTTP_1_1, method, url);

    // Remove the scheme header
    httpHeadersFrame.headers().remove(":scheme");

    // Replace the SPDY host header with the HTTP host header
    String host = httpHeadersFrame.headers().get(":authority");
    httpHeadersFrame.headers().remove(":authority");
    httpHeadersFrame.headers().set("host", host);

    for (Map.Entry<String, String> e : httpHeadersFrame.headers()) {
        String name = e.getKey();
        String value = e.getValue();
        if (name.charAt(0) != ':') {
            request.headers().add(name, value);
        }//w  ww .j  a v a  2  s .c  om
    }

    // Set the Stream-ID as a header
    request.headers().set("X-SPDY-Stream-ID", httpHeadersFrame.getStreamId());

    // The Connection and Keep-Alive headers are no longer valid
    HttpHeaders.setKeepAlive(request, true);

    // Transfer-Encoding header is not valid
    request.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING);

    if (httpHeadersFrame.isLast()) {
        request.getContent().close();
        request.setDecoderResult(DecoderResult.SUCCESS);
    } else {
        request.setDecoderResult(DecoderResult.UNFINISHED);
    }

    return request;
}

From source file:com.vmware.dcp.common.http.netty.NettyHttpServiceClient.java

License:Open Source License

private void sendRequest(Operation op) {
    if (!checkScheme(op)) {
        return;/*from   w w w .  j  a va 2s. c  o  m*/
    }

    try {
        byte[] body = Utils.encodeBody(op);
        String pathAndQuery;
        String path = op.getUri().getPath();
        String query = op.getUri().getQuery();
        path = path == null || path.isEmpty() ? "/" : path;
        if (query != null) {
            pathAndQuery = path + "?" + query;
        } else {
            pathAndQuery = path;
        }

        if (this.httpProxy != null) {
            pathAndQuery = op.getUri().toString();
        }

        HttpRequest request = null;
        HttpMethod method = HttpMethod.valueOf(op.getAction().toString());

        if (body == null || body.length == 0) {
            request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, pathAndQuery);
        } else {
            ByteBuf content = Unpooled.wrappedBuffer(body);
            request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, pathAndQuery, content);
        }

        for (Entry<String, String> nameValue : op.getRequestHeaders().entrySet()) {
            request.headers().set(nameValue.getKey(), nameValue.getValue());
        }

        request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Long.toString(op.getContentLength()));
        request.headers().set(HttpHeaderNames.CONTENT_TYPE, op.getContentType());
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

        if (op.getContextId() != null) {
            request.headers().set(Operation.CONTEXT_ID_HEADER, op.getContextId());
        }

        if (op.getReferer() != null) {
            request.headers().set(Operation.REFERER_HEADER, op.getReferer().toString());
        }

        if (op.getCookies() != null) {
            String header = CookieJar.encodeCookies(op.getCookies());
            request.headers().set(HttpHeaderNames.COOKIE, header);
        }

        request.headers().set(HttpHeaderNames.USER_AGENT, this.userAgent);

        request.headers().set(HttpHeaderNames.ACCEPT, Operation.MEDIA_TYPE_APPLICATION_JSON);

        request.headers().set(HttpHeaderNames.HOST,
                op.getUri().getHost() + ((op.getUri().getPort() != -1) ? (":" + op.getUri().getPort()) : ""));

        op.nestCompletion((o, e) -> {
            if (e != null) {
                fail(e, op);
                return;
            }
            // After request is sent control is transferred to the
            // NettyHttpServerResponseHandler. The response handler will nest completions
            // and call complete() when response is received, which will invoke this completion
            op.complete();
        });

        op.getSocketContext().writeHttpRequest(request);
    } catch (Throwable e) {
        op.setBody(ServiceErrorResponse.create(e, Operation.STATUS_CODE_BAD_REQUEST,
                EnumSet.of(ErrorDetail.SHOULD_RETRY)));
        fail(e, op);
    }
}

From source file:com.vmware.xenon.common.http.netty.NettyHttpServiceClient.java

License:Open Source License

private void doSendRequest(Operation op) {
    final Object originalBody = op.getBodyRaw();
    try {/*w w  w  . j  ava  2  s .c om*/
        byte[] body = Utils.encodeBody(op);
        if (op.getContentLength() > getRequestPayloadSizeLimit()) {
            stopTracking(op);
            Exception e = new IllegalArgumentException("Content-Length " + op.getContentLength()
                    + " is greater than max size allowed " + getRequestPayloadSizeLimit());
            op.setBody(ServiceErrorResponse.create(e, Operation.STATUS_CODE_BAD_REQUEST));
            op.fail(e);
            return;
        }

        String pathAndQuery;
        String path = op.getUri().getPath();
        String query = op.getUri().getRawQuery();
        String userInfo = op.getUri().getRawUserInfo();
        path = path == null || path.isEmpty() ? "/" : path;
        if (query != null) {
            pathAndQuery = path + "?" + query;
        } else {
            pathAndQuery = path;
        }

        /**
         * NOTE: Pay close attention to calls that access the operation request headers, since
         * they will cause a memory allocation. We avoid the allocation by first checking if
         * the operation has any custom headers to begin with, then we check for the specific
         * header
         */
        boolean hasRequestHeaders = op.hasRequestHeaders();
        boolean useHttp2 = op.isConnectionSharing();
        if (this.httpProxy != null || useHttp2 || userInfo != null) {
            pathAndQuery = op.getUri().toString();
        }

        NettyFullHttpRequest request = null;
        HttpMethod method = HttpMethod.valueOf(op.getAction().toString());
        if (body == null || body.length == 0) {
            request = new NettyFullHttpRequest(HttpVersion.HTTP_1_1, method, pathAndQuery, Unpooled.buffer(0),
                    false);
        } else {
            ByteBuf content = Unpooled.wrappedBuffer(body, 0, (int) op.getContentLength());
            request = new NettyFullHttpRequest(HttpVersion.HTTP_1_1, method, pathAndQuery, content, false);
        }

        if (useHttp2) {
            // when operation is cloned, it may contain original streamId header. remove it.
            if (hasRequestHeaders) {
                op.getRequestHeaders().remove(Operation.STREAM_ID_HEADER);
            }
            // We set the operation so that once a streamId is assigned, we can record
            // the correspondence between the streamId and operation: this will let us
            // handle responses properly later.
            request.setOperation(op);

        }

        String pragmaHeader = op.getRequestHeader(Operation.PRAGMA_HEADER);

        if (op.isFromReplication() && pragmaHeader == null) {
            request.headers().set(HttpHeaderNames.PRAGMA, Operation.PRAGMA_DIRECTIVE_REPLICATED);
        }

        if (op.getTransactionId() != null) {
            request.headers().set(Operation.TRANSACTION_ID_HEADER, op.getTransactionId());
        }

        if (op.getContextId() != null) {
            request.headers().set(Operation.CONTEXT_ID_HEADER, op.getContextId());
        }

        AuthorizationContext ctx = op.getAuthorizationContext();
        if (ctx != null && ctx.getToken() != null) {
            request.headers().set(Operation.REQUEST_AUTH_TOKEN_HEADER, ctx.getToken());
        }

        boolean isXenonToXenon = op.isFromReplication();
        boolean isRequestWithCallback = false;
        if (hasRequestHeaders) {
            for (Entry<String, String> nameValue : op.getRequestHeaders().entrySet()) {
                String key = nameValue.getKey();
                if (!isXenonToXenon) {
                    if (Operation.REQUEST_CALLBACK_LOCATION_HEADER.equals(key)) {
                        isRequestWithCallback = true;
                        isXenonToXenon = true;
                    } else if (Operation.RESPONSE_CALLBACK_STATUS_HEADER.equals(key)) {
                        isXenonToXenon = true;
                    }
                }
                request.headers().set(nameValue.getKey(), nameValue.getValue());
            }
        }

        request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Long.toString(op.getContentLength()));
        request.headers().set(HttpHeaderNames.CONTENT_TYPE, op.getContentType());
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

        if (!isXenonToXenon) {
            if (op.getCookies() != null) {
                String header = CookieJar.encodeCookies(op.getCookies());
                request.headers().set(HttpHeaderNames.COOKIE, header);
            }

            if (op.hasReferer()) {
                request.headers().set(HttpHeaderNames.REFERER, op.getRefererAsString());
            }

            request.headers().set(HttpHeaderNames.USER_AGENT, this.userAgent);
            if (op.getRequestHeader(Operation.ACCEPT_HEADER) == null) {
                request.headers().set(HttpHeaderNames.ACCEPT, Operation.MEDIA_TYPE_EVERYTHING_WILDCARDS);
            }

            request.headers().set(HttpHeaderNames.HOST, op.getUri().getHost());
        }

        boolean doCookieJarUpdate = !isXenonToXenon;
        boolean stopTracking = !isRequestWithCallback;
        op.nestCompletion((o, e) -> {
            if (e != null) {
                fail(e, op, originalBody);
                return;
            }

            if (stopTracking) {
                stopTracking(op);
            }

            if (doCookieJarUpdate) {
                updateCookieJarFromResponseHeaders(o);
            }

            // After request is sent control is transferred to the
            // NettyHttpServerResponseHandler. The response handler will nest completions
            // and call complete() when response is received, which will invoke this completion
            op.complete();
        });

        op.getSocketContext().writeHttpRequest(request);
    } catch (Throwable e) {
        op.setBody(ServiceErrorResponse.create(e, Operation.STATUS_CODE_BAD_REQUEST,
                EnumSet.of(ErrorDetail.SHOULD_RETRY)));
        fail(e, op, originalBody);
    }
}

From source file:de.dfki.kiara.http.HttpRequestMessage.java

License:Open Source License

@Override
public TransportMessage set(String name, Object value) {
    if (Names.CONTENT_TYPE.equals(name)) {
        request.headers().set(HttpHeaders.Names.CONTENT_TYPE, value);
    } else if (Names.SESSION_ID.equals(name)) {
        request.headers().set("x-kiara-session", value);
    } else if (Names.REQUEST_URI.equals(name)) {
        request.setUri((String) value);
    } else if (Names.HTTP_METHOD.equals(name)) {
        request.setMethod(HttpMethod.valueOf(name));
    }/*from  w  ww  .  j  a v  a2 s.  c om*/
    return this;
}

From source file:io.apigee.trireme.container.netty.NettyHttpRequest.java

License:Open Source License

@Override
public void setMethod(String method) {
    req.setMethod(HttpMethod.valueOf(method));
}

From source file:io.higgs.http.server.HttpRequestDecoder.java

License:Apache License

@Override
protected HttpMessage createMessage(String[] initialLine) throws Exception {
    return new HttpRequest(HttpVersion.valueOf(initialLine[2]), HttpMethod.valueOf(initialLine[0]),
            initialLine[1]);//from www  .  j a v  a 2  s  . c  o m
}

From source file:io.jsync.http.impl.DefaultHttpClientRequest.java

License:Open Source License

private DefaultHttpClientRequest(final DefaultHttpClient client, final String method, final String uri,
        final Handler<HttpClientResponse> respHandler, final DefaultContext context, final boolean raw) {
    this.client = client;
    this.request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method), uri, false);
    this.chunked = false;
    this.respHandler = respHandler;
    this.context = context;
    this.raw = raw;

}

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;
    }// ww  w . j a v a 2 s  .com
    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:io.vertx.core.http.impl.VertxHttpRequestDecoder.java

License:Open Source License

@Override
protected HttpMessage createMessage(String[] initialLine) {
    return new DefaultHttpRequest(HttpVersion.valueOf(initialLine[2]), HttpMethod.valueOf(initialLine[0]),
            initialLine[1], new VertxHttpHeaders());
}