[Play 2.5.3 - Java] Getting JavaActionInvokerFactory exception while using own requesthandler

98 views
Skip to first unread message

Dhirendra Pratap

unread,
May 10, 2016, 4:00:34 AM5/10/16
to play-framework
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);
}
}
.....
....
.
}

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.

Timo Pagel

unread,
Aug 25, 2016, 7:44:39 AM8/25/16
to play-framework
You forgot to add:
if (handler instanceof JavaHandler) {
 handler
= ((JavaHandler) handler).withComponents(components);
}

Here is a working example:
// https://www.playframework.com/documentation/2.5.x/JavaActionCreator
//https://groups.google.com/forum/#!msg/play-framework/pKQpqrJN2fM/h_mLhuW2LQAJ

import javax.inject.Inject;

import play.Logger;
import play.routing.Router;
import play.api.mvc.Handler;
import play.http.*;
import play.mvc.*;
import play.libs.streams.Accumulator;
import play.core.j.JavaHandler;
import play.core.j.JavaHandlerComponents;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class VirtualHostRequestHandler implements HttpRequestHandler {
   
private final Router router;
   
private final subdomain.Routes subdomainRouter;
   
private final JavaHandlerComponents components;

   
@Inject
    public VirtualHostRequestHandler(Router router, subdomain.Routes subdomainRouter, JavaHandlerComponents components) {
       
this.router = router;
       
this.subdomainRouter = subdomainRouter;
       
this.components = components;
   
}

   
public HandlerForRequest handlerForRequest(Http.RequestHeader request) {
       
Handler handler;
       
if (isSubdomain(request.host())) {
           
Logger.info("subdomain detected");
            handler
= subdomainRouter.asJava().route(request).get();
       
} else {
            handler
= router.route(request).orElseGet(() ->
                   
EssentialAction.of(req -> Accumulator.done(Results.notFound()))
           
);
       
}
       
if (handler instanceof JavaHandler) {
            handler
= ((JavaHandler) handler).withComponents(components);
       
}
       
return new HandlerForRequest(request, handler);
   
}

   
public static Boolean isSubdomain(String host) {
       
String mainDomain = play.Play.application().configuration().getString("fhunii.completeMainDomain");
       
String domain = play.Play.application().configuration().getString("fhunii.domain");
       
if (host.equals(mainDomain)) {
           
return false;
       
}
        host
= host.replaceAll(":.*", "");
       
if (checkIPv4(host)) {
           
return false;
       
}

       
return true;
   
}

   
public static final boolean checkIPv4(final String ip) {
       
boolean isIPv4;
       
try {
           
final InetAddress inet = InetAddress.getByName(ip);
            isIPv4
= inet.getHostAddress().equals(ip)
                   
&& inet instanceof Inet4Address;
       
} catch (final UnknownHostException e) {
            isIPv4
= false;
       
}
       
return isIPv4;
   
}
}


Reply all
Reply to author
Forward
0 new messages