Analog of PHP .htaccess for JSP/Glassfish - java

How is it possible to restrict files(PDF) access in JSP/Glassfish so they can be opened only from a source code not with a straight url. For PHP projects I used .htaccess.

Anything under the webapp's WEB-INF directory cannot be accessed via direct URL, but application code can access it. This is a good place to put internal resources, config, JSPs, etc.

Wow,
.htaccess is an Apache HTTPD feature. As far as I know there isn't anything comparable in GlassFish. What you could do is: Write a Servlet or Servlet Filter which takes care of this and/or map the *.pdf extension in web.xml to it..
Thanks,
M

Related

Webapp map static resources to jar in Tomcat

I am porting a web app to tomcat and I have a problem with static resources.
I have a jar that contains the web resources (css, img, js, ... files). These files are packaged in a path resembling this:
data.jar
com/bizname/application/web/css
com/bizname/application/web/img
com/bizname/application/web/js
The jar is inside the web application. I want to be able to map the resources to public acces. For example if I acces :
http://myserver:8080/mywebapp/css/style.css
I want the resource com/bizname/application/web/css/style.css to be delivered.
I cant find how to set up this in the web.xml-file of Tomcat.
In Jetty embedded, I was able to use handles to achieve this.
final URL url_css = Servidor.class.getClassLoader().getResource("com/bizname/application/web/css");
HandlerList handlers = new HandlerList();
handlers.addHandler(new WebAppContext(url_css.toExternalForm(),"/css"));
// ... adding handler to context
Maybe jawr can help you.
If not, you can implement a Servlet that does it for you (reading the file and writing it to the response). If you use something like Resteasy it will be easier because you don't need to write the IO stuff.
I am not quite sure this is right, considering I have only worked with Tomcat from Eclipse, but I'll give it a shot anyway.
I am assuming you are using Java Servlets in this app? If that is the case, then you need to specify the URL-patterns in web.xml. Here is a great example of a web.xml file from apache.org which explains the aspects of the file:
https://tomcat.apache.org/tomcat-5.5-doc/appdev/web.xml.txt
Hope this helps you!

Serve files from a folder different of context directory in a servlet container

I got a situation that I must serve files from different folders then the one of the context my web app is running. As an example, suppose my web app is running in a servlet context on "/opt/tomcat/webapps/ROOT/" and I must serve files existent in "/opt/my_other_folder/". These folders can be changed in runtime by the client, so I can't simply add a new context pointing to these directories. I would like a solution that I wouldn't have to rewrite a web server only for that. Also, the product I work on is generic, so I can't have a solution specific to some servlet container.
Thanks!
If you're only serving files, I would consider fronting your servlet container with something like Apache HTTP Server, where you could simply use its various directives to provide a "virtual directory" pointing to an easily configured location.
Otherwise, you could write and configure a standard Java servlet that would do essentially the same thing - storing the actual path in a Java properties file that would be read by the servlet. But while this isn't a lot of work, it would be significantly more work that the above Apache HTTP Server solution. This would be very similar to several of the answers posted at Servlet for serving static content . Specifically, you could either use or extend upon Apache Tomcat's DefaultServlet. (There are some Tomcat-specific classes used in here, but they could be easily replaced with generic equivalents.) http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html looks even closer to what you'd be looking for, and it is completely generic - while still having some additional, significant features.
Either of these options would be very generic, and not specific to any particular servlet container.

How to publish non-Java resources generated at runtime on a Tomcat server?

I have a Java webapp running on Tomcat.
At runtime, I create images files that I want to be publicly published on the tomcat server.
1/ How can I get the local URL where I want to copy my image files? (ie /mylocalpath/to/where/i/should/store/the/file/)
2/ How can I know the URL where other machines can access this public files? (ie http://mydomainname/myapp/myresource.png)
Keep the path in a servlet init-param, a JNDI string, or in a property file. (Or whatever is provided by your framework that allows simple configuration.)
Create a servlet/action/controller/etc. that's mapped to a known URL. Either pass in a param with the filename or make the filename part of the URL. Stream the contents of the file back to the user. (Search for "image servlet" for examples.)
Bear in mind the mime type of the file and set the appropriate header. If necessary, check if the requesting user has access to the file in question. (There are several ways to implement that.)
I've figured a much simpler way to do this (which may sound obvious to Tomcat experts but useful to others).
In Tomcat 6 "server.xml" file, I've added this line in the <Host> element :
<Context docBase="/mylocalpath/to/where/i/should/store/the/file" path="/uploads" />
Then, when i create my resource i copy it in this local directory and figure out the public URL pretty easily : http://myserver/uploads/myfilename
Hope it can help other people.
(I even think the context can be defined in a context.xml included in the WAR rather than in Tomcat's global configuration but a global definition was enough for my needs).

understanding jboss framework - URL

I am trying to figure out what the following url does.
http://server/abc/testmodule/runtest.do?action=edit&id=123
I am new to jboss/jsp but I am very familiar with .net.
When I see this url, I expect to see the physical folder called "abc" and subfolder called "testmodule" and a physical file "runtest". am i wrong? what does runtest.do? is "runtest" class and "do" is a method within it?
It could be anything--URLs can map to arbitrary resources. It might be a Struts action, it might be a servlet, it might be a Spring controller, etc.
You'd need to check your web.xml file and/or any framework configuration files, or provide more information.
(Also, JBoss isn't a framework, it's a Java EE container :)
The /abc entry is the name of the context in which the application is running. If it's a web app, deployed in a WAR file, that would be the name of the WAR that's deployed (abc.war).
The .do extension suggests a Struts or JSF action mapping.
There are two parameters passed in the GET: action, with value edit, and id, with value 123. Looks like a REST-ful API to me.

Apache vs Tomcat configuration question

I was searching in Google and found that Apache can be configured via mod_access directives in the httpd.conf file to block a web site from a particular IP.
Is there anything equivalent in Tomcat?
I am not sure I understand what are the corresponding configuration files.
Thanks
Try the Remote Address Filter. http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html
See the Request filters section of this doc. Doing this via tomcat configuration is pretty static, you need to restart to edit configuration. If you need something dynamic, it's probably best to implement a custom servlet filter.

Categories