![JAR search and dependency download from the Maven repository](/logo.png)
com.github.yoojia.next.NextRequest Maven / Gradle / Ivy
package com.github.yoojia.next;
import com.github.yoojia.next.lang.Joiner;
import com.github.yoojia.next.lang.Primitives;
import com.github.yoojia.next.lang.Text;
import com.github.yoojia.next.utils.URIs;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author YOOJIA.CHEN ([email protected])
*/
public final class NextRequest {
public final long createTime;
public final ServletContext servletContext;
public final List resources;
public final HttpServletRequest httpRequest;
public final String uri;
public final String contextPath;
public final String method;
final HashMap coreParams = new HashMap<>();
final HashMap userParams = new HashMap<>();
NextRequest(HttpServletRequest request) {
this.createTime = System.nanoTime();
this.httpRequest = request;
this.servletContext = request.getServletContext();
this.method = httpRequest.getMethod().toUpperCase(); // !! UpperCase
this.contextPath = request.getContextPath();
// Request path should exclude context path
final String uri = httpRequest.getRequestURI();
if (Text.isEmpty(contextPath)) {
this.uri = uri;
}else {
this.uri = uri.substring(contextPath.length());
}
this.resources = URIs.fastParseURL(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();
final T value;
if(str1 != null){
value = Primitives.valueOf(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 = Primitives.valueOf(str2, type);
}
}
return value;
}
/**
* 获取全部参数
*/
public final Map getAllParams() {
final Map params = new HashMap<>();
final Map httpParams = httpRequest.getParameterMap();
final Joiner joiner = new Joiner(",");
for (Map.Entry entry : httpParams.entrySet()) {
params.put(entry.getKey(), joiner.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);
}
/**
* 获取客户端推送的数据
*/
public final String getDataThrows() throws IOException {
final BufferedReader reader = httpRequest.getReader();
final StringBuilder output = new StringBuilder();
final char[] buffer = new char[4 * 1024];
int read;
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
return output.toString();
}
/**
* 获取客户端推送的数据,如果为空,返回设置的默认值
*/
public final String getData(String defaultValue) {
try {
final String ret = getDataThrows();
return ret.isEmpty() ? defaultValue : ret;
} catch (IOException e) {
return defaultValue;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy