This commit is contained in:
@@ -224,6 +224,12 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
super.removeProperty(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端地址IP, 与getRemoteAddres() 的区别在于:本方法优先取header中指定为RemoteAddress名的值,没有则返回getRemoteAddres()的getHostAddress()。
|
||||
* 本方法适用于服务前端有如nginx的代理服务器进行中转,通过getRemoteAddres()是获取不到客户端的真实IP。
|
||||
*
|
||||
* @return 地址
|
||||
*/
|
||||
public String getRemoteAddr() {
|
||||
if (remoteAddrHeader != null) {
|
||||
String val = getHeader(remoteAddrHeader);
|
||||
@@ -235,18 +241,29 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
return String.valueOf(addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求内容指定的编码字符串
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getBody(final Charset charset) {
|
||||
return array.toString(charset);
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return array.toString();
|
||||
return charset == null ? array.toString() : array.toString(charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求内容的UTF-8编码字符串
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getBodyUTF8() {
|
||||
return array.toString(UTF8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端地址IP
|
||||
*
|
||||
* @return 地址
|
||||
*/
|
||||
public SocketAddress getRemoteAddress() {
|
||||
return this.channel.getRemoteAddress();
|
||||
}
|
||||
@@ -260,10 +277,21 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
+ ", host:" + this.host + ", params:" + this.params + ", header:" + this.header + "}";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件上传信息列表
|
||||
*
|
||||
* @return 文件上传对象集合
|
||||
* @throws IOException
|
||||
*/
|
||||
public final Iterable<MultiPart> multiParts() throws IOException {
|
||||
return getMultiContext().parts();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件上传对象
|
||||
*
|
||||
* @return 文件上传对象
|
||||
*/
|
||||
public final MultiContext getMultiContext() {
|
||||
return new MultiContext(context.getCharset(), this.getContentType(), this.params,
|
||||
new BufferedInputStream(Channels.newInputStream(this.channel), Math.max(array.count(), 8192)) {
|
||||
@@ -295,6 +323,12 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
super.recycle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sessionid
|
||||
*
|
||||
* @param create 无sessionid是否自动创建
|
||||
* @return sessionid
|
||||
*/
|
||||
public String getSessionid(boolean create) {
|
||||
String sessionid = getCookie(SESSIONID_NAME, null);
|
||||
if (create && (sessionid == null || sessionid.isEmpty())) {
|
||||
@@ -304,24 +338,50 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
return sessionid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新sessionid
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String changeSessionid() {
|
||||
this.newsessionid = ((HttpContext) context).createSessionid();
|
||||
this.newsessionid = context.createSessionid();
|
||||
return newsessionid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使sessionid失效
|
||||
*/
|
||||
public void invalidateSession() {
|
||||
this.newsessionid = ""; //为空表示删除sessionid
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有Cookie对象
|
||||
*
|
||||
* @return cookie对象数组
|
||||
*/
|
||||
public HttpCookie[] getCookies() {
|
||||
if (this.cookies == null) this.cookies = parseCookies(this.cookiestr);
|
||||
return this.cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Cookie值
|
||||
*
|
||||
* @param name cookie名
|
||||
* @return cookie值
|
||||
*/
|
||||
public String getCookie(String name) {
|
||||
return getCookie(name, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Cookie值, 没有返回默认值
|
||||
*
|
||||
* @param name cookie名
|
||||
* @param dfvalue 默认cookie值
|
||||
* @return cookie值
|
||||
*/
|
||||
public String getCookie(String name, String dfvalue) {
|
||||
for (HttpCookie cookie : getCookies()) {
|
||||
if (name.equals(cookie.getName())) return cookie.getValue();
|
||||
@@ -345,18 +405,38 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
return cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Connection的Header值
|
||||
*
|
||||
* @return Connection
|
||||
*/
|
||||
public String getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求方法 GET、POST等
|
||||
*
|
||||
* @return method
|
||||
*/
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取协议名 http、https、ws、wss等
|
||||
*
|
||||
* @return protocol
|
||||
*/
|
||||
public String getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Host的Header值
|
||||
*
|
||||
* @return Host
|
||||
*/
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
@@ -395,6 +475,15 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
return requestURI.substring(requestURI.indexOf(prefix) + prefix.length() + (prefix.endsWith("/") ? 0 : 1)).split("/");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求URL分段中含prefix段的值
|
||||
* 例如请求URL /pipes/record/query/name:hello
|
||||
* 获取name参数: String name = request.getRequstURIPath("name:", "none");
|
||||
*
|
||||
* @param prefix prefix段前缀
|
||||
* @param defvalue 默认值
|
||||
* @return prefix截断后的值
|
||||
*/
|
||||
public String getRequstURIPath(String prefix, String defvalue) {
|
||||
if (requestURI == null || prefix == null) return defvalue;
|
||||
int pos = requestURI.indexOf(prefix);
|
||||
@@ -404,121 +493,324 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
return pos < 0 ? sub : sub.substring(0, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求URL分段中含prefix段的short值
|
||||
* 例如请求URL /pipes/record/query/type:10
|
||||
* 获取type参数: short type = request.getRequstURIPath("type:", (short)0);
|
||||
*
|
||||
* @param prefix prefix段前缀
|
||||
* @param defvalue 默认short值
|
||||
* @return short值
|
||||
*/
|
||||
public short getRequstURIPath(String prefix, short defvalue) {
|
||||
String val = getRequstURIPath(prefix, null);
|
||||
return val == null ? defvalue : Short.parseShort(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求URL分段中含prefix段的int值
|
||||
* 例如请求URL /pipes/record/query/page:2/size:50
|
||||
* 获取page参数: int page = request.getRequstURIPath("page:", 1);
|
||||
* 获取size参数: int size = request.getRequstURIPath("size:", 20);
|
||||
*
|
||||
* @param prefix prefix段前缀
|
||||
* @param defvalue 默认int值
|
||||
* @return int值
|
||||
*/
|
||||
public int getRequstURIPath(String prefix, int defvalue) {
|
||||
String val = getRequstURIPath(prefix, null);
|
||||
return val == null ? defvalue : Integer.parseInt(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求URL分段中含prefix段的long值
|
||||
* 例如请求URL /pipes/record/query/time:1453104341363/id:40
|
||||
* 获取time参数: long time = request.getRequstURIPath("time:", 0L);
|
||||
*
|
||||
* @param prefix prefix段前缀
|
||||
* @param defvalue 默认long值
|
||||
* @return long值
|
||||
*/
|
||||
public long getRequstURIPath(String prefix, long defvalue) {
|
||||
String val = getRequstURIPath(prefix, null);
|
||||
return val == null ? defvalue : Long.parseLong(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求的URL
|
||||
*
|
||||
* @return 请求的URL
|
||||
*/
|
||||
public String getRequestURI() {
|
||||
return requestURI;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求内容的长度, 为-1表示内容长度不确定
|
||||
*
|
||||
* @return 内容长度
|
||||
*/
|
||||
public long getContentLength() {
|
||||
return contentLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Content-Type的header值
|
||||
*
|
||||
* @return contentType
|
||||
*/
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* 获取所有的header名
|
||||
*
|
||||
* @return header名数组
|
||||
*/
|
||||
public String[] getHeaderNames() {
|
||||
return header.getNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header值
|
||||
*
|
||||
* @param name header名
|
||||
* @return header值
|
||||
*/
|
||||
public String getHeader(String name) {
|
||||
return header.getValue(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的json值
|
||||
*
|
||||
* @param clazz 反序列化的类名
|
||||
* @param name header名
|
||||
* @return header值
|
||||
*/
|
||||
public <T> T getJsonHeader(Class<T> clazz, String name) {
|
||||
String v = getHeader(name);
|
||||
return v == null || v.isEmpty() ? null : jsonConvert.convertFrom(clazz, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的json值
|
||||
*
|
||||
* @param convert JsonConvert对象
|
||||
* @param clazz 反序列化的类名
|
||||
* @param name header名
|
||||
* @return header值
|
||||
*/
|
||||
public <T> T getJsonHeader(JsonConvert convert, Class<T> clazz, String name) {
|
||||
String v = getHeader(name);
|
||||
return v == null || v.isEmpty() ? null : convert.convertFrom(clazz, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的boolean值, 没有返回默认boolean值
|
||||
*
|
||||
* @param name header名
|
||||
* @param defaultValue 默认boolean值
|
||||
* @return header值
|
||||
*/
|
||||
public boolean getBooleanHeader(String name, boolean defaultValue) {
|
||||
return header.getBoolValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的short值, 没有返回默认short值
|
||||
*
|
||||
* @param name header名
|
||||
* @param defaultValue 默认short值
|
||||
* @return header值
|
||||
*/
|
||||
public short getShortHeader(String name, short defaultValue) {
|
||||
return header.getShortValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的int值, 没有返回默认int值
|
||||
*
|
||||
* @param name header名
|
||||
* @param defaultValue 默认int值
|
||||
* @return header值
|
||||
*/
|
||||
public int getIntHeader(String name, int defaultValue) {
|
||||
return header.getIntValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的long值, 没有返回默认long值
|
||||
*
|
||||
* @param name header名
|
||||
* @param defaultValue 默认long值
|
||||
* @return header值
|
||||
*/
|
||||
public long getLongHeader(String name, long defaultValue) {
|
||||
return header.getLongValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的float值, 没有返回默认float值
|
||||
*
|
||||
* @param name header名
|
||||
* @param defaultValue 默认float值
|
||||
* @return header值
|
||||
*/
|
||||
public float getFloatHeader(String name, float defaultValue) {
|
||||
return header.getFloatValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header的double值, 没有返回默认double值
|
||||
*
|
||||
* @param name header名
|
||||
* @param defaultValue 默认double值
|
||||
* @return header值
|
||||
*/
|
||||
public double getDoubleHeader(String name, double defaultValue) {
|
||||
return header.getDoubleValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的header值, 没有返回默认值
|
||||
*
|
||||
* @param name header名
|
||||
* @param defaultValue 默认值
|
||||
* @return header值
|
||||
*/
|
||||
public String getHeader(String name, String defaultValue) {
|
||||
return header.getValue(name, defaultValue);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* 获取所有参数名
|
||||
*
|
||||
* @return 参数名数组
|
||||
*/
|
||||
public String[] getParameterNames() {
|
||||
parseBody();
|
||||
return params.getNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @return 参数值
|
||||
*/
|
||||
public String getParameter(String name) {
|
||||
parseBody();
|
||||
return params.getValue(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数json值
|
||||
*
|
||||
* @param clazz 反序列化的类名
|
||||
* @param name 参数名
|
||||
* @return 参数值
|
||||
*/
|
||||
public <T> T getJsonParameter(Class<T> clazz, String name) {
|
||||
String v = getParameter(name);
|
||||
return v == null || v.isEmpty() ? null : jsonConvert.convertFrom(clazz, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数json值
|
||||
*
|
||||
* @param convert JsonConvert对象
|
||||
* @param clazz 反序列化的类名
|
||||
* @param name 参数名
|
||||
* @return 参数值
|
||||
*/
|
||||
public <T> T getJsonParameter(JsonConvert convert, Class<T> clazz, String name) {
|
||||
String v = getParameter(name);
|
||||
return v == null || v.isEmpty() ? null : convert.convertFrom(clazz, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数boolean值, 没有返回默认boolean值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认boolean值
|
||||
* @return 参数值
|
||||
*/
|
||||
public boolean getBooleanParameter(String name, boolean defaultValue) {
|
||||
parseBody();
|
||||
return params.getBoolValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数short值, 没有返回默认short值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认short值
|
||||
* @return 参数值
|
||||
*/
|
||||
public short getShortParameter(String name, short defaultValue) {
|
||||
parseBody();
|
||||
return params.getShortValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数int值, 没有返回默认int值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认int值
|
||||
* @return 参数值
|
||||
*/
|
||||
public int getIntParameter(String name, int defaultValue) {
|
||||
parseBody();
|
||||
return params.getIntValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数long值, 没有返回默认long值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认long值
|
||||
* @return 参数值
|
||||
*/
|
||||
public long getLongParameter(String name, long defaultValue) {
|
||||
parseBody();
|
||||
return params.getLongValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数float值, 没有返回默认float值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认float值
|
||||
* @return 参数值
|
||||
*/
|
||||
public float getFloatParameter(String name, float defaultValue) {
|
||||
parseBody();
|
||||
return params.getFloatValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数double值, 没有返回默认double值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认double值
|
||||
* @return 参数值
|
||||
*/
|
||||
public double getDoubleParameter(String name, double defaultValue) {
|
||||
parseBody();
|
||||
return params.getDoubleValue(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的参数值, 没有返回默认值
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认值
|
||||
* @return 参数值
|
||||
*/
|
||||
public String getParameter(String name, String defaultValue) {
|
||||
parseBody();
|
||||
return params.getValue(name, defaultValue);
|
||||
|
||||
Reference in New Issue
Block a user