Why is my web.xml filter not being called in WebSphere? - websphere

I have a request filter defined in my web.xml in a WAR within an EAR. On a Base WebSphere 9.0.5.9 installation, the filter is being called as expected.
But when I deploy the same EAR file to a WebSphere Network Deployment (ND) instance, the filter is not called. I'm not seeing any errors in the WebSphere logs - it's like it's just ignoring the filter. Does anyone have any insights into why the filter might not be being called?
The filter in web.xml is
<filter>
<display-name>Request Filter</display-name>
<filter-name>Request Filter</filter-name>
<filter-class>some.package.RequestFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Request Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Related

Filter mapping in TomCat for unicode prevents localhost from getting resolved

I have a webApp that is suppose to display unicode characters, which is not working with the default tomcat settings. The unicode characters are displayed as "??????". After looking into it I found a solution where adding an encodingFilter to web.xml fixes the problem.
But it creates another problem where the default tomcat landing page "localhost:8080" and all other services like "localhost:8080/manager" fail to resolve. I get a HTTP Status 404 – Not Found. Although by webapp is working properly "localhost:8080/myWebapp/servletName", URL with my webapp is resolved properly and the unicode characters are also displayed properly.
The filter that I added to web.xml is as follows
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I am pretty new to this so there must some very basic things that I don't understand. But any pointers would help.

Servlet filters not invoked for _am/api/discovery/* URLs?

I'm updating a GAE application to the Java8 Cloud SDK environment; I'm also updating it to use Cloud Endpoints version 2.
My app registers some servlet filters in its web.xml file, one for Objectify and one to do some initialisations such as creating singleton instances of some utility classes the app uses.
This is an excerpt of web.xml:
<servlet>
<servlet-name>EndpointsServlet</servlet-name>
<servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>com.myapp.service.Service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>EndpointsServlet</servlet-name>
<url-pattern>/_ah/api/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>InitializerFilter</filter-name>
<filter-class>com.myapp.InitializerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>InitializerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Add a filter that fetches the service config from service management. -->
<filter>
<filter-name>endpoints-api-configuration</filter-name>
<filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
</filter>
<!-- Add a filter that performs Endpoints logging and monitoring. -->
<filter>
<filter-name>endpoints-api-controller</filter-name>
<filter-class>com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter</filter-class>
<init-param>
<param-name>endpoints.projectId</param-name>
<param-value>${appId}</param-value>
</init-param>
<init-param>
<param-name>endpoints.serviceName</param-name>
<param-value>${service}-dot-${appId}.appspot.com</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>endpoints-api-configuration</filter-name>
<servlet-name>EndpointsServlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>endpoints-api-controller</filter-name>
<servlet-name>EndpointsServlet</servlet-name>
</filter-mapping>
It looks like filters correctly kick for all URLs (for example, URLs handled by some other servlet that I instantiate but are not shown here), but not the _ah/api/discovery/* URLs that implement Google's nifty APIs Explorer tool.
No exceptions are thrown at deployment.
Note that I already tried changing the <url-pattern>/*</url-pattern> to <url-pattern>/_ah/api/*</url-pattern>, to <url-pattern>/_ah/api/discovery/*</url-pattern> and mapping using <servlet-name>EndpointsServlet</servlet-name> rather than a URL pattern, to no avail.
Awkwardly enough, the very same configuration did work on Friday morning, then after a redeployment made later in the afternoon it stopped. And I'm pretty positive I did not change anything.
Is this known behaviour? For example, this may be because the API Explorer is "stitched on" the endpoints URL externally rather than being part of EndpointServlet itself?
Otherwise, what am I doing wrong?
=========
I fixed this problem by moving the logic that was in the filter to a ServletContextListener, and this made the app stable. This is only viable for once-for-servlet-lifetime initialisations, of course, so the question still stands: is the cloud API Explorer expected to trigger the servlet filters registered on EndpointsServlet?

spring mvc encoding messy code when get value from the post request

I use spring mvc to develop a web site. I have put accept-charset="UTF-8" in the form attribute and
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
in the web.xml. But still, I get the messy code when saving an entity into the database. I have searched and found the solution is always be the characterEncodingFilter in the web.xml, which I have already done. Can somebody help understand this problem?

Endless redirect loop after ticket validation

I'm using the Liferay Portal 6.1.1 CE GA2.
After hours of research I got the following things to work:
Import from a LDAP-Server and custom mapping of an attribute to an usergroup.
Redirect to a specific page after login (based on the usergroup).
Authentication via CAS. This means getting a ServiceTicket and logging in the corresponding user.
Now I'm trying to obtain ProxyTickets so I can proxy to other applications behind the same CAS-Server.
I'm not really getting any error, but Mozilla gives me a redirection error, e.g. the page is redirected in such a way that it can never be loaded.
I googled a lot and tried different approaches but nothing helped.
My web.xml is configured as follows (I snipped out urls. If they're important I can hand them in later):
<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>* snip */cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>* snip *</param-value>
</init-param>
</filter>
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>* snip */cas/</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>* snip *</param-value>
</init-param>
<init-param>
<param-name>proxyCallbackUrl</param-name>
<param-value>https://* snip */pgtCallback</param-value>
</init-param>
<init-param>
<param-name>proxyReceptorUrl</param-name>
<param-value>/pgtCallback</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Authentication Filter</filter-name>
<url-pattern>/c/portal/login</url-pattern>
</filter-mapping>
I tried various combinations of the filter-mappings but nothing helped.
The output of the console in Eclipse hints that multiple consecutive requests are done. Each gets me a TGT, PGTIOU and PGT but after the ST is validated a new validation request is fired. This goes until Mozilla ends the redirect loop.
I also tried specifying service instead of serverName but all remains the same.
Setting the param redirectAfterValidation to false but then I get a MalformedURLException.
Hopefully I didn't forget any information, please help me.
Thanks in advance.

how to integrate UrlRewritingFilter with Struts 2.X

1) Is there a way to integrate UrlRewritingFilter with Struts2 tiles.i think there is a problem with
listeners in web xml.
<listener>
<listener-class>listener.ApplicationListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
2) how to use struts2 advanced wildcard mapping to write better urls with actions.anybody can share a working example please?
thanks.!!
This is how you need to configure your rewrite filter
web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
Please note the change done to S2 filter
Add a rewrite.xml file under WEB-INF with entry like
<urlrewrite>
<rule>
<from>^/some/olddir/(.*)$</from>
<to type="redirect">/very/newdir/$1</to>
</rule>
</urlrewrite>
For more details refer to there documents.
There is one more option to create clean URL using NamedVariablePatternMatcher,for details refer to this blog for example and understanding
better-urls-with-struts2

Resources