Search in sources :

Example 11 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class ClientResource method deleteInstance.

public Promise<ResourceResponse, ResourceException> deleteInstance(Context context, String resourceId, DeleteRequest request) {
    String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
    Map<String, String> responseVal = new HashMap<String, String>();
    JsonValue response;
    try {
        String realm = request.getAdditionalParameter("realm");
        if (realm == null) {
            realm = "/";
        }
        manager.deleteIdentity(resourceId, realm);
        try {
            //delete the tokens associated with that client_id
            final TokenFilter tokenFilter = new TokenFilterBuilder().and().withAttribute(OAuthTokenField.CLIENT_ID.getField(), resourceId).withAttribute(OAuthTokenField.REALM.getField(), realm).build();
            store.deleteOnQueryAsync(tokenFilter);
        } catch (CoreTokenException e) {
            if (auditLogger.isAuditLogEnabled()) {
                String[] obs = { "FAILED_DELETE_CLIENT", responseVal.toString() };
                auditLogger.logErrorMessage("FAILED_DELETE_CLIENT", obs, null);
            }
            if (debug.errorEnabled()) {
                debug.error("ClientResource :: DELETE by " + principal + ": Unable to delete client with ID, " + resourceId);
            }
            throw new InternalServerErrorException("Unable to delete client", e);
        }
        responseVal.put("success", "true");
        response = new JsonValue(responseVal);
        ResourceResponse resource = newResourceResponse("results", "1", response);
        if (auditLogger.isAuditLogEnabled()) {
            String[] obs = { "DELETED_CLIENT", response.toString() };
            auditLogger.logAccessMessage("DELETED_CLIENT", obs, null);
            if (debug.messageEnabled()) {
                debug.error("ClientResource :: DELETE by " + principal + ": delete client with ID, " + resourceId);
            }
        }
        return newResultPromise(resource);
    } catch (IdRepoException e) {
        responseVal.put("success", "false");
        if (auditLogger.isAuditLogEnabled()) {
            String[] obs = { "FAILED_DELETE_CLIENT", responseVal.toString() };
            auditLogger.logErrorMessage("FAILED_DELETE_CLIENT", obs, null);
        }
        if (debug.errorEnabled()) {
            debug.error("ClientResource :: DELETE by " + principal + ": Unable to delete client with ID, " + resourceId, e);
        }
        return new InternalServerErrorException("Unable to delete client", e).asPromise();
    } catch (SSOException e) {
        responseVal.put("success", "false");
        if (auditLogger.isAuditLogEnabled()) {
            String[] obs = { "FAILED_DELETE_CLIENT", responseVal.toString() };
            auditLogger.logErrorMessage("FAILED_DELETE_CLIENT", obs, null);
        }
        if (debug.errorEnabled()) {
            debug.error("ClientResource :: DELETE by " + principal + ": Unable to delete client with ID, " + resourceId, e);
        }
        return new InternalServerErrorException("Unable to delete client", e).asPromise();
    } catch (InternalServerErrorException e) {
        responseVal.put("success", "false");
        if (auditLogger.isAuditLogEnabled()) {
            String[] obs = { "FAILED_DELETE_CLIENT", responseVal.toString() };
            auditLogger.logErrorMessage("FAILED_DELETE_CLIENT", obs, null);
        }
        if (debug.errorEnabled()) {
            debug.error("ClientResource :: DELETE by " + principal + ": Unable to delete client with ID, " + resourceId, e);
        }
        return new InternalServerErrorException("Unable to delete client", e).asPromise();
    }
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) HashMap(java.util.HashMap) JsonValue(org.forgerock.json.JsonValue) IdRepoException(com.sun.identity.idm.IdRepoException) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) SSOException(com.iplanet.sso.SSOException) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter)

Aggregations

TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)11 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)10 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)7 ArrayList (java.util.ArrayList)6 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)5 Test (org.testng.annotations.Test)5 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)4 Token (org.forgerock.openam.cts.api.tokens.Token)3 QueryFilter (org.forgerock.util.query.QueryFilter)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)2 Connection (org.forgerock.opendj.ldap.Connection)2 Filter (org.forgerock.opendj.ldap.Filter)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 SessionException (com.iplanet.dpro.session.SessionException)1 SSOException (com.iplanet.sso.SSOException)1 IdRepoException (com.sun.identity.idm.IdRepoException)1 Map (java.util.Map)1