mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-23 06:06:11 +00:00
Bug 4032: Initial implementation of a filter that transfers a user's
preferred language using a Cookie
This commit is contained in:
@@ -28,6 +28,15 @@
|
||||
<url-pattern>/service/sailing</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<filter>
|
||||
<filter-name>LocaleInjectionFilter</filter-name>
|
||||
<filter-class>com.sap.sse.security.ui.server.LocaleInjectionFilter</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>LocaleInjectionFilter</filter-name>
|
||||
<url-pattern>*.html</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<servlet>
|
||||
<display-name>Dashboard Dispatch Service</display-name>
|
||||
<servlet-name>dashboardDispatch</servlet-name>
|
||||
|
||||
@@ -53,6 +53,15 @@
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<filter>
|
||||
<filter-name>LocaleInjectionFilter</filter-name>
|
||||
<filter-class>com.sap.sse.security.ui.server.LocaleInjectionFilter</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>LocaleInjectionFilter</filter-name>
|
||||
<url-pattern>*.html</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<servlet>
|
||||
<display-name>Sailing Service</display-name>
|
||||
<servlet-name>SailingService</servlet-name>
|
||||
|
||||
@@ -44,6 +44,15 @@
|
||||
<dispatcher>ERROR</dispatcher>
|
||||
</filter-mapping>
|
||||
|
||||
<filter>
|
||||
<filter-name>LocaleInjectionFilter</filter-name>
|
||||
<filter-class>com.sap.sse.security.ui.server.LocaleInjectionFilter</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>LocaleInjectionFilter</filter-name>
|
||||
<url-pattern>*.html</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<servlet>
|
||||
<display-name>User Management Service</display-name>
|
||||
<servlet-name>UserManagementService</servlet-name>
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.sap.sse.security.ui.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.util.tracker.ServiceTracker;
|
||||
|
||||
import com.sap.sse.security.SecurityService;
|
||||
import com.sap.sse.security.User;
|
||||
|
||||
/**
|
||||
* {@link Filter ServletFilter} implementation that sets a short living {@link Cookie} that carries a user's preferred
|
||||
* locale if one is logged in. This allows the GWT UI to be shown in the user's preferred language without the need to
|
||||
* rewrite the URL with the locale parameter added. This also ensures that shared URLs are locale-independent unless a
|
||||
* user manually selects a specific locale for a page.
|
||||
*/
|
||||
public class LocaleInjectionFilter implements Filter {
|
||||
private ServiceTracker<SecurityService, SecurityService> securityServiceTracker;
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
if (securityServiceTracker == null) {
|
||||
// It would be preferable to do this in init, but somehow, the context is sometimes null
|
||||
final BundleContext context = Activator.getContext();
|
||||
securityServiceTracker = new ServiceTracker<>(context, SecurityService.class, /* customizer */ null);
|
||||
securityServiceTracker.open();
|
||||
}
|
||||
|
||||
final HttpServletResponse resp = (HttpServletResponse) response;
|
||||
final Subject currentSubject = SecurityUtils.getSubject();
|
||||
if (currentSubject != null) {
|
||||
final Object principal = currentSubject.getPrincipal();
|
||||
if (principal instanceof String) {
|
||||
final SecurityService service = securityServiceTracker.getService();
|
||||
if (service != null) {
|
||||
final User user = service.getUserByName((String) principal);
|
||||
if (user != null) {
|
||||
final Locale preferredLocale = user.getLocale();
|
||||
if (preferredLocale != null) {
|
||||
final Cookie cookie = new Cookie(UserManagementConstants.LOCALE_COOKIE_NAME,
|
||||
preferredLocale.toLanguageTag());
|
||||
cookie.setMaxAge(-1);
|
||||
resp.addCookie(cookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
if (securityServiceTracker != null) {
|
||||
securityServiceTracker.close();
|
||||
securityServiceTracker = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.sap.sse.security.ui.server;
|
||||
|
||||
public interface UserManagementConstants {
|
||||
/**
|
||||
* Name of the Cookie to transfer a user's preferred language to the UI.
|
||||
*/
|
||||
public static final String LOCALE_COOKIE_NAME = "GWT_LOCALE";
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user