From 0cbd60bd1891596efa6c234cc66b5e429cd9fc2a Mon Sep 17 00:00:00 2001 From: Vitaly Litvak Date: Fri, 17 Apr 2015 23:20:55 +0300 Subject: [PATCH] For #141 #148 - set up locale cookie in special filter --- .../web/server/model/GuiceServletConfig.java | 1 + .../web/server/model/LocaleFilter.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/org/traccar/web/server/model/LocaleFilter.java diff --git a/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java b/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java index 52f5823d..831c0b5b 100644 --- a/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java +++ b/src/main/java/org/traccar/web/server/model/GuiceServletConfig.java @@ -57,6 +57,7 @@ protected void configureServlets() { install(new JpaPersistModule(persistenceUnit)); filter("/traccar/*").through(PersistFilter.class); + filter("/", "/traccar.html", "/m/", "/m/index.html").through(LocaleFilter.class); serve("/traccar/dataService").with(DataServiceImpl.class); serve("/traccar/uiStateService").with(UIStateServiceImpl.class); diff --git a/src/main/java/org/traccar/web/server/model/LocaleFilter.java b/src/main/java/org/traccar/web/server/model/LocaleFilter.java new file mode 100644 index 00000000..b1ae9eea --- /dev/null +++ b/src/main/java/org/traccar/web/server/model/LocaleFilter.java @@ -0,0 +1,53 @@ +/* + * Copyright 2015 Vitaly Litvak (vitavaque@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.web.server.model; + +import com.google.inject.persist.Transactional; +import org.traccar.web.shared.model.ApplicationSettings; + +import javax.inject.Inject; +import javax.inject.Provider; +import javax.inject.Singleton; +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 java.io.IOException; + +@Singleton +public class LocaleFilter implements Filter { + @Inject + Provider applicationSettings; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Transactional + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + ((HttpServletResponse) servletResponse).addCookie(new Cookie("GWT_LOCALE", applicationSettings.get().getLanguage())); + filterChain.doFilter(servletRequest, servletResponse); + } + + @Override + public void destroy() { + } +}