use of org.apache.sling.api.scripting.SlingScriptHelper in project acs-aem-commons by Adobe-Consulting-Services.
the class AvailableProcessDefinitions method activate.
@Override
@SuppressWarnings("checkstyle:parametername")
public void activate() throws Exception {
SlingScriptHelper sling = getSlingScriptHelper();
User user = sling.getRequest().getResourceResolver().adaptTo(User.class);
ProcessDefinitionFactory[] allDefinitionFactories = sling.getServices(ProcessDefinitionFactory.class, null);
definitions = Stream.of(allDefinitionFactories).filter(o -> o.isAllowed(user)).collect(Collectors.toMap(ProcessDefinitionFactory::getName, o -> o, (a, b) -> a, TreeMap::new));
String processDefinitionName = get("processDefinition", String.class);
if (StringUtils.isEmpty(processDefinitionName)) {
processDefinitionName = getRequest().getParameter("processDefinition");
}
if (StringUtils.isNotEmpty(processDefinitionName) && definitions.containsKey(processDefinitionName)) {
Class clazz = definitions.get(processDefinitionName).createProcessDefinition().getClass();
fieldComponents = AnnotatedFieldDeserializer.getFormFields(clazz, sling);
}
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class XProcScriptEngine method eval.
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
String scriptName = helper.getScript().getScriptResource().getPath();
try {
XplBuilder xplBuilder = new XplBuilder();
Pipeline xpl = (Pipeline) xplBuilder.build(reader);
xpl.getEnv().setSling(helper);
xpl.eval();
} catch (Throwable t) {
log.error("Failure running XProc script.", t);
final ScriptException se = new ScriptException("Failure running XProc script " + scriptName);
se.initCause(t);
throw se;
}
return null;
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class JavaUseProvider method provide.
@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
if (!JAVA_PATTERN.matcher(identifier).matches()) {
LOG.debug("Identifier {} does not match a Java class name pattern.", identifier);
return ProviderOutcome.failure();
}
Bindings globalBindings = renderContext.getBindings();
SlingScriptHelper sling = BindingsUtils.getHelper(globalBindings);
SlingHttpServletRequest request = BindingsUtils.getRequest(globalBindings);
Map<String, Object> overrides = setRequestAttributes(request, arguments);
Object result;
try {
result = sightlyJavaCompilerService.getResourceBackedUseObject(renderContext, identifier);
if (result != null) {
if (result instanceof Use) {
((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
}
return ProviderOutcome.success(result);
} else {
LOG.debug("Attempting to load class {} from the classloader cache.", identifier);
Class<?> cls = classLoaderWriter.getClassLoader().loadClass(identifier);
// attempt OSGi service load
result = sling.getService(cls);
if (result != null) {
return ProviderOutcome.success(result);
}
result = request.adaptTo(cls);
if (result == null) {
Resource resource = BindingsUtils.getResource(globalBindings);
result = resource.adaptTo(cls);
}
if (result != null) {
return ProviderOutcome.success(result);
} else {
/*
* the object was cached by the class loader but it's not adaptable from {@link Resource} or {@link
* SlingHttpServletRequest}; attempt to load it like a regular POJO that optionally could implement {@link Use}
*/
result = cls.newInstance();
if (result instanceof Use) {
((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
}
return ProviderOutcome.notNullOrFailure(result);
}
}
} catch (Exception e) {
// any other exception is an error
return ProviderOutcome.failure(e);
} finally {
resetRequestAttribute(request, overrides);
}
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class I18nRuntimeExtension method get.
private String get(final Bindings bindings, String text, String locale, String basename, String hint) {
final SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
final SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
final ResourceBundleProvider resourceBundleProvider = slingScriptHelper.getService(ResourceBundleProvider.class);
if (resourceBundleProvider != null) {
String key = text;
if (StringUtils.isNotEmpty(hint)) {
key += " ((" + hint + "))";
}
if (StringUtils.isEmpty(locale)) {
Enumeration<Locale> requestLocales = request.getLocales();
while (requestLocales.hasMoreElements()) {
Locale l = requestLocales.nextElement();
String translation = getTranslation(resourceBundleProvider, basename, key, l);
if (translation != null) {
return translation;
}
}
} else {
try {
Locale l = LocaleUtils.toLocale(locale);
String translation = getTranslation(resourceBundleProvider, basename, key, l);
if (translation != null) {
return translation;
}
} catch (IllegalArgumentException e) {
LOG.warn("Invalid locale detected: {}.", locale);
return text;
}
}
}
LOG.warn("No translation found for string '{}' using expression provided locale '{}' or default locale '{}'", text, locale, request.getLocale().getLanguage());
return text;
}
use of org.apache.sling.api.scripting.SlingScriptHelper in project sling by apache.
the class JsUseProvider method provide.
@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
Bindings globalBindings = renderContext.getBindings();
if (!Utils.isJsScript(identifier)) {
return ProviderOutcome.failure();
}
ScriptEngine jsEngine = scriptEngineManager.getEngineByName(JS_ENGINE_NAME);
if (jsEngine == null) {
return ProviderOutcome.failure(new SightlyException("No JavaScript engine was defined."));
}
SlingScriptHelper scriptHelper = Utils.getHelper(globalBindings);
JsEnvironment environment = null;
try {
environment = new JsEnvironment(jsEngine);
environment.initialize();
ResourceResolver slingScriptingResolver = scriptingResourceResolverProvider.getRequestScopedResourceResolver();
Resource callerScript = slingScriptingResolver.getResource(scriptHelper.getScript().getScriptResource().getPath());
Resource scriptResource = Utils.getScriptResource(callerScript, identifier, globalBindings);
globalBindings.put(ScriptEngine.FILENAME, scriptResource.getPath());
proxyAsyncScriptableFactory.registerProxies(globalBindings);
AsyncContainer asyncContainer = environment.runResource(scriptResource, globalBindings, arguments);
return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
} finally {
if (environment != null) {
environment.cleanup();
}
}
}
Aggregations