Hi Sergey,

I'm trying the Response approach you suggested. Its for a file
download service so I need to be able to set the Content-Type and
Content-Length headers and also provide the file contents as the HTTP
response body.

Code snippet:

    @ProduceMime({"application/xml", "application/json"})
    class MailService

            @ProduceMime("*/*")
            public Response getFile(){

            .....

            MimetypesFileTypeMap map = new MimetypesFileTypeMap();
            String contentType = map.getContentType("file." + fileExtension);
            int index = contentType.indexOf("/");
            String type = contentType.substring(0, index);
            String subType = contentType.substring(index + 1);
            MediaType media = new MediaType(type, subType);

            ResponseBuilder builder =
Response.status(HttpURLConnection.HTTP_OK);
            builder.entity(f);
            builder.type(media);
            builder.header("Content-Length", f.length());
            resp = Response.ok().build();

I assumed that builder.type(media) would set the Content-Type but it
doesn't so I'm also adding it manually:

            builder.header("Content-Type", contentType);

Its still not setting the content type though. I'm wondering if its
down to the class-wide @ProduceMime() which is more restrictive?

Also, is builder.entity() the right way to set the response body? I'm
not seeing my file contents in the response at the client.

Cheers,
brad.


On Thu, Jun 12, 2008 at 4:46 PM, Sergey Beryozkin
<[EMAIL PROTECTED]> wrote:
> Hi Brad
>
> If you can afford returning a Response in a signature then that would be the
> easiest way to have
> custom response headers set.
> I'm wondering does @ProduceMime("contextTypeValue") has an effect ? It
> should, the only problem is that on the trunk the parameters are ignored (to
> be fixed shortly) that is, if you do something like
>
> @ProduceMime("application/atom+xml;type=feed")
>
> then ';type=feed' will be ignored.
>
> Finally you can have a field
>
> @Resource HttpServletResponse response;
>
> @Context-annotatted injection of these types will be supported shorty too,
> but in meantime you can try this option. The only problem is that it's not
> thread-safe (will be fixed too), unless you're using a per-request resource
> creation. So, once the big update is in, you  can do :
>
> YourXmlObject doIt(@Context HttpServletResponse response)
>
> which would be thread-safe even for singletons. Thread-local proxies for
> context fields will also be supported, not sure though I'll get a chance to
> fox it during the next week or so...
>
> Cheers, Sergey
>
>
> ----- Original Message ----- From: "Brad" <[EMAIL PROTECTED]>
> To: <[email protected]>
> Sent: Thursday, June 12, 2008 4:10 PM
> Subject: getting javax.servlet.http objects in JAX-RS
>
>
>> Hi all,
>>
>> could anyone tell me how to get the standard HTTP objects
>> HttpServletRequest and HttpServletResponset in a JAX-RS service
>> method?
>>
>> I'm particularly interested in being able to set the content-type
>> header on my response.
>>
>> Thanks,
>> Brad.
>
> ----------------------------
> IONA Technologies PLC (registered in Ireland)
> Registered Number: 171387
> Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland
>

Reply via email to