Search in sources :

Example 11 with ApplicationWrapper

use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.

the class ApplicationsResource method createApplicationWrapper.

/**
     * Creates an {@link ApplicationWrapper} to hold the {@link Application} object, after having deserialized it
     * via Jackson.
     *
     * @param jsonValue The JSON to deserialize
     * @param mySubject The subject authorizing the request
     * @return An ApplicationWrapper containing an Application, null
     * @throws EntitlementException If there were issues generating the application wrapper
     */
protected ApplicationWrapper createApplicationWrapper(JsonValue jsonValue, Subject mySubject) throws EntitlementException {
    final ApplicationWrapper wrapp = createApplicationWrapper(jsonValue);
    final JsonValue appTypeValue = jsonValue.get("applicationType");
    if (appTypeValue.getObject() == null || appTypeValue.asString().isEmpty() || !wrapp.setApplicationType(mySubject, appTypeValue.asString())) {
        if (debug.errorEnabled()) {
            debug.error("ApplicationsResource.createApplicationWrapper() : " + "Specified Application Type was not available.");
        }
        throw new EntitlementException(EntitlementException.INVALID_APP_TYPE);
    }
    return wrapp;
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ApplicationWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper) JsonValue(org.forgerock.json.JsonValue)

Example 12 with ApplicationWrapper

use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.

the class ApplicationsResource method readInstance.

/**
     * Reads an instance of an application.
     *
     * @param context {@inheritDoc}
     * @param resourceId {@inheritDoc}
     * @param request {@inheritDoc}
     */
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
    final Subject mySubject = getContextSubject(context);
    if (mySubject == null) {
        debug.error("ApplicationsResource :: READ : Unknown Subject");
        return new BadRequestException().asPromise();
    }
    final String realm = getRealm(context);
    final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject);
    try {
        final Application app = appManager.getApplication(mySubject, realm, resourceId);
        if (app == null) {
            throw new EntitlementException(EntitlementException.APP_RETRIEVAL_ERROR, new String[] { realm });
        }
        final ApplicationWrapper wrapp = createApplicationWrapper(app, appTypeManagerWrapper);
        ResourceResponse resource = newResourceResponse(resourceId, Long.toString(app.getLastModifiedDate()), wrapp.toJsonValue());
        return newResultPromise(resource);
    } catch (EntitlementException e) {
        if (debug.errorEnabled()) {
            debug.error("ApplicationsResource :: READ by " + principalName + ": Application failed to retrieve the resource specified.", e);
        }
        return exceptionMappingHandler.handleError(context, request, e).asPromise();
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ApplicationWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper) ResourceResponse(org.forgerock.json.resource.ResourceResponse) BadRequestException(org.forgerock.json.resource.BadRequestException) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject)

Example 13 with ApplicationWrapper

use of org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper in project OpenAM by OpenRock.

the class ApplicationsResource method createInstance.

/**
     * Create an {@link Application}.
     *
     * @param context {@inheritDoc}
     * @param request {@inheritDoc}
     */
@Override
public Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request) {
    //auth
    final Subject callingSubject = getContextSubject(context);
    if (callingSubject == null) {
        debug.error("ApplicationsResource :: CREATE : Unknown Subject");
        return new BadRequestException().asPromise();
    }
    final String realm = getRealm(context);
    //select
    final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(callingSubject);
    final JsonValue creationRequest = request.getContent();
    final ApplicationWrapper wrapp;
    final Application previousApp;
    try {
        wrapp = createApplicationWrapper(creationRequest, callingSubject);
        String wrappName = wrapp.getName();
        String newResourceId = request.getNewResourceId();
        if (wrappName != null && newResourceId != null) {
            if (!wrappName.equals(newResourceId)) {
                debug.error("ApplicationsResource :: CREATE : Resource name and JSON body name do not match.");
                throw new EntitlementException(EntitlementException.APPLICATION_NAME_MISMATCH);
            }
        }
        // user.
        if (wrappName == null) {
            wrapp.setName(newResourceId);
        }
        String appName = wrapp.getApplication().getName();
        if (!appName.equals(DN.escapeAttributeValue(appName))) {
            throw new EntitlementException(EntitlementException.INVALID_VALUE, new Object[] { "policy name \"" + appName + "\"" });
        }
        previousApp = appManager.getApplication(callingSubject, realm, appName);
        if (previousApp != null) {
            //return conflict
            throw new EntitlementException(EntitlementException.APPLICATION_ALREADY_EXISTS);
        }
        appManager.saveApplication(callingSubject, realm, wrapp.getApplication());
        Application savedApp = appManager.getApplication(callingSubject, realm, appName);
        ApplicationWrapper savedAppWrapper = createApplicationWrapper(savedApp, appTypeManagerWrapper);
        ResourceResponse resource = newResourceResponse(savedAppWrapper.getName(), Long.toString(savedAppWrapper.getLastModifiedDate()), savedAppWrapper.toJsonValue());
        if (debug.messageEnabled()) {
            debug.message("ApplicationsResource :: CREATE by " + principalName + ": for Application: " + wrapp.getName());
        }
        return newResultPromise(resource);
    } catch (EntitlementException e) {
        if (debug.errorEnabled()) {
            debug.error("ApplicationsResource :: CREATE by " + principalName + ": Application creation failed. ", e);
        }
        return exceptionMappingHandler.handleError(context, request, e).asPromise();
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) ApplicationWrapper(org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject)

Aggregations

EntitlementException (com.sun.identity.entitlement.EntitlementException)13 ApplicationWrapper (org.forgerock.openam.entitlement.rest.wrappers.ApplicationWrapper)13 Subject (javax.security.auth.Subject)12 ResourceResponse (org.forgerock.json.resource.ResourceResponse)11 Application (com.sun.identity.entitlement.Application)9 JsonValue (org.forgerock.json.JsonValue)9 RealmContext (org.forgerock.openam.rest.RealmContext)7 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7 Test (org.testng.annotations.Test)7 ResourceException (org.forgerock.json.resource.ResourceException)6 ApplicationTypeManagerWrapper (org.forgerock.openam.entitlement.rest.wrappers.ApplicationTypeManagerWrapper)6 BadRequestException (org.forgerock.json.resource.BadRequestException)4 CreateRequest (org.forgerock.json.resource.CreateRequest)4 ClientContext (org.forgerock.services.context.ClientContext)4 Context (org.forgerock.services.context.Context)4 Matchers.anyString (org.mockito.Matchers.anyString)4 Set (java.util.Set)3 QueryRequest (org.forgerock.json.resource.QueryRequest)3 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)3 QueryResponse (org.forgerock.json.resource.QueryResponse)2