users@jersey.java.net

[Jersey] Re: How to set error response code from Rest web service and get it by Jersey client

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Thu, 07 Jul 2011 15:24:26 +0200

Hi Jitesh,

please see in-line...

On 07/04/2011 08:38 AM, Jitesh Sinha -X (jisinha - Siliconweb Inc. at
Cisco) wrote:
>
> How can I preserve encoding sent by server?
>
> Here is server side code --
>
> Response.ok(translationContent.getTranslatedContent().getBytes("UTF-8")).build();
>
>

I do not know the context, but i see bellow you store
the response to the retrieveTest.html file.
So i guess you should be using content-type:text/html; charset=utf-8
header where the charset parameter tells the client, what is the
character set,
such a resource should be then easily testable using a web browser.

> Here is client side code --
>
> InputStream translatedContent =
> response.getEntity(InputStream.class) ;
>
> String content =
> convertStreamToString(translatedContent) ;
>
> FileOutputStream fos = new
> FileOutputStream("retrieveTest.html") ;
>
> fos.write(content.getBytes("UTF-8")) ;
>
> fos.close() ;
>

This seems a bit complicated, you should be able to do just:

File translatedContent = response.getEntity(File.class);
translatedContent.renameTo(...);

Why do you ask? Anything does not work for you?

~Jakub

> String convertStreamToString(InputStream is) throws Exception {
>
> BufferedReader reader = new BufferedReader(new
> InputStreamReader(is,"UTF-8"));
>
> StringBuilder sb = new StringBuilder();
>
> String line = null;
>
> while ((line = reader.readLine()) != null) {
>
> sb.append(line + "\n");
>
> }
>
> is.close();
>
> return sb.toString();
>
> }
>
> Is something wrong in above code?
>
> Thanks
>
> -Jitesh
>
> *From:*Jitesh Sinha -X (jisinha - Siliconweb Inc. at Cisco)
> *Sent:* Sunday, July 03, 2011 9:37 PM
> *To:* users_at_jersey.java.net
> *Subject:* [Jersey] Re: How to set error response code from Rest web
> service and get it by Jersey client
>
> Hi Jakub,
>
> Thanks for your response. I tried HeaderParam code that you mentioned
> in your email and it still does not work. I still get appKey as null.
>
> Do I need to do something else?
>
> Thanks
>
> -Jitesh
>
> *From:*Jakub Podlesak [mailto:jakub.podlesak_at_oracle.com]
> *Sent:* Wednesday, June 29, 2011 10:47 PM
> *To:* users_at_jersey.java.net
> *Subject:* [Jersey] Re: How to set error response code from Rest web
> service and get it by Jersey client
>
> Hi Jitesh,
>
> the client side looks fine.
>
> On the server side, you might want to consume the header
> parameter with the @HeaderParam annotation. E.g.
>
> @GET
> public Response getMethod(@HeaderParam("appKey") String appKey) {
> ...
> }
>
> ~Jakub
>
> On 06/29/2011 02:32 AM, Jitesh wrote:
>
> Sorry one correction --
>
> On serverside it is
>
> String appKey = request.getHeader("appKey") ;
>
> *From:*Jitesh Sinha -X (jisinha - Siliconweb Inc. at Cisco)
> *Sent:* Tuesday, June 28, 2011 5:26 PM
> *To:* 'Pavel Bucek-2 [via Jersey]'
> *Subject:* RE: How to set error response code from Rest web service
> and get it by Jersey client
>
> Thanks Pavel.
>
> How do we send header parameters through GET request?
>
> I am using following code and on server side I get appKey as null --
>
> This is client side code -
>
> Client client = Client.create();
>
> WebResource service = client.resource(URL);
>
> MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
>
> queryParams.add("transactionId",testCommand.getTransactionId()) ;
>
> ClientResponse response =
> service.queryParams(queryParams).header("appKey",appkey).get(ClientResponse.class);
>
> Server Side --
>
> String appKey = request.getParameter("appKey") ;
>
> Is something wrong in above code?
>
> Thanks
>
> -Jitesh
>
> *From:*Pavel Bucek-2 [via Jersey] [mailto:[hidden email]
> </user/SendEmail.jtp?type=node&node=6527169&i=0>]
> *Sent:* Tuesday, June 28, 2011 12:53 PM
> *To:* Jitesh Sinha -X (jisinha - Siliconweb Inc. at Cisco)
> *Subject:* Re: How to set error response code from Rest web service
> and get it by Jersey client
>
> http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/core/Response.html
> http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/client/ClientResponse.html
> http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e363
>
>
> On 6/28/11 9:31 PM, Jitesh Sinha -X (jisinha - Siliconweb Inc. at Cisco)
> wrote:
> > Is there a way to set error code and message by Rest API and then to
> > retrieve it by using Jersey client?
> > Currently I know only about Response.ok(variable).build() which is
> > supposed to be called when request has been processed ok.
> >
> > Thanks
> > -Jitesh
> >
>
> ------------------------------------------------------------------------
>
> *If you reply to this email, your message will be added to the
> discussion below:*
>
> http://jersey.576304.n2.nabble.com/How-to-post-a-file-and-other-form-fields-by-Jersey-client-tp6502947p6526332.html
>
>
> To unsubscribe from How to post a file and other form fields by Jersey
> client, click here.
>
> ------------------------------------------------------------------------
>
> View this message in context: RE: How to set error response code from
> Rest web service and get it by Jersey client
> <http://jersey.576304.n2.nabble.com/How-to-post-a-file-and-other-form-fields-by-Jersey-client-tp6502947p6527169.html>
> Sent from the Jersey mailing list archive
> <http://jersey.576304.n2.nabble.com/> at Nabble.com.
>