users@jersey.java.net

[Jersey] Re: WS itself playing the role of web-client

From: Arthur Yeo <artyyeo_at_gmail.com>
Date: Thu, 24 Mar 2011 12:40:16 -0700

I am intending to put the Client.create in a singleton when I get it working properly.

I'll grab the whole stack dump and send it later...

Thanks,
Art

Sent from my iPhone

On Mar 24, 2011, at 12:31 PM, Jakub Podlesak <jakub.podlesak_at_oracle.com> wrote:

> Hi Art,
>
> i can not see the whole stack trace, but it seems,
> the exception is coming from
>
> WebResource wr = client.resource(extUrl );
>
> i guess, you run a bit different code,
> where the extUrl value is not a static String,
> and sometimes you just got an invalid URI.
>
> Another thing is, that as the Client.create()
> operation is costly, you might want to avoid
> calling it with each request.
>
> ~Jakub
>
> On 03/24/2011 01:35 AM, Arthur Yeo wrote:
>> Thanks again, Jakub.
>>
>> I tried with this new code and it works for a several times and then it
>> stops working. Once it stops, I would have to bounce GF in order to get
>> it working again.
>> BTW, I do not need to process the image, I just need to return the
>> image. And, so, having it in an Image object is not important.
>>
>> When it stops working, Server log has this WARNING:
>> ===========
>> PWC1406: Servlet.service() for servlet ServletAdaptor threw exception
>> java.lang.IllegalArgumentException at java.net.URI.create(URI.java:842)
>> at com.sun.jersey.api.client.Client.resource(Client.java:350) at
>> ==============
>>
>> My new code ...
>> --------------
>> @POST
>> @Consumes("application/x-www-form-urlencoded")
>> @Produces("image/*")
>> public File myRsrc( @FormParam("upc") String blah1,
>> @FormParam("descr") String blah2) throws
>> IOException
>> {
>>
>> String extUrl = "https://SomeWsCreatedByOthers
>> <https://somewscreatedbyothers/>";
>>
>> Client client = Client.create();
>> WebResource wr = client.resource(extUrl );
>>
>> File imgFile = wr.accept("image/*").get(File.class);
>>
>> if (imgFile.canRead()) {
>> System.out.println("gotten the image file from WS and it is
>> readable.");
>> }
>> return (imgFile);
>> }
>> ----------------
>>
>> On Wed, Mar 23, 2011 at 5:16 PM, Jakub Podlesak
>> <jakub.podlesak_at_oracle.com <mailto:jakub.podlesak_at_oracle.com>> wrote:
>>
>> Art,
>>
>> i was wrong. Jersey in fact provides image message body workers,
>> but you have to use specifically java.awt.image.RenderedImage for
>> writing (output),
>> and java.awt.image.RenderedImage or java.awt.image.BufferedImage for
>> reading (input).
>>
>> I.e.
>>
>> BufferedImage r = wr.get(Image.class);
>>
>> shoud just work.
>>
>> ~Jakub
>>
>>
>> On 03/23/2011 11:27 PM, Jakub Podlesak wrote:
>>
>> Hi Art,
>>
>> Jersey does not provide message body workers for java.awt.Image
>> class
>> out of the box. You can get access to the message body input stream,
>> or get it (the message body) written into a temporary file and
>> process
>> it from there, e.g.:
>>
>> File imgFile = wr.accept("image/*").get(File.class);
>> Image image = ImageIO.read(imgFile);
>>
>> After you fix this, you will get similar problem, when
>> trying to return the Image instance out from your resource method.
>> This time, you will be getting a missing message body writer error.
>> And again, you can return an OutputStream/File instead of the Image
>> to fix this.
>>
>> Another approach would be, if you wish to make your original code
>> work intact, to implement your custom java.awt.Image message
>> body workers. Please see the entity-provider example [1]
>> for reference.
>>
>> HTH,
>>
>> ~Jakub
>>
>> [1]http://download.java.net/maven/2/com/sun/jersey/samples/entity-provider/1.5/entity-provider-1.5-project.zip
>>
>>
>>
>> On 03/23/2011 10:05 PM, Arthur Yeo wrote:
>>
>> I am having problems receiving an image using the Client
>> class. The
>> error I am getting in the Server log is this:
>> -------
>> WC1406: Servlet.service() for servlet ServletAdaptor threw
>> exception
>> com.sun.jersey.api.client.ClientHandlerException: A message
>> body reader
>> for Java type, class java.awt.Image, and MIME media type,
>> image/png, was
>> not found at
>> --------
>>
>> here's the code I am running ... is it too simplistic here?
>>
>> ------
>> @POST
>> @Consumes("application/x-www-form-urlencoded")
>> @Produces("image/*")
>> public Image genQrc( @FormParam("upc") String blah1,
>> @FormParam("descr") String
>> blah2) throws
>> IOException
>> {
>>
>> String extUrl = "https://SomeWsCreatedByOthers";
>>
>> Client client = Client.create();
>> WebResource wr = client.resource(extUrl );
>>
>> Image r = wr.get(Image.class);
>> return (r);
>> }
>>
>> On Wed, Mar 23, 2011 at 9:17 AM, Arthur Yeo
>> <artyyeo_at_gmail.com <mailto:artyyeo_at_gmail.com>
>> <mailto:artyyeo_at_gmail.com <mailto:artyyeo_at_gmail.com>>> wrote:
>>
>> Thanks, Pavel, for pointing me to the right direction.
>> I found Jakub's blog.
>>
>> Now, one more question: if the WS I am calling is
>> returning an
>> image, what's the right way to handle this?
>>
>> Thanks,
>> Art
>>
>>
>> On Wed, Mar 23, 2011 at 3:46 AM, Pavel Bucek
>> <pavel.bucek_at_oracle.com <mailto:pavel.bucek_at_oracle.com>
>> <mailto:pavel.bucek_at_oracle.com
>> <mailto:pavel.bucek_at_oracle.com>>> wrote:
>>
>> On 03/23/2011 05:20 AM, Arthur Yeo wrote:
>>
>> All,
>> In the mix of things, I need one of my WS's to
>> call another
>> WS provided by another organization. In other
>> words, my WS
>> is now playing the role of a web client.
>>
>> What's the best way to do this in Jersey?
>> Do I just stuff Java client code, like making
>> use of the std
>> URL class, in there?
>> Or, Is there something else in Jersey you recommend?
>>
>>
>> former, you need to create Client and WebResource by
>> yourself,
>> there is nothing better yet (but it might be soon).
>>
>> you might want to cache created client instance in some
>> singleton, Client.create() is relatively expensive
>> operation.
>>
>> Pavel
>>
>>
>> --
>> Arthur Y.
>>
>>
>>
>>
>>
>> --
>> Arthur Y.
>>
>>
>>
>>
>> --
>> Arthur Y.
>>
>>
>>
>>
>>
>>
>>
>> --
>> Arthur Y.
>