Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLRewrite in Tomcat 7

Tags:

java

tomcat

I am planning to develop an Intranet application (Java client, JSP, SQLite)

Goal of this is that when a user clicks on the link, if the user has access (to a team which is handled in the business logic) a file should be provided for download

There is a table in the db which holds the info and below is the example row

ID | file                               | team   | md5
1  | D:\test\output_20140915_100012.zip | Falcon | 5cceaf4cc400fd8f5c7fb4b2af754f4093369f59

where MD5 is the MD% checksum of the string "D:\test\output.zip" and not the file itself

I create the MD5 just for the sake of having random number. Reason I don't use RAND is to avoid collision (this part is trivial)

I want my URL to look like this

http://mywebserver:8080/<appname>/5cceaf4cc400fd8f5c7fb4b2af754f4093369f59

Which should be taken to

http://mywebserver:8080/<appname>/download.jsp?id=5cceaf4cc400fd8f5c7fb4b2af754f4093369f59

I am using Tomcat and I am wondering if how we could have a URL rewrite for this scenario

like image 328
KK99 Avatar asked Dec 25 '22 04:12

KK99


2 Answers

For Tomcat 7 you can also use tuckey . Tucket is a Java Web Filter for any compliant web application servers and allows mod_rewrite just like Apache

like image 41
sol4me Avatar answered Dec 28 '22 07:12

sol4me


I would recommend to do it with Apache HTTP Server or another server. But if you want to do it only with Tomcat 7 use Tuckey UrlRewriteFilter:

A Java Web Filter for any compliant web application servers (such as Tomcat, JBoss, Jetty or Resin), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.

This filter allows you to define rules like:

Clean a url

<rule>
    <from>/products/([0-9]+)</from>
    <to>/products/index.jsp?product_id=$1</to> 
</rule> 

eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing

from Tuckey urlrewrite example

Since Tomcat 8 you can use the rewrite valve.

The rewrite valve implements URL rewrite functionality in a way that is very similar to mod_rewrite from Apache HTTP Server.

Rewrite Valve docs

like image 142
Tobías Avatar answered Dec 28 '22 06:12

Tobías