Search in sources :

Example 11 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ServletRegistryTest method testSimpleHiding.

@Test
public void testSimpleHiding() throws InvalidSyntaxException, ServletException {
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // empty reg
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
    // register servlets
    final ServletHandler h1 = createServletHandler(1L, 10, "/foo");
    reg.addServlet(h1);
    verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
    final ServletHandler h2 = createServletHandler(2L, 0, "/foo");
    reg.addServlet(h2);
    verify(h2.getServlet(), never()).init(Matchers.any(ServletConfig.class));
    verify(h1.getServlet(), never()).destroy();
    // two entries in reg
    // h1 is active
    // h2 is hidden
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertNull(dto.resourceDTOs);
    assertTrue(holder.failedResourceDTOs.isEmpty());
    assertFalse(holder.failedServletDTOs.isEmpty());
    assertNotNull(dto.servletDTOs);
    assertEquals(1, dto.servletDTOs.length);
    assertEquals(1, dto.servletDTOs[0].patterns.length);
    assertEquals("/foo", dto.servletDTOs[0].patterns[0]);
    assertEquals(1, dto.servletDTOs[0].serviceId);
    assertEquals(1, holder.failedServletDTOs.size());
    final FailedServletDTO failedDTO = holder.failedServletDTOs.iterator().next();
    assertEquals(1, failedDTO.patterns.length);
    assertEquals("/foo", failedDTO.patterns[0]);
    assertEquals(2, failedDTO.serviceId);
    // remove servlet 1
    final Servlet s1 = h1.getServlet();
    reg.removeServlet(h1.getServletInfo(), true);
    verify(s1).destroy();
    verify(h2.getServlet()).init(Matchers.any(ServletConfig.class));
    // h2 is active
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertNull(dto.resourceDTOs);
    assertTrue(holder.failedResourceDTOs.isEmpty());
    assertTrue(holder.failedServletDTOs.isEmpty());
    assertNotNull(dto.servletDTOs);
    assertEquals(1, dto.servletDTOs.length);
    assertEquals(1, dto.servletDTOs[0].patterns.length);
    assertEquals("/foo", dto.servletDTOs[0].patterns[0]);
    assertEquals(2, dto.servletDTOs[0].serviceId);
    // remove servlet 2
    final Servlet s2 = h2.getServlet();
    reg.removeServlet(h2.getServletInfo(), true);
    verify(s2).destroy();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedServletDTO(org.osgi.service.http.runtime.dto.FailedServletDTO) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletConfig(javax.servlet.ServletConfig) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 12 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ServletRegistryTest method testMatcherOrdering.

@Test
public void testMatcherOrdering() throws InvalidSyntaxException {
    final ServletHandler h1 = createServletHandler(1L, 0, "/foo");
    final ServletHandler h2 = createServletHandler(2L, 0, "/foo/*");
    final List<PathResolver> resolvers = new ArrayList<PathResolver>();
    resolvers.add(PathResolverFactory.createPatternMatcher(h1, "/foo"));
    resolvers.add(PathResolverFactory.createPatternMatcher(h2, "/foo/*"));
    Collections.sort(resolvers);
    assertEquals("/foo", resolvers.get(0).getPattern());
    assertEquals("/foo/*", resolvers.get(1).getPattern());
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ServletRegistryTest method testSingleServlet.

@Test
public void testSingleServlet() throws InvalidSyntaxException, ServletException {
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // empty reg
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
    // register servlet
    final ServletHandler h1 = createServletHandler(1L, 0, "/foo");
    reg.addServlet(h1);
    verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
    // one entry in reg
    // check DTO
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertNull(dto.resourceDTOs);
    assertTrue(holder.failedResourceDTOs.isEmpty());
    assertTrue(holder.failedServletDTOs.isEmpty());
    assertNotNull(dto.servletDTOs);
    assertEquals(1, dto.servletDTOs.length);
    assertEquals(1, dto.servletDTOs[0].patterns.length);
    assertEquals("/foo", dto.servletDTOs[0].patterns[0]);
    // remove servlet
    final Servlet s = h1.getServlet();
    reg.removeServlet(h1.getServletInfo(), true);
    verify(s).destroy();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedServletDTOs, holder.failedResourceDTOs);
    assertEmpty(dto, holder);
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletConfig(javax.servlet.ServletConfig) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 14 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ErrorPageRegistryTest method testRangeRegistration.

@Test
public void testRangeRegistration() throws InvalidSyntaxException {
    final ErrorPageRegistry reg = new ErrorPageRegistry();
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    final ServletHandler handler4 = createServletHandler(1L, 0, "4xx");
    final ServletHandler handler5 = createServletHandler(2L, 0, "5xx");
    reg.addServlet(handler4);
    reg.addServlet(handler5);
    // check DTO
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertTrue(holder.failedErrorPageDTOs.isEmpty());
    assertEquals(2, dto.errorPageDTOs.length);
    assertEquals(100, dto.errorPageDTOs[0].errorCodes.length);
    final Set<Long> codes4 = new HashSet<Long>();
    for (final long c : dto.errorPageDTOs[0].errorCodes) {
        assertTrue(c >= 400 && c < 500);
        codes4.add(c);
    }
    assertEquals(100, codes4.size());
    assertEquals(100, dto.errorPageDTOs[1].errorCodes.length);
    final Set<Long> codes5 = new HashSet<Long>();
    for (final long c : dto.errorPageDTOs[1].errorCodes) {
        assertTrue(c >= 500 && c < 600);
        codes5.add(c);
    }
    assertEquals(100, codes5.size());
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ServletRegistryTest method testServletOrdering.

@Test
public void testServletOrdering() throws InvalidSyntaxException {
    final ServletHandler h1 = createServletHandler(1L, 0, "/foo");
    reg.addServlet(h1);
    final ServletHandler h2 = createServletHandler(2L, 0, "/foo/*");
    reg.addServlet(h2);
    final ServletHandler h3 = createServletHandler(3L, 0, "/foo/rsrc");
    reg.addServlet(h3);
    final ServletHandler h4 = createServletHandler(4L, 0, "/foo/rsrc/*");
    reg.addServlet(h4);
    final ServletHandler h5 = createServletHandler(5L, 0, "/other");
    reg.addServlet(h5);
    PathResolution pr = null;
    pr = reg.resolve("/foo");
    assertNotNull(pr);
    assertEquals("/foo", pr.patterns[0]);
    assertEquals(h1, pr.handler);
    pr = reg.resolve("/fool");
    assertNull(pr);
    pr = reg.resolve("/foo/bar");
    assertEquals("/foo", pr.patterns[0]);
    assertEquals(h1, pr.handler);
    pr = reg.resolve("/foo/rsrc");
    assertEquals("/foo/rsrc", pr.patterns[0]);
    assertEquals(h3, pr.handler);
    pr = reg.resolve("/foo/rsrc/some");
    assertEquals("/foo/rsrc", pr.patterns[0]);
    assertEquals(h3, pr.handler);
    pr = reg.resolve("/other");
    assertEquals("/other", pr.patterns[0]);
    assertEquals(h5, pr.handler);
    pr = reg.resolve("/other/bla");
    assertEquals("/other", pr.patterns[0]);
    assertEquals(h5, pr.handler);
    // cleanup
    reg.removeServlet(h1.getServletInfo(), true);
    reg.removeServlet(h2.getServletInfo(), true);
    reg.removeServlet(h3.getServletInfo(), true);
    reg.removeServlet(h4.getServletInfo(), true);
    reg.removeServlet(h5.getServletInfo(), true);
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) Test(org.junit.Test)

Aggregations

ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)22 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)12 Test (org.junit.Test)9 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)7 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)7 Servlet (javax.servlet.Servlet)6 ServletConfig (javax.servlet.ServletConfig)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)3 FileNotFoundException (java.io.FileNotFoundException)2 RequestDispatcher (javax.servlet.RequestDispatcher)2 ServletException (javax.servlet.ServletException)2 RequestDispatcherImpl (org.apache.felix.http.base.internal.dispatch.RequestDispatcherImpl)2 RequestInfo (org.apache.felix.http.base.internal.dispatch.RequestInfo)2 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)2 ServletResolution (org.apache.felix.http.base.internal.registry.ServletResolution)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1