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.

Thursday, July 3, 2008

Thanks "BIG RED": JRockit for download is available again

Since the bea's acquisition, I missed the JRockit download during a long time, but now JRockit download link is back here: http://www.oracle.com/technology/software/products/jrockit/index.html

Comments aside, at least we can use this cool JVM for testing and to obtaing a good performance and monitoring features for JBoss environments.

Good download, even with a new name!

E