I have been trying to implement the WURFL Cloud API in my application using the play framework 1.2.4. Unfortunately, play doesn't have the HttpServletRequest and HttpServletResponse nonetheless has a Request and a Response object that can be cast to HttpServeletRequest and HttpServletResponse but when i tried this, i had a NullPointerException .... can any one please help me address this issue or better still guide me as to the implementation. I also looked at this module ( https://github.com/revbingo/play-wurfl ) but there is little information as to its implementation..
below is my code snippet
package controllers;
import play.*;
import play.mvc.*;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Http.Header;
import play.server.ServletWrapper;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.scientiamobile.wurflcloud.CloudClientLoader;
import com.scientiamobile.wurflcloud.CloudClientManager;
import com.scientiamobile.wurflcloud.device.AbstractDevice;
import com.sun.xml.internal.ws.client.RequestContext;
import net.sourceforge.wurfl.core.Device;
import net.sourceforge.wurfl.core.WURFLHolder;
import net.sourceforge.wurfl.core.WURFLManager;
import models.*;
public class Application extends Controller {
private static final String API_KEY = "xxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
public static void index() throws Exception {
HttpServletRequest request = (HttpServletRequest) Request.current().args.get(ServletWrapper.SERVLET_REQ);
HttpServletResponse response = (HttpServletResponse) Request.current().args.get(ServletWrapper.SERVLET_RES);
CloudClientLoader loader = new CloudClientLoader(API_KEY);
CloudClientManager manager = (CloudClientManager) loader.getClientManager();
try{
AbstractDevice device = manager.getDeviceFromRequest(request,response);
Object deviceCapability = device.getCapabilities();
render(request, response, deviceCapability);
}catch(NullPointerException ex){
render(request, response);
}
}
}
Taking a look at the github link you posted, the way it gets the request is different: request
is actually a protected field in the Controller class you are already extending.
First of all, I would try to use it instead of Request.current(), as it is suggested here: https://stackoverflow.com/a/17006963/2617826
If you still get a null reference, notice that HttpServletRequest
is an interface, hence you could just write a wrapper, from Play Framework play.mvc.Http.Request
. WURFL should only use HttpServletRequest.getHeader()
and HttpServletRequest.getHeaderNames()
methods to perform the matches. You can try to wrap the play.mvc.Http.Request
like this:
import play.mvc.Http.Request;
class HttpServletRequestWrapper implements HttpServletRequest {
private Http.Request mRequest;
public HttpServletRequestWrapper(Http.Request request) {
mRequest = request;
}
public String getHeader(String name) {
Http.Header header = mRequest.headers.get(name);
if (header != null) {
return header.value();
} else {
return null;
}
}
public Enumeration<String> getHeaderNames() {
Vector<String> headerNames = new Vector<String>();
for (String header : mRequest.headers.keySet()) {
headerNames.add(header);
}
return headerNames.elements();
}
}
and returning null for all others inherited methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.