Search in sources :

Example 11 with ScriptException

use of org.forgerock.openam.scripting.ScriptException 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 12 with ScriptException

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

the class ScriptExceptionMappingHandlerTest method shouldHandleAllScriptExceptionCodes.

@Test
public void shouldHandleAllScriptExceptionCodes() throws Exception {
    for (ScriptErrorCode errorCode : ScriptErrorCode.values()) {
        // when
        ResourceException re = mappingHandler.handleError(new ScriptException(errorCode));
        // then
        assertNotNull(re);
        assertNotNull(re.getMessage());
        assertTrue(isClientError(re.getCode()) || re.isServerError());
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) ScriptErrorCode(org.forgerock.openam.scripting.ScriptConstants.ScriptErrorCode) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Example 13 with ScriptException

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

the class ScriptResourceTest method setUp.

@BeforeMethod
public void setUp() throws ResourceException {
    Logger logger = mock(Logger.class);
    ScriptingService scriptingService = new MockScriptingService();
    ScriptingServiceFactory serviceFactory = mock(ScriptingServiceFactory.class);
    when(serviceFactory.create(any(Subject.class), anyString())).thenReturn(scriptingService);
    ExceptionMappingHandler<ScriptException, ResourceException> errorHandler = new ScriptExceptionMappingHandler();
    scriptResource = new ScriptResource(logger, serviceFactory, errorHandler, new StandardScriptValidator(new StandardScriptEngineManager()));
    context = mock(Context.class);
    given(context.asContext(HttpContext.class)).willReturn(new HttpContext(json(object(field("headers", Collections.emptyMap()), field("parameters", Collections.emptyMap()))), null));
}
Also used : StandardScriptEngineManager(org.forgerock.openam.scripting.StandardScriptEngineManager) HttpContext(org.forgerock.json.resource.http.HttpContext) ScriptContext(org.forgerock.openam.scripting.ScriptConstants.ScriptContext) Context(org.forgerock.services.context.Context) ScriptingService(org.forgerock.openam.scripting.service.ScriptingService) HttpContext(org.forgerock.json.resource.http.HttpContext) StandardScriptValidator(org.forgerock.openam.scripting.StandardScriptValidator) Logger(org.slf4j.Logger) ScriptingServiceFactory(org.forgerock.openam.scripting.service.ScriptingServiceFactory) Subject(javax.security.auth.Subject) ScriptException(org.forgerock.openam.scripting.ScriptException) ResourceException(org.forgerock.json.resource.ResourceException) ScriptExceptionMappingHandler(org.forgerock.openam.scripting.rest.ScriptExceptionMappingHandler) ScriptResource(org.forgerock.openam.scripting.rest.ScriptResource) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 14 with ScriptException

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

the class ScriptConfigurationServiceTest method shouldFailIfUuidDoesNotExistOnGet.

@Test
public void shouldFailIfUuidDoesNotExistOnGet() throws ScriptException {
    // given
    String uuid = "1234567890";
    when(dataStore.containsUuid(anyString())).thenReturn(false);
    // when
    try {
        service.delete(uuid);
        fail("shouldFailIfUuidDoesNotExistOnGet");
    } catch (ScriptException e) {
        // then
        assertEquals(e.getScriptErrorCode(), SCRIPT_UUID_NOT_FOUND);
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

ScriptException (org.forgerock.openam.scripting.ScriptException)14 Test (org.testng.annotations.Test)8 ScriptConfiguration (org.forgerock.openam.scripting.service.ScriptConfiguration)7 ScriptObject (org.forgerock.openam.scripting.ScriptObject)4 HashMap (java.util.HashMap)3 Bindings (javax.script.Bindings)3 Subject (javax.security.auth.Subject)3 ResourceException (org.forgerock.json.resource.ResourceException)3 SSOException (com.iplanet.sso.SSOException)2 SSOToken (com.iplanet.sso.SSOToken)2 AuthSPrincipal (com.sun.identity.rest.AuthSPrincipal)2 Set (java.util.Set)2 SimpleBindings (javax.script.SimpleBindings)2 JsonValue (org.forgerock.json.JsonValue)2 NotSupportedException (org.forgerock.json.resource.NotSupportedException)2 HttpContext (org.forgerock.json.resource.http.HttpContext)2 ScriptErrorCode (org.forgerock.openam.scripting.ScriptConstants.ScriptErrorCode)2 Context (org.forgerock.services.context.Context)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)1