jax-rs资源方法可以获取JSON,它是文档的一部分。 我的问题是我必须更新现有对象(实体)。所以我决定创建jax-rs ContainerRequestFilter。此过滤器必须获取现有对象,将其属性替换为新属性并将其重新放入流中。因此,我希望我在我的资源方法中完全获得实体。 首先,我必须获取经过身份验证的用户的数据。但是' securityContext.getAuthenticatedUser()'返回部分提供的JSON数据?
是否有可能在jax-rs过滤器(在ibm MobileFirst平台上)获取经过身份验证的用户数据?
我的过滤器的代码是:
@Provider
//@ManagedBean
public class UpdateFilter implements ContainerRequestFilter {
//ReaderInterceptor {
//@Inject
//ExistingObjectDao existingObjectDao;
@Context
AdapterSecurityContext securityContext;
@Override
@OAuthSecurity(scope = "protected") //doesn't work
public void filter(ContainerRequestContext context) throws IOException {
//context.getSecurityContext().getUserPrincipal() // is null
AuthenticatedUser user = securityContext.getAuthenticatedUser(); //is null
Map<String, String> authParams = (Map<String, String>) user.getAttributes().get("lotusCredentials");
InputStream inputStream = context.getEntityStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String responseContent = new String(bytes);
String id = context.getUriInfo().getPathParameters().getFirst("id");
Object existingObject = null;
try {
existingObject = existingObjectDao.get(id, authParams);
} catch (Exception e) {
e.printStackTrace();
}
if (existingObject != null) {
ObjectMapper objectMapper = new ObjectMapper();
ObjectReader reader = objectMapper.readerForUpdating(existingObject );
JsonNode r = reader.readTree(responseContent);
responseContent = objectMapper.writer().writeValueAsString(r);
}
context.setEntityStream(new ByteArrayInputStream(responseContent.getBytes()));
}
}