Java 类io.netty.handler.codec.http.router.RouteResult 实例源码
项目:netty-router
文件:HttpRouterServerHandler.java
private static HttpResponse createResponse(HttpRequest req, Router<String> router) {
RouteResult<String> routeResult = router.route(req.method(), req.uri());
String content =
"router: \n" + router + "\n" +
"req: " + req + "\n\n" +
"routeResult: \n" +
"target: " + routeResult.target() + "\n" +
"pathParams: " + routeResult.pathParams() + "\n" +
"queryParams: " + routeResult.queryParams() + "\n\n" +
"allowedMethods: " + router.allowedMethods(req.uri());
FullHttpResponse res = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
Unpooled.copiedBuffer(content, CharsetUtil.UTF_8)
);
res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
res.headers().set(HttpHeaderNames.CONTENT_LENGTH, res.content().readableBytes());
return res;
}
项目:sinetja
文件:Request.java
public Request(Server server, Channel channel, FullHttpRequest request, RouteResult<Action> routeResult) {
this.server = server;
this.channel = channel;
this.request = request;
this.routeResult = routeResult;
// Get client IP while the client is still connected; Netty may not allow
// us to get this info later when the connection may be closed
clientIp = getClientIpFromChannel();
remoteIp = getRemoteIpFromClientIpOrReverseProxy();
// Parse body params
String contentTye = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
if (HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.contentEqualsIgnoreCase(contentTye)) {
String content = request.content().toString(server.charset());
QueryStringDecoder qsd = new QueryStringDecoder("?" + content);
bodyParams = qsd.parameters();
} else {
bodyParams = Collections.<String, List<String>>emptyMap();
}
}
项目:botan-core
文件:HttpRouterServerHandler.java
private static HttpResponse createResponse(HttpRequest req, Router<Route> router, String body) {
final RouteResult<Route> routeResult = router.route(req.method(), req.uri());
if (routeResult != null) {
final BotanHttpResponse res = new BotanHttpResponse();
final Object obj = routeResult.target().handle(new BotanHttpRequest(routeResult, body), res);
final String content;
final String type;
final HttpResponseStatus responseStatus;
if (obj instanceof BotanHttpResponse) {
content = ((BotanHttpResponse) obj).content();
type = ((BotanHttpResponse) obj).type();
responseStatus = HttpResponseStatus.OK;
} else if (obj instanceof Integer) {
responseStatus = HttpResponseStatus.valueOf((int) obj);
return new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, responseStatus);
} else {
content = obj.toString();
type = res.type();
responseStatus = HttpResponseStatus.OK;
}
final FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, responseStatus,
Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, type);
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return response;
} else {
return new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
}
}
项目:botan-core
文件:BotanHttpRequest.java
public BotanHttpRequest(final RouteResult result, String body) {
this.result = result;
this.body = body;
}
项目:sinetja
文件:RouterHandler.java
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
// Log time taken to process the request
final long beginNano = System.nanoTime();
final Channel channel = ctx.channel();
final RouteResult<Action> routeResult = server.route(msg.method(), msg.uri());
final Request request = new Request(server, channel, msg, routeResult);
final Response response = new Response(server, channel, request);
// Release request and response when the connection is closed, just in case
channel.closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture arg0) throws Exception {
if (request.refCnt() > 0) {
request.release(request.refCnt());
}
if (response.refCnt() > 0) {
response.release(response.refCnt());
}
}
});
try {
if (server.before() != null) {
server.before().run(request, response);
}
if (!response.doneResponding()) {
// After filter is run at Response#respond
routeResult.target().run(request, response);
// If this is async, pause reading and resume at Response#respond
if (!response.doneResponding()) NoRealPipelining.pauseReading(channel);
}
} catch (Exception e1) {
try {
if (server.error() != null) {
server.error().run(request, response, e1);
}
} catch (Exception e2) {
new DefaultErrorHandler().run(request, response, e2);
}
}
// Access log; the action can be async
final long endNano = System.nanoTime();
final long dt = endNano - beginNano;
final Object asyncOrResponseStatus = response.doneResponding() ? response.getStatus() : "async";
if (dt >= 1000000L) {
Log.info("[{}] {} {} - {} {} [ms]", request.remoteIp(), request.getMethod(), request.getUri(), asyncOrResponseStatus, dt / 1000000L);
} else if (dt >= 1000) {
Log.info("[{}] {} {} - {} {} [us]", request.remoteIp(), request.getMethod(), request.getUri(), asyncOrResponseStatus, dt / 1000);
} else {
Log.info("[{}] {} {} - {} {} [ns]", request.remoteIp(), request.getMethod(), request.getUri(), asyncOrResponseStatus, dt);
}
}