Assume it is a struts project..
the filter configuration is as follows,
<filter>
<filter-name>samplefilter</filter-name>
<filter-class>org.samplepack.SampleFilterXXX</filter-class>
</filter>
<filter-mapping>
<filter-name>samplefilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
and the servlet mapping is as follows,
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
I want to know whether the samplefilter will be executed for every action class in the struts project?
can you post the situations whereever i can apply this effectively ?
The simple answer is Yes, provided that all your struts actions are accessed via the pattern *.do. This is the default configuration for Struts, so I expect that this is the case.
However as the Struts mapping us configurable, it is possible for you to define a different mapping to access a Struts action, and therefore your filter will not be picked up. But, as mentioned above, this is uncommon practice, so I expect you will be okay.
Related
I have 2 domains for my site which is hosted in google app engine. I want to 301 redirect any request on the less desirable domain to the more desirable domain.
I have tried adding the following to my web.xml
<servlet>
<servlet-name>WebServlet</servlet-name>
<servlet-class>com.wmar.api.WebServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>redirectFilter</filter-name>
<filter-class>com.wmar.api.WebFilterRedirect</filter-class>
</filter>
<filter-mapping>
<filter-name>redirectFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
For the servlet it hits it, but then I don't know how to just load the static resource (ideally just passing onto some default static file handler) if the domain is correct.
For the filter it doesn't hit it.
What is the best way to achieve what I want with servlet 2.5 / google app engine java?
Thanks
I realize now that the current ideal solution is to create a separate app engine project and point non-canonical domains at it which has a simple servlet that just 301 redirects to the canonical domain.
For Http to https 301s app.yaml can be used with secure: always.
As noted by konqi there are undesirable side effects by trying to do it all in the one app engine project/module.
is there any possibility in Java's web.xml of redirect all subdomains to one servlet?
For example:
<url-pattern>*.</url-pattern>
You can definitely do that but I think you would like to define your URL pattern using *.ext or *.* as below:
<url-pattern>*.*</url-pattern>
This is also used by some populare MVC frameworks such as struts where UL pattern
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
is mapped to Struts controller servlet.
I look at this, very popular page and see that it start with
This page describes Hibernate 3.1.x and code shown here does not work in older versions.
So my question very easy: how to implement behavior like this in newest versions of hibernate (4.1+)? Maybe it exist more elegant decision of lazy initialization problem? Any advice and links are welcome.
Take a look at the ThreadLocalSessionContext and ManagedSessionContext classes. It should help you do what you need.
If you look at spring's implementation of the filter, it will most likely be using the ThreadLocalSessionContext class.
Pretty much the same as Hibernate 3 but reference the Hibernate 4 package:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I want to use two different Spring web contexts, each have own contextConfig, spring servlet and filter, that should be mapped to different urls. I have a
Standard Grails project, mapped to '/'
And an existing Spring webapp, that I want to map to /extra/
I know that I can deploy both into one Tomcat, but I'm looking for a way of making one app (one war, etc), because It can simplify our deployment and development process.
This applications don't need to share beans or anything, should be completely separate. Both have DispatcherServlet and DispatcherFilter (and both are using Spring Security, but different configuration)
How I can configure web.xml for such webapp?
I've tried to add new filter:
<filter>
<filter-name>extraSpringSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.extraSpring</param-value>
</init-param>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>extraSecurityFilterBean</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>extraSpringSecurityFilterChain</filter-name>
<url-pattern>/extra/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
and spring dispatcher servlet:
<servlet>
<servlet-name>extraSpring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>springConfigLocation</param-name>
<param-value>classpath:extra-spring-web.xml</param-value>
</init-param>
</servlet>
Where:
two context xml in classpath (inside exra library jar):
extra-spring-web.xml
extra-spring-security.xml (!!! how I should configure it?)
extra-spring-security.xml
is pretty standard Spring Security config
have configured bean extraSecurityFilterBean
have dependecy to beans from -web context (but it's not required to be)
It's semi-working now:
as I see from logs, extraSpring servlet successfully load beans from extra-spring-web.xml
but after accessing url /extra/ I got NoSuchBeanDefinitionException: No bean named 'extraSecurityFilterBean' is defined.
So, the question, how I can define context for DelegatingFilterProxy? I even tried to add this files into main context (contextConfigLocation param), it's not what i'm looking for, but it didn't work.
I've taken a look into DelegatingFilterProxy sources, but it's not clear for me how it loads the context.
As per my comment on the question, if the security filter chain is defined in extra-spring-security.xml then you need to ensure that that file is loaded by your extra DispatcherServlet in addition to extra-spring-web.xml either by <import>ing the -security file from the -web one or configuring it as:
<servlet>
<servlet-name>extraSpring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:extra-spring-web.xml
classpath:extra-spring-security.xml
</param-value>
</init-param>
</servlet>
You will also need to ensure that the security filter in the Grails application doesn't apply to /extra URIs, exactly how you do this depends on whether you're using annotations, database RequestMap entries etc.
If the modules are completely separate: the easiest way is to package them as two different webapp. Tens of different spring-based apps can run in one appserver -even on a modest developer machine- without issues.
A few questions
What does your Spring Security configuration look like?
I'm confused why the error states "No bean named 'apiservSecurityFilterChain' is defined" but the web.xml you have posted only references extraSpringSecurityFilterChain (the bean names should match or some important configuration is being left out).
Possible Answer
I'm guessing the problem is that the filter-name needs to match Spring Security's bean name (cannot know for sure without seeing the Spring Security configuration you are using). The default value used by the Spring Security namespace is springSecurityFilterChain, so try the following in the web.xml instead (notice extraSpringSecurityFilterChain changed to springSecurityFilterChain):
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.extraSpring</param-value>
</init-param>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>extraSecurityFilterBean</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/extra/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
So after installing UrlRewriteFilter, I set up a rule and a corresponding velocity template. And when I go to the test page, the velocity script is shown as raw code instead of being compiled.
example of the code for the rule:
<rule>
<from>/test/([0-9]+)</from>
<to>/downloads/test.vm?Id=$1</to>
</rule>
example of the urlrewritefilter
<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>DEBUG</param-value>
</init-param>
</filter>
any ideas why the velocity template wouldn't render even though the rule executed correctly? All other vm pages render correctly (when accessed directly) and the rewrite works perfectly with jsp.
This happens when the test.vm is served by the default servlet for static content.
I suspect you changed the mapping for velocity somehow. If you are using Velocity Tools, you should have a mapping like this,
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.VelocityViewServlet
</servlet-class>
</servlet>
<!-- Map all *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
Another possibility is that other filter might interfere with UrlRewrite filter. It would be helpful if you can post your web.xml.