Search in sources :

Example 1 with BufferingRepresentation

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;
}
Also used : BufferingRepresentation(org.restlet.representation.BufferingRepresentation) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation)

Example 2 with BufferingRepresentation

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);
        }
    };
}
Also used : BufferingRepresentation(org.restlet.representation.BufferingRepresentation) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) IOException(java.io.IOException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Aggregations

AuditException (org.forgerock.audit.AuditException)2 BufferingRepresentation (org.restlet.representation.BufferingRepresentation)2 Representation (org.restlet.representation.Representation)2 IOException (java.io.IOException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)1 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)1