Java tutorial
/* * Copyright 2013 Robert Gacki <robert.gacki@cgi.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 de.otto.mongodb.profiler.web; import freemarker.ext.beans.BeansWrapper; import freemarker.ext.beans.ResourceBundleModel; import freemarker.ext.beans.StringModel; import freemarker.template.TemplateModel; import freemarker.template.TemplateModelException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.*; import static com.google.common.base.Preconditions.checkNotNull; public class I18nInterceptor extends HandlerInterceptorAdapter { private final String baseName; private final String modelName; public I18nInterceptor(final String baseName, final String modelName) { this.baseName = checkNotNull(baseName); this.modelName = checkNotNull(modelName); } @Override public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception { if (modelAndView == null) { return; } final Locale locale = request.getLocale(); final ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale); final ForgivingResourceBundleModel bundleModel = new ForgivingResourceBundleModel(bundle, BeansWrapper.getDefaultInstance()); modelAndView.addObject(modelName, bundleModel); } private static class ForgivingResourceBundleModel extends ResourceBundleModel { public ForgivingResourceBundleModel(ResourceBundle bundle, BeansWrapper wrapper) { super(bundle, wrapper); } @Override protected TemplateModel invokeGenericGet(Map keyMap, Class clazz, String key) throws TemplateModelException { try { return wrap(((ResourceBundle) object).getObject(key)); } catch (MissingResourceException e) { return wrap("???" + key + "???"); } } @Override public Object exec(List arguments) throws TemplateModelException { // Must have at least one argument - the key if (arguments.size() < 1) throw new TemplateModelException("No message key was specified"); // Read it Iterator it = arguments.iterator(); String key = unwrap((TemplateModel) it.next()).toString(); try { if (!it.hasNext()) { return wrap(((ResourceBundle) object).getObject(key)); } // Copy remaining arguments into an Object[] int args = arguments.size() - 1; Object[] params = new Object[args]; for (int i = 0; i < args; ++i) params[i] = unwrap((TemplateModel) it.next()); // Invoke format return new StringModel(format(key, params), wrapper); } catch (MissingResourceException e) { return wrap("???" + key + "???"); } catch (Exception e) { throw new TemplateModelException(e.getMessage()); } } } }