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.base.Joiner;
import com.google.common.collect.Maps;

import javax.servlet.ServletContext;
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 final class HaloRequest {

    public final long createTime;
    public final ServletContext servletContext;
    public final List resources;
    public final HttpServletRequest httpRequest;
    public final String uri;
    public final String method;

    // HALOEngine access level
    final Map coreParams = Maps.newHashMap();
    final Map userParams = Maps.newHashMap();

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

    /**
     * 添加参数及数值到本次请求。在后续的处理器中可以获取这些数据。
     */
    public final void putParam(String name, Object value){
        this.userParams.put(name, value);
    }

    /**
     * 添加多个参数及数值到本次请求。在后续的处理器中可以获取这些数据。
     */
    public final void putAllParams(Map params) {
        this.userParams.putAll(params);
    }

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

    /**
     * 获取指定字段名的参数值,返回字符串类型值。如果参数不存在,返回指定的默认值。获取参数按以下顺序:
     *  - HTTP请求;
     *  - UserParams;
     */
    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;
    }

    /**
     * 获取全部参数
     */
    public final Map getAllParams() {
        final Map params = Maps.newHashMap();
        final Map httpParams = httpRequest.getParameterMap();
        for (Map.Entry entry : httpParams.entrySet()) {
            params.put(entry.getKey(), Joiner.on(',').join(entry.getValue()));
        }
        params.putAll(userParams);
        return params;
    }

    /**
     * 获取指定字段名的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){
        getSessionCreated().setAttribute(name, value);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy