Search in sources :

Example 6 with RequestDispatcherOptions

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);
    }
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 7 with RequestDispatcherOptions

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()));
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) Resource(org.apache.sling.api.resource.Resource) RequestDispatcher(javax.servlet.RequestDispatcher) Test(org.junit.Test)

Example 8 with 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);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestDispatcher(javax.servlet.RequestDispatcher) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 9 with RequestDispatcherOptions

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);
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions)

Example 10 with RequestDispatcherOptions

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);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)11 RequestDispatcher (javax.servlet.RequestDispatcher)6 Resource (org.apache.sling.api.resource.Resource)5 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)4 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)2 OptionItem (com.adobe.cq.wcm.core.components.models.form.OptionItem)1 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)1 JspTagException (javax.servlet.jsp.JspTagException)1 JSONWriter (org.apache.felix.utils.json.JSONWriter)1 RequestPathInfo (org.apache.sling.api.request.RequestPathInfo)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 ChildResource (org.apache.sling.models.annotations.injectorspecific.ChildResource)1 JspSlingHttpServletResponseWrapper (org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper)1 SightlyException (org.apache.sling.scripting.sightly.SightlyException)1 ValidationFailure (org.apache.sling.validation.ValidationFailure)1 ValidationResult (org.apache.sling.validation.ValidationResult)1