use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.
the class StreamRendererServlet method renderDirectory.
private void renderDirectory(final SlingHttpServletRequest request, final SlingHttpServletResponse response, final boolean included) throws ServletException, IOException {
// request is included or committed, not rendering index
if (included || response.isCommitted()) {
request.getRequestProgressTracker().log("StreamRendererServlet: Not rendering index, response is committed or request included");
log.warn("StreamRendererServlet: Not rendering index, response is committed or request included");
return;
}
Resource resource = request.getResource();
ResourceResolver resolver = request.getResourceResolver();
// check for an index file
for (String index : indexFiles) {
Resource fileRes = resolver.getResource(resource, index);
if (fileRes != null && !ResourceUtil.isSyntheticResource(fileRes)) {
setHeaders(fileRes, response);
if (isHeadRequest(request)) {
return;
}
// include the index resource with no suffix and selectors !
RequestDispatcherOptions rdo = new RequestDispatcherOptions();
rdo.setReplaceSuffix("");
rdo.setReplaceSelectors("");
RequestDispatcher dispatcher;
if (index.indexOf('.') < 0) {
String filePath = fileRes.getPath() + ".html";
dispatcher = request.getRequestDispatcher(filePath, rdo);
} else {
dispatcher = request.getRequestDispatcher(fileRes, rdo);
}
dispatcher.include(request, response);
return;
}
}
if (index) {
if (isHeadRequest(request)) {
setHeaders(resource, response);
return;
}
renderIndex(resource, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
}
use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.
the class MockSlingHttpServletRequestTest method testGetRequestDispatcher.
@Test
public void testGetRequestDispatcher() {
MockRequestDispatcherFactory requestDispatcherFactory = mock(MockRequestDispatcherFactory.class);
RequestDispatcher requestDispatcher = mock(RequestDispatcher.class);
when(requestDispatcherFactory.getRequestDispatcher(any(Resource.class), any(RequestDispatcherOptions.class))).thenReturn(requestDispatcher);
when(requestDispatcherFactory.getRequestDispatcher(any(String.class), any(RequestDispatcherOptions.class))).thenReturn(requestDispatcher);
request.setRequestDispatcherFactory(requestDispatcherFactory);
assertSame(requestDispatcher, request.getRequestDispatcher("/path"));
assertSame(requestDispatcher, request.getRequestDispatcher("/path", new RequestDispatcherOptions()));
assertSame(requestDispatcher, request.getRequestDispatcher(resource));
assertSame(requestDispatcher, request.getRequestDispatcher(resource, new RequestDispatcherOptions()));
}
use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.
the class ResourceRuntimeExtension method includeResource.
private void includeResource(final Bindings bindings, PrintWriter out, Resource includeRes, String dispatcherOptions, String resourceType) {
if (includeRes == null) {
throw new SightlyException("Resource cannot be null");
} else {
SlingHttpServletResponse customResponse = new PrintWriterResponseWrapper(out, BindingsUtils.getResponse(bindings));
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
RequestDispatcherOptions opts = new RequestDispatcherOptions(dispatcherOptions);
if (StringUtils.isNotEmpty(resourceType)) {
opts.setForceResourceType(resourceType);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(includeRes, opts);
try {
if (dispatcher != null) {
dispatcher.include(request, customResponse);
} else {
throw new SightlyException("Failed to include resource " + includeRes.getPath());
}
} catch (Exception e) {
throw new SightlyException("Failed to include resource " + includeRes.getPath(), e);
}
}
}
use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.
the class FormServlet method succeed.
private void succeed(final Form form, final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
final RequestDispatcherOptions options = new RequestDispatcherOptions();
options.setAddSelectors(SUCCESS_SELECTOR);
forward(form, request, response, options);
}
use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.
the class SlingIncludeAttributeTagProcessor method doProcess.
@Override
protected void doProcess(final ITemplateContext templateContext, final IProcessableElementTag processableElementTag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler elementTagStructureHandler) {
try {
final SlingHttpServletRequest slingHttpServletRequest = (SlingHttpServletRequest) templateContext.getVariable(SlingBindings.REQUEST);
final SlingHttpServletResponse slingHttpServletResponse = (SlingHttpServletResponse) templateContext.getVariable(SlingBindings.RESPONSE);
final IEngineConfiguration configuration = templateContext.getConfiguration();
final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
final IStandardExpression expression = expressionParser.parseExpression(templateContext, attributeValue);
final Object include = expression.execute(templateContext);
String path = null;
if (include instanceof String) {
path = (String) include;
}
Resource resource = null;
if (include instanceof Resource) {
resource = (Resource) include;
}
// request dispatcher options
final RequestDispatcherOptions requestDispatcherOptions = prepareRequestDispatcherOptions(expressionParser, templateContext, processableElementTag, elementTagStructureHandler);
// dispatch
final String content = dispatch(resource, path, slingHttpServletRequest, slingHttpServletResponse, requestDispatcherOptions);
// add output
final Boolean unwrap = (Boolean) parseAttribute(expressionParser, templateContext, processableElementTag, elementTagStructureHandler, UNWRAP_ATTRIBUTE_NAME);
if (unwrap != null && unwrap) {
elementTagStructureHandler.replaceWith(content, false);
} else {
elementTagStructureHandler.setBody(content, false);
}
} catch (Exception e) {
throw new RuntimeException("unable to process include attribute", e);
}
}
Aggregations