Search in sources :

Example 96 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class SmsRealmProvider method handleDelete.

@Override
public Promise<ResourceResponse, ResourceException> handleDelete(Context serverContext, DeleteRequest request) {
    RealmContext realmContext = serverContext.asContext(RealmContext.class);
    String realmPath = realmContext.getResolvedRealm();
    try {
        OrganizationConfigManager realmManager = new OrganizationConfigManager(getSSOToken(), realmPath);
        final ResourceResponse resource = getResource(getJsonValue(realmPath));
        realmManager.deleteSubOrganization(null, false);
        String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
        debug.message("RealmResource.deleteInstance :: DELETE of realm " + realmPath + " performed by " + principalName);
        return newResultPromise(resource);
    } catch (SMSException smse) {
        ResourceException exception = configureErrorMessage(smse);
        if (exception instanceof NotFoundException) {
            debug.warning("RealmResource.deleteInstance() : Cannot find {}", realmPath, smse);
            return exception.asPromise();
        } else if (exception instanceof ForbiddenException || exception instanceof PermanentException || exception instanceof ConflictException || exception instanceof BadRequestException) {
            debug.warning("RealmResource.deleteInstance() : Cannot DELETE {}", realmPath, smse);
            return exception.asPromise();
        } else {
            return new BadRequestException(exception.getMessage(), exception).asPromise();
        }
    } catch (Exception e) {
        return new BadRequestException(e.getMessage(), e).asPromise();
    }
}
Also used : ForbiddenException(org.forgerock.json.resource.ForbiddenException) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SMSException(com.sun.identity.sm.SMSException) ConflictException(org.forgerock.json.resource.ConflictException) OrganizationConfigManager(com.sun.identity.sm.OrganizationConfigManager) PermanentException(org.forgerock.json.resource.PermanentException) NotFoundException(org.forgerock.json.resource.NotFoundException) BadRequestException(org.forgerock.json.resource.BadRequestException) ResourceException(org.forgerock.json.resource.ResourceException) ConflictException(org.forgerock.json.resource.ConflictException) PermanentException(org.forgerock.json.resource.PermanentException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ForbiddenException(org.forgerock.json.resource.ForbiddenException) SSOException(com.iplanet.sso.SSOException) NotFoundException(org.forgerock.json.resource.NotFoundException) NotSupportedException(org.forgerock.json.resource.NotSupportedException) BadRequestException(org.forgerock.json.resource.BadRequestException) IdRepoException(com.sun.identity.idm.IdRepoException) SMSException(com.sun.identity.sm.SMSException) ResourceException(org.forgerock.json.resource.ResourceException) SessionException(com.iplanet.dpro.session.SessionException)

Example 97 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class CoreTokenResource method readInstance.

/**
     * Read the token based on its Token ID.
     *
     * If successful, the Token will be returned to the caller in serialised JSON format.
     *
     * @param serverContext Required context.
     * @param tokenId The TokenID of the Token to read.
     * @param readRequest Not used.
     */
public Promise<ResourceResponse, ResourceException> readInstance(Context serverContext, String tokenId, ReadRequest readRequest) {
    String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
    try {
        Token token = store.read(tokenId);
        if (token == null) {
            error("READ by {0}: No token resource to read with ID: {1}", principal, tokenId);
            return generateNotFoundException(tokenId).asPromise();
        }
        String json = serialisation.serialise(token);
        ResourceResponse response = newResourceResponse(tokenId, String.valueOf(System.currentTimeMillis()), JsonValueBuilder.toJsonValue(json));
        debug("READ by {0}: Read token resource with ID: {1}", principal, tokenId);
        return newResultPromise(response);
    } catch (CoreTokenException e) {
        error(e, "READ by {0}: Error reading token resource with ID: {1}", principal, tokenId);
        return generateException(e).asPromise();
    }
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) Token(org.forgerock.openam.cts.api.tokens.Token)

Example 98 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class CoreTokenResource method deleteInstance.

/**
     * Delete the instance referred to by its Resource path.
     *
     * @param serverContext Required context.
     * @param tokenId The TokenID of the token to delete.
     * @param deleteRequest Not used.
     */
public Promise<ResourceResponse, ResourceException> deleteInstance(Context serverContext, String tokenId, DeleteRequest deleteRequest) {
    String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
    try {
        store.deleteAsync(tokenId);
        Map<String, String> result = new HashMap<String, String>();
        result.put(TOKEN_ID, tokenId);
        ResourceResponse resource = newResourceResponse(tokenId, String.valueOf(System.currentTimeMillis()), new JsonValue(result));
        debug("DELETE by {0}: Deleted token resource with ID: {1}", principal, tokenId);
        return newResultPromise(resource);
    } catch (CoreTokenException e) {
        error(e, "DELETE by {0}: Error deleting token resource with ID: {1}", principal, tokenId);
        return generateException(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) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException)

Example 99 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class CoreTokenResource method updateInstance.

/**
     * Update a Token based on the Resource path.
     *
     * @param serverContext Required context.
     * @param tokenId The tokenId to update. This must be the same TokenId as the serialised Token.
     * @param updateRequest Contains the JSON serialised Token to update.
     */
public Promise<ResourceResponse, ResourceException> updateInstance(Context serverContext, String tokenId, UpdateRequest updateRequest) {
    String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
    String value = updateRequest.getContent().toString();
    Token newToken = serialisation.deserialise(value, Token.class);
    try {
        store.updateAsync(newToken);
        ResourceResponse resource = newResourceResponse(newToken.getTokenId(), String.valueOf(System.currentTimeMillis()), new JsonValue("Token Updated"));
        debug("UPDATE by {0}: Updated token resource with ID: {1}", principal, tokenId);
        return newResultPromise(resource);
    } catch (CoreTokenException e) {
        error(e, "UPDATE by {0}: Error updating token resource with ID: {1}", principal, tokenId);
        return generateException(e).asPromise();
    }
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) Token(org.forgerock.openam.cts.api.tokens.Token)

Example 100 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class IdentityResourceV2 method createInstance.

/**
     * {@inheritDoc}
     */
@Override
public Promise<ResourceResponse, ResourceException> createInstance(final Context context, final CreateRequest request) {
    RealmContext realmContext = context.asContext(RealmContext.class);
    final String realm = realmContext.getResolvedRealm();
    try {
        // anyone can create an account add
        SSOToken admin = getSSOToken(getCookieFromServerContext(context));
        final JsonValue jVal = request.getContent();
        String resourceId = request.getNewResourceId();
        IdentityDetails identity = jsonValueToIdentityDetails(objectType, jVal, realm);
        // check to see if request has included resource ID
        if (resourceId != null) {
            if (identity.getName() != null) {
                if (!resourceId.equalsIgnoreCase(identity.getName())) {
                    ResourceException be = new BadRequestException("id in path does not match id in request body");
                    debug.error("IdentityResource.createInstance() :: Cannot CREATE ", be);
                    return be.asPromise();
                }
            }
            identity.setName(resourceId);
        } else {
            resourceId = identity.getName();
        }
        UserAttributeInfo userAttributeInfo = configHandler.getConfig(realm, UserAttributeInfoBuilder.class);
        enforceWhiteList(context, request.getContent(), objectType, userAttributeInfo.getValidCreationAttributes());
        final String id = resourceId;
        return attemptResourceCreation(realm, admin, identity, resourceId).thenAsync(new AsyncFunction<IdentityDetails, ResourceResponse, ResourceException>() {

            @Override
            public Promise<ResourceResponse, ResourceException> apply(IdentityDetails dtls) {
                if (dtls != null) {
                    String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
                    debug.message("IdentityResource.createInstance :: CREATE of resourceId={} in realm={} " + "performed by principalName={}", id, realm, principalName);
                    ResourceResponse resource = newResourceResponse(id, "0", identityDetailsToJsonValue(dtls));
                    return newResultPromise(resource);
                } else {
                    debug.error("IdentityResource.createInstance() :: Identity not found");
                    return new NotFoundException("Identity not found").asPromise();
                }
            }
        });
    } catch (SSOException e) {
        return new ForbiddenException(e).asPromise();
    } catch (BadRequestException bre) {
        return bre.asPromise();
    }
}
Also used : UserAttributeInfo(org.forgerock.openam.core.rest.UserAttributeInfo) ForbiddenException(org.forgerock.json.resource.ForbiddenException) SSOToken(com.iplanet.sso.SSOToken) RealmContext(org.forgerock.openam.rest.RealmContext) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) SSOException(com.iplanet.sso.SSOException) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) IdentityDetails(com.sun.identity.idsvcs.IdentityDetails) BadRequestException(org.forgerock.json.resource.BadRequestException) ResourceException(org.forgerock.json.resource.ResourceException)

Aggregations

ResourceResponse (org.forgerock.json.resource.ResourceResponse)227 ResourceException (org.forgerock.json.resource.ResourceException)171 Test (org.testng.annotations.Test)154 JsonValue (org.forgerock.json.JsonValue)123 Context (org.forgerock.services.context.Context)83 RealmContext (org.forgerock.openam.rest.RealmContext)72 Subject (javax.security.auth.Subject)57 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)48 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)44 CreateRequest (org.forgerock.json.resource.CreateRequest)41 ClientContext (org.forgerock.services.context.ClientContext)41 ArrayList (java.util.ArrayList)38 EntitlementException (com.sun.identity.entitlement.EntitlementException)30 ReadRequest (org.forgerock.json.resource.ReadRequest)30 NotFoundException (org.forgerock.json.resource.NotFoundException)29 UpdateRequest (org.forgerock.json.resource.UpdateRequest)29 Matchers.anyString (org.mockito.Matchers.anyString)28 HashSet (java.util.HashSet)27 BadRequestException (org.forgerock.json.resource.BadRequestException)26 DeleteRequest (org.forgerock.json.resource.DeleteRequest)26