gpt4 book ai didi

java - Apache Felix 过滤器问题

转载 作者:行者123 更新时间:2023-11-30 04:07:53 25 4
gpt4 key购买 nike

我正在开发 Apache Sling WCMS 应用程序。我需要过滤发送到服务器的所有请求并在响应对象中设置一些 header 。我根据 Felix Http Filter Sample 实现了过滤器和捆绑激活器类并作为 bundle 安装到吊带上。

过滤器类:

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

捆绑激活器类:

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

上面的代码有问题。它不起作用,不会过滤任何请求。

提前感谢任何帮助。

最佳答案

请注意,您可以使用作为 OSGi 声明性服务 (DS) 实现的 Sling Filters 以更简单的方式实现此目的。

然后,您只需要在过滤器中添加一些注释,不需要 Activator,并且为处理 DS 注释的 Maven 插件进行正确的 pom.xml 设置。

文档位于 https://sling.apache.org/documentation/the-sling-engine/filters.html它指向 NoPropertyFilter Sling 集成测试的示例。

关于java - Apache Felix 过滤器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20310491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com