use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class RewriterFilter method doFilter.
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (!(request instanceof SlingHttpServletRequest)) {
throw new ServletException("Request is not a Apache Sling HTTP request.");
}
final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
final RewriterResponse rewriterResponse = new RewriterResponse(slingRequest, slingResponse, this.pipelineManager);
boolean errorOccured = true;
try {
chain.doFilter(request, rewriterResponse);
errorOccured = false;
} finally {
rewriterResponse.finished(errorOccured);
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class ScriptUtils method resolveScript.
public static Resource resolveScript(ResourceResolver resolver, RenderContext renderContext, String scriptIdentifier) {
SlingHttpServletRequest request = BindingsUtils.getRequest(renderContext.getBindings());
Resource caller = ResourceResolution.getResourceForRequest(resolver, request);
Resource result = ResourceResolution.getResourceFromSearchPath(caller, scriptIdentifier);
if (result == null) {
SlingScriptHelper sling = BindingsUtils.getHelper(renderContext.getBindings());
caller = resolver.getResource(sling.getScript().getScriptResource().getPath());
result = ResourceResolution.getResourceFromSearchPath(caller, scriptIdentifier);
}
return result;
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class RequestAdapterFactory method getAdapter.
@Override
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (type == RequestAdapterUseObject.class && adaptable instanceof SlingHttpServletRequest) {
SlingHttpServletRequest request = (SlingHttpServletRequest) adaptable;
Resource resource = request.getResource();
ValueMap properties = resource.adaptTo(ValueMap.class);
String title = (String) properties.get("jcr:title");
return (AdapterType) new RequestAdapterUseObjectImpl(title);
}
return null;
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class SlingModelsUseProvider method provide.
@Override
public ProviderOutcome provide(final String identifier, final RenderContext renderContext, final Bindings arguments) {
if (!JAVA_PATTERN.matcher(identifier).matches()) {
LOGGER.debug("Identifier {} does not match a Java class name pattern.", identifier);
return ProviderOutcome.failure();
}
final Class<?> cls;
try {
cls = dynamicClassLoaderManager.getDynamicClassLoader().loadClass(identifier);
} catch (ClassNotFoundException e) {
LOGGER.debug("Could not find class with the given name {}.", identifier);
// next use provider will be queried
return ProviderOutcome.failure();
}
if (!modelFactory.isModelClass(cls)) {
LOGGER.debug("{} is not a Sling Model.");
// next use provider will be queried
return ProviderOutcome.failure();
}
Bindings globalBindings = renderContext.getBindings();
SlingHttpServletRequest request = (SlingHttpServletRequest) globalBindings.get(SlingBindings.REQUEST);
if (request == null) {
return ProviderOutcome.failure(new IllegalStateException("Could not get request from bindings."));
}
// pass parameters as request attributes
Map<String, Object> overrides = setRequestAttributes(request, arguments);
try {
// try to instantiate class via Sling Models (first via request, then via resource)
if (modelFactory.canCreateFromAdaptable(request, cls)) {
LOGGER.debug("Trying to instantiate class {} as Sling Model from request.", cls);
return ProviderOutcome.notNullOrFailure(modelFactory.createModel(request, cls));
}
Resource resource = (Resource) globalBindings.get(SlingBindings.RESOURCE);
if (resource == null) {
LOGGER.debug("Could not get resource from bindings.");
return ProviderOutcome.failure(new IllegalStateException("Could not get resource from bindings."));
}
if (modelFactory.canCreateFromAdaptable(resource, cls)) {
LOGGER.debug("Trying to instantiate class {} as Sling Model from resource.", cls);
return ProviderOutcome.notNullOrFailure(modelFactory.createModel(resource, cls));
}
return ProviderOutcome.failure(new IllegalStateException("Could not adapt the given Sling Model from neither request nor resource: " + cls));
} catch (Throwable e) {
return ProviderOutcome.failure(e);
} finally {
resetRequestAttribute(request, overrides);
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class DefaultSlingScript method verifySlingBindings.
Bindings verifySlingBindings(final SlingBindings slingBindings) throws IOException {
final Bindings bindings = new SimpleBindings();
final SlingHttpServletRequest request = slingBindings.getRequest();
// check sling object
Object slingObject = slingBindings.get(SLING);
if (slingObject == null) {
if (request != null) {
slingObject = new InternalScriptHelper(this.bundleContext, this, request, slingBindings.getResponse(), this.cache);
} else {
slingObject = new InternalScriptHelper(this.bundleContext, this, this.cache);
}
} else if (!(slingObject instanceof SlingScriptHelper)) {
throw fail(SLING, "Wrong type");
}
final SlingScriptHelper sling = (SlingScriptHelper) slingObject;
bindings.put(SLING, sling);
if (request != null) {
final SlingHttpServletResponse response = slingBindings.getResponse();
if (response == null) {
throw fail(RESPONSE, "Missing or wrong type");
}
Object resourceObject = slingBindings.get(RESOURCE);
if (resourceObject != null && !(resourceObject instanceof Resource)) {
throw fail(RESOURCE, "Wrong type");
}
Object resolverObject = slingBindings.get(RESOLVER);
if (resolverObject != null && !(resolverObject instanceof ResourceResolver)) {
throw fail(RESOLVER, "Wrong type");
}
Object writerObject = slingBindings.get(OUT);
if (writerObject != null && !(writerObject instanceof PrintWriter)) {
throw fail(OUT, "Wrong type");
}
// if there is a provided sling script helper, check arguments
if (slingBindings.get(SLING) != null) {
if (sling.getRequest() != request) {
throw fail(REQUEST, "Not the same as request field of SlingScriptHelper");
}
if (sling.getResponse() != response) {
throw fail(RESPONSE, "Not the same as response field of SlingScriptHelper");
}
if (resourceObject != null && sling.getRequest().getResource() != resourceObject) {
throw fail(RESOURCE, "Not the same as resource of the SlingScriptHelper request");
}
if (resolverObject != null && sling.getRequest().getResourceResolver() != resolverObject) {
throw fail(RESOLVER, "Not the same as the resource resolver of the SlingScriptHelper request's resolver");
}
if (writerObject != null && sling.getResponse().getWriter() != writerObject) {
throw fail(OUT, "Not the same as writer of the SlingScriptHelper response");
}
}
// set base variables when executing inside a request
bindings.put(REQUEST, sling.getRequest());
bindings.put(READER, sling.getRequest().getReader());
bindings.put(RESPONSE, sling.getResponse());
bindings.put(RESOURCE, sling.getRequest().getResource());
bindings.put(RESOLVER, sling.getRequest().getResourceResolver());
bindings.put(OUT, sling.getResponse().getWriter());
}
Object logObject = slingBindings.get(LOG);
if (logObject == null) {
logObject = LoggerFactory.getLogger(getLoggerName());
} else if (!(logObject instanceof Logger)) {
throw fail(LOG, "Wrong type");
}
bindings.put(LOG, logObject);
// copy non-base variables
for (Map.Entry<String, Object> entry : slingBindings.entrySet()) {
if (!bindings.containsKey(entry.getKey())) {
bindings.put(entry.getKey(), entry.getValue());
}
}
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);
}
}
return bindings;
}
Aggregations