dhirendra dhirendra - 3 months ago 25
Java Question

Multi Routes handling - Getting exception while using own requesthandler

I am using play 2.5.3 and having 2 routes files sitting under subproject. Idea is to use the domain related route file.
Code Structure:

root
- modules
- core
- admin
- admin.routes
- web
- web.routes


My RequestHandler looks like this--

public class MyRequestHandler implements HttpRequestHandler {
@Inject
public MyRequestHandler(web.Routes webRoutes, admin.Routes adminRoutes) {
this.webRoutes = webRoutes;
this.adminRoutes = adminRoutes;
}

@Override
public HandlerForRequest handlerForRequest(Http.RequestHeader request) {
String subDomain = getSubDomain(request);
if("admin".equalsIgnoreCase(subDomain)){
Handler handler = adminRoutes.asJava().route(request).get();
return new HandlerForRequest(request,handler);
}else{
Handler handler = webRoutes.asJava().route(request).get();
return new HandlerForRequest(request,handler);
}
}

}


Getting Following exception -

2016-05-10 13:02:29,956 - [ERROR] - p.c.s.n.PlayRequestHandler - Exception caught in Netty
scala.MatchError: Right((play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3@50a3f51b,play.api.DefaultApplication@2072c28f)) (of class scala.util.Right)
at play.core.server.netty.PlayRequestHandler.handle(PlayRequestHandler.scala:93)
at play.core.server.netty.PlayRequestHandler.channelRead(PlayRequestHandler.scala:163)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
at com.typesafe.netty.http.HttpStreamsHandler.channelRead(HttpStreamsHandler.java:129)
at com.typesafe.netty.http.HttpStreamsServerHandler.channelRead(HttpStreamsServerHandler.java:96)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
2016-05-10 13:02:34,020 - [ERROR] - p.c.s.n.PlayRequestHandler - Exception caught in Netty


Please help us in this regard. Or let us know is there any other way.

Answer

This worked for me. Need to create Javacompatible RequestHandler. I changed my code as below. No 404 anymore.

public class MyRequestHandler implements HttpRequestHandler { 
private play.api.http.JavaCompatibleHttpRequestHandler adminRequestHandler;
private play.api.http.JavaCompatibleHttpRequestHandler webRequestHandler;

@Inject
public MyRequestHandler(web.Routes webRoutes, HttpErrorHandler httpErrorHandler,admin.Routes adminRoutes,
                        HttpConfiguration configuration, HttpFilters httpFilters,
                        JavaHandlerComponents components) {
    adminRequestHandler = new JavaCompatibleHttpRequestHandler(adminRoutes,httpErrorHandler
            ,configuration, httpFilters,components);
    webRequestHandler = new JavaCompatibleHttpRequestHandler(webRoutes,httpErrorHandler
            ,configuration, httpFilters,components);
}

 @Override
public HandlerForRequest handlerForRequest(Http.RequestHeader request) {
    Logger.info("Request ---> ["+ request.method()+"] "+request.host() + request.uri() );
    if("admin".equalsIgnoreCase(getSubDomain(request))){
        Tuple2<RequestHeader, Handler> result = adminRequestHandler.handlerForRequest(request._underlyingHeader());
        return new HandlerForRequest(new RequestHeaderImpl(result._1()), result._2());
    }else {
        Tuple2<RequestHeader, Handler> result = webRequestHandler.handlerForRequest(request._underlyingHeader());
        return new HandlerForRequest(new RequestHeaderImpl(result._1()), result._2());
    }
}