use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.
the class EvalTagHandler 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("EvalTagHandler doEndTag");
final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
final SlingScriptHelper scriptHelper = bindings.getSling();
final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
final Servlet servlet;
if (!this.ignoreResourceTypeHierarchy) {
// detecte resource
final Resource resource;
if (this.resource != null) {
resource = this.resource;
} else if (this.resourceType != null) {
resource = new SyntheticResource(bindings.getRequest().getResourceResolver(), bindings.getResource().getPath(), this.resourceType);
} else {
resource = bindings.getResource();
}
servlet = servletResolver.resolveServlet(resource, this.script);
} else {
final ResourceResolver rr = bindings.getRequest().getResourceResolver();
final String scriptPath;
if (!script.startsWith("/")) {
// resolve relative script
String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
// check if parent resides on search path
for (String sp : rr.getSearchPath()) {
if (parentPath.startsWith(sp)) {
parentPath = parentPath.substring(sp.length());
break;
}
}
scriptPath = parentPath + '/' + script;
} else {
scriptPath = this.script;
}
servlet = servletResolver.resolveServlet(rr, scriptPath);
}
if (servlet == null) {
throw new JspException("Could not find script '" + script + "' referenced in jsp " + scriptHelper.getScript().getScriptResource().getPath());
}
try {
if (flush && !(pageContext.getOut() instanceof BodyContent)) {
// might throw an IOException of course
pageContext.getOut().flush();
}
// wrap the response to get the correct output order
SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
servlet.service(pageContext.getRequest(), response);
return EVAL_PAGE;
} catch (Exception e) {
log.error("Error while executing script " + script, e);
throw new JspException("Error while executing script " + script, e);
}
}
use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.
the class IncludeRuntimeExtension method includeScript.
private void includeScript(final Bindings bindings, String script, PrintWriter out) {
if (StringUtils.isEmpty(script)) {
throw new SightlyException("Path for data-sly-include is empty");
} else {
LOG.debug("Attempting to include script {}.", script);
SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class);
if (servletResolver != null) {
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Servlet servlet = servletResolver.resolveServlet(request.getResource(), script);
if (servlet != null) {
try {
SlingHttpServletResponse response = BindingsUtils.getResponse(bindings);
PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response);
servlet.service(request, resWrapper);
} catch (Exception e) {
throw new SightlyException("Failed to include script " + script, e);
}
} else {
throw new SightlyException("Failed to locate script " + script);
}
} else {
throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script);
}
}
}
use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.
the class UrlFilter method doFilter.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof SlingHttpServletRequest && response instanceof SlingHttpServletResponse) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
RequestPathInfo pathInfo = slingRequest.getRequestPathInfo();
Resource definitionResource = findUrlFilterDefinitionResource(slingRequest.getResource(), slingRequest.getResourceResolver());
if (definitionResource != null) {
logger.debug("found url filter definition resource at {}", definitionResource.getPath());
ValueMap properties = definitionResource.adaptTo(ValueMap.class);
if (properties != null) {
if (checkSelector(pathInfo, properties) && checkSuffix(pathInfo, properties) && checkExtension(pathInfo, properties)) {
logger.debug("url filter definition resource at {} passed for request {}.", definitionResource.getPath(), slingRequest.getRequestPathInfo());
} else {
logger.info("url filter definition resource at {} FAILED for request {}.", definitionResource.getPath(), slingRequest.getRequestPathInfo());
slingResponse.sendError(403);
return;
}
}
}
}
chain.doFilter(request, response);
}
use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.
the class StreamRendererServletTest method test_setHeaders.
@Test
public void test_setHeaders() throws Throwable {
final Resource resource = Mockito.mock(Resource.class);
final SlingHttpServletResponse response = Mockito.mock(SlingHttpServletResponse.class);
final ResourceMetadata meta = Mockito.mock(ResourceMetadata.class);
final ServletContext sc = Mockito.mock(ServletContext.class);
StreamRendererServlet streamRendererServlet = new StreamRendererServlet(true, new String[] { "/" }) {
@Override
public ServletContext getServletContext() {
return sc;
}
};
Mockito.when(resource.getResourceMetadata()).thenReturn(meta);
PrivateAccessor.invoke(streamRendererServlet, "setHeaders", new Class[] { Resource.class, SlingHttpServletResponse.class }, new Object[] { resource, response });
Mockito.verify(response, Mockito.times(1)).setContentType("application/octet-stream");
}
use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.
the class ThymeleafScriptEngine method eval.
@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
final SlingHttpServletRequest request = helper.getRequest();
final SlingHttpServletResponse response = helper.getResponse();
// only used by Thymeleaf's ServletContextResourceResolver (TODO check if still true for 3.0)
final ServletContext servletContext = null;
final Locale locale = helper.getResponse().getLocale();
final String scriptName = helper.getScript().getScriptResource().getPath();
final Writer writer = scriptContext.getWriter();
try {
final ResourceResolver resourceResolver = thymeleafScriptEngineFactory.getRequestScopedResourceResolver();
final IContext context = new SlingWebContext(request, response, servletContext, resourceResolver, locale, bindings);
thymeleafScriptEngineFactory.getTemplateEngine().process(scriptName, context, writer);
} catch (Exception e) {
logger.error("Failure rendering Thymeleaf template '{}': {}", scriptName, e.getMessage());
throw new ScriptException(e);
}
return null;
}
Aggregations