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;
}
}
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));
}
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());
}
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());
}
});
}
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;
}
Aggregations