t.
I don't wanna introduce nothing about REST, Bill Burke, already did it at this entry on his blog. I strongly recommend you read it first. Bill Burke is the RESTEasy Project leader, so you can find out accurate information about all over the Projec
Figuring Out a Service to be used for the most Stupid and Poor HTML Client
When we are talking about integration, we never can forget about limited languages used to build an UI, so my example shows a really simple HTML, where I wanna inform any file and upload to a specific folder where my Web container is running on. The client to this service can be from the simplest html until the a really nice extJS in PHP or Asp.net.
<form method="POST" action="http://localhost:8080/flowlet/upload/process" enctype="multipart/form-data">
<label style="COLOR: #1a3663; FONT-WEIGHT: bold; FONT-SIZE: x-small; FONT-FAMILY: 'Verdana';">Please enter with the jar file location:</label><br>
<input type="file" size="50" name="file">
<input type="submit" value="Upload now!">
</p>
</form>
As far you can see, the action from my html form is just a simple URL and the method is POST, basically I will inform the file name, and then using Commons FileUpload I will upload a file using a RESTFul service.
Using javax.http.HttpServletRequest into your RESTFul Service
When you create your first service based on REST Model, you can feel really excited with REST approach, however you still need access some commons objects from a Java Web Application, so you may need access the Request object to get all their methods to interact with your application. In my case, I must get the Request object to use with my file upload component. To do that, with REST is really great, basically we will use something similar to what you can see in Dependency Injection in EJB3 or Seam. See the the code to implement a method to do this action:
@POST
@Path("/upload/process")
@ProduceMime("text/plain")
public String uploadProcess(@Context HttpServletRequest req) throws Exception {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
System.out.println("FORM FIELD");
} else {
if (!item.isFormField()) {
String fileName = item.getName();
System.out.println("File Name:" + fileName);
File fullFile = new File(item.getName());
File savedFile = new File("/home/jsilva/Desktop/test", fullFile.getName());
item.write(savedFile);
}
}
}
return "OK";
}
You see, that I can catch the request through the annotation @Context as reference parameter in my simple Java method. You can get several other contextual object just using this annotation.
I hope this tip could be quite useful for your RESTEasy development.