Search in sources :

Example 6 with ScriptObject

use of org.forgerock.openam.scripting.ScriptObject 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)

Example 7 with ScriptObject

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

the class OidcClaimsExtensionTest method setupScript.

@BeforeClass
public void setupScript() throws Exception {
    String rawScript = IOUtils.readStream(this.getClass().getClassLoader().getResourceAsStream("oidc-claims-extension.groovy"));
    SupportedScriptingLanguage scriptType = SupportedScriptingLanguage.GROOVY;
    this.script = new ScriptObject("oidc-claims-script", rawScript, scriptType, null);
    StandardScriptEngineManager scriptEngineManager = new StandardScriptEngineManager();
    scriptEngineManager.registerEngineName(SupportedScriptingLanguage.GROOVY_ENGINE_NAME, new GroovyScriptEngineFactory());
    scriptEvaluator = new StandardScriptEvaluator(scriptEngineManager);
}
Also used : StandardScriptEngineManager(org.forgerock.openam.scripting.StandardScriptEngineManager) StandardScriptEvaluator(org.forgerock.openam.scripting.StandardScriptEvaluator) ScriptObject(org.forgerock.openam.scripting.ScriptObject) GroovyScriptEngineFactory(org.codehaus.groovy.jsr223.GroovyScriptEngineFactory) SupportedScriptingLanguage(org.forgerock.openam.scripting.SupportedScriptingLanguage) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

ScriptObject (org.forgerock.openam.scripting.ScriptObject)7 Bindings (javax.script.Bindings)4 ScriptException (org.forgerock.openam.scripting.ScriptException)4 ScriptConfiguration (org.forgerock.openam.scripting.service.ScriptConfiguration)4 SSOException (com.iplanet.sso.SSOException)3 SSOToken (com.iplanet.sso.SSOToken)3 HashMap (java.util.HashMap)3 SimpleBindings (javax.script.SimpleBindings)3 AMIdentity (com.sun.identity.idm.AMIdentity)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ScriptException (javax.script.ScriptException)2 JsonValue (org.forgerock.json.JsonValue)2 NotSupportedException (org.forgerock.json.resource.NotSupportedException)2 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)2 SupportedScriptingLanguage (org.forgerock.openam.scripting.SupportedScriptingLanguage)2 AMHashMap (com.iplanet.am.sdk.AMHashMap)1 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 IdRepoException (com.sun.identity.idm.IdRepoException)1