Search in sources :

Example 26 with ServletContextDTO

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

the class HttpServiceRuntimeTest method successfulSetup.

@Test
public void successfulSetup() throws InterruptedException {
    CountDownLatch initLatch = new CountDownLatch(6);
    registerContext("test-context", "/test-context");
    registerServlet("default servlet", "/default", initLatch);
    registerFilter("default filter", "/default", initLatch);
    registerErrorPage("default error page", asList(Exception.class.getName()), initLatch);
    registerResource("/", "/default/resource");
    registerListener(ServletRequestListener.class, true);
    registerServlet("context servlet", "/default", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
    registerFilter("context filter", "/default", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
    registerErrorPage("context error page", asList("500"), "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
    registerResource("/", "/test-contextd/resource", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)");
    registerListener(ServletRequestListener.class, true, "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)");
    awaitServiceRegistration(initLatch);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
    assertEquals(0, runtimeDTO.failedFilterDTOs.length);
    assertEquals(0, runtimeDTO.failedListenerDTOs.length);
    assertEquals(0, runtimeDTO.failedResourceDTOs.length);
    assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    assertEquals(3, runtimeDTO.servletContextDTOs.length);
    assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
    assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
    assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
    ServletContextDTO defaultContextDTO = runtimeDTO.servletContextDTOs[2];
    long contextServiceId = defaultContextDTO.serviceId;
    assertEquals(Arrays.toString(defaultContextDTO.servletDTOs), 2, defaultContextDTO.servletDTOs.length);
    assertServlet(defaultContextDTO.servletDTOs, "default servlet", contextServiceId);
    assertServlet(defaultContextDTO.servletDTOs, "default error page", contextServiceId);
    assertEquals(1, defaultContextDTO.filterDTOs.length);
    assertEquals("default filter", defaultContextDTO.filterDTOs[0].name);
    assertEquals(contextServiceId, defaultContextDTO.filterDTOs[0].servletContextId);
    assertEquals(1, defaultContextDTO.errorPageDTOs.length);
    assertEquals(Exception.class.getName(), defaultContextDTO.errorPageDTOs[0].exceptions[0]);
    assertEquals(contextServiceId, defaultContextDTO.errorPageDTOs[0].servletContextId);
    assertEquals(1, defaultContextDTO.listenerDTOs.length);
    assertEquals(ServletRequestListener.class.getName(), defaultContextDTO.listenerDTOs[0].types[0]);
    assertEquals(contextServiceId, defaultContextDTO.listenerDTOs[0].servletContextId);
    ServletContextDTO testContextDTO = runtimeDTO.servletContextDTOs[1];
    contextServiceId = testContextDTO.serviceId;
    assertEquals(2, testContextDTO.servletDTOs.length);
    assertServlet(testContextDTO.servletDTOs, "context servlet", contextServiceId);
    assertServlet(testContextDTO.servletDTOs, "context error page", contextServiceId);
    assertEquals(1, testContextDTO.filterDTOs.length);
    assertEquals("context filter", testContextDTO.filterDTOs[0].name);
    assertEquals(contextServiceId, testContextDTO.filterDTOs[0].servletContextId);
    assertEquals(1, testContextDTO.errorPageDTOs.length);
    assertEquals(500L, testContextDTO.errorPageDTOs[0].errorCodes[0]);
    assertEquals(contextServiceId, testContextDTO.errorPageDTOs[0].servletContextId);
    assertEquals(1, testContextDTO.listenerDTOs.length);
    assertEquals(ServletRequestListener.class.getName(), testContextDTO.listenerDTOs[0].types[0]);
    assertEquals(contextServiceId, testContextDTO.listenerDTOs[0].servletContextId);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletRequestListener(javax.servlet.ServletRequestListener) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 27 with ServletContextDTO

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

the class HttpServiceRuntimeTest method servletWithoutNameGetsFullyQualifiedName.

// As specified in OSGi Compendium Release 6, Chapter 140.4
@Test
public void servletWithoutNameGetsFullyQualifiedName() throws InterruptedException {
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet");
    CountDownLatch initLatch = new CountDownLatch(1);
    registrations.add(m_context.registerService(Servlet.class.getName(), new TestServlet(initLatch, null), properties));
    awaitServiceRegistration(initLatch);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTO.failedServletDTOs.length);
    ServletContextDTO defaultContext = assertDefaultContext(serviceRuntime.getRuntimeDTO());
    assertEquals(1, defaultContext.servletDTOs.length);
    assertEquals(TestServlet.class.getName(), 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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 28 with ServletContextDTO

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

the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredErrorPageWithWildcardErrorCode.

public void dtosForSuccesfullyRegisteredErrorPageWithWildcardErrorCode(String code, long startCode) throws Exception {
    registerErrorPage("error page 1", asList(code));
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTOWithErrorPage = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithErrorPage.failedServletDTOs.length);
    assertEquals(0, runtimeDTOWithErrorPage.failedErrorPageDTOs.length);
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithErrorPage);
    assertEquals(1, contextDTO.errorPageDTOs.length);
    assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
    assertContainsAllHundredFrom(startCode, contextDTO.errorPageDTOs[0].errorCodes);
}
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)

Example 29 with ServletContextDTO

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

the class HttpServiceRuntimeTest method requestInfoDTO.

@Test
public void requestInfoDTO() throws Exception {
    registerServlet("servlet", "/default");
    registerFilter("filter1", "/default");
    registerFilter("filter2", "/default");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    ServletContextDTO defaultContext = assertDefaultContext(serviceRuntime.getRuntimeDTO());
    long defaultContextId = defaultContext.serviceId;
    RequestInfoDTO requestInfoDTO = serviceRuntime.calculateRequestInfoDTO("/default");
    assertEquals("/default", requestInfoDTO.path);
    assertEquals(defaultContextId, requestInfoDTO.servletContextId);
    assertEquals("servlet", requestInfoDTO.servletDTO.name);
    assertEquals(2, requestInfoDTO.filterDTOs.length);
    assertEquals("filter1", requestInfoDTO.filterDTOs[0].name);
    assertEquals("filter2", requestInfoDTO.filterDTOs[1].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) Test(org.junit.Test)

Example 30 with ServletContextDTO

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

the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredListeners.

@Test
public void dtosForSuccesfullyRegisteredListeners() throws Exception {
    // register a servlet context listenere as first listener
    registerListener(ServletContextListener.class, true);
    awaitService(ServletContextListener.class.getName());
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTOWithFirstListener = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithFirstListener.failedListenerDTOs.length);
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstListener);
    assertEquals(1, contextDTO.listenerDTOs.length);
    assertEquals(ServletContextListener.class.getName(), contextDTO.listenerDTOs[0].types[0]);
    // register all other listener types
    registerListener(ServletContextAttributeListener.class, true);
    registerListener(ServletRequestListener.class, true);
    registerListener(ServletRequestAttributeListener.class, true);
    registerListener(HttpSessionListener.class, true);
    registerListener(HttpSessionAttributeListener.class, true);
    awaitService(ServletContextAttributeListener.class.getName());
    awaitService(ServletRequestListener.class.getName());
    awaitService(ServletRequestAttributeListener.class.getName());
    awaitService(HttpSessionListener.class.getName());
    awaitService(HttpSessionAttributeListener.class.getName());
    RuntimeDTO runtimeDTOWithAllListeners = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithAllListeners.failedListenerDTOs.length);
    contextDTO = assertDefaultContext(runtimeDTOWithAllListeners);
    assertEquals(6, contextDTO.listenerDTOs.length);
    assertEquals(ServletContextListener.class.getName(), contextDTO.listenerDTOs[0].types[0]);
    assertEquals(ServletContextAttributeListener.class.getName(), contextDTO.listenerDTOs[1].types[0]);
    assertEquals(ServletRequestListener.class.getName(), contextDTO.listenerDTOs[2].types[0]);
    assertEquals(ServletRequestAttributeListener.class.getName(), contextDTO.listenerDTOs[3].types[0]);
    assertEquals(HttpSessionListener.class.getName(), contextDTO.listenerDTOs[4].types[0]);
    assertEquals(HttpSessionAttributeListener.class.getName(), contextDTO.listenerDTOs[5].types[0]);
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletContextListener(javax.servlet.ServletContextListener) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletRequestListener(javax.servlet.ServletRequestListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) 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