Search in sources :

Example 1 with RequestDispatcherOptions

use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.

the class ModifyUserServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    ValueMap requestParameters = request.adaptTo(ValueMap.class);
    String[] resourceTypeValues = requestParameters.get("sling:resourceType", String[].class);
    String resourceType = null;
    if (resourceTypeValues != null && resourceTypeValues.length > 0) {
        resourceType = resourceTypeValues[0];
    }
    if (resourceType != null && !"".equals(resourceType)) {
        String resourcePath = request.getRequestPathInfo().getResourcePath();
        ValidationModel vm = validationService.getValidationModel(resourceType, resourcePath, false);
        if (vm != null) {
            ValidationResult vr = validationService.validate(requestParameters, vm);
            if (vr.isValid()) {
                RequestDispatcherOptions options = new RequestDispatcherOptions();
                options.setReplaceSelectors(" ");
                request.getRequestDispatcher(request.getResource(), options).forward(request, response);
            } else {
                response.setContentType("application/json");
                JSONWriter writer = new JSONWriter(response.getWriter());
                writer.object();
                writer.key("success").value(false);
                writer.key("failures").array();
                for (ValidationFailure failure : vr.getFailures()) {
                    writer.object();
                    writer.key("message").value(failure.getMessage(request.getResourceBundle(Locale.US)));
                    writer.key("location").value(failure.getLocation());
                    writer.endObject();
                }
                writer.endArray();
                writer.endObject();
                response.setStatus(400);
            }
        }
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ValidationModel(org.apache.sling.validation.model.ValidationModel) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) ValueMap(org.apache.sling.api.resource.ValueMap) ValidationResult(org.apache.sling.validation.ValidationResult) ValidationFailure(org.apache.sling.validation.ValidationFailure)

Example 2 with RequestDispatcherOptions

use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.

the class AbstractDispatcherTagHandler method doEndTag.

/**
     * Called after the body has been processed.
     *
     * @return whether additional evaluations of the body are desired
     */
