com.junly.service.helper.TicketHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.junly.service.helper.TicketHelper.java

Source

/*
 * ?(C) lijun2016-2020
 * Copyright 2016-2020 Zhejiang lijun Technology Co., Ltd.
 *  
 * This software is the confidential and proprietary information of
 * Zhejiang lijun Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Zhejiang lijun
 */
package com.junly.service.helper;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.junly.common.contants.ViewContants;
import com.junly.common.enums.HttpChannelType;

/** <p class="detail">
 * ??
 * </p>
 * @ClassName: TicketHelper 
 * @version V1.0  
 * @date 2017324 
 * @author junly
 * Copyright2016lijun.com,Inc.Allrightsreserved
 */
public class TicketHelper {

    /**
     * jar?? org.apache.commons.logging.*
     */
    private static final Log logger = LogFactory.getLog("HK-SESSION");

    /** <p class="detail">
    * cookie
    * </p>
    * @author junly
    * @date 2016422 
    * @param response
    * @param ticket    
    */
    public void setCookie(HttpServletRequest request, HttpServletResponse response, String ticket) {
        // ?
        Cookie cookie = new Cookie(ViewContants.LOGIN_TICKET_KEY, ticket);

        // ? ???
        cookie.setDomain(request.getServerName());
        // path
        cookie.setPath("/");
        // ??
        cookie.setMaxAge(ViewContants.TRUST_COOKIE_TIME); // 
        response.addCookie(cookie);
    }

    /** <p class="detail">
    * ???ticket
    * </p>
    * @author junly
    * @date 2017324 
    * @param request
    * @param httpChannelType
    * @return    
    */
    public String getTicket(HttpServletRequest request, HttpChannelType httpChannelType) {

        Cookie cookies[] = request.getCookies();

        String ticket = null;

        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (StringUtils.equals(ViewContants.LOGIN_TICKET_KEY, cookie.getName())) {

                    ticket = StringUtils.trim(cookie.getValue());

                    StringBuilder builder = new StringBuilder(httpChannelType.name());
                    builder.append("?cookieticket=").append(ticket);
                    builder.append("?=").append(request.getServerName()).append("====");
                    builder.append(cookie.getDomain());
                    logger.info(builder.toString());

                    break;
                }
            }
        }

        // java??cookieheader???,
        // ???cookie? ??
        if (StringUtils.isBlank(ticket)) {
            ticket = customHeadTicket(request, httpChannelType);
        }

        if (StringUtils.isBlank(ticket)) {
            ticket = StringUtils.trim(request.getParameter(ViewContants.LOGIN_TICKET_KEY));
        }

        return ticket;
    }

    /** <p class="detail">
    * ?cookie
    * </p>
    * @author junly
    * @date 2016420 
    * @param response
    * @param cookie    
    */
    public void deleteCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie != null) {
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }

    /** <p class="detail">
    * header ticket
    * </p>
    * @author junly
    * @date 2017324 
    * @param request
    * @param httpChannelType
    * @return    
    */
    private String customHeadTicket(HttpServletRequest request, HttpChannelType httpChannelType) {

        String ticket = request.getHeader(ViewContants.LOGIN_TICKET_KEY);

        if (StringUtils.isNotBlank(ticket)) {
            logger.info(httpChannelType.name() + ": header?ticket=" + ticket);
        }

        /*        Enumeration<?> headerNames = request.getHeaderNames();
            
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                logger.info(httpChannelType.name() + ": header: key=" + key + "; value="
                            + request.getHeader(key));
            }*/

        return StringUtils.trim(ticket);
    }

    /** <p class="detail">
    * Header?
    * </p>
    * @author junly
    * @date 201668 
    * @param request
    * @return    
    */
    /*    private String getHeadTicket(HttpServletRequest request, HttpChannelType httpChannelType) {
        
        String headerCookie = StringUtils.trim(request.getHeader(ViewContants.LOGIN_HEADER_COOKIE));
        
        if (StringUtils.isNotBlank(headerCookie)) {
            String[] cookies = StringUtils.split(headerCookie, ";");
            for (String cookie : cookies) {
                if (cookie.contains(ViewContants.LOGIN_TICKET_KEY)) {
                    String tickets[] = StringUtils.split(cookie, "=");
                    if (tickets.length == 2) {
                        logger.info(httpChannelType.name() + ": header?cookie-ticket="
                                    + tickets[1]);
                        return StringUtils.trim(tickets[1]);
                    }
                }
            }
        }
        return null;
    }*/

    /** <p class="detail">
    * app? request cookies
    * </p>
    * @author junly
    * @date 2016419 
    * @param request
    * @return    
    */
    /*    public Cookie getTicketCookie(HttpServletRequest request, HttpChannelType httpChannelType) {
        
        Cookie cookies[] = request.getCookies();
        
        Cookie cookie = null;
        
        if (null != cookies) {
            for (Cookie c : cookies) {
        
                if (!StringUtils.equals(ViewContants.LOGIN_TICKET_KEY, c.getName())) {
                    continue;
                }
        
                logger.info(httpChannelType.name() + ": cookie?ticket" + request.getServerName()
                            + "====" + c.getDomain());
                cookie = c;
        
                break;
            }
        }
        
        return cookie;
    }*/

}