Search in sources :

Example 1 with SlingRequestProcessor

use of org.apache.sling.engine.SlingRequestProcessor in project sling by apache.

the class SlingRequestStatusHealthCheckTest method setup.

@Before
public void setup() throws Exception {
    hc = new SlingRequestStatusHealthCheck();
    final ResourceResolverFactory rrf = Mockito.mock(ResourceResolverFactory.class);
    SetField.set(hc, "resolverFactory", rrf);
    final Answer<Void> a = new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            final HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[0];
            final HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1];
            final String path = request.getPathInfo();
            if (path.length() > 0) {
                final String status = path.substring(0, path.indexOf('.'));
                response.setStatus(Integer.valueOf(status));
            }
            return null;
        }
    };
    final SlingRequestProcessor srp = Mockito.mock(SlingRequestProcessor.class);
    SetField.set(hc, "requestProcessor", srp);
    Mockito.doAnswer(a).when(srp).processRequest(Matchers.any(HttpServletRequest.class), Matchers.any(HttpServletResponse.class), Matchers.any(ResourceResolver.class));
    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("path", paths);
    hc.activate(properties);
}
Also used : HashMap(java.util.HashMap) SlingRequestProcessor(org.apache.sling.engine.SlingRequestProcessor) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Answer(org.mockito.stubbing.Answer) SlingRequestStatusHealthCheck(org.apache.sling.hc.support.impl.SlingRequestStatusHealthCheck) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Before(org.junit.Before)

Example 2 with SlingRequestProcessor

use of org.apache.sling.engine.SlingRequestProcessor in project sling by apache.

the class SlingMainServlet method activate.

