-->

Create a servlet-filter websphere liberty profile?

2019-09-04 10:02发布

问题:

I'm working with an websphere liberty profile webserver where i have deployed a couple of applications. These applications are sending request message's, I want to create a servlet-filter without changing the applications so i can see what the application is sending and receiving. Also i want to add new request headers.

回答1:

You can use a ServletContainerInitializer to register new ServletFilters. An example implementation that adds a response header might look like this:

public class SCI implements ServletContainerInitializer {
  @Override
  public void onStartup(Set<Class<?>> arg0, ServletContext arg1)
    throws ServletException {
    arg1.addFilter("myFilter", MyFilter.class).addMappingForUrlPatterns(null, false, "/*");
  }
}

The MyFilter class would look like this:

public static class MyFilter implements Filter {

  @Override
  public void destroy() { }

  @Override
  public void doFilter(ServletRequest arg0, ServletResponse arg1,
      FilterChain arg2) throws IOException, ServletException {
    if (arg1 instanceof HttpServletResponse) {
      ((HttpServletResponse) arg1).addHeader("Test", "Test");
    }
    arg2.doFilter(arg0, arg1);
  }

  @Override
  public void init(FilterConfig arg0) throws ServletException { }
}

You then need to register this using a file in called META-INF/services/ServletContainerInitializer which should contain the fully qualified class name of the Servlet Container Initializer, for example:

test.SCI

Normally you package these in a jar in the application, but since you don't want to update the application you instead configure the server like this:

<featureManager>
   <feature>bells-1.0</feature>
</featureManager>

<library id="init">
  <file name="path/to/jar"/>
</library>

<bell libraryRef="init"/>

The ServletContainerInitializer will be called for all started Web applications allowing you to add the filter. Note this will be called for all started Web applications including ones integrated into the Liberty runtime, such as the Admin Center and the REST connector.



回答2:

I got the below response when I tried to install the same bells utility

CWWKF1295E: The bells-1.0 asset cannot be downloaded or installed to IBM WebSphere Application Server Liberty (ILAN) 19.0.0.4 because it applies only to the following product editions and versions:

  • IBM WebSphere Application Server Liberty 8.5.5.7
  • IBM WebSphere Application Server Liberty for Developers 8.5.5.7
  • IBM WebSphere Application Server Liberty - Express 8.5.5.7
  • IBM WebSphere Application Server Liberty Liberty Core 8.5.5.7
  • IBM WebSphere Application Server Liberty Network Deployment 8.5.5.7
  • IBM WebSphere Application Server Liberty z/OS 8.5.5.7

Use the installUtility find action to retrieve a list of assets that apply to your installation.