Search in sources :

Example 21 with RestAuthException

use of org.forgerock.openam.core.rest.authn.exceptions.RestAuthException in project OpenAM by OpenRock.

the class AuthenticationServiceV1 method handleErrorResponse.

/**
     * Processes the given Exception into a Restlet response representation or wrap it into
     * a ResourceException, which will be thrown.
     *
     * @param status The status to set the response to.
     * @param exception The Exception to be handled.
     * @return The Restlet Response Representation.
     * @throws ResourceException If the given exception is wrapped in a ResourceException.
     */
protected Response handleErrorResponse(Request request, Status status, Exception exception) {
    Reject.ifNull(status);
    Response response = new Response(status);
    final Map<String, Object> rep = new HashMap<>();
    if (exception instanceof RestAuthResponseException) {
        final RestAuthResponseException authResponseException = (RestAuthResponseException) exception;
        for (Map.Entry<String, String> entry : authResponseException.getResponseHeaders().entrySet()) {
            response.getHeaders().put(entry.getKey(), entry.getValue());
        }
        rep.putAll(authResponseException.getJsonResponse().asMap());
    } else if (exception instanceof RestAuthException) {
        final RestAuthException authException = (RestAuthException) exception;
        if (authException.getFailureUrl() != null) {
            rep.put("failureUrl", authException.getFailureUrl());
        }
        rep.put("errorMessage", getLocalizedMessage(request, exception));
    } else if (exception == null) {
        rep.put("errorMessage", status.getReasonPhrase());
    } else {
        rep.put("errorMessage", getLocalizedMessage(request, exception));
    }
    response.setEntity(rep);
    return response;
}
Also used : Response(org.forgerock.http.protocol.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) HashMap(java.util.HashMap) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with RestAuthException

use of org.forgerock.openam.core.rest.authn.exceptions.RestAuthException in project OpenAM by OpenRock.

the class AuthenticationServiceV2 method handleErrorResponse.

@Override
protected Response handleErrorResponse(Request request, Status status, Exception exception) {
    Reject.ifNull(status);
    Response response = new Response(status);
    if (exception instanceof RestAuthResponseException) {
        final RestAuthResponseException authResponseException = (RestAuthResponseException) exception;
        for (Map.Entry<String, String> entry : authResponseException.getResponseHeaders().entrySet()) {
            response.getHeaders().add(entry.getKey(), entry.getValue());
        }
        response.setEntity(authResponseException.getJsonResponse().asMap());
        return response;
    } else if (exception instanceof RestAuthException) {
        final RestAuthException rae = (RestAuthException) exception;
        ResourceException cause = ResourceException.getException(rae.getStatusCode(), getLocalizedMessage(request, rae));
        if (rae.getFailureUrl() != null) {
            cause.setDetail(json(object(field("failureUrl", rae.getFailureUrl()))));
        }
        return createExceptionResponse(response, cause);
    } else if (exception == null) {
        return createExceptionResponse(response, ResourceException.getException(status.getCode()));
    } else {
        return createExceptionResponse(response, ResourceException.getException(status.getCode(), exception.getMessage(), exception));
    }
}
Also used : Response(org.forgerock.http.protocol.Response) RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) ResourceException(org.forgerock.json.resource.ResourceException) Map(java.util.Map)

Aggregations

RestAuthException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthException)22 Test (org.testng.annotations.Test)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 JsonValue (org.forgerock.json.JsonValue)5 Request (org.forgerock.http.protocol.Request)4 Response (org.forgerock.http.protocol.Response)4 LoginConfiguration (org.forgerock.openam.core.rest.authn.core.LoginConfiguration)4 RestAuthResponseException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException)4 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 SignedJwt (org.forgerock.json.jose.jws.SignedJwt)3 AuthContextLocalWrapper (org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper)3 SSOToken (com.iplanet.sso.SSOToken)2 PublicKey (java.security.PublicKey)2 Map (java.util.Map)2 SigningHandler (org.forgerock.json.jose.jws.handlers.SigningHandler)2 SessionID (com.iplanet.dpro.session.SessionID)1 SSOException (com.iplanet.sso.SSOException)1 RedirectCallback (com.sun.identity.authentication.spi.RedirectCallback)1 L10NMessageImpl (com.sun.identity.shared.locale.L10NMessageImpl)1