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);
}
}
}
}
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;
}
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);
}
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);
}
}
}
}
}
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());
}
Aggregations