我有一个用例,对于所有PUT / Delete / Update调用,我都希望将一些对象发送到ApiFilter的PostCheck进行某些处理。
我正在这样尝试:
@Context
HttpServletRequest request;
@Context
ContainerResponseContext responseContext ;
@PUT
@Path("/snmp")
@Produces(MediaType.APPLICATION_JSON)
@Timed
public Response<List<SNMPSettingsFactory.ExternalSNMPSettings>> updateSNMPTargetSettingsList(
@Nonnull SNMPSettingsFactory.ExternalSNMPSettings externalSNMPSettings)
{
request.setAttribute("old",oldSettingObject);
request.setAttribute("new",currentSettingObject);
}
在ApiFilter类中,我试图获取它,但它以NULL出现。
@Provider
public class ApiFilter implements ContainerRequestFilter,
ContainerResponseFilter {
private final ApiNonFunctionalHandler handler;
private final Logger logger = LoggerFactory.getLogger(ApiFilter.class);
public ApiFilter(ApiNonFunctionalHandler handler) {
this.handler = handler;
}
@Context
HttpServletRequest request;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
try {
String urlPath = requestContext.getUriInfo().getRequestUri().getPath();
if (!urlPath.startsWith(ApiConstants.BASE_URL)) {
return;
}
UserInfo userInfo;
String authorization = NullOrEmpty.isFalse(requestContext.getHeaderString(CspClient.AUTH_TOKEN_HEADER))?
requestContext.getHeaderString(CspClient.AUTH_TOKEN_HEADER) : requestContext.getHeaderString("Authorization");
userInfo = handler.preCheck(ApiUtils.getApiCategory(urlPath),
authorization, isAuthRequired(urlPath, requestContext.getMethod()));
requestContext.setSecurityContext(new CustomSecurityContext(userInfo,""));
} catch (RuntimeException e) {
throw Throwables.propagate(e);
} catch (Exception e) {
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws
IOException {
String urlPath = requestContext.getUriInfo().getRequestUri().getPath();
String callerIpAddress = null;
if (request != null) {
callerIpAddress = request.getRemoteAddr();
}
String callerUserName = null;
if (request != null) {
callerUserName = request.getRemoteUser();
}
Object oldObject = request.getAttribute("old");
Object currentObject = request.getAttribute("new");
请帮助我如何将所有API调用的对象传递给postfilter,以执行一些常见的处理,例如查找diff并将其记录下来。