Protect static files hosted on Apache with Tomcat / Java - java

I'm looking for a way to protect some static content hosted on Apache.
Those static files could be downloaded only by people based on rights that are defined in a database.
I am using Spring MVC for the web application.
I tried to forward the user to the relative path of the static content after I checked that the user has the appropriate rights. But it looks like forward in servlet can't forward to resource external to the Tomcat container. And I don't want to host the static content inside the web application folder.
I also tried to symlink a directory outside of the web application inside the web application folder. But again, it's not working.
The solutions I want to avoid is to redirect to the static content and then expose the actual URL of the resource on Apache which can then be access without any authorization.
I also don't want to use Tomcat to serve the static content with a servlet reading it blocks by blocks.

Related

How to do URL-rewriting in Websphere when Angular application is hosted in subdirectory

We're using Websphere to host a series of applications for us:
We have an API that's hosted at the root, written in Java
We have an Angular application hosted at /admin
We have another Angular application hosted at /marketing
My concern is about deep-linking. If a user is at /marketing/products/1, and they refresh their browser or share the link, I need the server to send that route to the correct Angular application so it can be generated correctly.
In a simpler setup, where the Angular application is living at the root, I would use the Java application's web.xml file to redirect traffic to "/". But in my current scenario, I need traffic for the marketing site to go to "/marketing", not just to "/". Just like a deep-link from the admin site would need to go to "/admin".
Furthermore, the base URLs for these Angular applications are subject to change, and we also plan to add additional Angular sites to this same server. So I'm really looking for a solution that can work dynamically and have the server redirect to the first "slug" in the URL rather than matching specific directories.
Any ideas? (And please excuse and correct any misconceptions I've demonstrated above -- I currently know very little about WebSphere)
I can see a couple possible ways forward.
You could still use the error-page directive in web.xml, but specify the URL of a servlet in your application that could do the inspection manually and issue a redirect as appropriate. How the list of context roots is provided to your app will differ based on how it's packaged, but it could be done using files, environment variables, or JNDI entries in server.xml.
If the URLs could be changed, the Angular apps could be changed to use HashLocationStrategy in their routers which would sidestep the error page. It doesn't seem likely that that's the case but I'll put it here to get it out of the way.
You could consider splitting each Angular app into its own .war file and configuring the context root in the webApplication element in server.xml. Then redirecting to / in web.xml would work since that / is relative to the context-root.
We ended up combining those separate Angular applications into 1 so that WebSphere could direct everything to "/" and Angular routing could handle everything from there.

Package DukeScript client artifacts in a war and serve from a web container (such as Tomcat)

I've followed the instructions here to create the sample DukeScript "Words" app. Everything works fine. I'd now like to serve the application using a web container. Is there an example of how to package the whole thing so that it can be deployed in a web container such as Tomcat.
Ideally - what I would also like to be able to do is make code changes, deploy to Tomcat, start tomcat, fire up my browser and visit the app. i.e. follow the normal Java webapp development cycle.
Is this possible?
In this scenario Tomcat wouldn't do anything but serve static content. The words application is pure static client code (no servlets, nothing...). So you can use a simple browser to access the index.html on disk. If you insist on using tomcat for development: There's no need to redeploy. The only thing you need to do is configure tomcat to serve the static content that the build generates. If you named your application "helloworld" this would be:
/helloworld/client-web/target/helloworld-web-1.0-SNAPSHOT-bck2brwsr/public_html
Make your code changes, build, reload the page in browser and the browser will see the updates

how to serve files for download in j2ee webapp

I am working on a feature where i have to provide a direct link to download certain files. all my files are in htdocs folder and i want to provide a direct link to download them like
webapp.com/files/file1.avi
could write a servlet and send the user a file.I know how to do it.
The other way is that if i directly go to webapp.com/files i should be able to see all of the files present in the directory. How do i do that?
do i have to make any server config changes to achieve it?
the webapp is running on Apache tomcat and jdk 1.4
You can directly serve your static content from tomcat(if it is your app server) root directory. I assume other app/web servers will have a static content directory as well.

Linking external files from Java Web Server

I have a java servlet that upon a request crunches on data and produces an image. There can potentially be millions of images and once produced they don't need to be re-rendered so I'd like to cache them and avoid the render step as it is quite tedious.
I have the cacheing working fine but the problem is I need these rendered images to persist between deployments of my web application, i.e., I can't write them into the docbase or else they get destroyed upon redeployment.
What I've been doing is using the 'allowLinking' attribute of the Context as my web application is deployed as a war file (context is in META-INF/context.xml). This is somewhat tedious because I need to break the symbolic link before my application is undeployed or else the images in the link are destroyed, but it seems to work.
But this only works for Tomcat and when testing with JBoss (5.1) it doesn't seem to honor the symbolic link and doesn't allow linking to anything outside of the docbase. I'm thinking there has to be a more practical way to accomplish this that works for all Java Web Servers. What am I missing?
You could just configure a servlet that would serve the images from an external directory. This servlet would just have to extract the image file name or ID from the request, read the file from an external directory and write the bytes to the servlet response's output stream (with the appropriate content type set on the response).
Or you could add an Apache httpd server front-end which would serve the static images from some external directory, and delegate to your servlet container for the other URLs.

Deliver files from a web server from outside the web app

Is there anyway to serve a file from a web server through the web, that is not within the web application.
I am using Tomcat and a Java servlets based application.
I don't want to put the files within the webapp because they are several 100GB and I will have to replace them every time I update the WAR if I put them inside it.
Apache web server can serve static files and delegate dynamic content requests to Tomcat.
You can simply map a URL to a servlet and the servlet can open the file from anywhere and read bytes from the fileinputsream and write them to the response output stream. You should add browser cache headers and also compress the data as you write it to the browser see http://onjava.com/pub/a/onjava/2003/11/19/filters.html

Categories

Resources