File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes Resolving the webApp root from the context of a REST Resource Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Resolving the webApp root from the context of a REST Resource Object" Watch "Resolving the webApp root from the context of a REST Resource Object" New topic

Resolving the webApp root from the context of a REST Resource Object

Felix Woitzel

Joined: May 19, 2010
Posts: 5

This is my first post on the boards, so please be kind I guess my questions is already answered somewhere, but I'm still a little helpless with the search.
I have written REST APIs for database access before and I'm familiar with Servlets and Web Services. The work here is part of my diploma thesis and i could really need a hint or two ;)

It was easy to find out, how to resolve the webapp root from a simple Servlet (ServletContext#getRealPath(url)), but for a Rest Resource... no clue^^ I'm sure it must be possible in a few lines of code, but i didn't find anything on the net yet. Thanks in advance!

The ultimate goal is to store files (sent by a java applet) on the server (a Tomcat6.0) with a simple HTTP PUT command. The files are placed in a subdirectory of WEB-INF. The Rest API should list all files under the URL "MyWebAppRoot/messages/" and later i want to store and get files by the URL "MyWebAppRoot/messages/{message}".
The whole story is, that a Java Applet records voice and sends it to the server (The processing part is not my construction site). Using files is only a quick'n'dirty fallback, personally i would prefer a streaming solution, but since i have no experience with that, it would just bloat things up at the moment. Anyway, i would be thankful for hints on that topic too.
The big picture is, there's a set of tools developed at our university for generating user interfaces from in-house XML models - including previous work of mine. My work consists of 1) creating a generator for GWT interfaces, 2) adding sound input/output to the XML models, and 3) GWT elements for sound I/O.
I have already implemented the Java applet for recording (.wav files) and a wrapping GWT element. The job is now to bring the recording to the server. If a realtime streaming thing would be possible without too much overhead, that would be just great! But my time is limited and there's other work to do, so i think i will stick with the Rest API.

Will code Java for Food: Coffee, Cookies, Milk, Fishies, I'm an omnivore...
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
I am not sure I understand your question, but I will nevertheless make an attempt at providing what I hope will be some clues to an answer.
Have you seen then JAX-RS specification, chapter 6?
Using the @Context annotation, you can inject objects of the type ServletContext, HttpServletRequest etc into instances of your resource-handling class(es).
Perhaps this is of help.
Best wishes!
Felix Woitzel

Joined: May 19, 2010
Posts: 5
I'm new to uploading files and i have just found a way, with one Servlet for saving form uploads and the REST Resource using this Servlet over a HttpClient object. I came up with the idea that the Resource object makes use of a dedicated file upload Servlet like the CommonsFileUploadServlet with extended renaming options.

I'm using the Restlet implementation and Eclipse with the WTP and GWT plugins. The servlets get deployed on a Tomcat6.0.

But i have not answered the thread title's question yet. It was a good learning session for me, but it would have been quicker if the org.restlet.resource.Resource object had an obvious way to resolve the root directory of the nesting web application on the server or a dynamic path pointing on the WEB-INF directory. Maybe the annotation thing is the key, but I'm working on the HttpClient.

anyway, thank you!
I agree. Here's the link:
subject: Resolving the webApp root from the context of a REST Resource Object
It's not a secret anymore!