Hi Charles,
You can inject HttpHeaders and use:
https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/HttpHeaders.html
#getAcceptableLanguages%28%29
to obtain the Accept-Language values.
The @Produces can only be used for media types, it cannot be used to
match the content language.
To match on language can use the variants support if you wish:
https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/Request.html
#selectVariant%28java.util.List%29
You can choose to do this in the resource method or in the message
body writer, arguably the latter might better suit your needs. For the
latter just be sure you set the header value before you write anything
to the output stream.
What will happen if an acceptable language is not supported?
Paul.
On May 27, 2010, at 3:18 AM, Charles Overbeck wrote:
> Hi,
>
> I'm writing my first MessageBodyWriter, one that produces PDFs. Our
> PDFs can be localized to various languages, so I want to use the
> HTTP Accept-Language header to determine the language that the PDF
> should be generated in.
>
> The MessageBodyWriter.writeTo() method has an HttpHeaders parameter,
> but it is the outbound headers, not the inbound ones.
>
> The only think I can think of is to set the Content-Language header
> in my annotated GET method, and then read the outbound header in my
> writeTo() method. It changes my resource code from this though:
>
> @GET
> @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
> public JAXBElement<InvoiceType> retrieveInvoice(@PathParam("id")
> Long id) {
> InvoiceType invoice = findInvoice(id);
> return OBJECT_FACTORY.createInvoice(invoice);
> }
>
> to:
>
> @Context
> protected HttpServletRequest httpRequest;
> ...
> @GET
> @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_JSON,
> "application/pdf"})
> public Response retrieveInvoice(@PathParam("id") Long id) {
> InvoiceType invoice = findInvoice(id);
> GenericEntity<JAXBElement<InvoiceType>> entity = new
> GenericEntity<JAXBElement<InvoiceType>>(
> invoice) {
> };
> return
> Response
> .status
> (Status.OK).entity(entity).header(HttpHeaders.CONTENT_LANGUAGE,
> determineContentLanguage(httpRequest).build();
> }
>
> I'm hoping I'm missing something, and that I can keep my first
> method, only having to change the @Produces.
>
> I'm using Jersey 1.5.1.1.
>
> Any help or thoughts appreciated in advance.
>
> Thanks,
>
> Charles