我试图在我的自定义过滤器中提取用户代理:
public class MyFilter extends Filter {
@Override
protected int beforeHandle(Request request, Response response) {
String ua = request.getClientInfo().getAgent();
}
}
但我总是ua
null 。
当请求进入我的Resource
时,我确实设法使用呼叫提取用户代理:
String ua = getRequest().getClientInfo().getAgent();
问题:
答案 0 :(得分:2)
我刚用Restlet版本2.3.1对您的用例进行了测试,但我无法重现您的问题。你能否给我们更多关于你背景的提示。
这是我使用的。
<强> pom.xml
强>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.restlet</groupId>
<artifactId>restlet-user-agent</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<version>1.0.0-snapshot</version>
<properties>
<java-version>1.7</java-version>
<restlet-version>2.3.1</restlet-version>
</properties>
<dependencies>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet-version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.com</url>
</repository>
</repositories>
</project>
过滤强>
public class MyFilter extends Filter {
public MyFilter() {
super();
}
public MyFilter(Context context, Restlet next) {
super(context, next);
}
public MyFilter(Context context) {
super(context);
}
@Override
protected int beforeHandle(Request request, Response response) {
String ua = request.getClientInfo().getAgent();
System.out.println("[filter] ua = " + ua);
return Filter.CONTINUE;
}
}
服务器资源
public class MyServerResource extends ServerResource {
@Get
public String test() {
String ua = getRequest().getClientInfo().getAgent();
System.out.println("[server resource] ua = " + ua);
return "test";
}
}
<强>应用强>
public class RestletApplication extends Application {
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/test", MyServerResource.class);
MyFilter filter = new MyFilter(getContext(), router);
return filter;
}
}
以下是我在Chrome中使用Postman的痕迹:
[filter] ua = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36
[server resource] ua = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36
希望它能帮到你。 亨利