use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.
the class ExportServlet method addScriptBindings.
private void addScriptBindings(SlingScriptHelper scriptHelper, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
SimpleBindings bindings = new SimpleBindings();
bindings.put(SLING, scriptHelper);
bindings.put(RESOURCE, request.getResource());
bindings.put(RESOLVER, request.getResource().getResourceResolver());
bindings.put(REQUEST, request);
bindings.put(RESPONSE, response);
bindings.put(READER, request.getReader());
bindings.put(OUT, response.getWriter());
bindings.put(LOG, logger);
final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProvidersByContext.getBindingsValuesProviders(scriptEngineFactory, BINDINGS_CONTEXT);
if (!bindingsValuesProviders.isEmpty()) {
Set<String> protectedKeys = new HashSet<String>();
protectedKeys.addAll(PROTECTED_KEYS);
ProtectedBindings protectedBindings = new ProtectedBindings(bindings, protectedKeys);
for (BindingsValuesProvider provider : bindingsValuesProviders) {
provider.addBindings(protectedBindings);
}
}
SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
request.setAttribute(SlingBindings.class.getName(), slingBindings);
}
use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.
the class SlingBindingsVariablesListJsonServlet method getBindingsByEngine.
/**
* Gets the {@link Bindings} object for the given {@link ScriptEngineFactory}.
* It only considers the default context "request".
*
* @see <a href="https://issues.apache.org/jira/browse/SLING-3038">binding contexts(SLING-3083)</a>
*
* @param scriptEngineFactory the factory of the script engine, for which to retrieve the bindings
* @param request the current request (necessary to create the bindings)
* @param response the current response (necessary to create the bindings)
* @return the bindings (list of key/value pairs) as defined by {@link Bindings} for the given script engine.
* @throws IOException
*/
private Bindings getBindingsByEngine(ScriptEngineFactory scriptEngineFactory, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
// use default context only
String context = SlingScriptAdapterFactory.BINDINGS_CONTEXT;
final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(scriptEngineFactory, context);
Resource invalidScriptResource = new NonExistingResource(request.getResourceResolver(), "some/invalid/scriptpath");
DefaultSlingScript defaultSlingScript = new DefaultSlingScript(bundleContext, invalidScriptResource, scriptEngineFactory.getScriptEngine(), bindingsValuesProviders, null, null);
// prepare the bindings (similar as in DefaultSlingScript#service)
final SlingBindings initalBindings = new SlingBindings();
initalBindings.setRequest(request);
initalBindings.setResponse(response);
final Bindings bindings = defaultSlingScript.verifySlingBindings(initalBindings);
// only thing being added in {DefaultSlingScript#call(...)} is resource resolver
bindings.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, request.getResourceResolver());
return bindings;
}
use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.
the class SlingScriptAdapterFactory method getAdapter.
// ---------- AdapterFactory -----------------------------------------------
@Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
Resource resource = (Resource) adaptable;
String path = resource.getPath();
String ext = path.substring(path.lastIndexOf('.') + 1);
ScriptEngine engine = scriptEngineManager.getEngineByExtension(ext);
if (engine != null) {
final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(engine.getFactory(), BINDINGS_CONTEXT);
// unchecked cast
return (AdapterType) new DefaultSlingScript(this.bundleContext, resource, engine, bindingsValuesProviders, this.serviceCache, scriptCache);
}
return null;
}
use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.
the class BindingsValuesProvidersByContextIT method addBVP.
private void addBVP(final String id, String context, String engineName) {
final BindingsValuesProvider bvp = new BindingsValuesProvider() {
@Override
public String toString() {
return id;
}
@Override
public void addBindings(Bindings b) {
}
};
regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp, getProperties(context, engineName)));
}
use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.
the class BindingsValuesProvidersByContextIT method addBVPWithServiceRanking.
private void addBVPWithServiceRanking(final String id, String context, String engineName, int serviceRanking) {
final BindingsValuesProvider bvp = new BindingsValuesProvider() {
@Override
public String toString() {
return id;
}
@Override
public void addBindings(Bindings b) {
}
};
final Dictionary<String, Object> properties = getProperties(context, engineName);
properties.put(Constants.SERVICE_RANKING, serviceRanking);
regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp, properties));
}
Aggregations