Search in sources :

Example 16 with ScriptException

use of javax.script.ScriptException in project OpenAM by OpenRock.

the class RhinoScriptEngine method eval.

/**
     * {@inheritDoc}
     */
@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
    Reject.ifNull(reader, scriptContext);
    Object result = null;
    final Context context = factory.getContext();
    try {
        final Scriptable scope = getScope(context, scriptContext);
        final String filename = getFilename(scriptContext);
        result = context.evaluateReader(scope, reader, filename, 1, null);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } catch (IOException ex) {
        throw new ScriptException(ex);
    } finally {
        factory.releaseContext(context);
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ScriptException(javax.script.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException) Scriptable(org.mozilla.javascript.Scriptable)

Example 17 with ScriptException

use of javax.script.ScriptException in project OpenAM by OpenRock.

the class RhinoCompiledScriptTest method shouldPropagateExceptions.

@Test(expectedExceptions = ScriptException.class)
public void shouldPropagateExceptions() throws ScriptException {
    // Given
    ScriptContext context = mock(ScriptContext.class);
    given(mockEngine.evalCompiled(mockScript, context)).willThrow(new ScriptException("test exception"));
    // When
    testScript.eval(context);
// Then - exception
}
Also used : ScriptException(javax.script.ScriptException) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 18 with ScriptException

use of javax.script.ScriptException in project OpenAM by OpenRock.

the class OpenAMScopeValidator method getUserInfo.

/**
     * {@inheritDoc}
     */
public UserInfoClaims getUserInfo(AccessToken token, OAuth2Request request) throws UnauthorizedClientException, NotFoundException {
    Map<String, Object> response = new HashMap<>();
    Bindings scriptVariables = new SimpleBindings();
    SSOToken ssoToken = getUsersSession(request);
    String realm;
    Set<String> scopes;
    AMIdentity id;
    OAuth2ProviderSettings providerSettings = providerSettingsFactory.get(request);
    Map<String, Set<String>> requestedClaimsValues = gatherRequestedClaims(providerSettings, request, token);
    try {
        if (token != null) {
            OpenIdConnectClientRegistration clientRegistration;
            try {
                clientRegistration = clientRegistrationStore.get(token.getClientId(), request);
            } catch (InvalidClientException e) {
                logger.message("Unable to retrieve client from store.");
                throw new NotFoundException("No valid client registration found.");
            }
            final String subId = clientRegistration.getSubValue(token.getResourceOwnerId(), providerSettings);
            //data comes from token when we have one
            realm = token.getRealm();
            scopes = token.getScope();
            id = identityManager.getResourceOwnerIdentity(token.getResourceOwnerId(), realm);
            response.put(OAuth2Constants.JWTTokenParams.SUB, subId);
            response.put(OAuth2Constants.JWTTokenParams.UPDATED_AT, getUpdatedAt(token.getResourceOwnerId(), token.getRealm(), request));
        } else {
            //otherwise we're simply reading claims into the id_token, so grab it from the request/ssoToken
            realm = DNMapper.orgNameToRealmName(ssoToken.getProperty(ISAuthConstants.ORGANIZATION));
            id = identityManager.getResourceOwnerIdentity(ssoToken.getProperty(ISAuthConstants.USER_ID), realm);
            String scopeStr = request.getParameter(OAuth2Constants.Params.SCOPE);
            scopes = splitScope(scopeStr);
        }
        scriptVariables.put(OAuth2Constants.ScriptParams.SCOPES, getScriptFriendlyScopes(scopes));
        scriptVariables.put(OAuth2Constants.ScriptParams.IDENTITY, id);
        scriptVariables.put(OAuth2Constants.ScriptParams.LOGGER, logger);
        scriptVariables.put(OAuth2Constants.ScriptParams.CLAIMS, response);
        scriptVariables.put(OAuth2Constants.ScriptParams.SESSION, ssoToken);
        scriptVariables.put(OAuth2Constants.ScriptParams.REQUESTED_CLAIMS, requestedClaimsValues);
        ScriptObject script = getOIDCClaimsExtensionScript(realm);
        try {
            return scriptEvaluator.evaluateScript(script, scriptVariables);
        } catch (ScriptException e) {
            logger.message("Error running OIDC claims script", e);
            throw new ServerException("Error running OIDC claims script: " + e.getMessage());
        }
    } catch (ServerException e) {
        //API does not allow ServerExceptions to be thrown!
        throw new NotFoundException(e.getMessage());
    } catch (SSOException e) {
        throw new NotFoundException(e.getMessage());
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) OpenIdConnectClientRegistration(org.forgerock.openidconnect.OpenIdConnectClientRegistration) SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) HashSet(java.util.HashSet) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) AMHashMap(com.iplanet.am.sdk.AMHashMap) HashMap(java.util.HashMap) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) SSOException(com.iplanet.sso.SSOException) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) AMIdentity(com.sun.identity.idm.AMIdentity) InvalidClientException(org.forgerock.oauth2.core.exceptions.InvalidClientException) JSONObject(org.json.JSONObject) ScriptObject(org.forgerock.openam.scripting.ScriptObject) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings)

Example 19 with ScriptException

use of javax.script.ScriptException in project enhydrator by AdamBien.

the class ScriptableSink method init.

@Override
public void init() {
    requireNonNull(this.scriptFile, "Cannot initialize ScriptableSink without script.");
    try {
        this.scriptContent = load(this.scriptFile);
    } catch (IOException ex) {
        throw new IllegalStateException("Cannot load script from: " + this.scriptFile, ex);
    }
    ScriptEngineManager sem = new ScriptEngineManager();
    this.engine = sem.getEngineByName("javascript");
    try {
        this.engine.eval(this.scriptContent);
    } catch (ScriptException ex) {
        throw new IllegalStateException("Parsing script failed. Problem in line: " + ex.getLineNumber() + " and column: " + ex.getColumnNumber(), ex);
    }
    this.invocable = (Invocable) engine;
    this.sink = this.invocable.getInterface(Sink.class);
    requireNonNull(this.sink, "Sink instantiation failed - script: " + this.scriptContent + " is incompatible");
    this.sink.init();
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException)

Example 20 with ScriptException

use of javax.script.ScriptException in project enhydrator by AdamBien.

the class FilterExpression method execute.

public Boolean execute(Row columns, String expression) {
    Bindings bindings = ScriptingEnvironmentProvider.create(this.manager, this.scriptEngineBindings, columns);
    try {
        this.expressionListener.accept("Executing: " + expression);
        Object result = this.engine.eval(expression, bindings);
        this.expressionListener.accept("Got result: " + result);
        if (!(result instanceof Boolean)) {
            return Boolean.FALSE;
        } else {
            return (Boolean) result;
        }
    } catch (ScriptException ex) {
        throw new IllegalStateException(ex.getMessage(), ex);
    }
}
Also used : ScriptException(javax.script.ScriptException) Bindings(javax.script.Bindings)

Aggregations

ScriptException (javax.script.ScriptException)98 IOException (java.io.IOException)41 ScriptEngine (javax.script.ScriptEngine)36 Bindings (javax.script.Bindings)27 ScriptEngineManager (javax.script.ScriptEngineManager)17 CompiledScript (javax.script.CompiledScript)12 InputStreamReader (java.io.InputStreamReader)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)10 Map (java.util.Map)9 SimpleBindings (javax.script.SimpleBindings)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)7 File (java.io.File)6 Writer (java.io.Writer)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)4