// ---------- Property Setter for SCR --------------------------------------
@Activate
protected void activate(final BundleContext bundleContext, final Map<String, Object> componentConfig, final Config config) {
    final String[] props = config.sling_additional_response_headers();
    if (props != null) {
        final ArrayList<StaticResponseHeader> mappings = new ArrayList<>(props.length);
        for (final String prop : props) {
            if (prop != null && prop.trim().length() > 0) {
                try {
                    final StaticResponseHeader mapping = new StaticResponseHeader(prop.trim());
                    mappings.add(mapping);
                } catch (final IllegalArgumentException iae) {
                    log.info("configure: Ignoring '{}': {}", prop, iae.getMessage());
                }
            }
        }
        RequestData.setAdditionalResponseHeaders(mappings);
    }
    configuredServerInfo = config.sling_serverinfo();
    // setup server info
    setProductInfo(bundleContext);
    // prepare the servlet configuration from the component config
    final Hashtable<String, Object> configuration = new Hashtable<>(componentConfig);
    // ensure the servlet name
    if (!(configuration.get("servlet-name") instanceof String)) {
        configuration.put("servlet-name", this.productInfo);
    }
    // configure method filter
    allowTrace = config.sling_trace_allow();
    // configure the request limits
    RequestData.setMaxIncludeCounter(config.sling_max_inclusions());
    RequestData.setMaxCallCounter(config.sling_max_calls());
    RequestData.setSlingMainServlet(this);
    // Warn about the obsolete parameter encoding configuration
    if (componentConfig.get(DEPRECATED_ENCODING_PROPERTY) != null) {
        log.warn("Please configure the default request parameter encoding using " + "the 'org.apache.sling.engine.parameters' configuration PID; the property " + DEPRECATED_ENCODING_PROPERTY + "=" + componentConfig.get(DEPRECATED_ENCODING_PROPERTY) + " is obsolete and ignored");
    }
    // register the servlet context
    final Dictionary<String, String> contextProperties = new Hashtable<>();
    contextProperties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, SERVLET_CONTEXT_NAME);
    contextProperties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, SLING_ROOT);
    contextProperties.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Engine Servlet Context Helper");
    contextProperties.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
    this.contextRegistration = bundleContext.registerService(ServletContextHelper.class, this.slingHttpContext, contextProperties);
    // register the servlet
    final Dictionary<String, String> servletConfig = toStringConfig(configuration);
    servletConfig.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + SERVLET_CONTEXT_NAME + ")");
    servletConfig.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, SLING_ROOT);
    servletConfig.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Engine Main Servlet");
    servletConfig.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
    this.servletRegistration = bundleContext.registerService(Servlet.class, this, servletConfig);
    log.info("{} ready to serve requests", this.getServerInfo());
    // now that the sling main servlet is registered with the HttpService
    // and initialized we can register the servlet context
    slingServletContext = new SlingServletContext(bundleContext, this);
    // register render filters already registered after registration with
    // the HttpService as filter initialization may cause the servlet
    // context to be required (see SLING-42)
    filterManager = new ServletFilterManager(bundleContext, slingServletContext);
    filterManager.open();
    requestProcessor.setFilterManager(filterManager);
    // initialize requestListenerManager
    requestListenerManager = new RequestListenerManager(bundleContext, slingServletContext);
    // Setup configuration printer
    this.printerRegistration = WebConsoleConfigPrinter.register(bundleContext, filterManager);
    // setup the request info recorder
    try {
        int maxRequests = config.sling_max_record_requests();
        String[] patterns = config.sling_store_pattern_requests();
        if (patterns == null)
            patterns = new String[0];
        List<Pattern> compiledPatterns = new ArrayList<>(patterns.length);
        for (String pattern : patterns) {
            if (pattern != null && pattern.trim().length() > 0) {
                compiledPatterns.add(Pattern.compile(pattern));
            }
        }
        RequestHistoryConsolePlugin.initPlugin(bundleContext, maxRequests, compiledPatterns);
    } catch (Throwable t) {
        log.debug("Unable to register web console request recorder plugin.", t);
    }
    try {
        Dictionary<String, String> mbeanProps = new Hashtable<>();
        mbeanProps.put("jmx.objectname", "org.apache.sling:type=engine,service=RequestProcessor");
        RequestProcessorMBeanImpl mbean = new RequestProcessorMBeanImpl();
        requestProcessorMBeanRegistration = bundleContext.registerService(RequestProcessorMBean.class, mbean, mbeanProps);
        requestProcessor.setMBean(mbean);
    } catch (Throwable t) {
        log.debug("Unable to register mbean");
    }
    // provide the SlingRequestProcessor service
    Hashtable<String, String> srpProps = new Hashtable<>();
    srpProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
    srpProps.put(Constants.SERVICE_DESCRIPTION, "Sling Request Processor");
    requestProcessorRegistration = bundleContext.registerService(SlingRequestProcessor.class, requestProcessor, srpProps);
}
Also used : Pattern(java.util.regex.Pattern) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) SlingRequestProcessor(org.apache.sling.engine.SlingRequestProcessor) SlingServletContext(org.apache.sling.engine.impl.helper.SlingServletContext) ServletContextHelper(org.osgi.service.http.context.ServletContextHelper) RequestProcessorMBean(org.apache.sling.engine.jmx.RequestProcessorMBean) RequestListenerManager(org.apache.sling.engine.impl.helper.RequestListenerManager) ServletFilterManager(org.apache.sling.engine.impl.filter.ServletFilterManager) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

SlingRequestProcessor (org.apache.sling.engine.SlingRequestProcessor)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Pattern (java.util.regex.Pattern)1 GenericServlet (javax.servlet.GenericServlet)1 Servlet (javax.servlet.Servlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)1 ServletFilterManager (org.apache.sling.engine.impl.filter.ServletFilterManager)1 RequestListenerManager (org.apache.sling.engine.impl.helper.RequestListenerManager)1 SlingServletContext (org.apache.sling.engine.impl.helper.SlingServletContext)1 RequestProcessorMBean (org.apache.sling.engine.jmx.RequestProcessorMBean)1 SlingRequestStatusHealthCheck (org.apache.sling.hc.support.impl.SlingRequestStatusHealthCheck)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 Activate (org.osgi.service.component.annotations.Activate)1