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

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

Introduction

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

Prototype

AsciiString name

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

Click Source Link

Usage

From source file:com.barchart.netty.rest.server.RestHandlerBase.java

License:BSD License

@Override
public void handle(final HttpServerRequest request) throws IOException {

    final HttpMethod method = request.getMethod();

    try {/*  w  ww  . ja  v a2s  .  c  o  m*/
        if (method == HttpMethod.GET) {
            get(request);
        } else if (method == HttpMethod.POST) {
            post(request);
        } else if (method == HttpMethod.PUT) {
            put(request);
        } else if (method == HttpMethod.DELETE) {
            delete(request);
        } else {
            complete(request.response(), HttpResponseStatus.METHOD_NOT_ALLOWED,
                    method.name() + " not implemented");
        }
    } catch (final Throwable t) {
        complete(request.response(), HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }

}

From source file:com.ibasco.agql.core.AbstractWebRequest.java

License:Open Source License

protected void method(HttpMethod method) {
    request().setMethod(method.name());
}

From source file:com.linecorp.armeria.server.http.tomcat.TomcatServiceInvocationHandler.java

License:Apache License

private Request convertRequest(ServiceInvocationContext ctx) {
    final FullHttpRequest req = ctx.originalRequest();
    final String mappedPath = ctx.mappedPath();

    final Request coyoteReq = new Request();

    // Set the remote host/address.
    final InetSocketAddress remoteAddr = (InetSocketAddress) ctx.remoteAddress();
    coyoteReq.remoteAddr().setString(remoteAddr.getAddress().getHostAddress());
    coyoteReq.remoteHost().setString(remoteAddr.getHostString());
    coyoteReq.setRemotePort(remoteAddr.getPort());

    // Set the local host/address.
    final InetSocketAddress localAddr = (InetSocketAddress) ctx.localAddress();
    coyoteReq.localAddr().setString(localAddr.getAddress().getHostAddress());
    coyoteReq.localName().setString(hostname());
    coyoteReq.setLocalPort(localAddr.getPort());

    // Set the method.
    final HttpMethod method = req.method();
    coyoteReq.method().setString(method.name());

    // Set the request URI.
    final byte[] uriBytes = mappedPath.getBytes(StandardCharsets.US_ASCII);
    coyoteReq.requestURI().setBytes(uriBytes, 0, uriBytes.length);

    // Set the query string if any.
    final int queryIndex = req.uri().indexOf('?');
    if (queryIndex >= 0) {
        coyoteReq.queryString().setString(req.uri().substring(queryIndex + 1));
    }/*from  w w  w  .  j  a v a2  s. c  o  m*/

    // Set the headers.
    final MimeHeaders cHeaders = coyoteReq.getMimeHeaders();
    convertHeaders(req.headers(), cHeaders);
    convertHeaders(req.trailingHeaders(), cHeaders);

    // Set the content.
    final ByteBuf content = req.content();
    if (content.isReadable()) {
        coyoteReq.setInputBuffer(new InputBufferImpl(content));
    }

    return coyoteReq;
}

From source file:com.mastfrog.acteur.headers.Method.java

License:Open Source License

public static Method get(HttpRequest req) {
    HttpMethod m = req.getMethod();
    return Method.valueOf(m.name().toUpperCase());
}

From source file:com.nextcont.ecm.fileengine.http.nettyServer.HttpUploadServerHandler.java

License:Apache License

private void doHttpRequest(ChannelHandlerContext ctx, HttpRequest httpRequest) throws URISyntaxException {
    HttpRequest request = this.request = httpRequest;
    HttpMethod httpMethod = request.getMethod();

    if (httpMethod.equals(HttpMethod.GET)) {
        doGet(ctx, request);//from  www.  j av  a2 s  .  co m

    } else if (httpMethod.equals(HttpMethod.POST)) {
        doPost(ctx, request);

    } else {
        responseContent.setLength(0);
        responseContent.append(httpMethod.name()).append(" method not support!");
        writeResponse(ctx.channel());
        logger.error(responseContent.toString());
    }
}

From source file:com.spotify.google.cloud.pubsub.client.Pubsub.java

License:Apache License

/**
 * Make an HTTP request using {@link java.net.HttpURLConnection}.
 *///from   w  w w  .  jav  a2 s  .  co  m
private <T> PubsubFuture<T> requestJavaNet(final String operation, final HttpMethod method, final String path,
        final Object payload, final ResponseReader<T> responseReader) {

    final HttpRequestFactory requestFactory = transport.createRequestFactory();

    final String uri = baseUri + path;

    final HttpHeaders headers = new HttpHeaders();
    final HttpRequest request;
    try {
        request = requestFactory.buildRequest(method.name(), new GenericUrl(URI.create(uri)), null);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    headers.setAuthorization("Bearer " + accessToken);
    headers.setUserAgent("Spotify");

    final long payloadSize;
    if (payload != NO_PAYLOAD) {
        final byte[] json = gzipJson(payload);
        payloadSize = json.length;
        headers.setContentEncoding(GZIP.toString());
        headers.setContentLength((long) json.length);
        headers.setContentType(APPLICATION_JSON_UTF8);
        request.setContent(new ByteArrayContent(APPLICATION_JSON_UTF8, json));
    } else {
        payloadSize = 0;
    }

    request.setHeaders(headers);

    final RequestInfo requestInfo = RequestInfo.builder().operation(operation).method(method.toString())
            .uri(uri).payloadSize(payloadSize).build();

    final PubsubFuture<T> future = new PubsubFuture<>(requestInfo);

    executor.execute(() -> {
        final HttpResponse response;
        try {
            response = request.execute();
        } catch (IOException e) {
            future.fail(e);
            return;
        }

        // Return null for 404'd GET & DELETE requests
        if (response.getStatusCode() == 404 && method == HttpMethod.GET || method == HttpMethod.DELETE) {
            future.succeed(null);
            return;
        }

        // Fail on non-2xx responses
        final int statusCode = response.getStatusCode();
        if (!(statusCode >= 200 && statusCode < 300)) {
            future.fail(new RequestFailedException(response.getStatusCode(), response.getStatusMessage()));
            return;
        }

        if (responseReader == VOID) {
            future.succeed(null);
            return;
        }

        try {

            future.succeed(responseReader.read(ByteStreams.toByteArray(response.getContent())));
        } catch (Exception e) {
            future.fail(e);
        }
    });

    return future;
}

From source file:httprestfulserver.HttpRESTfulServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        byte[] CONTENT = "Hello World".getBytes();
        HttpRequest req = (HttpRequest) msg;

        if (HttpHeaders.is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }/*from ww w  .j a va2 s  . c o  m*/
        boolean keepAlive = HttpHeaders.isKeepAlive(req);
        HttpMethod method = req.getMethod();
        CONTENT = method.name().getBytes();
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT));
        response.headers().set(CONTENT_TYPE, "text/plain");
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

        if (!keepAlive) {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        } else {
            response.headers().set(CONNECTION, Values.KEEP_ALIVE);
            ctx.write(response);
        }
    }
}

