Wednesday 31 October 2007

GWT and I18n, authorization on the server/in a service

I've read various posts on how to internationalize text in a gwt service - these usually recommend adding the locale as a string in a constants file and passing this as an extra parameter to each service call.

The approach I've adopted is to override the service method from HttpServlet and use this to make the request available by storing it in a class variable. Once you've got the request then you can use it to read the locale and therefore avoid the mucking about with your properties files and method parameters.
This technique is also quite useful because it allows you access to the session which means that you can get hold of user information which is stored in the session

Also as an aside it appears that the IE operation aborted problem should be fixed in 1.4.61, having said that it also can't hurt to move the inclusion to the end of the page



public abstract class GwtRpcListener extends RemoteServiceServlet {
private HttpServletRequest m_request;
private HttpServletResponse m_response;
/**
* @return Returns the response.
*/
public HttpServletResponse getResponse() {
return m_response;
}
/**
* @param response The response to set.
*/
public void setResponse(HttpServletResponse response) {
m_response = response;
}
/**
* @return Returns the request.
*/
public HttpServletRequest getRequest() {
return m_request;
}
/**
* @param request The request to set.
*/
public void setRequest(HttpServletRequest request) {
m_request = request;
}

/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#service (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)throws ServletException, IOException {
setRequest(arg0);
setResponse(arg1);
super.service(arg0, arg1);
}
}