0

我一直在尝试使用播放框架 1.2.4 在我的应用程序中实现 WURFL Cloud API。不幸的是,play 没有 HttpServletRequest 和 HttpServletResponse 仍然有一个 Request 和一个 Response 对象,可以转换为 HttpServeletRequest 和 HttpServletResponse 但是当我尝试这个时,我有一个NullPointerException ....谁能帮我解决这个问题或者更好地指导我实施。我还查看了这个模块(https://github.com/revbingo/play-wurfl),但是关于它的实现的信息很少。

下面是我的代码片段

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);
        }
    }
}
4

1 回答 1

2

看看您发布的 github 链接,它获取请求的方式是不同的:request实际上是您已经扩展的 Controller 类中的受保护字段。

首先,我会尝试使用它而不是 Request.current(),正如这里建议的那样:https ://stackoverflow.com/a/17006963/2617826

如果您仍然得到一个空引用,请注意这HttpServletRequest是一个接口,因此您可以从 Play Framework 编写一个包装器play.mvc.Http.Request。WURFL 应该只使用HttpServletRequest.getHeader()HttpServletRequest.getHeaderNames()方法来执行匹配。您可以尝试play.mvc.Http.Request像这样包装:

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

并为所有其他继承的方法返回 null。

于 2013-07-25T09:53:00.917 回答