This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes Override request headers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Override request headers" Watch "Override request headers" New topic
Author

Override request headers

Jose Berardo
Greenhorn

Joined: Oct 28, 2010
Posts: 9
Is it possible to override a request header?

Is it possible to change browser's Accept-Language header to another one, built by any bean?

Jose Berardo
Greenhorn

Joined: Oct 28, 2010
Posts: 9
I've tried this:



But it throws an UnsupportedOperationException. Of course, this map is immutable.

Sorry about opening another thread, but the problem still the same. How to make components, like graphicImage, load resources using localePrefix that was not supplied by Accept-Language.

See this thread please: http://www.coderanch.com/t/515506/JSF/java/problems-JSF

I don't believe that I have much knowledge to think that it's a bug. I prefer to believe that I'm on a wrong way.

Anybody could help me?

I'm been crazy or is it a bug?
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
getResponse().setCharacterEncoding()? just an idea. What are you trying to do?
Jose Berardo
Greenhorn

Joined: Oct 28, 2010
Posts: 9
Thank you Brendan,

But I don't see how it can help me.
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
My apologies Jose, I wasn't very helpful. Please can you give us a high level description of the
issue you are trying to solve? Are you attempting to restrict the browser language preferences?
That is not the way to go. The server says "this is what I've got" and the browser says "this is
what I want". It works well. Please explain further.

Jose Berardo
Greenhorn

Joined: Oct 28, 2010
Posts: 9
(my mom always tell me that I have to study more english =)

I need to localize resources (I am doing this now) but I need to change the resource's localePrefix when the user picks up the choice on selectOneMenu.

Let me show you my codes:

faces-config.xml


There are several locales and one resource-bundle and one message-bundle. There is no problem with resource bundle. Messages like #{mens.someMessage} always be shown localized.

There are several localized files for bundles.mensagemsistema. One for each supported-locale.
Inside each file there is an entry:

When the browser sends Accept-Language: en; My messages appear in english and my images too.
When the browser sends Accept-Language: pt_BR; My messages appear in portuguese and my images too.

When the user chooses a locale from a selectOneMenu, he fires on the server a valueChangeListener which calls
It works, my messages change to the locale picked by the user. But my images don't.

For any reason that don't know, the message-bundle tag choose the localized version of bundles.mensagenssistema based only on Accept-Language sent by the browser. Any changes on ViewRoot (or the locale attribute of a f:view tag) affect this behavior.

I need to drive for one of two ways. Find how ViewRoot's changes may affect the search of localized version of my message-bundle, which has the localePrefix entry (the best way I think). Or be a man in the middle to change the Accept-Language before it came to the server's search (a very ugly approach I realize).

I was clear?
Can you help me?
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218

Hi, thanks for the very clear explanation. I don't use setLocale() myself, instead using the browser
settings, but I just gave it a try and it seems to work fine for me with Mojarra 2.0.3.

Are you storing the image path in the resource bundle(s) and then displaying with h:graphicImage?
presumably after callling setLocale() you need to reload the current page for any changes to take
effect or use ajax.

What should be impossible is for the application to be using two different resource bundle files at
once. Are you actually seeing, for example, a page containing portuguese text and an image that
is specific to the english variant (or a similar combination).

I can only suggest that you post a few more lines on here showing how you display an image and
the corresponding entry from the resource bundle. It's got to be something like that.

Regards,
Brendan.
Jose Berardo
Greenhorn

Joined: Oct 28, 2010
Posts: 9
Hum...

I'm using Glassfish with Mojarra 2.0.2. I will try to update to 2.0.3 and test again.

But, for now, that's my codes:

faces-config.xml


bundles.mensagenssistema_pt_BR.properties


bundles.mensagenssistema_en.properties


myview.xhtml (form and message that work fine after changes)


... myview.xhtml (images and commandButtons)


GeralBean.java


As you can see, I have <resource-bundle> and <message-bundle> tags at faces-config.

When the browser sends this Accept-Language: en,en-us;q=0.8,pt-br;q=0.5,es;q=0.3
It show #{mens.escolha} as an english text and show images from resources/ingles/imagens/.

When I change the browser to make it send this Accept-Language: pt-br,en;q=0.8,en-us;q=0.5,es;q=0.3
It show #{mens.escolha} as a portuguese text and show images from resources/portugues/imagens/.

The same occurs with all of the other languages.

When the user picks a choice through the selectOneMenu, only #{mens.escolha} (and eventually other messages) is translated to the new language. The images stay on the same language even after page refresh. To change the language I just need to change the accept-language, but I don't believe that my users will know how to do that.

Fix me if I'm wrong, but I believe that the locale used by the ViewRoot, which controls the resource-bundles to write down the page is different than the locale used by the message-bundles which loads the javax.faces.resource.localePrefix to get the external resources.

The source codes of the view sent to the browser bring tags like this:



See loc=portugues. It occurs even when the ViewRoot's locale is "en".

What am I still doing wrong?

Ps.: Brendan, really thank you for your attention.
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
Ok, I'm on your wavelength now, there are so many different ways of doing all this stuff it's
enough to drive you mad.

It looks like you're doing everything right, but, does the value change listener actually fire
when the user selects the locale in the drop down? If you're not sure can you put some
debug code in there to check.

Why not use this approach: add an entry to each of your localised resource bundle file like
this:

imagesLibrary=ingles/images

Then access the images using h:graphicImage name="salvar.png" library={mens.imagesLibrary}.

I'm sure this will work and the added benefit is that the mechanism you are presently using
has no fallback mechanism, i.e. if it can't find a "de_at" variant it won't try to use "de" if the
first choice can't be found.

As for displaying images on your buttons, I just use commandLink with a child graphicImage
tag. No doubt there are 73 other ways of doing this, but this works for me.

Regards,
Brendan.

p.s. a couple of edits, written in a hurry!
Jose Berardo
Greenhorn

Joined: Oct 28, 2010
Posts: 9
Thank you again, Brendan.

I agree with you. I know that are so many ways to solve this problem.

The question is that I am a Java teacher and this issue was threw at one class. So I need to know if this "recomended" way to internationalize images and other resources really works.

IMHO, if this doesn't work, all the new resource management, where we have to save resources at resources folder inside another folder referred by javax.faces.resources.localePrefix, is put on checkmate.

If anybody still doesn't understand the big question ... The problem is that the server only checks the accept-language browser's header. It never checks the ViewRoot's locale. In other words, now we can not create an artifact to give the user a chance to modify the language of resources like images (graphicImage, commandButtons, etc) and others (outputScripts, outputStyleSheets) at the same way he could modify the messages of the view loaded by resource-bundle faces-config's tag.

I'd never studied the JSF bakstage before, so of course I can be wrong, but I think that I solved this problem. Reading the Mojarra's source code, I found a class called MultiViewHandler, which has a method called calculateLocale. It is used by the server to get the locale to pick the localized file referred at message-bundle. It does not call View Handler. Look:



I've create a new attribute (why not call it locale) and used it to get the ViewRoot's locale if it was defined.

Only this change makes me happy. All of this stuff is working fine. Resource-bundles and now Message-bundles are localized based on http header and user inputs.

If I am wrong, please sorry for that, but if I am right, how can I give my little contribution?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Override request headers
 
Similar Threads
Filter question
Date or Time Info from the Request Header
set header and getheader
About overriding
Adding http headers