Quantcast

Adding Header to Response

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Adding Header to Response

ebear
Hi,

how can I add an additional header to this call:
Response.temporaryRedirect(uri).build()
The header is needed for basic authentication, so I'd like to add the
header as pair of strings.
I read something about ||
<http://www.j2ee.me/javaee/6/docs/api/javax/ws/rs/ext/RuntimeDelegate.HeaderDelegate.html>RuntimeDelegate.HeaderDelegate
but I didn't get it work.
I'm using jersey 1.0.3.1

Thanks,
Andy

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding Header to Response

Paul Sandoz
Administrator

On Oct 21, 2009, at 6:40 PM, Andy wrote:

> Hi,
>
> how can I add an additional header to this call:  
> Response.temporaryRedirect(uri).build()


   Response.temporaryRedirect(uri).header("X-FOO", "foo").build()

See:

https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/Response.html

Paul.

> The header is needed for basic authentication, so I'd like to add  
> the header as pair of strings.
> I read something about || <http://www.j2ee.me/javaee/6/docs/api/javax/ws/rs/ext/RuntimeDelegate.HeaderDelegate.html 
> >RuntimeDelegate.HeaderDelegate but I didn't get it work.
> I'm using jersey 1.0.3.1
>
> Thanks,
> Andy
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding Header to Response

ebear
On 21.10.2009 18:57, Paul Sandoz wrote:
> On Oct 21, 2009, at 6:40 PM, Andy wrote:
>> Hi,
>>
>> how can I add an additional header to this call:
>> Response.temporaryRedirect(uri).build()
>   Response.temporaryRedirect(uri).header("X-FOO", "foo").build()
Somehow it doesn't work. I have set up a mock-up server that receives
this message an prints out all header fields. The result looks like this
"user-agent, host, accept, connection" but the header field "X-FOO" is
missing. This is my code:

@Path("mypath")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod() {
    URI uri =
UriBuilder.fromUri("http://localhost:9998/mockup/dummy").build();
    return Response.temporaryRedirect(uri).header("X-FOO", "foo").build();
}

Andy

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding Header to Response

Paul Sandoz
Administrator

On Oct 22, 2009, at 11:38 AM, Andy wrote:

> On 21.10.2009 18:57, Paul Sandoz wrote:
>> On Oct 21, 2009, at 6:40 PM, Andy wrote:
>>> Hi,
>>>
>>> how can I add an additional header to this call:  
>>> Response.temporaryRedirect(uri).build()
>>  Response.temporaryRedirect(uri).header("X-FOO", "foo").build()
> Somehow it doesn't work. I have set up a mock-up server that  
> receives this message an prints out all header fields. The result  
> looks like this "user-agent, host, accept, connection" but the  
> header field "X-FOO" is missing.

What about the Location header?

Perhaps you are observing the response to the request made to the  
redirected URI?

What client are you using? is it performing automatic redirection on  
requests?

Paul.

