Caching with Tuckey UrlRewrite - spring

I am trying to cache images, js and css by configuring the cache settings in urlrewrite.
My js/css files are getting cached as expected but the images won't. I am unable to see the Expires header for the images, despite of various combinations of configurations.
Here's the configuration in urlrewrite.xml:
<rule>
<from>^.*\.(js|css|jpeg|jpg|png)$</from>
<set type="expires">2 years</set>
</rule>
Here's how the directories (and files) look like on the server
/html/images/foo.jpg
/html/js/bar.js
/html/css/xyz.css
/WEB-INF/urlrewrite.xml
/WEB-INF/web.xml
I have hunted for configurations everywhere I could think of and there is no specific configuration for images.
Where should I look further? Any help would be appreciated. Thank You.
PS : I have a struts/spring project.

in which application server you are deploying tomcat?
if yes
<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType image</param-name>
<param-value>access plus 100000 minutes</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType text/css</param-name>
<param-value>access plus 100000 minutes</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType application/javascript</param-name>
<param-value>access plus 10000 minutes</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ExpiresFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
use this url rewrite not required

First:
You should post all your url rewrite configuration you have.
Second:
Always check documentation for examples, i dont see what have you tried.
url rewrite version 4.0
url rewrite version 3.0
Three:
Know what version your working on.
Finally:
Here is Solution i did and its working nice.
The portion i have to modify in my case is adding the both lines
in three places yes three. why keep reading ))
<set type="response-header" name="Cache-Control">max-age=290304000</set>
<set type="expires" name="expires">24 hours</set>
......
<rule>
<from>/images/((?:(?!/).)*)/(.*)$</from>
<to encode="true" last="true" qsappend = "true">/files/get.html?file=$2&preset=$1</to>
<set type="response-header" name="Cache-Control">max-age=290304000</set>
<set type="expires" name="expires">24 hours</set>
</rule>
<outbound-rule>
<from>/images/(.*)/(.*).(jpg|png|jpeg|gif).html</from>
<to>/images/$1/$2.$3</to>
<set type="response-header" name="Cache-Control">max-age=290304000</set>
<set type="expires" name="expires">24 hours</set>
</outbound-rule>
<rule>
<from>/files/original/(.*)$</from>
<to encode="true" last="true" qsappend = "true">/files/get.html?file=$1</to>
<set type="response-header" name="Cache-Control">max-age=290304000</set>
<set type="expires" name="expires">24 hours</set>
</rule>
Reason:
Depending on your settings for your project there is rules
and there is out bound and other stuff ; they might re write
the url several times according to your rules complexity and logic
so in my case tried to do it with minimum addition but, it did not
work only when i add it to the three places ( without showing all my rules but you got the idea :) )
And i did this as well its Better Approach as well
Using APACHE configuration

Related

Servlet Filter for URIs in alternatedocroot

