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();
}
}
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());
}
}
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));
}
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);
}
}
Aggregations