我在我的应用程序类中注册了两个ContainerRequestFilter
过滤器和一个ContainerResponseFilter
,扩展了JAX-RS应用程序:
过滤为:
register(LoggingResponseFilter.class);
register(AuthorizationRequestFilter.class);
register(LikeRequestFilter.class);
当我尝试调用目标方法资源时,我有一个异常。
资源方法:
@Override
@POST
@Path("/{userId}/like")
@TokenResource
@GMTResource // to validate GMT of client and server
@Like // for like filter
public Response like(@HeaderParam("token") @NotNull String token,
@HeaderParam("user-agent") String userAgent,
@NotNull @Valid UserFriendsBaseModel userFriendsBaseModel)
throws BadRequestException, UnauthorizedException,
ForbiddenException, InternalServerError {
userService.like(userFriendsBaseModel,userAgent);
return Response.ok().build();
}
例外:
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.io.IOException: Stream closed
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:419)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.glassfish.jersey.server.ContainerException: java.io.IOException: Stream closed
org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:256)
org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:238)
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:480)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:311)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.io.IOException: Stream closed
org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:312)
org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:200)
org.glassfish.jersey.message.internal.EntityInputStream.read(EntityInputStream.java:101)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream.read(ReaderInterceptorExecutor.java:301)
com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.ensureLoaded(ByteSourceJsonBootstrapper.java:503)
com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.detectEncoding(ByteSourceJsonBootstrapper.java:129)
com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.constructParser(ByteSourceJsonBootstrapper.java:224)
com.fasterxml.jackson.core.JsonFactory._createParser(JsonFactory.java:1242)
com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:753)
com.fasterxml.jackson.jaxrs.base.ProviderBase._createParser(ProviderBase.java:791)
com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:760)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:259)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:235)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:74)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1075)
org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:853)
org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:270)
org.glassfish.jersey.server.internal.inject.EntityParamValueFactoryProvider$EntityValueFactory.provide(EntityParamValueFactoryProvider.java:96)
org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:81)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:125)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:158)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我还将请求过滤器的优先级设置为:
@Priority(200)
public class LikeRequestFilter implements ContainerRequestFilter {
private final Logger _logger = LoggerFactory.getLogger(LikeRequestFilter.class);
@Inject
ResourceInfo resourceInfo;
@Inject
UriInfo uriInfo;
@Autowired
UserManager userManager;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (resourceInfo.getResourceMethod().isAnnotationPresent(Like.class)) {
try {
ObjectMapper jsonMapper = new ObjectMapper();
final UserFriendsBaseModel usm = jsonMapper
.readValue(requestContext.getEntityStream(), UserFriendsBaseModel.class);
boolean isLiked = userManager.isLiked(usm);
/**
* If already liked (on api server db ) the check for other conditions
* otherwise consider as first time like request and send to resource for processing
*/
if(isLiked) {
/**
* Check if already liked on openfire
*/
boolean isRoster = userManager.isRoster(usm);
/**
* if already roster then check for reverse like
* else add entry in add roster failure request
*/
if(isRoster) {
/**
* Check if reverse like on api server if liked then check for openfire
*/
final UserFriendsBaseModel reverseModel = UserFriendsBaseModel.reverse(usm);
boolean isReverseLiked = userManager.isLiked(reverseModel);
/**
* Reverse liked(Friends) on api server db . Check on openfire
*/
if(isReverseLiked) {
boolean isReverseRoster = userManager.isRosterFriend(reverseModel);
/**
* If friend on openfire then send push to both user for new Match
*/
if(isReverseRoster) {
// TODO send push
} else {
userManager.addRosterFailureRequest(reverseModel);
}
}
} else {
userManager.addRosterFailureRequest(usm);
}
requestContext.abortWith(Response.ok().build());
}
} catch (Exception e) {
_logger.error("Something went wrong in Like request Filter", e);
}
_logger.info("end of if in LIKE Filter : ");
}
_logger.info("End of LIKE Filter : ");
}
}
和AuthorizationRequestFilter
过滤为:
@Priority(100)
public class AuthorizationRequestFilter implements ContainerRequestFilter {
private final Logger _logger = LoggerFactory
.getLogger(AuthorizationRequestFilter.class);
@Inject
ResourceInfo resourceInfo;
@Inject
UriInfo uriInfo;
@Autowired
AuthorizationService authorizationService;
和响应过滤器:
public class LoggingResponseFilter implements ContainerResponseFilter {
final static Logger _logger = Logger.getLogger(LoggingResponseFilter.class
.getName());
public void filter(ContainerRequestContext requestContext,ContainerResponseContext responseContext) throws IOException {
String method = requestContext.getMethod();
_logger.info("Requesting : " + method + " for path " + requestContext.getUriInfo().getPath());
然后在LikeRequestFilter
之后调用过滤器链时AuthorizationRequestFilter
出现问题。
我无法找出出了什么问题?如何使用响应过滤器链接请求过滤器?
答案 0 :(得分:3)
问题在于LikeRequestFilter
。您正在使用InputStream
阅读请求实体ObjectMapper
。通常,InputStream
只能读取一次,然后才为空。因此,在ObjectMapper
读取流后,它会关闭它。因此,当泽西岛试图阅读它时,它不能,因为它已关闭。即使它没有关闭,它仍然是空的。
对于这个用例,Jersey允许我们缓冲流,因此可以多次读取。您需要做的是将ContainerRequestContext
转换为具有ContainerRequest
方法的bufferEntity()
。它还有一些方法可以帮助您阅读流,例如readEntity
,与客户Response#readEntity()
类似。
所以你可以做类似
的事情@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
try {
ContainerRequest cr = (ContainerRequest) requestContext;
cr.bufferEntity();
final UserFriendsBaseModel bm = cr.readEntity(UserFriendsBaseModel.class);