public int doEndTag() throws JspException {
    log.debug("AbstractDispatcherTagHandler.doEndTag");
    final SlingHttpServletRequest request = TagUtil.getRequest(pageContext);
    // set request dispatcher options according to tag attributes. This
    // depends on the implementation, that using a "null" argument
    // has no effect
    final RequestDispatcherOptions opts = new RequestDispatcherOptions();
    opts.setForceResourceType(resourceType);
    opts.setReplaceSelectors(replaceSelectors);
    opts.setAddSelectors(addSelectors);
    opts.setReplaceSuffix(replaceSuffix);
    // ensure the path (if set) is absolute and normalized
    if (path != null) {
        if (!path.startsWith("/")) {
            path = request.getResource().getPath() + "/" + path;
        }
        path = ResourceUtil.normalize(path);
    }
    // check the resource
    if (resource == null) {
        if (path == null) {
            // neither resource nor path is defined, use current resource
            resource = request.getResource();
        } else {
            // check whether the path (would) resolve, else SyntheticRes.
            Resource tmp = request.getResourceResolver().resolve(path);
            if (tmp == null && resourceType != null) {
                resource = new DispatcherSyntheticResource(request.getResourceResolver(), path, resourceType);
                // remove resource type overwrite as synthetic resource
                // is correctly typed as requested
                opts.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
            }
        }
    }
    try {
        // create a dispatcher for the resource or path
        RequestDispatcher dispatcher;
        if (resource != null) {
            dispatcher = request.getRequestDispatcher(resource, opts);
        } else {
            dispatcher = request.getRequestDispatcher(path, opts);
        }
        if (dispatcher != null) {
            SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
            dispatch(dispatcher, request, response);
        } else {
            TagUtil.log(log, pageContext, "No content to include...", null);
        }
    } catch (final JspTagException jte) {
        throw jte;
    } catch (final IOException ioe) {
        throw new JspTagException(ioe);
    } catch (final ServletException ce) {
        throw new JspTagException(TagUtil.getRootCause(ce));
    }
    return EVAL_PAGE;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletException(javax.servlet.ServletException) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) JspTagException(javax.servlet.jsp.JspTagException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 3 with RequestDispatcherOptions

use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.

the class SyntheticResourceFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    final String resourceType = getResourceTypeFromSuffix(slingRequest);
    final Configuration config = configurationWhiteboard.getConfiguration(slingRequest, resourceType);
    if (config == null || !config.hasIncludeSelector(slingRequest) || !ResourceUtil.isSyntheticResource(slingRequest.getResource())) {
        chain.doFilter(request, response);
        return;
    }
    final RequestDispatcherOptions options = new RequestDispatcherOptions();
    options.setForceResourceType(resourceType);
    final RequestDispatcher dispatcher = slingRequest.getRequestDispatcher(slingRequest.getResource(), options);
    dispatcher.forward(request, response);
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 4 with RequestDispatcherOptions

use of org.apache.sling.api.request.RequestDispatcherOptions in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class OptionsImpl method populateOptionItemsFromDatasource.

@SuppressWarnings("unchecked")
private void populateOptionItemsFromDatasource() {
    if (StringUtils.isBlank(datasourceRT)) {
        return;
    }
    // build the options by running the datasource code (the list is set as a request attribute)
    RequestDispatcherOptions opts = new RequestDispatcherOptions();
    opts.setForceResourceType(datasourceRT);
    RequestDispatcher dispatcher = request.getRequestDispatcher(resource, opts);
    try {
        if (dispatcher != null) {
            dispatcher.include(request, response);
        } else {
            LOGGER.error("Failed to include the datasource at " + datasourceRT);
        }
    } catch (Exception e) {
        LOGGER.error("Failed to include the datasource at " + datasourceRT, e);
    }
    // retrieve the datasource from the request and adapt it to form options
    SimpleDataSource dataSource = (SimpleDataSource) request.getAttribute(DataSource.class.getName());
    if (dataSource != null) {
        Iterator<Resource> itemIterator = dataSource.iterator();
        if (itemIterator != null) {
            while (itemIterator.hasNext()) {
                Resource item = itemIterator.next();
                OptionItem optionItem = item.adaptTo(OptionItem.class);
                if (optionItem != null && (optionItem.isDisabled() || StringUtils.isNotBlank(optionItem.getValue()))) {
                    optionItems.add(optionItem);
                }
            }
        }
    }
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) OptionItem(com.adobe.cq.wcm.core.components.models.form.OptionItem) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.models.annotations.injectorspecific.ChildResource) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 5 with RequestDispatcherOptions

use of org.apache.sling.api.request.RequestDispatcherOptions in project sling by apache.

the class SlingRequestPathInfoTest method testMerge.

public void testMerge() {
    SlingRequestPathInfo p = new SlingRequestPathInfo(new MockResource("/some/path", ".s1.s2.ext"));
    assertEquals("s1.s2", p.getSelectorString());
    assertEquals("ext", p.getExtension());
    // test to replace selectors with a new one
    RequestDispatcherOptions o = new RequestDispatcherOptions();
    o.setReplaceSelectors("a");
    RequestPathInfo result = p.merge(o);
    assertEquals("a", result.getSelectorString());
    assertEquals("ext", result.getExtension());
    // test to replace selector with the empty string
    o.setReplaceSelectors("");
    result = p.merge(o);
    assertEquals(null, result.getSelectorString());
    assertEquals("ext", result.getExtension());
    // now add a selector
    o.setAddSelectors("b");
    result = p.merge(o);
    assertEquals("b", result.getSelectorString());
    assertEquals("ext", result.getExtension());
    // replace ext
    o.setReplaceSuffix("html");
    result = p.merge(o);
    assertEquals("b", result.getSelectorString());
    assertEquals("html", result.getSuffix());
}
Also used : RequestPathInfo(org.apache.sling.api.request.RequestPathInfo) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions)

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