Wednesday, July 23, 2008

RESTEasy tips: Part 1

If you are planning build an application using RESTFul WebServices (I don't like this terminology), RESTEasy for sure could be your #1 option. Besides a cool documentation here, I will publish here a cool example of how use RESTEasy method to upload some file via HTTP protocol.


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
t.

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.

3 comments:

Pranav said...

Hi,

I'm try to access the form parameters through the HttpServletRequest object in my RESTful servlet ,but I'm getting the following error:


(javax.servlet.http.HttpServletRequest)
15:11:20,504 ERROR [STDERR] at org.resteasy.ResourceMethod.internalInvoke(ResourceMethod.java:136)


My code is as follows:


@POST
@Path("/basic")
@ProduceMime("text/plain")
public String doPost(@Context HttpServletRequest req){

logger.info("In doPost..");
String firstName = req.getParameter("Firstname");

String LastName = req.getParameter("Lastname");
return "Boo-Ya!";

}

The control goes inside the method but throws error when req object is accessed.
Can you please help me to fix this?

Thanks,
Pranav

Anonymous said...

hi,
i'm try to do this tutorial..but i always have the 404 response when i run this upload method,i have tried another get method deployed in the same application with the upload one which just returns a string and it works ok.
any hints plz?
thnx in advance
shady

Edgar Ankiewsky Silva said...

Which error are you getting?