All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yoojia.halo.HaloRequest Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.github.yoojia.halo;

import com.github.yoojia.halo.supports.Mapper;
import com.github.yoojia.halo.utils.ValueTypes;
import com.google.common.collect.Maps;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author YOOJIA.CHEN ([email protected])
 */
public class HaloRequest {

    public final long createTime;
    public final ServletContext servletContext;
    public final List resources;
    public final HttpServletRequest httpRequest;
    public final ServletRequest request;
    public final String uri;
    public final String method;
    public final Map userParams = Maps.newHashMap();

    final Map coreParams = Maps.newHashMap();

    HaloRequest(ServletRequest request) {
        createTime = System.nanoTime();
        this.request = request;
        this.httpRequest = (HttpServletRequest) request;
        this.servletContext = request.getServletContext();
        this.uri = httpRequest.getRequestURI();
        this.method = httpRequest.getMethod().toUpperCase(); // !! UpperCase
        this.resources = Mapper.fastParse(this.uri);
    }

    /**
     * 添加参数
     */
    public void putParam(String name, Object value){
        this.userParams.put(name, value);
    }

    /**
     * 获取指定字段名的参数值,返回字符串类型值。获取参数按以下顺序:
     *  - HTTP请求;
     *  - ExtraParams;
     */
    public final String getParam(String name) {
        String value = httpRequest.getParameter(name);
        if(null == value){
            value = (String) userParams.get(name);
        }
        return value;
    }

    /**
     * 获取指定字段名的参数值,返回字符串类型值。如果参数不存在,返回指定的默认值。获取参数按以下顺序:
     *  - HTTP请求;
     *  - ExtraParams;
     */
    public final  T getParam(String name, T defaultValue) {
        if(defaultValue == null) {
            throw new NullPointerException("Default value is NULL");
        }
        final String str1 = httpRequest.getParameter(name);
        final Class type = defaultValue.getClass();
        T value;
        if(str1 != null){
            value = ValueTypes.callValueOf(str1, type);
        }else{
            if( ! userParams.containsKey(name)){
                value = defaultValue;
            }else{
                final Object userValue = userParams.get(name);
                final String str2 = userValue == null ? null : userValue.toString();
                value = ValueTypes.callValueOf(str2, type);
            }
        }
        return value;
    }

    /**
     * 获取指定字段名的Header字段值
     */
    public final String getHeader(String name) {
        return httpRequest.getHeader(name);
    }

    /**
     * 获取指定字段名的Cookie对象
     */
    public final Cookie getCookieObject(String name) {
        final Cookie[] cookies = httpRequest.getCookies();
        if(cookies == null || cookies.length == 0) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals(name)) {
                return cookie;
            }
        }
        return null;
    }

    /**
     * 获取指定字段名的Cookie数值
     */
    public final String getCookieValue(String name){
        final Cookie cookie = getCookieObject(name);
        if(cookie != null) {
            return cookie.getValue();
        }else{
            return null;
        }
    }

    /**
     * 获取Session对象。如果Session不存在,返回null。
     */
    public final HttpSession getSession(){
        return httpRequest.getSession(false);
    }

    /**
     * 获取Session对象。如果Session不存在,则创建一个并返回。
     */
    public final HttpSession getSessionCreated(){
        return httpRequest.getSession(true);
    }

    /**
     * 获取Session字段值
     */
    @SuppressWarnings("unchecked")
    public final  T getSession(String name){
        final HttpSession session = getSession();
        if(session == null) {
            return null;
        }else{
            return (T) session.getAttribute(name);
        }
    }

    /**
     * 将参数保存到Session中
     */
    public final void putSession(String name, Object value){
        final HttpSession session = getSessionCreated();
        session.setAttribute(name, value);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy