Search in sources :

Example 11 with ScriptConfiguration

use of org.forgerock.openam.scripting.service.ScriptConfiguration in project OpenAM by OpenRock.

the class BatchResource method actionCollection.

@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context serverContext, ActionRequest actionRequest) {
    if (!actionRequest.getAction().equals(BATCH)) {
        final String msg = "Action '" + actionRequest.getAction() + "' not implemented for this resource";
        final NotSupportedException exception = new NotSupportedException(msg);
        debug.error(msg, exception);
        return exception.asPromise();
    }
    String scriptId = null;
    try {
        JsonValue scriptIdValue = actionRequest.getContent().get(SCRIPT_ID);
        if (scriptIdValue == null) {
            if (debug.errorEnabled()) {
                debug.error("BatchResource :: actionCollection - ScriptId null. Default scripts not implemented.");
            }
            return new BadRequestException().asPromise();
        } else {
            scriptId = scriptIdValue.asString();
        }
        final JsonValue requests = actionRequest.getContent().get(REQUESTS);
        final String realm = getRealm(serverContext);
        final ScriptConfiguration scriptConfig = scriptingServiceFactory.create(SubjectUtils.createSuperAdminSubject(), realm).get(scriptId);
        final ScriptObject script = new ScriptObject(scriptConfig.getName(), scriptConfig.getScript(), scriptConfig.getLanguage());
        final ScriptResponse response = new ScriptResponse();
        final Bindings bindings = new SimpleBindings();
        bindings.put(PAYLOAD, requests);
        bindings.put(CONTEXT, serverContext);
        bindings.put(LOGGER, debug);
        bindings.put(REQUESTER, requester);
        bindings.put(RESPONSE, response);
        return newResultPromise(newActionResponse((JsonValue) scriptEvaluator.evaluateScript(script, bindings)));
    } catch (ScriptException e) {
        debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
        return exceptionMappingHandler.handleError(serverContext, actionRequest, e).asPromise();
    } catch (javax.script.ScriptException e) {
        debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId);
        return new InternalServerErrorException().asPromise();
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) JsonValue(org.forgerock.json.JsonValue) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptException(org.forgerock.openam.scripting.ScriptException) SimpleBindings(javax.script.SimpleBindings) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ScriptResponse(org.forgerock.openam.scripting.rest.batch.helpers.ScriptResponse)

Aggregations

ScriptConfiguration (org.forgerock.openam.scripting.service.ScriptConfiguration)11 ScriptException (org.forgerock.openam.scripting.ScriptException)7 SSOException (com.iplanet.sso.SSOException)5 SMSException (com.sun.identity.sm.SMSException)4 ScriptObject (org.forgerock.openam.scripting.ScriptObject)4 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)3 Bindings (javax.script.Bindings)3 SSOToken (com.iplanet.sso.SSOToken)2 AuthSPrincipal (com.sun.identity.rest.AuthSPrincipal)2 ServiceConfig (com.sun.identity.sm.ServiceConfig)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 SimpleBindings (javax.script.SimpleBindings)2 Subject (javax.security.auth.Subject)2 ResourceResponse (org.forgerock.json.resource.ResourceResponse)2 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 AMIdentity (com.sun.identity.idm.AMIdentity)1 IdRepoException (com.sun.identity.idm.IdRepoException)1