Search in sources :

Example 76 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class RequestData method initServlet.

public void initServlet(final Resource resource, final ServletResolver sr) {
    // the resource and the request path info, will never be null
    RequestPathInfo requestPathInfo = new SlingRequestPathInfo(resource);
    ContentData contentData = setContent(resource, requestPathInfo);
    requestProgressTracker.log("Resource Path Info: {0}", requestPathInfo);
    // finally resolve the servlet for the resource
    requestProgressTracker.startTimer("ServletResolution");
    Servlet servlet = sr.resolveServlet(slingRequest);
    requestProgressTracker.logTimer("ServletResolution", "URI={0} handled by Servlet={1}", getServletRequest().getRequestURI(), (servlet == null ? "-none-" : RequestUtil.getServletName(servlet)));
    contentData.setServlet(servlet);
}
Also used : RequestPathInfo(org.apache.sling.api.request.RequestPathInfo) SlingMainServlet(org.apache.sling.engine.impl.SlingMainServlet) Servlet(javax.servlet.Servlet)

Example 77 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class CallTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final RequestProgressTracker tracker = TagUtil.getRequest(pageContext).getRequestProgressTracker();
    String servletName = null;
    final Servlet servlet;
    if (!ignoreComponentHierarchy) {
        final Resource resource = bindings.getResource();
        servlet = servletResolver.resolveServlet(resource, this.script);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} for path={1}, type={2}: {3}", script, resource.getPath(), resource.getResourceType(), servletName);
        }
    } else {
        final ResourceResolver resolver = 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 : resolver.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + "/" + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(resolver, scriptPath);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} (ignoring component hierarchy): {1}", script, servletName);
        }
    }
    if (servlet == null) {
        throw new JspException("Could not find script " + script);
    }
    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);
        tracker.startTimer(servletName);
        servlet.service(pageContext.getRequest(), response);
        tracker.logTimer(servletName);
        return EVAL_PAGE;
    } catch (Exception e) {
        throw new JspException("Error while executing script " + script, e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Example 78 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class DefaultGetServlet method destroy.

@Override
public void destroy() {
    for (Servlet servlet : rendererMap.values()) {
        try {
            servlet.destroy();
        } catch (Throwable t) {
            logger.error("Error while destroying servlet " + servlet, t);
        }
    }
    streamerServlet = null;
    rendererMap.clear();
    super.destroy();
}
Also used : JsonRendererServlet(org.apache.sling.servlets.get.impl.helpers.JsonRendererServlet) StreamRendererServlet(org.apache.sling.servlets.get.impl.helpers.StreamRendererServlet) HtmlRendererServlet(org.apache.sling.servlets.get.impl.helpers.HtmlRendererServlet) Servlet(javax.servlet.Servlet) PlainTextRendererServlet(org.apache.sling.servlets.get.impl.helpers.PlainTextRendererServlet) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) XMLRendererServlet(org.apache.sling.servlets.get.impl.helpers.XMLRendererServlet)

Example 79 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class DefaultGetServlet method init.

@Override
public void init() throws ServletException {
    super.init();
    // use the servlet for rendering StreamRendererServlet.EXT_RES as the
    // streamer servlet
    streamerServlet = getDefaultRendererServlet(StreamRendererServlet.EXT_RES);
    // Register renderer servlets
    rendererMap.put(StreamRendererServlet.EXT_RES, streamerServlet);
    if (enableHtml) {
        rendererMap.put(HtmlRendererServlet.EXT_HTML, getDefaultRendererServlet(HtmlRendererServlet.EXT_HTML));
    }
    if (enableTxt) {
        rendererMap.put(PlainTextRendererServlet.EXT_TXT, getDefaultRendererServlet(PlainTextRendererServlet.EXT_TXT));
    }
    if (enableJson) {
        rendererMap.put(JsonRendererServlet.EXT_JSON, getDefaultRendererServlet(JsonRendererServlet.EXT_JSON));
    }
    if (enableXml) {
        rendererMap.put(XMLRendererServlet.EXT_XML, getDefaultRendererServlet(XMLRendererServlet.EXT_XML));
    }
    // check additional aliases
    if (this.aliases != null) {
        for (final String m : aliases) {
            final int pos = m.indexOf(':');
            if (pos != -1) {
                final String type = m.substring(0, pos);
                Servlet servlet = rendererMap.get(type);
                if (servlet == null) {
                    servlet = getDefaultRendererServlet(type);
                }
                if (servlet != null) {
                    final String extensions = m.substring(pos + 1);
                    final StringTokenizer st = new StringTokenizer(extensions, ",");
                    while (st.hasMoreTokens()) {
                        final String ext = st.nextToken();
                        rendererMap.put(ext, servlet);
                    }
                } else {
                    logger.warn("Unable to enable renderer alias(es) for {} - type not supported", m);
                }
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) JsonRendererServlet(org.apache.sling.servlets.get.impl.helpers.JsonRendererServlet) StreamRendererServlet(org.apache.sling.servlets.get.impl.helpers.StreamRendererServlet) HtmlRendererServlet(org.apache.sling.servlets.get.impl.helpers.HtmlRendererServlet) Servlet(javax.servlet.Servlet) PlainTextRendererServlet(org.apache.sling.servlets.get.impl.helpers.PlainTextRendererServlet) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) XMLRendererServlet(org.apache.sling.servlets.get.impl.helpers.XMLRendererServlet)

Example 80 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class RedirectServlet method getJsonRendererServlet.

private Servlet getJsonRendererServlet() {
    if (jsonRendererServlet == null) {
        Servlet jrs = new JsonRendererServlet(jsonMaximumResults);
        try {
            jrs.init(getServletConfig());
        } catch (Exception e) {
        // don't care too much here
        }
        jsonRendererServlet = jrs;
    }
    return jsonRendererServlet;
}
Also used : JsonRendererServlet(org.apache.sling.servlets.get.impl.helpers.JsonRendererServlet) JsonRendererServlet(org.apache.sling.servlets.get.impl.helpers.JsonRendererServlet) Servlet(javax.servlet.Servlet) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

Servlet (javax.servlet.Servlet)86 Test (org.junit.Test)18 HttpServlet (javax.servlet.http.HttpServlet)16 ServletException (javax.servlet.ServletException)15 IOException (java.io.IOException)11 OptingServlet (org.apache.sling.api.servlets.OptingServlet)11 GenericServlet (javax.servlet.GenericServlet)10 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)9 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)9 ServletContext (javax.servlet.ServletContext)8 UnavailableException (javax.servlet.UnavailableException)8 Resource (org.apache.sling.api.resource.Resource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)7 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)7 ServletInfo (io.undertow.servlet.api.ServletInfo)6 ArrayList (java.util.ArrayList)5 Filter (javax.servlet.Filter)5 Context (org.apache.catalina.Context)5 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)5