From source file:io.gatling.http.client.ahc.oauth.OAuthSignatureCalculatorInstance.java

License:Apache License

StringBuilder signatureBaseString(ConsumerKey consumerAuth, RequestToken userAuth, HttpMethod method, Uri uri,
        List<Param> formParams, long oauthTimestamp, String percentEncodedNonce) {

    // beware: must generate first as we're using pooled StringBuilder
    String baseUrl = uri.toBaseUrl();
    String encodedParams = encodedParams(consumerAuth, userAuth, oauthTimestamp, percentEncodedNonce,
            uri.getEncodedQueryParams(), formParams);

    StringBuilder sb = StringBuilderPool.DEFAULT.get();
    sb.append(method.name()); // POST / GET etc (nothing to URL encode)
    sb.append('&');
    Utf8UrlEncoder.encodeAndAppendPercentEncoded(sb, baseUrl);

    // and all that needs to be URL encoded (... again!)
    sb.append('&');
    Utf8UrlEncoder.encodeAndAppendPercentEncoded(sb, encodedParams);
    return sb;/*  w  w w  .j a v  a2s. c o m*/
}

From source file:io.gatling.http.client.oauth.OAuthSignatureCalculatorInstance.java

License:Apache License

StringBuilder signatureBaseString(ConsumerKey consumerAuth, RequestToken userAuth, HttpMethod method, Uri uri,
        List<Param> formParams, long oauthTimestamp, String percentEncodedNonce) {

    // beware: must generate first as we're using pooled StringBuilder
    String baseUrl = uri.toUrlWithoutQuery();
    String encodedParams = encodedParams(consumerAuth, userAuth, oauthTimestamp, percentEncodedNonce,
            uri.getEncodedQueryParams(), formParams);

    StringBuilder sb = StringBuilderPool.DEFAULT.get();
    sb.append(method.name()); // POST / GET etc (nothing to URL encode)
    sb.append('&');
    Utf8UrlEncoder.encodeAndAppendPercentEncoded(sb, baseUrl);

    // and all that needs to be URL encoded (... again!)
    sb.append('&');
    Utf8UrlEncoder.encodeAndAppendPercentEncoded(sb, encodedParams);
    return sb;/* w w w  .  j  av a 2 s  .  co  m*/
}

From source file:io.selendroid.server.BaseTest.java

License:Apache License

public HttpResponse executeRequestWithPayload(String url, HttpMethod method, String payload) throws Exception {
    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(method.name(), url);
    request.setEntity(new StringEntity(payload, "UTF-8"));
    return executeRequest((HttpRequest) request);
}