Search in sources :

Example 16 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredResources.

@Test
public void dtosForSuccesfullyRegisteredResources() throws Exception {
    // register first resource service
    registerResource("/resources", "/resource_1/*");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTOWithFirstResource = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithFirstResource.failedResourceDTOs.length);
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstResource);
    assertEquals(1, contextDTO.resourceDTOs.length);
    assertEquals("/resources", contextDTO.resourceDTOs[0].prefix);
    assertArrayEquals(new String[] { "/resource_1/*" }, contextDTO.resourceDTOs[0].patterns);
    // register second resource service
    registerResource("/resources", "/resource_2/*");
    RuntimeDTO runtimeDTOWithBothResources = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithBothResources.failedResourceDTOs.length);
    contextDTO = assertDefaultContext(runtimeDTOWithBothResources);
    assertEquals(2, contextDTO.resourceDTOs.length);
    assertEquals("/resources", contextDTO.resourceDTOs[0].prefix);
    assertEquals(1, contextDTO.resourceDTOs[0].patterns.length);
    assertEquals(1, contextDTO.resourceDTOs[1].patterns.length);
    final Set<String> patterns = new HashSet<>();
    patterns.add(contextDTO.resourceDTOs[0].patterns[0]);
    patterns.add(contextDTO.resourceDTOs[1].patterns[0]);
    assertTrue(patterns.contains("/resource_1/*"));
    assertTrue(patterns.contains("/resource_2/*"));
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredErrorPages.

@Test
public void dtosForSuccesfullyRegisteredErrorPages() throws Exception {
    // register first error page
    registerErrorPage("error page 1", asList("404", NoSuchElementException.class.getName()));
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTOWithFirstErrorPage = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithFirstErrorPage.failedServletDTOs.length);
    assertEquals(0, runtimeDTOWithFirstErrorPage.failedErrorPageDTOs.length);
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstErrorPage);
    assertEquals(1, contextDTO.errorPageDTOs.length);
    assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
    assertArrayEquals(new String[] { NoSuchElementException.class.getName() }, contextDTO.errorPageDTOs[0].exceptions);
    assertArrayEquals(new long[] { 404 }, contextDTO.errorPageDTOs[0].errorCodes);
    // register second error page
    registerErrorPage("error page 2", asList("500", ServletException.class.getName()));
    RuntimeDTO runtimeDTOWithBothErrorPages = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithBothErrorPages.failedServletDTOs.length);
    assertEquals(0, runtimeDTOWithBothErrorPages.failedErrorPageDTOs.length);
    contextDTO = assertDefaultContext(runtimeDTOWithBothErrorPages);
    assertEquals(2, contextDTO.errorPageDTOs.length);
    assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
    assertEquals("error page 2", contextDTO.errorPageDTOs[1].name);
    assertArrayEquals(new String[] { ServletException.class.getName() }, contextDTO.errorPageDTOs[1].exceptions);
    assertArrayEquals(new long[] { 500 }, contextDTO.errorPageDTOs[1].errorCodes);
}
Also used : ServletException(javax.servlet.ServletException) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 18 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method differentTargetIsIgnored.

// As specified in OSGi Compendium Release 6, Chapter 140.3
@Test
public void differentTargetIsIgnored() throws InterruptedException {
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet", HTTP_WHITEBOARD_SERVLET_NAME, "servlet", HTTP_WHITEBOARD_TARGET, "(org.osgi.service.http.port=8282)");
    registrations.add(m_context.registerService(Servlet.class.getName(), new TestServlet(), properties));
    awaitServiceRegistration();
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(0, defaultContext.servletDTOs.length);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) Test(org.junit.Test)

Example 19 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method namedServletIsNotIgnored.

@Test
public void namedServletIsNotIgnored() throws InterruptedException {
    // Neither pattern nor error page specified
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_NAME, "servlet");
    registrations.add(m_context.registerService(Servlet.class.getName(), new TestServlet(), properties));
    awaitServiceRegistration();
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
    ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(1, defaultContext.servletDTOs.length);
    assertEquals(0, defaultContext.servletDTOs[0].patterns.length);
    assertEquals("servlet", defaultContext.servletDTOs[0].name);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) Test(org.junit.Test)

Example 20 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO in project felix by apache.

the class HttpServiceRuntimeTest method mulitpleServletsWithSamePatternHttpServiceRegistrationWins.

// As specified in OSGi Compendium Release 6, Chapter 140.4
@Test
public void mulitpleServletsWithSamePatternHttpServiceRegistrationWins() throws Exception {
    registerServlet("servlet 1", "/pathcollision");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(1, defaultContext.servletDTOs.length);
    CountDownLatch initLatch = new CountDownLatch(1);
    CountDownLatch destroyLatch = new CountDownLatch(1);
    TestServlet testServlet = new TestServlet(initLatch, destroyLatch);
    register("/pathcollision", testServlet);
    RuntimeDTO runtimeWithShadowedServlet = serviceRuntime.getRuntimeDTO();
    awaitServiceRegistration(initLatch);
    defaultContext = assertDefaultContext(runtimeWithShadowedServlet);
    ServletContextDTO httpServiceContext = runtimeWithShadowedServlet.servletContextDTOs[0];
    assertEquals(HTTP_CONTEXT_NAME, httpServiceContext.name);
    assertEquals(1, httpServiceContext.servletDTOs.length);
    assertArrayEquals(new String[] { "/pathcollision" }, httpServiceContext.servletDTOs[0].patterns);
    assertEquals(1, defaultContext.servletDTOs.length);
    ServletDTO servletDTO = defaultContext.servletDTOs[0];
    assertEquals("servlet 1", servletDTO.name);
    // check request info DTO to see which servlet responds
    final RequestInfoDTO infoDTO = serviceRuntime.calculateRequestInfoDTO("/pathcollision");
    assertEquals(httpServiceContext.serviceId, infoDTO.servletDTO.servletContextId);
    unregister("/pathcollision");
    awaitServiceRegistration(destroyLatch);
    runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(1, defaultContext.servletDTOs.length);
    assertEquals("servlet 1", defaultContext.servletDTOs[0].name);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) RequestInfoDTO(org.osgi.service.http.runtime.dto.RequestInfoDTO) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) FailedServletDTO(org.osgi.service.http.runtime.dto.FailedServletDTO) ServletDTO(org.osgi.service.http.runtime.dto.ServletDTO) Test(org.junit.Test)

Aggregations

ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)34 Test (org.junit.Test)29 RuntimeDTO (org.osgi.service.http.runtime.dto.RuntimeDTO)21 HttpServiceRuntime (org.osgi.service.http.runtime.HttpServiceRuntime)20 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 Servlet (javax.servlet.Servlet)7 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)7 ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)7 ServletConfig (javax.servlet.ServletConfig)5 HashSet (java.util.HashSet)4 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)4 ArrayList (java.util.ArrayList)3 FailedServletContextDTO (org.osgi.service.http.runtime.dto.FailedServletContextDTO)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 NoSuchElementException (java.util.NoSuchElementException)2 ServletContextListener (javax.servlet.ServletContextListener)2 ServletException (javax.servlet.ServletException)2 ServletRequestListener (javax.servlet.ServletRequestListener)2