use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class SlingRequestDispatcher method dispatch.
private void dispatch(ServletRequest request, ServletResponse sResponse, boolean include) throws ServletException, IOException {
SlingHttpServletRequest cRequest = RequestData.unwrap(request);
RequestData rd = RequestData.getRequestData(cRequest);
String absPath = getAbsolutePath(cRequest, path);
RequestProgressTracker requestProgressTracker = cRequest.getRequestProgressTracker();
// doing anything
if (!(sResponse instanceof HttpServletResponse)) {
log.error("include: Failed to include {}, response has wrong type", absPath);
return;
}
if (resource == null) {
String timerName = "resolveIncludedResource(" + absPath + ")";
requestProgressTracker.startTimer(timerName);
// resolve the absolute path in the resource resolver, using
// only those parts of the path as if it would be request path
resource = cRequest.getResourceResolver().resolve(absPath);
// if the resource could not be resolved, fail gracefully
if (resource == null) {
log.error("include: Could not resolve {} to a resource, not including", absPath);
return;
}
requestProgressTracker.logTimer(timerName, "path={0} resolves to Resource={1}", absPath, resource);
}
// ensure request path info and optional merges
SlingRequestPathInfo info = getMergedRequestPathInfo(cRequest);
requestProgressTracker.log("Including resource {0} ({1})", resource, info);
rd.getSlingRequestProcessor().dispatchRequest(request, sResponse, resource, info, include);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class ConfigurationBindingsValueProvider method addBindings.
@Override
public void addBindings(Bindings bindings) {
if (!enabled || !bindings.containsKey(SlingBindings.REQUEST)) {
return;
}
SlingHttpServletRequest request = (SlingHttpServletRequest) bindings.get(SlingBindings.REQUEST);
Resource resource = request.getResource();
if (resource == null) {
return;
}
Map<String, Object> configMap = new ConfigMap(resource, configMetadataProvider);
bindings.put(BINDING_VARIABLE, configMap);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class AbstractResourceTypeViaProvider method getAdaptable.
@Override
public Object getAdaptable(Object original, String value) {
if (!handle(value)) {
return ORIGINAL;
}
if (original instanceof Resource) {
final Resource resource = (Resource) original;
final String resourceType = getResourceType(resource, value);
if (resourceType == null) {
log.warn("Could not determine forced resource type for {} using via value {}.", resource, value);
return null;
}
return new ResourceTypeForcingResourceWrapper(resource, resourceType);
} else if (original instanceof SlingHttpServletRequest) {
final SlingHttpServletRequest request = (SlingHttpServletRequest) original;
final Resource resource = request.getResource();
if (resource == null) {
return null;
}
final String resourceType = getResourceType(resource, value);
if (resourceType == null) {
log.warn("Could not determine forced resource type for {} using via value {}.", resource, value);
return null;
}
return new ResourceTypeForcingRequestWrapper(request, resource, resourceType);
} else {
log.warn("Received unexpected adaptable of type {}.", original.getClass().getName());
return null;
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class ResourceResolverInjectorTest method testFromRequest.
@Test
public void testFromRequest() {
SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
when(request.getResourceResolver()).thenReturn(resourceResolver);
Object result = injector.getValue(request, "resourceResolver", ResourceResolver.class, element, registry);
assertEquals(resourceResolver, result);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class DefaultSlingScript method service.
/**
* @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
*/
public void service(ServletRequest req, ServletResponse res) {
final SlingHttpServletRequest request = (SlingHttpServletRequest) req;
try {
// prepare the properties for the script
final SlingBindings props = new SlingBindings();
props.setRequest((SlingHttpServletRequest) req);
props.setResponse((SlingHttpServletResponse) res);
// try to set content type (unless included)
if (request.getAttribute(SlingConstants.ATTR_INCLUDE_SERVLET_PATH) == null) {
final String contentType = request.getResponseContentType();
if (contentType != null) {
res.setContentType(contentType);
// see SLING-679
if (contentType.startsWith("text/")) {
res.setCharacterEncoding("UTF-8");
}
} else {
LOGGER.debug("service: No response content type defined for request {}.", request.getRequestURI());
}
} else {
LOGGER.debug("service: Included request, not setting content type and encoding");
}
// evaluate the script now using the ScriptEngine
eval(props);
} catch (ScriptEvaluationException see) {
// log in the request progress tracker
logScriptError(request, see);
throw see;
} catch (SlingException e) {
// log in the request progress tracker
logScriptError(request, e);
throw e;
} catch (Exception e) {
// log in the request progress tracker
logScriptError(request, e);
throw new SlingException("Cannot get DefaultSlingScript: " + e.getMessage(), e);
}
}
Aggregations