com.sammyun.interceptor.MemberInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.sammyun.interceptor.MemberInterceptor.java

Source

/*
 * Copyright 2012-2014 sammyun.com.cn. All rights reserved.
 * Support: http://www.sammyun.com.cn
 * License: http://www.sammyun.com.cn/license
 */
package com.sammyun.interceptor;

import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.sammyun.Principal;
import com.sammyun.entity.Member;
import com.sammyun.service.MemberService;
import com.sammyun.util.HttpRequestDeviceUtils;

/**
 * Interceptor - ??
 * 
 * @author Sencloud Team
 * @version 3.0
 */
public class MemberInterceptor extends HandlerInterceptorAdapter {

    /** ????? */
    private static final String REDIRECT_VIEW_NAME_PREFIX = "redirect:";

    /** "??URL"??? */
    private static final String REDIRECT_URL_PARAMETER_NAME = "redirectUrl";

    /** ""?? */
    private static final String MEMBER_ATTRIBUTE_NAME = "member";

    /** URL */
    private static final String DEFAULT_LOGIN_URL = "/login.ct";

    private static final String DEFAULT_MOBILE_LOGIN_RUL = "/mobile/login/index.ct";

    /** URL */
    private String loginUrl = DEFAULT_LOGIN_URL;

    @Value("${url_escaping_charset}")
    private String urlEscapingCharset;

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        Principal principal = (Principal) session.getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
        if (HttpRequestDeviceUtils.isMobileDevice(request)) {
            loginUrl = DEFAULT_MOBILE_LOGIN_RUL;
        } else {
            //2014120812:02:08Bug fix ?waploginUrl????DEFAULT_MOBILE_LOGIN_RUL
            loginUrl = DEFAULT_LOGIN_URL;
        }
        if (principal != null) {
            return true;
        } else {
            String requestType = request.getHeader("X-Requested-With");
            if (requestType != null && requestType.equalsIgnoreCase("XMLHttpRequest")) {
                response.addHeader("loginStatus", "accessDenied");
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return false;
            } else {
                if (request.getMethod().equalsIgnoreCase("GET")) {
                    String redirectUrl = request.getQueryString() != null
                            ? request.getRequestURI() + "?" + request.getQueryString()
                            : request.getRequestURI();
                    response.sendRedirect(request.getContextPath() + loginUrl + "?" + REDIRECT_URL_PARAMETER_NAME
                            + "=" + URLEncoder.encode(redirectUrl, urlEscapingCharset));
                } else {
                    response.sendRedirect(request.getContextPath() + loginUrl);
                }
                return false;
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {
            String viewName = modelAndView.getViewName();
            if (!StringUtils.startsWith(viewName, REDIRECT_VIEW_NAME_PREFIX)) {
                modelAndView.addObject(MEMBER_ATTRIBUTE_NAME, memberService.getCurrent());
            }
        }
    }

    /**
     * ?URL
     * 
     * @return URL
     */
    public String getLoginUrl() {
        return loginUrl;
    }

    /**
     * URL
     * 
     * @param loginUrl
     *            URL
     */
    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

}