[英]Restlet Filter Post request
I would like to filter a Post
request in Filter
(prior it getting to the Resource
). 我想在
Filter
中Filter
Post
请求(在到达Resource
)。 To filter the request I want to retrieve a token from the bode request and do some testing on it. 为了过滤请求,我想从bode请求中检索令牌并对其进行一些测试。
Current Resource: 当前资源:
@Post
public JsonRepresentation init(JsonRepresentation jRep) {
String token = jRep.getJsonObject().getString("token");
.
.
.
}
Current Filter: 当前过滤器:
@Override
protected int beforeHandle(Request request, Response response) {
int result = STOP;
String token = (String) Request.getCurrent().getAttributes().get("token");
.
.
.
}
These code does not retrieve the token . 这些代码不会检索令牌 。
My question is how can I retrieve a body request? 我的问题是如何检索身体要求?
You can directly get the payload text of the request from its associated entity object, as described below: 您可以直接从其关联的实体对象获取请求的有效内容文本,如下所述:
Representation repr = request.getEntity();
String content = repr.getText();
Hope it helps you, Thierry 希望对您有帮助,蒂埃里
you can try something like this to retreive the body : 您可以尝试像这样的运动来恢复身体:
public static String getBody(HttpServletRequest request) throws IOException { 公共静态字符串getBody(HttpServletRequest request)引发IOException {
String body = null;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
body = stringBuilder.toString();
return body;
}
As it is dangerous to store request entities directly into memory (imagine if a client send a tera-bytes representation), the framework does not persist representations into memory by default, they can only be read once (from the socket). 由于将请求实体直接存储在内存中很危险(例如,如果客户端发送了TB表示形式),那么该框架默认不会将表示形式持久存储到内存中,因此只能(从套接字)读取一次。
I guess the answers to your issue may be read from here: Restlet reuse InputStream 我想您可能会在这里找到答案: Restlet重用InputStream
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.