[英]Apache Felix Filter Issue
I'm developing an Apache Sling WCMS application. 我正在开发一个Apache Sling WCMS应用程序。 I need to filter all requests sent to the server and set some headers in response object. 我需要过滤所有发送到服务器的请求,并在响应对象中设置一些标头。 I implemented filter and bundle activator class according to Felix Http Filter Sample and installed as bundle to sling. 我根据Felix Http Filter Sample实现了filter和bundle activator类,并作为捆绑安装到吊索上。
Fileter class: 内档类:
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HeaderFilter implements Filter {
private final String name;
public HeaderFilter(String name) {
this.name = name;
}
public void init(FilterConfig config) throws ServletException {
doLog("Init with config [" + config + "]");
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
try {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
doLog("Filter request [" + request + "]");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "-1");
chain.doFilter(req, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public void destroy() {
doLog("Destroyed filter");
}
private void doLog(String message) {
System.out.println("## [" + this.name + "] " + message);
}
}
Bundle Activator class: 捆绑激活器类:
import org.apache.felix.http.api.ExtHttpService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import java.util.Dictionary;
import java.util.Hashtable;
public class Activator implements BundleActivator {
private ServiceTracker tracker;
private HeaderFilter filter1 = new HeaderFilter("filter1");
Dictionary hashTable = new Hashtable<Object, Object>();
public void start(BundleContext context) throws Exception {
try {
this.tracker = new ServiceTracker(context, ExtHttpService.class.getName(), null) {
@Override
public Object addingService(ServiceReference ref) {
Object service = super.addingService(ref);
serviceAdded((ExtHttpService) service);
return service;
}
@Override
public void removedService(ServiceReference ref, Object service) {
serviceRemoved((ExtHttpService) service);
super.removedService(ref, service);
}
};
this.tracker.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop(BundleContext context)
throws Exception {
this.tracker.close();
}
private void serviceAdded(ExtHttpService service) {
try {
hashTable.put("filter.scope", new String[]{"request", "forward", "include"});
service.registerFilter(this.filter1, ".*", hashTable, 0, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private void serviceRemoved(ExtHttpService service) {
service.unregisterFilter(this.filter1);
}
}
The is a problem with the code above. 上面的代码有问题。 It's not working and no request will be filtered. 它不起作用,并且不会过滤任何请求。
Any help is appreciated in advance. 任何帮助都需要提前感谢。
Note that you can implement this in a much simpler way using Sling Filters implemented as OSGi Declarative Services (DS). 请注意,您可以使用作为OSGi声明性服务(DS)实现的吊索过滤器以更简单的方式实现此目的。
You'll then just need a few annotations in your Filter, no Activator and the correct pom.xml setup for the Maven plugins that handle the DS annotations. 然后,您只需要在过滤器中添加一些注释,就不需要Activator,并且需要处理DS注释的Maven插件的正确pom.xml设置。
The documentation is at https://sling.apache.org/documentation/the-sling-engine/filters.html and it points to the NoPropertyFilter example from the Sling integration tests. 该文档位于https://sling.apache.org/documentation/the-sling-engine/filters.html ,它指向Sling集成测试中的NoPropertyFilter示例。
我发现服务跟踪程序没有跟踪我的过滤器,因此通过将参数'true'传递给tracker.open()来跟踪过滤器:
this.tracker.open(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.