Search in sources :

Example 26 with Servlet

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

the class RequestData method service.

/**
     * Helper method to call the servlet for the current content data. If the
     * current content data has no servlet, <em>NOT_FOUND</em> (404) error is
     * sent and the method terminates.
     * <p>
     * If the the servlet exists, the
     * {@link org.apache.sling.api.SlingConstants#SLING_CURRENT_SERVLET_NAME} request attribute is set
     * to the name of that servlet and that servlet name is also set as the
     * {@link #setActiveServletName(String) currently active servlet}. After
     * the termination of the servlet (normal or throwing a Throwable) the
     * request attribute is reset to the previous value. The name of the
     * currently active servlet is only reset to the previous value if the
     * servlet terminates normally. In case of a Throwable, the active servlet
     * name is not reset and indicates which servlet caused the potential abort
     * of the request.
     *
     * @param request The request object for the service method
     * @param response The response object for the service method
     * @throws IOException May be thrown by the servlet's service method
     * @throws ServletException May be thrown by the servlet's service method
     */
public static void service(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException {
    RequestData requestData = RequestData.getRequestData(request);
    Servlet servlet = requestData.getContentData().getServlet();
    if (servlet == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "No Servlet to handle request");
    } else {
        String name = RequestUtil.getServletName(servlet);
        // verify the number of service calls in this request
        if (requestData.hasServletMaxCallCount(request)) {
            throw new TooManyCallsException(name);
        }
        // replace the current servlet name in the request
        Object oldValue = request.getAttribute(SLING_CURRENT_SERVLET_NAME);
        request.setAttribute(SLING_CURRENT_SERVLET_NAME, name);
        // setup the tracker for this service call
        String timerName = name + "#" + requestData.servletCallCounter;
        requestData.servletCallCounter++;
        requestData.getRequestProgressTracker().startTimer(timerName);
        try {
            String callerServlet = requestData.setActiveServletName(name);
            servlet.service(request, response);
            requestData.setActiveServletName(callerServlet);
        } finally {
            request.setAttribute(SLING_CURRENT_SERVLET_NAME, oldValue);
            requestData.getRequestProgressTracker().logTimer(timerName);
        }
    }
}
Also used : TooManyCallsException(org.apache.sling.api.request.TooManyCallsException) SlingMainServlet(org.apache.sling.engine.impl.SlingMainServlet) Servlet(javax.servlet.Servlet)

Example 27 with Servlet

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

the class VirtualInstance method startJetty.

public synchronized void startJetty() throws Throwable {
    if (jettyServer != null) {
        return;
    }
    servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
    servletContext.setContextPath("/");
    TopologyConnectorServlet servlet = new TopologyConnectorServlet();
    PrivateAccessor.setField(servlet, "config", config);
    PrivateAccessor.setField(servlet, "clusterViewService", clusterViewService);
    PrivateAccessor.setField(servlet, "announcementRegistry", announcementRegistry);
    Mockery context = new JUnit4Mockery();
    final HttpService httpService = context.mock(HttpService.class);
    context.checking(new Expectations() {

        {
            allowing(httpService).registerServlet(with(any(String.class)), with(any(Servlet.class)), with(any(Dictionary.class)), with(any(HttpContext.class)));
        }
    });
    PrivateAccessor.setField(servlet, "httpService", httpService);
    ComponentContext cc = null;
    PrivateAccessor.invoke(servlet, "activate", new Class[] { ComponentContext.class }, new Object[] { cc });
    ServletHolder holder = new ServletHolder(servlet);
    servletContext.addServlet(holder, "/system/console/topology/*");
    jettyServer = new Server();
    jettyServer.setHandler(servletContext);
    Connector connector = new SelectChannelConnector();
    jettyServer.setConnectors(new Connector[] { connector });
    jettyServer.start();
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) ComponentContext(org.osgi.service.component.ComponentContext) Server(org.eclipse.jetty.server.Server) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpContext(org.osgi.service.http.HttpContext) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) HttpService(org.osgi.service.http.HttpService) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) Servlet(javax.servlet.Servlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 28 with Servlet

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

the class SlingWebDavServlet method activate.

// ---------- SCR integration
protected void activate(ComponentContext context) throws NamespaceException, ServletException {
    this.ioManager.setComponentContext(context);
    this.propertyManager.setComponentContext(context);
    this.copyMoveManager.setComponentContext(context);
    this.deleteManager.setComponentContext(context);
    resourceConfig = new SlingResourceConfig(mimeTypeService, context.getProperties(), ioManager, propertyManager, copyMoveManager, deleteManager);
    // Register servlet, and set the contextPath field to signal successful
    // registration
    Servlet simpleServlet = new SlingSimpleWebDavServlet(resourceConfig, getRepository());
    httpService.registerServlet(resourceConfig.getServletContextPath(), simpleServlet, resourceConfig.getServletInitParams(), null);
    simpleWebDavServletRegistered = true;
}
Also used : SimpleWebdavServlet(org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet) Servlet(javax.servlet.Servlet) SlingResourceConfig(org.apache.sling.jcr.webdav.impl.helper.SlingResourceConfig)

Example 29 with Servlet

use of javax.servlet.Servlet 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);
    }
}
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) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) 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)

Example 30 with Servlet

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

the class JspServletWrapper method loadServlet.

@SuppressWarnings("unchecked")
private Servlet loadServlet() throws ServletException, IOException {
    Servlet servlet = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Loading servlet " + jspUri);
        }
        servlet = (Servlet) ctxt.load().newInstance();
        AnnotationProcessor annotationProcessor = (AnnotationProcessor) config.getServletContext().getAttribute(AnnotationProcessor.class.getName());
        if (annotationProcessor != null) {
            annotationProcessor.processAnnotations(servlet);
            annotationProcessor.postConstruct(servlet);
        }
        // update dependents
        final List<String> oldDeps = this.dependents;
        if (servlet != null && servlet instanceof JspSourceDependent) {
            this.dependents = (List<String>) ((JspSourceDependent) servlet).getDependants();
            if (this.dependents == null) {
                this.dependents = Collections.EMPTY_LIST;
            }
            this.ctxt.getRuntimeContext().addJspDependencies(this, this.dependents);
        }
        if (!equals(oldDeps, this.dependents)) {
            this.persistDependencies();
        }
    } catch (final IllegalAccessException e) {
        throw new JasperException(e);
    } catch (final InstantiationException e) {
        throw new JasperException(e);
    } catch (final Exception e) {
        throw new JasperException(e);
    }
    servlet.init(config);
    return servlet;
}
Also used : JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) Servlet(javax.servlet.Servlet) AnnotationProcessor(org.apache.sling.scripting.jsp.jasper.runtime.AnnotationProcessor) JspSourceDependent(org.apache.sling.scripting.jsp.jasper.runtime.JspSourceDependent) ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) UnavailableException(javax.servlet.UnavailableException) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingPageException(org.apache.sling.scripting.jsp.SlingPageException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException)

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