public Promise<ResourceResponse, ResourceException> handleUpdate( Context context, UpdateRequest request) { EventEntry measure = Publisher.start( Name.get( "openidm/internal/script/" + this.getScriptEntry().getName().getName() + "/update"), null, null); try { final ScriptEntry _scriptEntry = getScriptEntry(); if (!_scriptEntry.isActive()) { throw new ServiceUnavailableException("Inactive script: " + _scriptEntry.getName()); } final Script script = _scriptEntry.getScript(context); script.setBindings(script.createBindings()); customizer.handleUpdate(context, request, script.getBindings()); return evaluate(request, script); } catch (ScriptException e) { return convertScriptException(e).asPromise(); } catch (ResourceException e) { return e.asPromise(); } catch (Exception e) { return new InternalServerErrorException(e.getMessage(), e).asPromise(); } finally { measure.end(); } }
/** * Handle sync failure by counting retries on this sync token, passing to (optional) post-retry * handler when retries are exceeded. * * @param syncFailure @throws SyncHandlerException when retries are not exceeded * @param failureCause the cause of the sync failure */ public void invoke(Map<String, Object> syncFailure, Exception failureCause) throws SyncHandlerException { if (null == scriptEntry) { throw new SyncHandlerException("No script registered"); } Script script = scriptEntry.getScript(new RootContext()); script.put("syncFailure", syncFailure); script.put("failureCause", failureCause); script.put("failureHandlers", builtInHandlers); try { script.eval(); } catch (Exception e) { logger.debug( "sync failure script on {} encountered exception", syncFailure.get("systemIdentifier"), e); throw new SyncHandlerException( "Issue with handling the failure during synchronize " + syncFailure.get("uid") + " object: " + failureCause.getMessage() + ". Failure handling reported " + e.getMessage(), e); } }
public Promise<ActionResponse, ResourceException> handleAction( final Context context, final ActionRequest request) { EventEntry measure = Publisher.start( Name.get( "openidm/internal/script/" + this.getScriptEntry().getName().getName() + "/action"), null, null); try { final ScriptEntry _scriptEntry = getScriptEntry(); if (!_scriptEntry.isActive()) { throw new ServiceUnavailableException("Inactive script: " + _scriptEntry.getName()); } final Script script = _scriptEntry.getScript(context); script.setBindings(script.createBindings()); customizer.handleAction(context, request, script.getBindings()); Object result = script.eval(); if (null == result) { return newActionResponse(new JsonValue(null)).asPromise(); } else if (result instanceof JsonValue) { return newActionResponse((JsonValue) result).asPromise(); } else if (result instanceof Map) { return newActionResponse(new JsonValue(result)).asPromise(); } else { JsonValue resource = new JsonValue(new HashMap<String, Object>(1)); resource.put("result", result); return newActionResponse(new JsonValue(result)).asPromise(); } } catch (ScriptException e) { return convertScriptException(e).asPromise(); } catch (ResourceException e) { return e.asPromise(); } catch (Exception e) { return new InternalServerErrorException(e.getMessage(), e).asPromise(); } finally { measure.end(); } }
private Promise<ResourceResponse, ResourceException> evaluate( final Request request, final Script script) throws ScriptException { Object result = script.eval(); ResourcePath resourcePath = request.getResourcePathObject(); if (null == result) { return new NotFoundException("script returned null").asPromise(); } JsonValue resultJson = (result instanceof JsonValue) ? (JsonValue) result : new JsonValue(result); // If the resultJson isn't able to provide an ID, then we default to the resourcePath. String id = resultJson.get(ResourceResponse.FIELD_CONTENT_ID).defaultTo("").asString(); if (id.isEmpty() && resourcePath.size() > 0) { id = resourcePath.leaf(); } return newResourceResponse(id, null, resultJson).asPromise(); }
/** * TODO Implement this method * * <p>{@inheritDoc} */ public Promise<QueryResponse, ResourceException> handleQuery( final Context context, final QueryRequest request, final QueryResourceHandler handler) { EventEntry measure = Publisher.start( Name.get( "openidm/internal/script/" + this.getScriptEntry().getName().getName() + "/query"), null, null); try { final ScriptEntry _scriptEntry = getScriptEntry(); if (!_scriptEntry.isActive()) { throw new ServiceUnavailableException("Inactive script: " + _scriptEntry.getName()); } final Script script = _scriptEntry.getScript(context); script.setBindings(script.createBindings()); customizer.handleQuery(context, request, script.getBindings()); final Function<Void> queryCallback = new Function<Void>() { @Override public Void call(Parameter scope, Function<?> callback, Object... arguments) throws ResourceException, NoSuchMethodException { if (arguments.length == 3 && null != arguments[2]) { if (arguments[2] instanceof Map) {} if (arguments[2] instanceof JsonValue) { } else { throw new NoSuchMethodException( FunctionFactory.getNoSuchMethodMessage("callback", arguments)); } } else if (arguments.length >= 2 && null != arguments[1]) { if (arguments[1] instanceof Map) {} if (arguments[1] instanceof JsonValue) { } else { throw new NoSuchMethodException( FunctionFactory.getNoSuchMethodMessage("callback", arguments)); } } else if (arguments.length >= 1 && null != arguments[0]) { if (arguments[0] instanceof Map) {} if (arguments[0] instanceof JsonValue) { } else { throw new NoSuchMethodException( FunctionFactory.getNoSuchMethodMessage("callback", arguments)); } } else { throw new NoSuchMethodException( FunctionFactory.getNoSuchMethodMessage("callback", arguments)); } return null; } }; script.putSafe("callback", queryCallback); Object rawResult = script.eval(); JsonValue result = null; if (rawResult instanceof JsonValue) { result = (JsonValue) rawResult; } else { result = new JsonValue(rawResult); } QueryResponse queryResponse = newQueryResponse(); // Script can either // - return null and instead use callback hook to call // handleResource, handleResult, handleError // careful! script MUST call handleResult or handleError itself // or // - return a result list of resources // or // - return a full query result structure if (!result.isNull()) { if (result.isList()) { // Script may return just the result elements as a list handleQueryResultList(result, handler); } else { // Or script may return a full query response structure, // with meta-data and results field if (result.isDefined(QueryResponse.FIELD_RESULT)) { handleQueryResultList(result.get(QueryResponse.FIELD_RESULT), handler); queryResponse = newQueryResponse( result.get(QueryResponse.FIELD_PAGED_RESULTS_COOKIE).asString(), result .get(QueryResponse.FIELD_TOTAL_PAGED_RESULTS_POLICY) .asEnum(CountPolicy.class), result.get(QueryResponse.FIELD_TOTAL_PAGED_RESULTS).asInteger()); } else { logger.debug("Script returned unexpected query result structure: ", result.getObject()); return new InternalServerErrorException( "Script returned unexpected query result structure of type " + result.getObject().getClass()) .asPromise(); } } } return queryResponse.asPromise(); } catch (ScriptException e) { return convertScriptException(e).asPromise(); } catch (ResourceException e) { return e.asPromise(); } catch (Exception e) { return new InternalServerErrorException(e.getMessage(), e).asPromise(); } finally { measure.end(); } }