> This is my code:
>
> @Path("mypath")
> @GET
> @Produces(MediaType.APPLICATION_JSON)
> public Response myMethod() {
>   URI uri = UriBuilder.fromUri("http://localhost:9998/mockup/ 
> dummy").build();
>   return Response.temporaryRedirect(uri).header("X-FOO",  
> "foo").build();
> }
>
> Andy
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding Header to Response

ebear
On 22.10.2009 11:45, Paul Sandoz wrote:

> On Oct 22, 2009, at 11:38 AM, Andy wrote:
>> On 21.10.2009 18:57, Paul Sandoz wrote:
>>> On Oct 21, 2009, at 6:40 PM, Andy wrote:
>>>> Hi,
>>>>
>>>> how can I add an additional header to this call:
>>>> Response.temporaryRedirect(uri).build()
>>>  Response.temporaryRedirect(uri).header("X-FOO", "foo").build()
>> Somehow it doesn't work. I have set up a mock-up server that receives
>> this message an prints out all header fields. The result looks like
>> this "user-agent, host, accept, connection" but the header field
>> "X-FOO" is missing.
> What about the Location header?
>
> Perhaps you are observing the response to the request made to the
> redirected URI?
>
> What client are you using? is it performing automatic redirection on
> requests?
The mentioned headers are received at the redirected URI
(http://localhost:9998/mockup/dummy). This is where the header "X-FOO"
is expected, but it is missing.

I use the com.sun.jersey.api.client.Client which performs a GET request
to the server I'm implementing (running at http://../mypath). This
server should add an additional header and redirect the request to
http://localhost:9998/mockup/dummy.
The client even receives the correct response so everything works fine,
except for adding the header.

If I already add the header on client side, it is received at
http://localhost:9998/mockup/dummy but this is what I want to avoid.

Andy

>> This is my code:
>>
>> @Path("mypath")
>> @GET
>> @Produces(MediaType.APPLICATION_JSON)
>> public Response myMethod() {
>>   URI uri =
>> UriBuilder.fromUri("http://localhost:9998/mockup/dummy").build();
>>   return Response.temporaryRedirect(uri).header("X-FOO", "foo").build();
>> }

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding Header to Response

Paul Sandoz
Administrator

On Oct 22, 2009, at 1:38 PM, Andy wrote:

On 22.10.2009 11:45, Paul Sandoz wrote:
On Oct 22, 2009, at 11:38 AM, Andy wrote:
On 21.10.2009 18:57, Paul Sandoz wrote:
On Oct 21, 2009, at 6:40 PM, Andy wrote:
Hi,

how can I add an additional header to this call: Response.temporaryRedirect(uri).build()
Response.temporaryRedirect(uri).header("X-FOO", "foo").build()
Somehow it doesn't work. I have set up a mock-up server that receives this message an prints out all header fields. The result looks like this "user-agent, host, accept, connection" but the header field "X-FOO" is missing.
What about the Location header?

Perhaps you are observing the response to the request made to the redirected URI?

What client are you using? is it performing automatic redirection on requests?
The mentioned headers are received at the redirected URI (http://localhost:9998/mockup/dummy). This is where the header "X-FOO" is expected, but it is missing.

The X-FOO response header will only be sent in the 307 (Temporary Redirect) response (as is the case for the Location header with the URI to redirect to).

What is happening is the client API is performing automatic redirection to the redirected URI.

Try setting redirection to false:


and look at the response headers.

You need to modify the response returned from the URI ttp://localhost:9998/mockup/dummy to return the X-FOO header.

Paul.


I use the com.sun.jersey.api.client.Client which performs a GET request to the server I'm implementing (running at http://../mypath). This server should add an additional header and redirect the request to http://localhost:9998/mockup/dummy.
The client even receives the correct response so everything works fine, except for adding the header.

If I already add the header on client side, it is received at http://localhost:9998/mockup/dummy but this is what I want to avoid.

Andy
This is my code:

@Path("mypath")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod() {
 URI uri = UriBuilder.fromUri("http://localhost:9998/mockup/dummy").build();
 return Response.temporaryRedirect(uri).header("X-FOO", "foo").build();
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding Header to Response

ebear
On 22.10.2009 13:48, Paul Sandoz wrote:

> On Oct 22, 2009, at 1:38 PM, Andy wrote:
>>> What client are you using? is it performing automatic redirection on
>>> requests?
>> The mentioned headers are received at the redirected URI
>> (http://localhost:9998/mockup/dummy). This is where the header
>> "X-FOO" is expected, but it is missing.
>
> The X-FOO response header will only be sent in the 307 (Temporary
> Redirect) response (as is the case for the Location header with the
> URI to redirect to).
>
> What is happening is the client API is performing automatic
> redirection to the redirected URI.
>
> Try setting redirection to false:
>
> https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/api/client/Client.html#setFollowRedirects%28java.lang.Boolean%29
>
> and look at the response headers.
>
> You need to modify the response returned from the
> URI ttp://localhost:9998/mockup/dummy to return the X-FOO header.
Ok thanks, I think redirection is the wrong approach for my case.
Is there a way to simply forward a complete response (JSON or any other
format) from another server to the client without deserializing it into
a (JAXB-)Java class? Only the client needs to deserialize the message.

Andy

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding Header to Response

Paul Sandoz
Administrator

On Oct 22, 2009, at 3:17 PM, Andy wrote:

On 22.10.2009 13:48, Paul Sandoz wrote:
On Oct 22, 2009, at 1:38 PM, Andy wrote:
What client are you using? is it performing automatic redirection on requests?
The mentioned headers are received at the redirected URI (http://localhost:9998/mockup/dummy). This is where the header "X-FOO" is expected, but it is missing.

The X-FOO response header will only be sent in the 307 (Temporary Redirect) response (as is the case for the Location header with the URI to redirect to).

What is happening is the client API is performing automatic redirection to the redirected URI.

Try setting redirection to false:

https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/api/client/Client.html#setFollowRedirects%28java.lang.Boolean%29

and look at the response headers.

You need to modify the response returned from the URI ttp://localhost:9998/mockup/dummy to return the X-FOO header.
Ok thanks, I think redirection is the wrong approach for my case.
Is there a way to simply forward a complete response (JSON or any other format) from another server to the client without deserializing it into a (JAXB-)Java class? Only the client needs to deserialize the message.


You server can be a client too. Thus you could use the Jersey client API to make the request. For example:

@Path("mypath")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod() {
  URI uri = UriBuilder.fromUri("http://localhost:9998/mockup/dummy").build();
  Client c = ...
  WebResource r = c.create(uri);
  InputStream in = r.accept(MediaType.APPLICATION_JSON).get(InputSteam.class);
  return Response.ok(in).header("X-FOO", "foo").build();
}

Note that Client instances are expensive to create so you might want to reuse one instance rather that create a new instance for each request.

Paul.
Loading...