Package org.internna.iwebmvc.spring.mvc.interceptors

Source Code of org.internna.iwebmvc.spring.mvc.interceptors.ModelDecoratorInterceptor

/*
* Copyright 2002-2007 the original author or authors.
*
* 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.internna.iwebmvc.spring.mvc.interceptors;

import static org.springframework.util.StringUtils.hasText;

import java.util.List;
import java.util.Map;
import java.util.Locale;
import java.util.HashMap;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.crypto.Cipherer;
import org.internna.iwebmvc.spring.i18n.ContextLocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
* Inserts common objects into every view.
*
* @author Jose Noheda
* @since 1.0
*/
public class ModelDecoratorInterceptor extends HandlerInterceptorAdapter {

    public static final String SOUND_MANAGER = "soundManager";
    public static final String SYNTAX_HIGHLIGHTER = "syntaxhighlighter";
    public static final String DOJO_LOCATION = "dojoBase";
    public static final String CONTEXT_PATH = "contextPath";
    public static final String GOOGLE_API_KEY = "googleAPIKey";
    public static final String DEFAULT_CURRENCY = "defaultCurrency";
    public static final String CRUD_MAX_DEPTH_LEVEL = "maxDepthLevel";
    public static final String LOCALE_DATE_FORMAT = "localeDateFormat";

    protected Log logger = LogFactory.getLog(getClass());

    private Cipherer cipherer;
    private Integer crudMaxDepthLevel = 2;
    private List<Locale> supportedLocales;
    private boolean activateSyntaxHighlighter = false, activateSoundManager = false;
    private String dojoBase, googleAPIKey, servletContextPath, defaultCurrency = "EUR";

    @Required public final void setCipherer(Cipherer cipherer) {
        this.cipherer = cipherer;
    }

    public final void setDojoBase(String dojoBase) {
        this.dojoBase = dojoBase;
    }

    public final void setGoogleAPIKey(String googleAPIKey) {
        this.googleAPIKey = googleAPIKey;
    }

    public final void setDefaultCurrency(String defaultCurrency) {
        this.defaultCurrency = defaultCurrency;
    }

    public final void setCrudMaxDepthLevel(Integer crudMaxDepthLevel) {
        this.crudMaxDepthLevel = crudMaxDepthLevel;
    }

    public void setActivateSoundManager(boolean activateSoundManager) {
        this.activateSoundManager = activateSoundManager;
    }

    public void setActivateSyntaxHighlighter(boolean activateSyntaxHighlighter) {
        this.activateSyntaxHighlighter = activateSyntaxHighlighter;
    }

    public void setServletContextPath(String servletContextPath) {
        if (hasText(servletContextPath)) {
            this.servletContextPath = servletContextPath.startsWith("/") ? servletContextPath : "/" + servletContextPath;
            if (servletContextPath.endsWith("/")) this.servletContextPath = this.servletContextPath.substring(0, this.servletContextPath.length() - 1);
        } else this.servletContextPath = "";
    }

    @Required public final void setSupportedLocales(List<Locale> supportedLocales) {
        this.supportedLocales = supportedLocales;
    }

    private String getDateFormat(HttpServletResponse response) {
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, response.getLocale());
        String pattern = df instanceof SimpleDateFormat ? ((SimpleDateFormat) df).toPattern() : "dd/MM/yyyy";
        if ("MM/d/yy".equals(pattern)) pattern = "MM/dd/yyyy";
        if ("d/MM/yy".equals(pattern)) pattern = "dd/MM/yyyy";
        return pattern;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if ((modelAndView != null) && (modelAndView.getModelMap() != null)) {
            Map<String, Object> attributes = new HashMap<String, Object>(10);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + Cipherer.CIPHERER + "=" + cipherer + "]");
            attributes.put(Cipherer.CIPHERER, cipherer);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + DEFAULT_CURRENCY + "=" + defaultCurrency + "]");
            attributes.put(DEFAULT_CURRENCY, defaultCurrency);
            String dateFormat = getDateFormat(response);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + LOCALE_DATE_FORMAT + "=" + dateFormat + "]");
            attributes.put(LOCALE_DATE_FORMAT, dateFormat);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + ContextLocaleResolver.SUPPORTED_LOCALES + "=" + supportedLocales + "]");
            attributes.put(ContextLocaleResolver.SUPPORTED_LOCALES, supportedLocales);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + GOOGLE_API_KEY + "=" + googleAPIKey + "]");
            attributes.put(GOOGLE_API_KEY, googleAPIKey);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + DOJO_LOCATION + "=" + dojoBase + "]");
            attributes.put(DOJO_LOCATION, dojoBase);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + CONTEXT_PATH + "=" + servletContextPath + "]");
            attributes.put(CONTEXT_PATH, servletContextPath);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + CRUD_MAX_DEPTH_LEVEL + "=" + crudMaxDepthLevel + "]");
            attributes.put(CRUD_MAX_DEPTH_LEVEL, crudMaxDepthLevel);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + SOUND_MANAGER + "=" + activateSoundManager + "]");
            attributes.put(SOUND_MANAGER, activateSoundManager);
            if (logger.isDebugEnabled()) logger.debug("Populating view with [" + SYNTAX_HIGHLIGHTER + "=" + activateSyntaxHighlighter + "]");
            attributes.put(SYNTAX_HIGHLIGHTER, activateSyntaxHighlighter);
            modelAndView.getModelMap().addAllAttributes(attributes);
        }
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.mvc.interceptors.ModelDecoratorInterceptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.