在 JAX-RS WriterInterceptor 和 ReaderInterceptor 之间传递参数
Passing parameters between JAX-RS WriterInterceptor and ReaderInterceptor
我正在使用 JAX-RS 并在 WriterInterceptor 中,我需要访问原始请求中包含的一些信息。
例如,请考虑以下请求正文。
{
"ClientId": "MY_CLIENT_ID",
"UserId": "MY_USER_ID",
"AccountId": "MY_ACCOUNT_ID",
"Scope" : "MY_SCOPES",
}
在我的 WriteInterceptor 中,我需要从请求并将这些值添加到响应中。
我目前正在为此开发 ReadInterceptor 实现。我最初假设有一种方法可以将参数放入 ReaderInterceptorContext,然后以某种方式从 WriterInterceptorContext 读取它。但似乎没有办法做到这一点。 (如果我错了,请纠正我)。
所以,现在我正在尝试使用并发哈希图将这些参数存储在 ReaderInterceptor 中并在 WriteInterceptor 中检索它。我需要一个唯一的键来创建请求和响应之间的关联。为此可以使用 线程 ID 吗?
如果有更好的方法解决这个问题请指点我
我通过添加 container response filter 解决了这个问题,它可以在响应中添加 header。读取拦截器从请求中读取所需参数并将其设置为 上下文属性 。
@Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext)
throws IOException, WebApplicationException {
InputStream is = readerInterceptorContext.getInputStream();
String requestBody = new Scanner(is, StandardCharsets.UTF_8.name()).useDelimiter("\A").next();
JSONObject request = new JSONObject(requestBody);
//Adding the stream back to the context object
readerInterceptorContext.setInputStream(new ByteArrayInputStream(requestBody.getBytes()));
//Adding properties to read in filter
readerInterceptorContext.setProperty("ClientId", request.get("ClientId"));
readerInterceptorContext.setProperty("UserId","UserId"));
return readerInterceptorContext.proceed();
}
然后在容器响应过滤器中读取这些属性并添加为 响应 header。
@Override
public void filter(ContainerRequestContext containerReqContext, ContainerResponseContext containerResponseContext) {
//Adding temporary headers to read in WriterInterceptor
containerResponseContext.getHeaders().add(
"ClientId", containerReqContext.getProperty("ClientId"));
containerResponseContext.getHeaders().add(
"UserId", containerReqContext.getProperty("UserId"));
}
现有编写器拦截器读取这些 headers,将它们添加到 JWT,然后作为 header 值删除。我为此做了一个 POC,它按预期工作
@Override
public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException {
OutputStream outputStream = writerInterceptorContext.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writerInterceptorContext.setOutputStream(baos);
String clientId = writerInterceptorContext.getHeaders().getFirst("ClientId").toString();
String user = writerInterceptorContext.getHeaders().getFirst("UserId").toString();
}
我正在使用 JAX-RS 并在 WriterInterceptor 中,我需要访问原始请求中包含的一些信息。
例如,请考虑以下请求正文。
{
"ClientId": "MY_CLIENT_ID",
"UserId": "MY_USER_ID",
"AccountId": "MY_ACCOUNT_ID",
"Scope" : "MY_SCOPES",
}
在我的 WriteInterceptor 中,我需要从请求并将这些值添加到响应中。
我目前正在为此开发 ReadInterceptor 实现。我最初假设有一种方法可以将参数放入 ReaderInterceptorContext,然后以某种方式从 WriterInterceptorContext 读取它。但似乎没有办法做到这一点。 (如果我错了,请纠正我)。
所以,现在我正在尝试使用并发哈希图将这些参数存储在 ReaderInterceptor 中并在 WriteInterceptor 中检索它。我需要一个唯一的键来创建请求和响应之间的关联。为此可以使用 线程 ID 吗?
如果有更好的方法解决这个问题请指点我
我通过添加 container response filter 解决了这个问题,它可以在响应中添加 header。读取拦截器从请求中读取所需参数并将其设置为 上下文属性 。
@Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext)
throws IOException, WebApplicationException {
InputStream is = readerInterceptorContext.getInputStream();
String requestBody = new Scanner(is, StandardCharsets.UTF_8.name()).useDelimiter("\A").next();
JSONObject request = new JSONObject(requestBody);
//Adding the stream back to the context object
readerInterceptorContext.setInputStream(new ByteArrayInputStream(requestBody.getBytes()));
//Adding properties to read in filter
readerInterceptorContext.setProperty("ClientId", request.get("ClientId"));
readerInterceptorContext.setProperty("UserId","UserId"));
return readerInterceptorContext.proceed();
}
然后在容器响应过滤器中读取这些属性并添加为 响应 header。
@Override
public void filter(ContainerRequestContext containerReqContext, ContainerResponseContext containerResponseContext) {
//Adding temporary headers to read in WriterInterceptor
containerResponseContext.getHeaders().add(
"ClientId", containerReqContext.getProperty("ClientId"));
containerResponseContext.getHeaders().add(
"UserId", containerReqContext.getProperty("UserId"));
}
现有编写器拦截器读取这些 headers,将它们添加到 JWT,然后作为 header 值删除。我为此做了一个 POC,它按预期工作
@Override
public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException {
OutputStream outputStream = writerInterceptorContext.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writerInterceptorContext.setOutputStream(baos);
String clientId = writerInterceptorContext.getHeaders().getFirst("ClientId").toString();
String user = writerInterceptorContext.getHeaders().getFirst("UserId").toString();
}