Search in sources :

Example 16 with RuntimeDTO

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

the class HttpServiceRuntimeTest method exceptionInFilterInitAppearsAsFailure.

@Test
public void exceptionInFilterInitAppearsAsFailure() throws ServletException, InterruptedException {
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_FILTER_PATTERN, "/filter", HTTP_WHITEBOARD_FILTER_NAME, "filter");
    CountDownLatch initLatch = new CountDownLatch(1);
    Filter failingFilter = new TestFilter(initLatch, null) {

        @Override
        public void init(FilterConfig config) throws ServletException {
            super.init(config);
            throw new ServletException();
        }
    };
    registrations.add(m_context.registerService(Filter.class.getName(), failingFilter, properties));
    awaitServiceRegistration(initLatch);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(1, runtimeDTO.failedFilterDTOs.length);
    assertEquals("filter", runtimeDTO.failedFilterDTOs[0].name);
    assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedFilterDTOs[0].failureReason);
}
Also used : ServletException(javax.servlet.ServletException) Filter(javax.servlet.Filter) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) FilterConfig(javax.servlet.FilterConfig) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 17 with RuntimeDTO

use of org.osgi.service.http.runtime.dto.RuntimeDTO 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)

Example 18 with RuntimeDTO

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

the class HttpServiceRuntimeTest method dtosForSuccesfullyRegisteredFilters.

@Test
public void dtosForSuccesfullyRegisteredFilters() throws Exception {
    // register first filter
    registerFilter("testFilter 1", "/servlet_1");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTOWithFirstFilter = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithFirstFilter.failedFilterDTOs.length);
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstFilter);
    assertEquals(1, contextDTO.filterDTOs.length);
    assertEquals("testFilter 1", contextDTO.filterDTOs[0].name);
    // register second filter
    registerFilter("testFilter 2", "/servlet_1");
    RuntimeDTO runtimeDTOWithBothFilters = serviceRuntime.getRuntimeDTO();
    assertEquals(0, runtimeDTOWithBothFilters.failedFilterDTOs.length);
    contextDTO = assertDefaultContext(runtimeDTOWithBothFilters);
    assertEquals(2, contextDTO.filterDTOs.length);
    assertEquals("testFilter 1", contextDTO.filterDTOs[0].name);
    assertEquals("testFilter 2", contextDTO.filterDTOs[1].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 19 with RuntimeDTO

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

the class HttpServiceRuntimeTest method hiddenDefaultContextAppearsAsFailure.

// As specified in OSGi Compendium Release 6, Chapter 140.1 (TODO : exact version)
@Test
public void hiddenDefaultContextAppearsAsFailure() throws InterruptedException {
    registerContext("default", "");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
    assertEquals("default", runtimeDTO.failedServletContextDTOs[0].name);
    assertDefaultContext(runtimeDTO);
}
Also used : HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) Test(org.junit.Test)

Example 20 with RuntimeDTO

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

the class HttpServiceRuntimeTest method exceptionInServletInitDuringServletRemovalAppearsAsFailure.

@Test
public void exceptionInServletInitDuringServletRemovalAppearsAsFailure() throws ServletException, InterruptedException {
    Dictionary<String, ?> properties1 = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet1", HTTP_WHITEBOARD_SERVLET_NAME, "servlet1");
    final CountDownLatch initLatch1 = new CountDownLatch(1);
    @SuppressWarnings("serial") Servlet failingServlet1 = new TestServlet(initLatch1, null) {

        @Override
        public void init() throws ServletException {
            // fail when initialized the second time
            if (initLatch1.getCount() == 0) {
                throw new ServletException();
            }
            super.init();
        }
    };
    Dictionary<String, ?> properties2 = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet2", HTTP_WHITEBOARD_SERVLET_NAME, "servlet2");
    final CountDownLatch initLatch2 = new CountDownLatch(1);
    @SuppressWarnings("serial") Servlet failingServlet2 = new TestServlet(initLatch2, null) {

        @Override
        public void init() throws ServletException {
            // fail when initialized the second time
            if (initLatch2.getCount() == 0) {
                throw new ServletException();
            }
            super.init();
        }
    };
    Dictionary<String, ?> propertiesShadowing = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, asList("/servlet1", "/servlet2"), HTTP_WHITEBOARD_SERVLET_NAME, "servletShadowing", SERVICE_RANKING, Integer.MAX_VALUE);
    CountDownLatch initLatchShadowing = new CountDownLatch(1);
    Servlet servletShadowing = new TestServlet(initLatchShadowing, null);
    registrations.add(m_context.registerService(Servlet.class.getName(), failingServlet1, properties1));
    registrations.add(m_context.registerService(Servlet.class.getName(), failingServlet2, properties2));
    awaitServiceRegistration(initLatch1);
    awaitServiceRegistration(initLatch2);
    ServiceRegistration<?> shadowingRegistration = m_context.registerService(Servlet.class.getName(), servletShadowing, propertiesShadowing);
    registrations.add(shadowingRegistration);
    awaitServiceRegistration(initLatchShadowing);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(2, runtimeDTO.failedServletDTOs.length);
    assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, runtimeDTO.failedServletDTOs[0].failureReason);
    assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, runtimeDTO.failedServletDTOs[1].failureReason);
    shadowingRegistration.unregister();
    runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(2, runtimeDTO.failedServletDTOs.length);
    assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedServletDTOs[0].failureReason);
    assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedServletDTOs[1].failureReason);
}
Also used : ServletException(javax.servlet.ServletException) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) Servlet(javax.servlet.Servlet) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

RuntimeDTO (org.osgi.service.http.runtime.dto.RuntimeDTO)34 HttpServiceRuntime (org.osgi.service.http.runtime.HttpServiceRuntime)31 Test (org.junit.Test)30 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)21 CountDownLatch (java.util.concurrent.CountDownLatch)12 ServletException (javax.servlet.ServletException)5 Servlet (javax.servlet.Servlet)4 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)4 FailedErrorPageDTO (org.osgi.service.http.runtime.dto.FailedErrorPageDTO)3 FailedServletContextDTO (org.osgi.service.http.runtime.dto.FailedServletContextDTO)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 ServletRequestListener (javax.servlet.ServletRequestListener)2 FailedFilterDTO (org.osgi.service.http.runtime.dto.FailedFilterDTO)2 FailedListenerDTO (org.osgi.service.http.runtime.dto.FailedListenerDTO)2 FailedResourceDTO (org.osgi.service.http.runtime.dto.FailedResourceDTO)2 ServletDTO (org.osgi.service.http.runtime.dto.ServletDTO)2 PrintWriter (java.io.PrintWriter)1 Collection (java.util.Collection)1