Search in sources :

Example 1 with ServletResourceProvider

use of org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider in project sling by apache.

the class SlingServletResolver method createServlet.

private boolean createServlet(final Servlet servlet, final ServiceReference<Servlet> reference) {
    // check for a name, this is required
    final String name = getName(reference);
    // check for Sling properties in the service registration
    final ServletResourceProvider provider = servletResourceProviderFactory.create(reference, servlet);
    if (provider == null) {
        // this is expected if the servlet is not destined for Sling
        return false;
    }
    // initialize now
    try {
        servlet.init(new SlingServletConfig(servletContext, reference, name));
        LOGGER.debug("bindServlet: Servlet {} initialized", name);
    } catch (ServletException ce) {
        LOGGER.error("bindServlet: Servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference) + " failed to initialize", ce);
        return false;
    } catch (Throwable t) {
        LOGGER.error("bindServlet: Unexpected problem initializing servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference), t);
        return false;
    }
    final List<ServiceRegistration<ResourceProvider<Object>>> regs = new ArrayList<>();
    for (final String root : provider.getServletPaths()) {
        @SuppressWarnings("unchecked") final ServiceRegistration<ResourceProvider<Object>> reg = (ServiceRegistration<ResourceProvider<Object>>) context.registerService(ResourceProvider.class.getName(), provider, createServiceProperties(reference, provider, root));
        regs.add(reg);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Registered {}", provider.toString());
    }
    synchronized (this.servletsByReference) {
        servletsByReference.put(reference, new ServletReg(servlet, regs));
    }
    return true;
}
Also used : SlingServletConfig(org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with ServletResourceProvider

use of org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider in project sling by apache.

the class SlingServletResolverTest method testCreateServiceRegistrationProperties.

@Test
public void testCreateServiceRegistrationProperties() throws Throwable {
    MockServiceReference msr = new MockServiceReference(null);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, "sample");
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, "GET");
    Field srpf = SlingServletResolver.class.getDeclaredField("servletResourceProviderFactory");
    srpf.setAccessible(true);
    ServletResourceProviderFactory factory = (ServletResourceProviderFactory) srpf.get(servletResolver);
    ServletResourceProvider servlet = factory.create(msr, null);
    Method createServiceProperties = SlingServletResolver.class.getDeclaredMethod("createServiceProperties", ServiceReference.class, ServletResourceProvider.class, String.class);
    createServiceProperties.setAccessible(true);
    // no ranking
    assertNull(msr.getProperty(Constants.SERVICE_RANKING));
    @SuppressWarnings("unchecked") final Dictionary<String, Object> p1 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
    assertNull(p1.get(Constants.SERVICE_RANKING));
    // illegal type of ranking
    Object nonIntValue = "Some Non Integer Value";
    msr.setProperty(Constants.SERVICE_RANKING, nonIntValue);
    assertEquals(nonIntValue, msr.getProperty(Constants.SERVICE_RANKING));
    @SuppressWarnings("unchecked") final Dictionary<String, Object> p2 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
    assertNull(p2.get(Constants.SERVICE_RANKING));
    // illegal type of ranking
    Object intValue = Integer.valueOf(123);
    msr.setProperty(Constants.SERVICE_RANKING, intValue);
    assertEquals(intValue, msr.getProperty(Constants.SERVICE_RANKING));
    @SuppressWarnings("unchecked") final Dictionary<String, Object> p3 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
    assertEquals(intValue, p3.get(Constants.SERVICE_RANKING));
}
Also used : MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) Field(java.lang.reflect.Field) Dictionary(java.util.Dictionary) ServletResourceProviderFactory(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ServletResourceProvider (org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 ServletException (javax.servlet.ServletException)1 MockServiceReference (org.apache.sling.commons.testing.osgi.MockServiceReference)1 SlingServletConfig (org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig)1 ServletResourceProviderFactory (org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory)1 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)1 Test (org.junit.Test)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1