Search in sources :

Example 6 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project camel by apache.

the class NettyHttpHelper method createMethod.

/**
     * Creates the {@link HttpMethod} to use to call the remote server, often either its GET or POST.
     *
     * @param message  the Camel message
     * @return the created method
     */
public static HttpMethod createMethod(Message message, boolean hasPayload) {
    // use header first
    HttpMethod m = message.getHeader(Exchange.HTTP_METHOD, HttpMethod.class);
    if (m != null) {
        return m;
    }
    String name = message.getHeader(Exchange.HTTP_METHOD, String.class);
    if (name != null) {
        return HttpMethod.valueOf(name);
    }
    if (hasPayload) {
        // use POST if we have payload
        return HttpMethod.POST;
    } else {
        // fallback to GET
        return HttpMethod.GET;
    }
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 7 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project ratpack by ratpack.

the class WebSocketEngine method connect.

@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
    PublicAddress publicAddress = context.get(PublicAddress.class);
    URI address = publicAddress.get(context);
    URI httpPath = address.resolve(path);
    URI wsPath;
    try {
        wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
    } catch (URISyntaxException e) {
        throw uncheck(e);
    }
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
    Request request = context.getRequest();
    HttpMethod method = valueOf(request.getMethod().getName());
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
    nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
    final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
    final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
    final Channel channel = directChannelAccess.getChannel();
    if (!channel.config().isAutoRead()) {
        channel.config().setAutoRead(true);
    }
    handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DirectChannelAccess(ratpack.handling.direct.DirectChannelAccess) Channel(io.netty.channel.Channel) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Request(ratpack.http.Request) PublicAddress(ratpack.server.PublicAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 8 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project BRFS by zhangnianli.

the class Test method main.

public static void main(String[] args) throws Exception {
    // Runtime.getRuntime().addShutdownHook(new Thread() {
    // @Override
    // public void run() {
    // System.out.println("shutdown thread run...");
    // }
    // });
    // 
    // Thread.sleep(60000);
    Map<HttpMethod, String> map = new HashMap<HttpMethod, String>();
    map.put(HttpMethod.valueOf("CLOSE"), "1234");
    System.out.println(map.get(HttpMethod.valueOf("CLOSE")));
    System.out.println("--" + URLDecoder.decode("/mem/data/brfs/t3", "UTF-8"));
    File f = new File("/root/temp/a.txt");
    System.out.println(f.getParent());
    System.out.println(f.getName());
}
Also used : HashMap(java.util.HashMap) File(java.io.File) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 9 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project turbo-rpc by hank-whu.

the class NettyRestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, final FullHttpRequest httpRequest) throws Exception {
    boolean keepAlive = HttpUtil.isKeepAlive(httpRequest);
    String uri = httpRequest.uri();
    HttpMethod httpMethod = httpRequest.method();
    int index = uri.indexOf('&', invokerFactory.restPrefix.length());
    if (index < 0) {
        index = uri.length();
    }
    if (invokerFactory.restPrefix.length() >= index) {
        if (logger.isInfoEnabled()) {
            logger.info("not support this method " + toString(httpRequest));
        }
        doRequestFilter(httpRequest, null);
        ctx.write(new RestHttpResponse(null, httpRequest, NOT_FOUND, NOT_SUPPORT_THIS_METHOD, keepAlive), ctx.voidPromise());
        return;
    }
    String restPath = uri.substring(invokerFactory.restPrefix.length(), index);
    final Invoker<CompletableFuture<?>> invoker = invokerFactory.get(restPath);
    CompletableFuture<?> future = null;
    try {
        if (invoker == null) {
            if (logger.isInfoEnabled()) {
                logger.info("not support this method " + toString(httpRequest));
            }
            doRequestFilter(httpRequest, null);
            ctx.write(new RestHttpResponse(null, httpRequest, NOT_FOUND, NOT_SUPPORT_THIS_METHOD, keepAlive), ctx.voidPromise());
            return;
        }
        boolean allowHandle = doRequestFilter(httpRequest, invoker);
        if (!allowHandle) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, SERVICE_UNAVAILABLE, RestServerFilter.SERVER_FILTER_DENY, keepAlive), ctx.voidPromise());
            return;
        }
        Object params = null;
        if (httpMethod == HttpMethod.GET) {
            params = HttpParamExtractor.extractFromQueryPath(invoker, uri, index);
        } else if (httpMethod == HttpMethod.POST) {
            params = HttpParamExtractor.extractFromBody(invoker, jsonMapper, httpRequest.content());
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("only support get and post " + toString(httpRequest));
            }
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, ONLY_SUPPORT_GET_POST, keepAlive), ctx.voidPromise());
            return;
        }
        if (params == null) {
            future = invoker.invoke();
        } else if (params instanceof MethodParam) {
            future = invoker.invoke((MethodParam) params);
        } else if (params instanceof Object[]) {
            future = invoker.invoke((Object[]) params);
        } else {
            future = invoker.invoke((Object) params);
        }
    } catch (Throwable e) {
        if (logger.isWarnEnabled()) {
            logger.warn(uri + " error ", e);
        }
        ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, e, keepAlive), ctx.voidPromise());
        return;
    }
    if (future == null) {
        if (logger.isWarnEnabled()) {
            logger.warn("unknown error " + toString(httpRequest));
        }
        ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, UNKNOWN, keepAlive), ctx.voidPromise());
        return;
    }
    future.whenComplete((result, throwable) -> {
        if (result != null) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, OK, result, keepAlive), ctx.voidPromise());
        } else if (throwable != null) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, throwable, keepAlive), ctx.voidPromise());
        } else {
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, UNKNOWN, keepAlive), ctx.voidPromise());
        }
    });
}
Also used : MethodParam(rpc.turbo.param.MethodParam) CompletableFuture(java.util.concurrent.CompletableFuture) RestHttpResponse(rpc.turbo.transport.server.rest.protocol.RestHttpResponse) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 10 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project turbo-rpc by hank-whu.

the class NettyRestHandler method toString.

private String toString(FullHttpRequest httpRequest) {
    String uri = httpRequest.uri();
    HttpMethod httpMethod = httpRequest.method();
    return httpMethod.name() + " " + uri;
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)95 Test (org.junit.Test)51 HttpRequest (io.netty.handler.codec.http.HttpRequest)28 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)25 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)21 URI (java.net.URI)15 IOException (java.io.IOException)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)11 HttpVersion (io.netty.handler.codec.http.HttpVersion)11 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)11 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)11 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)11 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Subscription (rx.Subscription)11 Action2 (rx.functions.Action2)11 ByteBuf (io.netty.buffer.ByteBuf)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)10 Map (java.util.Map)9