How to hide request parameter value in url using Spring MVC - java

I'm working on language changes for my page.when the client clicks on any language, page should load in that particular language. For that I configured springs.xml like this
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
The page was loading fine, when I'm give the url like this, site/companykey?lang=french,
Now my problem is , I want to hide the param value which I have given ?lang=french.
Is there any way to hide that value from the url ??

locate the form in jsp/html page in which you have included these tags used for changing locale. change the attribute method="POST". it will make sure that the parameter will pass to the mapped action class without appearing in the url.

Related

How to add a value from a cookie to the HTTP header in Spring WebServiceTemplate?

I am using Spring WebServiceTemplate in my client side code to send request to an existing 3rd party web service.
<bean id="vehicleQuotationWebServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
<property name="marshaller" ref="vehicleQuotationMarshaller" />
<property name="unmarshaller" ref="vehicleQuotationMarshaller" />
<property name="faultMessageResolver" ref="vehicleServiceClientFaultMessageResolver" />
<property name="defaultUri" value="http://localhost:8080/quote/endpoints"/>
</bean>
Everything was working fine until they added security check from the server side. Right now, in order to pass the server side security authenication, I need to pass some values from a cookie to the server. This I can do easily in SoapUI by modifying the http header (adding the cookie's value there), but my question is how can I do it in Java code with the Spring's WebServiceTemplate?
You can extend the WebServiceTemplate or in a more easy way use a custom sender who extends Spring's
org.springframework.ws.transport.http.CommonsHttpMessageSender
and set in your bean definition
<bean id="vehicleQuotationWebServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
<property name="marshaller" ref="vehicleQuotationMarshaller" />
<property name="unmarshaller" ref="vehicleQuotationMarshaller" />
<property name="faultMessageResolver" ref="vehicleServiceClientFaultMessageResolver" />
<property name="defaultUri" value="http://localhost:8080/quote/endpoints"/>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.MyHttpComponentsMessageSender"/>
</property>
</bean>
take a look at Spring forums
JSESSIONID and setting cookie for WebServiceTemplate

JSP Spring internationalization using OSGi as service changing locale not working properly

First of all! Don't judge me the reason that I'm using MessageSource as a service. Since I'm in a phase learning OSGi and Spring.
I have a project that has many modules, in their pages, since I'm making internationalization for it. I saw that they use the same messages, so I put the codes in a common module that every module uses it. And I shared the message as a service osgi-context.xml:
<osgi:service ref="messageSource" interface="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
<osgi:service ref="localeResolver" interface="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<osgi:service ref="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
and in module-context.xml the beans:
<bean id="messageSource" scope="bundle" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" scope="bundle"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="et" />
</bean>
<bean id="localeChangeInterceptor" scope="bundle"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
in the module that uses the service:
<osgi:reference id="messageSource" interface="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
<osgi:reference id="localeResolver" interface="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<osgi:reference id="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
So internationalization works! But not completely... the problem comes when I try to change the locale, It partially works. The jsp pages where I use tag message like:
<spring:message code="general.welcome"/>
It does not change! But in the same time I pass some translations using a Controller to a JavaScript var like:
//Some page.jsp
<script>
translations = ${translations == null? '{}' : translations};
</script>
Since the controllers are wired to the messageSource:
#Autowired
MessageSource messageSource;
...
//the way that the request is returned by a method
//A map in JSON using messageSource is return
model.addAttribute("translations", someJSONmap);
It's working!
So in the controller the locale change is working, but in the JSP pages it isn't.
Do anyone know what I am missing? Or how to fix it?
Thanks for reading until here and sorry for the long question.
The problem was solved by removing the service:
module-context.xml:
<bean id="localeChangeInterceptor" scope="bundle"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
osgi-context.xml:
<osgi:service ref="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
and put it into the module, which is using the service, applicationContext.xml:
<mvc:interceptors>
...
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>

Is My Spring-MVC ContentNegotiatingViewResolver setup correctly? How can I send 404 error for unsupported media types?

I dunno if this is a valid question for this site, but I was wondering if someone experienced with the ContentNegotiatingViewResolver could look this over and let me know if I set it up correctly and well as help me send 404 messages.
What I'd like to do is make all urls with no extension default to the HTML representation (which is freemarker views in my case). I'd like to accept urls with ".json" appended to them to render json instead. This appears to work in firefox, ie and chrome. I'm guessing it works in other browsers? I made sure to disable the accept header because it's an evil feature that doesn't really work like the documentation says it does.
I have tried to access urls with ".stuff", just to see what happens, and with my configuration, a blank screen happens. Is this acceptable? Is there any way I can send a 404 error?
Is there anything else that I may have not configured properly?
<bean id="contentNegotiatingViewResolver"
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="useNotAcceptableStatusCode" value="true" />
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="application/json" />
</bean>
</list>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html" />
<property name="order" value="2"/>
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
</list>
</property>
</bean>
Because you have defaultContentType set, the negotiation always ends up finding a matching content type delivered by the freemarker view. A quote from the javadoc of ContentNegotiatingViewResolver:
You can also set the setDefaultContentType directly, which will be
returned when the other mechanisms (Accept header, file extension or
parameter) do not result in a match.
With this setting, file extension .stuff matches contentType text/html.
Then, with useNotAcceptableStatusCode:
406 (Not Acceptable) status code will be returned if no match is
found.
I just tried this (with the settings of another REST service app) and saw Chrome showing the message: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers().
Did you add url-pattern for ".stuff" to web.xml? I am using PathExtensionContentNegotiationStrategy, but should be same reason. Because spring servlet can not response this request, so got 404 error, not 500 or 416. If is 416, it should caused some header, can fixed by change jQuery seeting or http client header.

LocaleResolver in Spring

I am using session locale resolver for my application. I am showing the languages in dropdown. if the user selects on any of the language then repopulates all values from that language.
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="languageCode" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
But its is not reading it from the session. Always it considers the defaultlanguage from browser setting.
Please help on this.
You need to obtain the locale in this way:
Locale loc=RequestContextUtils.getLocale(request);
In Spring 4.0 we can Also use LocaleContextResolver.getLocale() method as well.

Spring MVC web app i18n

In a web application written using spring-MVC, I want to allow users to change the current language by clicking on a link which text is the name of the language.
I have already set up a messageSource and made all my jsp pages find the messages using this messageSource. Currently, the language is changing depending on the locale of the user browser.
So, what I want to do now is to allow to change the locale manually.
I have found that the class SessionLocaleResolver could help, but I do not know how to set it up in my application context file (which name is myAppName-servlet.xml) .
I have defined the bean :
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
But in which bean should I plug this ? Furthermore, how do I set a cookie related to locale into an user session ?
All informations I needed were in the documentation, in front of me, at :
http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-localeresolver
In brief, I adapted the following xml to myAppName-servlet.xml
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLanguage"/>
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="mappings">
<value>/**/*.view=someController</value>
</property>
</bean>
And now, it suffices to access any page with the parameter :
siteLanguage=locale
to change the locale for the whole site.
For example : http://localhost:8080/SBrowser/deliveries.html?siteLanguage=frenter code here

Categories

Resources