use of org.restlet.representation.BufferingRepresentation in project OpenAM by OpenRock.
the class AbstractRestletAccessAuditFilter method beforeHandle.
@Override
protected int beforeHandle(Request request, Response response) {
try {
Representation representation = request.getEntity();
// buffer in order to read from it during the event logging and later during authentication
if (representation.isTransient()) {
request.setEntity(new BufferingRepresentation(request.getEntity()));
}
auditAccessAttempt(request);
} catch (AuditException e) {
debug.error("Unable to publish {} audit event '{}' due to error: {} [{}]", ACCESS_TOPIC, EventName.AM_ACCESS_ATTEMPT, e.getMessage(), e);
}
return CONTINUE;
}
use of org.restlet.representation.BufferingRepresentation in project OpenAM by OpenRock.
the class RestletBodyAuditor method jsonAuditor.
/**
* Create a body auditor for JSON bodies.
* @param fields The fields that should be captured if they exist.
* @return The auditor object.
*/
public static RestletBodyAuditor jsonAuditor(String... fields) {
return new RestletBodyAuditor<JSONObject>(fields) {
@Override
public JsonValue apply(Representation representation) throws AuditException {
try {
boolean isBufferingRepresentation = (representation instanceof BufferingRepresentation);
boolean isEmptyBufferingRepresentation = isBufferingRepresentation && ((BufferingRepresentation) representation).getWrappedRepresentation().isEmpty();
if (isEmptyBufferingRepresentation || (!isBufferingRepresentation && representation.isEmpty())) {
return json(object());
}
return extractValues(new JsonRepresentation(representation).getJsonObject());
} catch (IOException | JSONException e) {
throw new AuditException("Could not parse body as JSON - wrong body auditor?", e);
}
}
@Override
Object getValue(String field, JSONObject object) throws AuditException {
return object.opt(field);
}
};
}
Aggregations