Is it possible to apply ServletFilters on content delivered via an alternatedocroot?
Filter mapping
<filter-mapping>
<filter-name>StaticContentAuthenticationFilter</filter-name>
<url-pattern>/img/users/*</url-pattern>
</filter-mapping>
Alternate Docroot
<glassfish-web-app>
<context-root>/xxx</context-root>
<property name="alternatedocroot_1" value="from=/img/* dir=C:/appdata" />
</glassfish-web-app>
Problem statement:
Do some authentication before serving static content (specifically images).
Since I am serving images I cannot use a database nor can I keep the images in the .war file
Sorry! The above code works! There was an error elsewhere in the application.

How can i Use Tuckey URL Rewrite with ADF Essentials?

Developed on ADF 11.1.2.4 (JSF2.0 -GF 3.1.2)
Expecting to implement urlrewrite for pretty urls.
Added into 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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
created urlrewrite.xml such as
<rule>
<from casesensitive="false">myTest</from>
<use-context>true</use-context>
<to>/faces/admin/admin.jspx</to>
</rule>
admin.jspx contains TF.
When i deploy project and request hostname:9999/mytest redirects right page(hostname:9999/faces/admin/admin.jspx) and page renders without problem. But my actual goal was that making supply to see never real url. But i can see the real url on browser such as:(...jspx?_adf.ctrl-state=4avl71cil_1) So, what am i missing? By the way; When i type the masked url, it redirects my real page, so it works good. But seems to be the real url on browser address bar. If i only use html pages out of 'faces' context, then urlrewrite works as fully expected.
Thx, brgds
As you have Tucky URL Rewriting filter, ADF comes with its JSF View Handler, and before ADF 12.1.x you won't be able to forward URLs unless you are using Apache Server Rewrites or Oracle HTTP Server, as the ADF internal filter will look for _adf.ctrl-state and if it's not found it'll append it to the URL which will show the actual URL of the page.
You can try to hack those _adf.ctrl-state by extending ServletRequest and when asked about _adf.ctrl-state to provide the last value saved in session, but I assure you it'll be very harmful for the application.

Special Characters in Request Parameter

I am developing services in spring and the services were deployed in JBOSS 7.1.0.
Sample code for request mapping:
#RequestMapping(value=/state, method=RequestMethod.GET)
public ResponseEntity<ListStatesResponseVO> getListOfStates(#RequestParam(required=false) Long id,
#RequestParam(required=false) Long page,
#RequestParam(required=false) Long pagesize);
My problem is when I pass special characters in request parameter, it’s returning me a valid xml response, but as per my understanding it should return “400 BAD REQUEST”.
Sample URI:
http://localhost:8080/location-services/location/api/state?id=$%^$^$#$%^$%
I also added
<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
<property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
Inside JBOSS’s standalone.xml.
And also
<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>
<!-- set forceEncoding to true if you want to override encoding of servlet -->
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
Inside web.xml.
But these doesn’t solved the problem.
Is there any solution available for this.
Thanks in advance.
You should not allow your users to enter the values in the query string themselves. It's a bad practice and is very risky for your web application security. To avoid such attacks and restrict your users from url tampering you should implement HDIV framework in your application.
Once you implement that no one can mess with your urls. And if someone tries to do so then "bad request" errors will be shown to them.
Hope this helps you. Cheers.

Tiles 2 And No mapping found for HTTP request with URI - Spring-MVC

I want to use Spring-Tiles intergration. Here you can see how my app looks like.
So my question is: why Spring-MVC dispatcher Servlet can not resolve my Target page ???
The problem is that you use <url-pattern>/*</url-pattern> in servlet mapping, so all requests are processed by DispatcherServlet, including request to *.jsp tiles. The most versatile way to solve it (but to keep restful urls without prefixes) is to use a UrlRewriteFilter.
I think you're missing a critical ViewResolver. I checked the post you mentioned in SpringSource but I didn't see the following ViewResolver:
org.springframework.web.servlet.view.tiles2.TilesViewResolver
Try adding that ViewResolver and see if that would help. I use Spring and Tiles as well. I just have to declare that and the TilesConfigurer.
Check out these references:
Add TilesViewResolver to enable fallback if tiles definition does not exist
TilesViewResolver
It's a common issue using Spring and it's due to the fact that the view (jsp) goes through the DispatcherServlet.
Try to modify your web.xml using
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
and then add to your urlrewrite.xml something like:
<urlrewrite default-match-type="wildcard">
<rule>
<from>/</from>
<to>/app/</to>
</rule>
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
I'm assuming you're using urlrewrite, if you're not import the jar and add the filter mapping in your web.xml such as:
<filter>
<filter-name>urlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Character-encoding problem spring

I am stuck in a big problem with encoding in my website!
I use spring 3, tomcat 6, and mysql db. I want to support German and Czech along with English in my website, I created all the JSPs as UTF-8 files, and in each jsp I include the following:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
I created messages.properties (the default which is Czech), messages_de.properties, and messages_en.properties. And all of them are saved as UTF-8 files.
I added the following to web.xml:
<filter>
<filter-name>encodingFilter</filter-name>
<filterclass>
org.springframework.web.filter.CharacterEncodingFilter</filterclass>
<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>
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>en</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>cz</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>de</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
And add the following to my applicationContext.xml:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basenames="messages"/>
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="locale" />
</mvc:interceptors>
<!-- Declare the Resolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
I set the useBodyEncodingForURI attribute to true in the element of server.xml under: %CATALINA_HOME%/conf, also another time tried to add URIEncoding="UTF-8" instead.
I created all the tables and fields with charset [utf8] and collection [utf8_general_ci]
The encoding in my browser is UTF-8 (BTW, I have IE8 and Firefox 3.6.3)
When I open the MYSQL Query browser and insert manually Czech or German data, it's being inserted correctly, and displayed correctly in my app as well.
So, here's the list of problems I have:
By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default.
In the web form, when I enter Czech data, then click submit, in the Controller I print out the data in the console before to save it to db, what's being printed is not correct having strange chars, and this is the exact data that saves to db.
I don't know where's the mistake! Why can't I get it working although I did what people did and worked for them! don't know..
Please help me, I am stuck in this crappy problem since days, and it drives me crazy!
Thank you in advance.
First, if your project is using Maven, make sure that the Maven Resources Plugin has UTF-8 set as its character encoding scheme, otherwise the message properties files could be written to your target with an incorrect encoding.
Second, you're using ResourceBundleMessageSource, which uses the standard java.util.ResourceBundle and java.util.Properties, which only support ISO-8859-1 encoding. You can instead use ReloadableResourceBundleMessageSource like:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
which I discovered from this Cake Solutions blog post.
If this still does not work and you're using Tomcat as your application server try to set the following option on every <Connector> element in the server.xml:
<Connector URIEncoding="UTF-8" ...>
...
</Connector>
This did the trick for me. There might be similar options for other application servers, so you might want to check the server documentation.
1: By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default.
I don't do Spring, so here's a shoot in the dark: maybe because English is ordered first and/or your platform/browser uses English as default locale? Rearrange the configuration and check accept-language in the HTTP request header to exclude one and other.
2: In the web form, when I enter Czech data, then click submit, in the Controller I print out the data in the console before to save it to db, what's being printed is not correct having strange chars, and this is the exact data that saves to db.
Is the console configured to use UTF-8 to display data? It would use the platform default encoding otherwise. In for example Eclipse you can configure it by Window > Preferences > General > Workspace > Text File Encoding.
Is the data access layer configured properly to handle the data as UTF-8? MySQL JDBC driver is known to be a bit non-smart in this. It won't use the DB-specified encoding, but the client platform default encoding. To the point, you'd like to use the useUnicode=true and characterEncoding=UTF-8 properties in the JDBC connection string. E.g.
jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=UTF-8

Resources