users@jersey.java.net

Re: [Jersey] 1)regaring testing http POST of restful webservices with multiple parameters and 2) restful file uploading

From: Bruno Luiz Pereira da Silva <blpsilva_at_gmail.com>
Date: Sun, 11 May 2008 09:10:36 -0300

Hi, I'll leave the answer to question 2 to the most experienced members of
the group, because I haven't used Jersey (nor REST) to upload images yet.

However, I'd like to comment on the first question. I work on the largest
media company in Latin America (www.globo.com) and I'm currently doing
something related to what you asked. We have 2 large applications that serve
many client applications inside the company. Our legacy architecture is
based on EJB 2.1 communication, through RMI. Some years ago pretty much all
of the client applications were Java apps. We distribute a client jar which
enables the client applications to communicate easily with the servers.

We had some problems with this approach. Coupling was too high. At some
point we would like to update the servers to new JVM and app server
versions, but were limited by old clients that were no longer maintained.
The server progress was limited by the older clients. This was really
annoying. Besides that, several client applications that are not Java based
started to show up.

We are currently migrating our architecture to a RESTFul approach, changing
our RMI interfaces to RESTFul ones. We are doing this step by step. This
migration is not something that will be priorized by the product owner, so
we are progressively migrating the interfaces in the pace we can.

Ok, let me finally get to your question. Our legacy architecture uses the
Command pattern to implement the communication between the servers and the
clients. We have many commands in the form "CmdDoSomethingComplicated". It's
an imperative architecture. Migrating this architecture to WS-* would be
just a matter of switching the RMI interfaces to SOAP ones. No change of
mind involved. However, we think a RESTFul approach suits better our needs,
so the migration isn't obvious.

Many of our commands have several "input parameters". Basically the client
send everything it needs to the server, and the server responds in an
adequate manner considering the request. We just migrated one command that
was something like CmdFinalizeOrder. The client uses this command to send
informations regarding an user order, and have the server process it. Among
the "input parameters" is the list of products the user wants to buy and
several billing details. This is definitely not CRUD, so the migration to
REST isn't so trivial.

What we did was identifying a new Order resource. This resource was not one
of the business objects. We didn't have an Order class in our domain model.
But using this new Resource made everything much clearer. The operation
become the creation of a new Order resouce. The Order resource contains the
products and the billing data.

In my opinion, you're probably going through a similar task. You must
implement RESTFully (declarative form) an operation that is currently
imperative. What I suggest is that you identify which resource you're
manipulating. REST manipulates resources or resource collections. You should
not have anything with more than one "parameter". The only "parameter" you
should have is the Resource. You can have a collection of resource, but not
two or more different resources. You should analyse your domain and figure
out what is this resource. In some cases it is easy, in other cases, not so
easy. But I haven't found any case where it's not possible.

I hope I was able to help you.

-- 
Regards,
Bruno Luiz Pereira da Silva
blpsilva_at_gmail.com
http://brunopereira.com.br
On Sun, May 11, 2008 at 3:04 AM, Jyothsna L <l.jyothsna_at_yahoo.com> wrote:
> Hi
>
>
> I have two questions.
>
> 1) while testing the POST of restful web services, I always get a string
> content as parameter that needs to be filled while posting.
> so , I am parsing the string based some delimiters like comma and then
> saving each element in database.
> for example my database has email,name as its columns in one table.
> so if my POST parameter : content has jyothsna_at_yahoo.com, jyothsna. I am
> parsing it into two elements jyothsna_at_yahoo.com(email) and jyothsna(name).
> and then I am creating entries in the table.
>
> I tried to add two parameters to postxml() instead of one. but, its not
> working properly.
>
> so do you suggesting parsing one single parameter or is there any other
> good way to have multiple parameters.
>
> my second question
> 2)
>
> I need to upload an image file to the backend. I want to save the file on
> disk and its URL in the database.
>
> I am using restful webservices and ejb module.
>
> here the http PUT method definition.
> Since, I will be uploading an image, i am consuming multipart/form-data.
> I dont know how to handle the file. I have the struts and jersey plugins
> too. I am new to struts and a bit familiar with jersey(restful).
>
>  @PUT
>     @ConsumeMime("multipart/form-data")
>     public void putXml(@PathParam("id")
>     String id, MimeMultipart data ) {
>         Images newImage= new Images();
>         newImage.setImageURL(url);//just showing the methods available for
> Images object. this statement is not completely correct
>                                                     I need to save the
> image file on disk and set its location in the imageresource.
>         newImage.setImagename(name); // 'name' needs to be extracted from
> the MimeMultipart or whatever..just wanted to show available methods for
> Images object.
>         lookupImagesFacade().create(newImage);
>     }
>
> My question is : how will i be able to save the file on disk and its url on
> database based on the template i have above. is the "MimeMultipart data "
> correct in the above code. which package has this "MimeMultipart"( I see
> there are different kinds of packages java mail etc.)
>
> or is there any other good way to do file uploading in restful using
> jersey.
>
> thanks,
> Jyothsna
>
> ------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it
> now.<http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ>
>