增加HttpParameters
This commit is contained in:
@@ -11,6 +11,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import org.redkale.convert.ConvertType;
|
||||
import org.redkale.net.http.HttpHeaders;
|
||||
import org.redkale.net.http.HttpParameters;
|
||||
import org.redkale.net.http.HttpSimpleRequest;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
@@ -49,7 +50,7 @@ public class HttpSimpleRequestCoder implements MessageCoder<HttpSimpleRequest> {
|
||||
byte[] contentType = MessageCoder.getBytes(data.getContentType());//short-string
|
||||
byte[] userid = MessageCoder.encodeUserid(data.getCurrentUserid());
|
||||
byte[] headers = MessageCoder.getSeriMapBytes(data.getHeaders() == null ? null : data.getHeaders().map());
|
||||
byte[] params = MessageCoder.getStringMapBytes(data.getParams());
|
||||
byte[] params = MessageCoder.getStringMapBytes(data.getParams() == null ? null : data.getParams().map());
|
||||
byte[] body = MessageCoder.getBytes(data.getBody());
|
||||
int count = 1 //rpc
|
||||
+ 4 //reqConvertType
|
||||
@@ -168,7 +169,10 @@ public class HttpSimpleRequestCoder implements MessageCoder<HttpSimpleRequest> {
|
||||
if (Utility.isNotEmpty(headerMap)) {
|
||||
req.setHeaders(HttpHeaders.ofValid(headerMap));
|
||||
}
|
||||
req.setParams(MessageCoder.getStringMap(buffer));
|
||||
Map<String, String> paramsMap = MessageCoder.getStringMap(buffer);
|
||||
if (Utility.isNotEmpty(paramsMap)) {
|
||||
req.setParams(HttpParameters.ofValid(paramsMap));
|
||||
}
|
||||
int len = buffer.getInt();
|
||||
if (len >= 0) {
|
||||
byte[] bs = new byte[len];
|
||||
|
||||
@@ -126,8 +126,8 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String key) {
|
||||
return this.map != null && this.map.containsKey(key);
|
||||
public boolean contains(String name) {
|
||||
return this.map != null && this.map.containsKey(name);
|
||||
}
|
||||
|
||||
public HttpHeaders addAll(HttpHeaders header) {
|
||||
@@ -149,106 +149,106 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
||||
}
|
||||
|
||||
//服务端接收,无需校验参数合法性
|
||||
void addValid(String key, String value) {
|
||||
void addValid(String name, String value) {
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
this.map.put(key, value);
|
||||
this.map.put(name, value);
|
||||
} else {
|
||||
Serializable old = this.map.get(key);
|
||||
Serializable old = this.map.get(name);
|
||||
if (old == null) {
|
||||
this.map.put(key, value);
|
||||
this.map.put(name, value);
|
||||
} else if (old instanceof Collection) {
|
||||
((Collection) old).add(value);
|
||||
} else {
|
||||
ArrayList list = new ArrayList();
|
||||
list.add(old);
|
||||
list.add(value);
|
||||
this.map.put(key, list);
|
||||
this.map.put(name, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, String value) {
|
||||
check(key, value);
|
||||
public HttpHeaders add(String name, String value) {
|
||||
check(name, value);
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
this.map.put(key, value);
|
||||
this.map.put(name, value);
|
||||
} else {
|
||||
Serializable old = this.map.get(key);
|
||||
Serializable old = this.map.get(name);
|
||||
if (old == null) {
|
||||
this.map.put(key, value);
|
||||
this.map.put(name, value);
|
||||
} else if (old instanceof Collection) {
|
||||
((Collection) old).add(value);
|
||||
} else {
|
||||
ArrayList list = new ArrayList();
|
||||
list.add(old);
|
||||
list.add(value);
|
||||
this.map.put(key, list);
|
||||
this.map.put(name, list);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, List<String> value) {
|
||||
public HttpHeaders add(String name, List<String> value) {
|
||||
if (value.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
for (String val : value) {
|
||||
check(key, val);
|
||||
check(name, val);
|
||||
}
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
this.map.put(key, new ArrayList(value));
|
||||
this.map.put(name, new ArrayList(value));
|
||||
} else {
|
||||
Serializable old = this.map.get(key);
|
||||
Serializable old = this.map.get(name);
|
||||
if (old == null) {
|
||||
this.map.put(key, new ArrayList(value));
|
||||
this.map.put(name, new ArrayList(value));
|
||||
} else if (old instanceof Collection) {
|
||||
((Collection) old).addAll(value);
|
||||
} else {
|
||||
ArrayList list = new ArrayList();
|
||||
list.add(old);
|
||||
list.addAll(value);
|
||||
this.map.put(key, list);
|
||||
this.map.put(name, list);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, TextConvert convert, Object value) {
|
||||
return add(key, (convert == null ? JsonConvert.root() : convert).convertTo(value));
|
||||
public HttpHeaders add(String name, TextConvert convert, Object value) {
|
||||
return add(name, (convert == null ? JsonConvert.root() : convert).convertTo(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, Object value) {
|
||||
return add(key, JsonConvert.root().convertTo(value));
|
||||
public HttpHeaders add(String name, Object value) {
|
||||
return add(name, JsonConvert.root().convertTo(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, boolean value) {
|
||||
return add(key, String.valueOf(value));
|
||||
public HttpHeaders add(String name, boolean value) {
|
||||
return add(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, short value) {
|
||||
return add(key, String.valueOf(value));
|
||||
public HttpHeaders add(String name, short value) {
|
||||
return add(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, int value) {
|
||||
return add(key, String.valueOf(value));
|
||||
public HttpHeaders add(String name, int value) {
|
||||
return add(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, float value) {
|
||||
return add(key, String.valueOf(value));
|
||||
public HttpHeaders add(String name, float value) {
|
||||
return add(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, long value) {
|
||||
return add(key, String.valueOf(value));
|
||||
public HttpHeaders add(String name, long value) {
|
||||
return add(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, double value) {
|
||||
return add(key, String.valueOf(value));
|
||||
public HttpHeaders add(String name, double value) {
|
||||
return add(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders add(String key, BigInteger value) {
|
||||
return add(key, String.valueOf(value));
|
||||
public HttpHeaders add(String name, BigInteger value) {
|
||||
return add(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders setAll(HttpHeaders header) {
|
||||
@@ -269,75 +269,75 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
||||
}
|
||||
|
||||
//服务端接收,无需校验参数合法性
|
||||
void setValid(String key, String value) {
|
||||
void setValid(String name, String value) {
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
this.map.put(key, value);
|
||||
this.map.put(name, value);
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, String value) {
|
||||
check(key, value);
|
||||
public HttpHeaders set(String name, String value) {
|
||||
check(name, value);
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
this.map.put(key, value);
|
||||
this.map.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, List<String> value) {
|
||||
public HttpHeaders set(String name, List<String> value) {
|
||||
if (value.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
for (String val : value) {
|
||||
check(key, val);
|
||||
check(name, val);
|
||||
}
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
this.map.put(key, new ArrayList(value));
|
||||
this.map.put(name, new ArrayList(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, TextConvert convert, Object value) {
|
||||
return set(key, (convert == null ? JsonConvert.root() : convert).convertTo(value));
|
||||
public HttpHeaders set(String name, TextConvert convert, Object value) {
|
||||
return set(name, (convert == null ? JsonConvert.root() : convert).convertTo(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, Object value) {
|
||||
return set(key, JsonConvert.root().convertTo(value));
|
||||
public HttpHeaders set(String name, Object value) {
|
||||
return set(name, JsonConvert.root().convertTo(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, boolean value) {
|
||||
return set(key, String.valueOf(value));
|
||||
public HttpHeaders set(String name, boolean value) {
|
||||
return set(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, short value) {
|
||||
return set(key, String.valueOf(value));
|
||||
public HttpHeaders set(String name, short value) {
|
||||
return set(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, int value) {
|
||||
return set(key, String.valueOf(value));
|
||||
public HttpHeaders set(String name, int value) {
|
||||
return set(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, float value) {
|
||||
return set(key, String.valueOf(value));
|
||||
public HttpHeaders set(String name, float value) {
|
||||
return set(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, long value) {
|
||||
return set(key, String.valueOf(value));
|
||||
public HttpHeaders set(String name, long value) {
|
||||
return set(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, double value) {
|
||||
return set(key, String.valueOf(value));
|
||||
public HttpHeaders set(String name, double value) {
|
||||
return set(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders set(String key, BigInteger value) {
|
||||
return set(key, String.valueOf(value));
|
||||
public HttpHeaders set(String name, BigInteger value) {
|
||||
return set(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpHeaders remove(String key) {
|
||||
public HttpHeaders remove(String name) {
|
||||
if (this.map != null) {
|
||||
this.map.remove(key);
|
||||
this.map.remove(name);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -358,12 +358,12 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
protected String check(String key, String value) {
|
||||
if (key.indexOf('\r') >= 0 || key.indexOf('\n') >= 0) {
|
||||
throw new RedkaleException("http-header name(name = " + key + ") is illegal");
|
||||
protected String check(String name, String value) {
|
||||
if (name.indexOf('\r') >= 0 || name.indexOf('\n') >= 0) {
|
||||
throw new RedkaleException("http-header name(name = " + name + ") is illegal");
|
||||
}
|
||||
if (value.indexOf('\r') >= 0 || value.indexOf('\n') >= 0) {
|
||||
throw new RedkaleException("http-header value(name = " + key + ", value = " + value + ") is illegal");
|
||||
throw new RedkaleException("http-header value(name = " + name + ", value = " + value + ") is illegal");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
194
src/main/java/org/redkale/net/http/HttpParameters.java
Normal file
194
src/main/java/org/redkale/net/http/HttpParameters.java
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.net.http;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigInteger;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import org.redkale.convert.TextConvert;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.RedkaleException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class HttpParameters implements RestParams, Serializable {
|
||||
|
||||
protected HashMap<String, String> map;
|
||||
|
||||
protected HttpParameters() {
|
||||
}
|
||||
|
||||
public static HttpParameters create() {
|
||||
return new HttpParameters();
|
||||
}
|
||||
|
||||
public static HttpParameters of(String... items) {
|
||||
HttpParameters params = new HttpParameters();
|
||||
int len = items.length / 2;
|
||||
for (int i = 0; i < len; i++) {
|
||||
params.set(items[i * 2], items[i * 2 + 1]);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无需校验参数合法性
|
||||
*
|
||||
* @param map 参数
|
||||
*
|
||||
* @return HttpParameters
|
||||
*/
|
||||
public static HttpParameters ofValid(Map<String, String> map) {
|
||||
HttpParameters params = new HttpParameters();
|
||||
if (map != null) {
|
||||
params.map = map instanceof HashMap ? (HashMap) map : new HashMap(map);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String name) {
|
||||
return get(name, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String name, String defaultValue) {
|
||||
if (map == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return map.getOrDefault(name, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(BiConsumer<String, String> consumer) {
|
||||
if (map != null) {
|
||||
map.forEach(consumer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] names() {
|
||||
if (this.map == null) {
|
||||
return new String[0];
|
||||
}
|
||||
Set<String> names = this.map.keySet();
|
||||
return names.toArray(new String[names.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String name) {
|
||||
return this.map != null && this.map.containsKey(name);
|
||||
}
|
||||
|
||||
public HttpParameters putAll(HttpParameters params) {
|
||||
if (params.map != null) {
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
this.map.putAll(params.map);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpParameters put(Map<String, String> values) {
|
||||
if (values != null) {
|
||||
values.forEach(this::put);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
//服务端接收,无需校验参数合法性
|
||||
void setValid(String name, String value) {
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
this.map.put(name, value);
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, String value) {
|
||||
check(name, value);
|
||||
if (this.map == null) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
this.map.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, TextConvert convert, Object value) {
|
||||
return put(name, (convert == null ? JsonConvert.root() : convert).convertTo(value));
|
||||
}
|
||||
|
||||
public HttpParameters set(String name, Object value) {
|
||||
return put(name, JsonConvert.root().convertTo(value));
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, boolean value) {
|
||||
return put(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, short value) {
|
||||
return put(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, int value) {
|
||||
return put(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, float value) {
|
||||
return put(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, long value) {
|
||||
return put(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, double value) {
|
||||
return put(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpParameters put(String name, BigInteger value) {
|
||||
return put(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public HttpParameters remove(String name) {
|
||||
if (this.map != null) {
|
||||
this.map.remove(name);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> map() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.map == null || this.map.isEmpty();
|
||||
}
|
||||
|
||||
public HttpParameters clear() {
|
||||
if (this.map != null) {
|
||||
this.map.clear();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected String check(String name, String value) {
|
||||
if (name.indexOf(' ') >= 0 || name.indexOf('\r') >= 0 || name.indexOf('\n') >= 0) {
|
||||
throw new RedkaleException("http-param name(name = " + name + ") is illegal");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(this.map);
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
|
||||
protected String newSessionid;
|
||||
|
||||
protected final Map<String, String> params = new HashMap<>();
|
||||
protected final HttpParameters params = HttpParameters.create();
|
||||
|
||||
protected boolean boundary = false;
|
||||
|
||||
@@ -1546,7 +1546,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
+ (this.getContentLength() >= 0 ? (", \r\n contentLength: " + this.contentLength) : "")
|
||||
+ (this.array.length() > 0 ? (", \r\n bodyLength: " + this.array.length()) : "")
|
||||
+ (this.boundary || this.array.isEmpty() ? "" : (", \r\n bodyContent: " + (this.respConvertType == null || this.respConvertType == ConvertType.JSON ? this.getBodyUTF8() : Arrays.toString(getBody()))))
|
||||
+ ", \r\n params: " + toMapString(this.params, 4)
|
||||
+ ", \r\n params: " + toMapString(this.params.map, 4)
|
||||
+ ", \r\n header: " + toMapString(this.headers.map, 4)
|
||||
+ "\r\n}"; //this.headers.toString(4)
|
||||
}
|
||||
@@ -1579,7 +1579,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
@ConvertDisabled
|
||||
public final MultiContext getMultiContext() {
|
||||
final InputStream in = newInputStream();
|
||||
return new MultiContext(context.getCharset(), this.getContentType(), this.params,
|
||||
return new MultiContext(context.getCharset(), this.getContentType(), this.params.map(),
|
||||
new BufferedInputStream(in, Math.max(array.length(), 8192)) {
|
||||
{
|
||||
array.copyTo(this.buf);
|
||||
@@ -2469,28 +2469,11 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
* @return AnyValue
|
||||
*/
|
||||
@AsmDepends
|
||||
public Map<String, String> getParameters() {
|
||||
public HttpParameters getParameters() {
|
||||
parseBody();
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将请求参数转换成Map
|
||||
*
|
||||
* @param map Map
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
@ConvertDisabled
|
||||
public Map<String, String> getParametersToMap(Map<String, String> map) {
|
||||
if (map == null) {
|
||||
map = new LinkedHashMap<>();
|
||||
}
|
||||
final Map<String, String> map0 = map;
|
||||
getParameters().forEach((k, v) -> map0.put(k, v));
|
||||
return map0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将请求参数转换成String, 字符串格式为: bean1={}&id=13&name=xxx <br>
|
||||
* 不会返回null,没有参数返回空字符串
|
||||
@@ -2529,8 +2512,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
@ConvertDisabled
|
||||
public String[] getParameterNames() {
|
||||
parseBody();
|
||||
Set<String> names = params.keySet();
|
||||
return names.toArray(new String[names.size()]);
|
||||
return params.names();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2555,7 +2537,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
*/
|
||||
public String getParameter(String name, String defaultValue) {
|
||||
parseBody();
|
||||
return params.getOrDefault(name, defaultValue);
|
||||
return params.get(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ package org.redkale.net.http;
|
||||
import java.io.Serializable;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.redkale.annotation.Comment;
|
||||
import org.redkale.convert.*;
|
||||
@@ -84,7 +83,7 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
||||
|
||||
@ConvertColumn(index = 15)
|
||||
@Comment("参数信息")
|
||||
protected Map<String, String> params;
|
||||
protected HttpParameters params;
|
||||
|
||||
@ConvertColumn(index = 16)
|
||||
@Comment("http body信息")
|
||||
@@ -236,7 +235,7 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpSimpleRequest params(Map<String, String> params) {
|
||||
public HttpSimpleRequest params(HttpParameters params) {
|
||||
this.params = params;
|
||||
return this;
|
||||
}
|
||||
@@ -296,35 +295,25 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
||||
|
||||
public HttpSimpleRequest param(String key, String value) {
|
||||
if (this.params == null) {
|
||||
this.params = new HashMap<>();
|
||||
this.params = HttpParameters.create();
|
||||
}
|
||||
this.params.put(key, value);
|
||||
this.params.set(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpSimpleRequest param(String key, TextConvert convert, Object value) {
|
||||
if (value == null) {
|
||||
return this;
|
||||
}
|
||||
if (this.params == null) {
|
||||
this.params = new HashMap<>();
|
||||
this.params = HttpParameters.create();
|
||||
}
|
||||
if (convert == null) {
|
||||
convert = JsonConvert.root();
|
||||
}
|
||||
this.params.put(key, convert.convertTo(value));
|
||||
this.params.put(key, convert, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpSimpleRequest param(String key, Object value) {
|
||||
if (value == null) {
|
||||
return this;
|
||||
}
|
||||
if (this.params == null) {
|
||||
this.params = new HashMap<>();
|
||||
}
|
||||
this.params.put(key, value instanceof CharSequence ? value.toString() : JsonConvert.root().convertTo(value));
|
||||
return this;
|
||||
return param(key, JsonConvert.root(), value);
|
||||
}
|
||||
|
||||
public HttpSimpleRequest body(byte[] body) {
|
||||
@@ -458,11 +447,11 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
||||
headers(headers);
|
||||
}
|
||||
|
||||
public Map<String, String> getParams() {
|
||||
public HttpParameters getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public void setParams(Map<String, String> params) {
|
||||
public void setParams(HttpParameters params) {
|
||||
params(params);
|
||||
}
|
||||
|
||||
|
||||
@@ -1102,7 +1102,8 @@ public final class Rest {
|
||||
final String httpResultDesc = Type.getDescriptor(HttpResult.class);
|
||||
final String httpScopeDesc = Type.getDescriptor(HttpScope.class);
|
||||
final String stageDesc = Type.getDescriptor(CompletionStage.class);
|
||||
final String httpHeaderDesc = Type.getDescriptor(HttpHeaders.class);
|
||||
final String httpHeadersDesc = Type.getDescriptor(HttpHeaders.class);
|
||||
final String httpParametersDesc = Type.getDescriptor(HttpParameters.class);
|
||||
final String flipperDesc = Type.getDescriptor(Flipper.class);
|
||||
final String httpServletName = HttpServlet.class.getName().replace('.', '/');
|
||||
final String actionEntryName = HttpServlet.ActionEntry.class.getName().replace('.', '/');
|
||||
@@ -1317,9 +1318,10 @@ public final class Rest {
|
||||
comment = "";
|
||||
n = "^"; //Http头信息类型特殊处理
|
||||
}
|
||||
RestParams annparams = param.getAnnotation(RestParams.class);
|
||||
if (annparams != null) {
|
||||
boolean annparams = param.getType() == RestParams.class;
|
||||
if (annparams) {
|
||||
comment = "";
|
||||
n = "?"; //Http参数类型特殊处理
|
||||
}
|
||||
RestParam annpara = param.getAnnotation(RestParam.class);
|
||||
if (annpara != null) {
|
||||
@@ -1382,21 +1384,21 @@ public final class Rest {
|
||||
RestURI annuri = (RestURI) ps[headIndex + 4];
|
||||
RestUserid annuserid = (RestUserid) ps[headIndex + 5];
|
||||
boolean annheaders = (Boolean) ps[headIndex + 6];
|
||||
RestParams annparams = (RestParams) ps[headIndex + 7];
|
||||
boolean annparams = (Boolean) ps[headIndex + 7];
|
||||
|
||||
if (CompletionHandler.class.isAssignableFrom(ptype)) { //HttpResponse.createAsyncHandler() or HttpResponse.createAsyncHandler(Class)
|
||||
} else if (annsid != null) { //HttpRequest.getSessionid(true|false)
|
||||
} else if (annaddr != null) { //HttpRequest.getRemoteAddr
|
||||
} else if (annlocale != null) { //HttpRequest.getLocale
|
||||
} else if (annparams != null) { //HttpRequest.getParameters
|
||||
} else if (annbody != null) { //HttpRequest.getBodyUTF8 / HttpRequest.getBody
|
||||
} else if (annfile != null) { //MultiContext.partsFirstBytes / HttpRequest.partsFirstFile / HttpRequest.partsFiles
|
||||
} else if (annuri != null) { //HttpRequest.getRequestURI
|
||||
} else if (annuserid != null) { //HttpRequest.currentUserid
|
||||
} else if ("#".equals(pname)) { //从request.getRequstURI 中取参数
|
||||
} else if (pname != null && pname.charAt(0) == '#') { //从request.getRequstURIPath 中去参数
|
||||
} else if ("#".equals(pname)) { //从request.getRequstURI 中取参数
|
||||
} else if ("&".equals(pname) && ptype == userType) { //当前用户对象的类名
|
||||
} else if ("^".equals(pname) && annheaders) { //HttpRequest.getHeaders Http头信息
|
||||
} else if ("?".equals(pname) && annparams) { //HttpRequest.getParameters Http参数信息
|
||||
} else if (ptype.isPrimitive()) {
|
||||
//do nothing
|
||||
} else if (ptype == String.class) {
|
||||
@@ -2154,65 +2156,65 @@ public final class Rest {
|
||||
required = false;
|
||||
}
|
||||
|
||||
RestParams annparams = param.getAnnotation(RestParams.class);
|
||||
if (annparams != null) {
|
||||
boolean annparams = param.getType() == RestParams.class;
|
||||
boolean annheaders = param.getType() == RestHeaders.class;
|
||||
if (annparams) {
|
||||
if (annhead != null) {
|
||||
throw new RestException("@RestParams and @RestHeader cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestHeader cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (anncookie != null) {
|
||||
throw new RestException("@RestParams and @RestCookie cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestCookie cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annsid != null) {
|
||||
throw new RestException("@RestParams and @RestSessionid cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestSessionid cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annaddr != null) {
|
||||
throw new RestException("@RestParams and @RestAddress cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestAddress cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annlocale != null) {
|
||||
throw new RestException("@RestParams and @RestLocale cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestLocale cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annbody != null) {
|
||||
throw new RestException("@RestParams and @RestBody cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestBody cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annfile != null) {
|
||||
throw new RestException("@RestParams and @RestUploadFile cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestUploadFile cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (userid != null) {
|
||||
throw new RestException("@RestParams and @RestUserid cannot on the same Parameter in " + method);
|
||||
throw new RestException("@RestUserid cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (!TYPE_MAP_STRING_STRING.equals(param.getParameterizedType())) {
|
||||
throw new RestException("@RestParams must on Map<String, String> Parameter in " + method);
|
||||
if (annheaders) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestParams.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
comment = "";
|
||||
}
|
||||
|
||||
boolean annheaders = param.getType() == RestHeaders.class;
|
||||
if (annheaders) {
|
||||
if (annhead != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestHeader cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (anncookie != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestCookie cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annsid != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestSessionid cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annaddr != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestAddress cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annlocale != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestLocale cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annbody != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestBody cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annfile != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestUploadFile cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (userid != null) {
|
||||
throw new RestException("@RestHeaders cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
throw new RestException("@RestUserid cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
if (annparams != null) {
|
||||
if (annparams) {
|
||||
throw new RestException("@RestParams cannot on the " + RestHeaders.class.getSimpleName() + " Parameter in " + method);
|
||||
}
|
||||
comment = "";
|
||||
@@ -2238,6 +2240,9 @@ public final class Rest {
|
||||
if (n == null && ptype == RestHeaders.class) {
|
||||
n = "^"; //Http头信息类型特殊处理
|
||||
}
|
||||
if (n == null && ptype == RestParams.class) {
|
||||
n = "?"; //Http参数类型特殊处理
|
||||
}
|
||||
if (n == null && asmParamNames != null && asmParamNames.size() > i) {
|
||||
n = asmParamNames.get(i);
|
||||
}
|
||||
@@ -2512,7 +2517,7 @@ public final class Rest {
|
||||
RestURI annuri = (RestURI) ps[headIndex + 4];
|
||||
RestUserid userid = (RestUserid) ps[headIndex + 5];
|
||||
boolean annheaders = (Boolean) ps[headIndex + 6];
|
||||
RestParams annparams = (RestParams) ps[headIndex + 7];
|
||||
boolean annparams = (Boolean) ps[headIndex + 7];
|
||||
java.lang.reflect.Type pgentype = (java.lang.reflect.Type) ps[headIndex + 8];
|
||||
if (dynsimple && (annsid != null || annaddr != null || annlocale != null || annhead != null || anncookie != null || annfile != null || annheaders)) {
|
||||
dynsimple = false;
|
||||
@@ -2557,12 +2562,12 @@ public final class Rest {
|
||||
varInsns.add(new int[]{ALOAD, maxLocals});
|
||||
} else if (annheaders) { //HttpRequest.getHeaders
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getHeaders", "()" + httpHeaderDesc, false);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getHeaders", "()" + httpHeadersDesc, false);
|
||||
mv.visitVarInsn(ASTORE, maxLocals);
|
||||
varInsns.add(new int[]{ALOAD, maxLocals});
|
||||
} else if (annparams != null) { //HttpRequest.getParameters
|
||||
} else if (annparams) { //HttpRequest.getParameters
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getParameters", "()Ljava/util/Map;", false);
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getParameters", "()" + httpParametersDesc, false);
|
||||
mv.visitVarInsn(ASTORE, maxLocals);
|
||||
varInsns.add(new int[]{ALOAD, maxLocals});
|
||||
} else if (annbody != null) { //HttpRequest.getBodyUTF8 / HttpRequest.getBody
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* 用于RestService类的方法的参数获取HttpHeader
|
||||
* 用于RestService类的方法的参数获取HttpHeaders
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
|
||||
@@ -5,24 +5,30 @@
|
||||
*/
|
||||
package org.redkale.net.http;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* 只能注解于RestService类的方法的参数或参数内的Map<String, String>字段
|
||||
* 用于RestService类的方法的参数获取HttpParams
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*
|
||||
* @since 2.4.0
|
||||
* @since 2.8.0
|
||||
*/
|
||||
@Inherited
|
||||
@Documented
|
||||
@Target({PARAMETER, FIELD})
|
||||
@Retention(RUNTIME)
|
||||
public @interface RestParams {
|
||||
public interface RestParams {
|
||||
|
||||
public String get(String name);
|
||||
|
||||
public String get(String name, String defaultValue);
|
||||
|
||||
public void forEach(BiConsumer<String, String> consumer);
|
||||
|
||||
public String[] names();
|
||||
|
||||
public boolean contains(String name);
|
||||
|
||||
public Map<String, String> map();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user