Search in sources :

Example 21 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO 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 22 with ServletContextDTO

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

the class HttpServiceRuntimeTest method testCombinedServletAndResourceRegistration.

/**
 * Test for FELIX-5319
 * @throws Exception
 */
@Test
public void testCombinedServletAndResourceRegistration() throws Exception {
    // register single component as Servlet and Resource
    final String servletPath = "/hello/sayHello";
    final String servletName = "Hello World";
    final String rsrcPattern = "/hello/static/*";
    final String rsrcPrefix = "/static";
    CountDownLatch initLatch = new CountDownLatch(1);
    List<Object> propertyEntries = Arrays.<Object>asList(HTTP_WHITEBOARD_SERVLET_PATTERN, servletPath, HTTP_WHITEBOARD_SERVLET_NAME, servletName, HTTP_WHITEBOARD_RESOURCE_PATTERN, rsrcPattern, HTTP_WHITEBOARD_RESOURCE_PREFIX, rsrcPrefix);
    Dictionary<String, ?> properties = createDictionary(propertyEntries.toArray());
    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);
    assertEquals(0, runtimeDTO.failedResourceDTOs.length);
    // check servlet registration
    ServletContextDTO contextDTO = assertDefaultContext(runtimeDTO);
    assertEquals(1, contextDTO.servletDTOs.length);
    assertEquals(servletName, contextDTO.servletDTOs[0].name);
    assertEquals(1, contextDTO.servletDTOs[0].patterns.length);
    assertEquals(servletPath, contextDTO.servletDTOs[0].patterns[0]);
    // check resource registration
    assertEquals(1, contextDTO.resourceDTOs.length);
    assertEquals(1, contextDTO.resourceDTOs[0].patterns.length);
    assertEquals(rsrcPattern, contextDTO.resourceDTOs[0].patterns[0]);
    assertEquals(rsrcPrefix, contextDTO.resourceDTOs[0].prefix);
}
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 23 with ServletContextDTO

use of org.osgi.service.http.runtime.dto.ServletContextDTO 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 24 with ServletContextDTO

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

the class HandlerRegistryTest method testInitialSetup.

@Test
public void testInitialSetup() {
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    dto.serviceId = HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID;
    assertFalse(registry.getRuntimeInfo(dto, holder));
    registry.init();
    assertTrue(registry.getRuntimeInfo(dto, holder));
    registry.shutdown();
    assertFalse(registry.getRuntimeInfo(dto, holder));
}
Also used : FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) Test(org.junit.Test)

Example 25 with ServletContextDTO

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

the class HttpServiceRuntimeTest method multipleErrorPagesForSameErrorCodeChoosenByServiceRankingRules.

// As specified in OSGi Compendium Release 6, Chapter 140.4.1
@Test
public void multipleErrorPagesForSameErrorCodeChoosenByServiceRankingRules() throws InterruptedException {
    registerErrorPage("error page 1", asList(NullPointerException.class.getName(), "500"));
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
    assertEquals(1, defaultContext.errorPageDTOs.length);
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, asList("500", IllegalArgumentException.class.getName()), HTTP_WHITEBOARD_SERVLET_NAME, "error page 2", SERVICE_RANKING, Integer.MAX_VALUE);
    CountDownLatch initLatch = new CountDownLatch(1);
    CountDownLatch destroyLatch = new CountDownLatch(1);
    TestServlet testServlet = new TestServlet(initLatch, destroyLatch);
    ServiceRegistration<?> higherRankingServlet = m_context.registerService(Servlet.class.getName(), testServlet, properties);
    registrations.add(higherRankingServlet);
    awaitServiceRegistration(initLatch);
    RuntimeDTO runtimeWithShadowedErrorPage = serviceRuntime.getRuntimeDTO();
    defaultContext = assertDefaultContext(runtimeWithShadowedErrorPage);
    assertEquals(2, defaultContext.errorPageDTOs.length);
    assertEquals("error page 2", defaultContext.errorPageDTOs[0].name);
    assertArrayEquals(new long[] { 500 }, defaultContext.errorPageDTOs[0].errorCodes);
    assertArrayEquals(new String[] { IllegalArgumentException.class.getName() }, defaultContext.errorPageDTOs[0].exceptions);
    assertEquals("error page 1", defaultContext.errorPageDTOs[1].name);
    assertEquals(0, defaultContext.errorPageDTOs[1].errorCodes.length);
    assertArrayEquals(new String[] { NullPointerException.class.getName() }, defaultContext.errorPageDTOs[1].exceptions);
    assertEquals(1, runtimeWithShadowedErrorPage.failedErrorPageDTOs.length);
    FailedErrorPageDTO failedErrorPageDTO = runtimeWithShadowedErrorPage.failedErrorPageDTOs[0];
    assertEquals("error page 1", failedErrorPageDTO.name);
    assertArrayEquals(new long[] { 500 }, failedErrorPageDTO.errorCodes);
    assertEquals(0, failedErrorPageDTO.exceptions.length);
    assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, failedErrorPageDTO.failureReason);
    higherRankingServlet.unregister();
    awaitServiceRegistration(destroyLatch);
    runtimeDTO = serviceRuntime.getRuntimeDTO();
    defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
    assertEquals(1, defaultContext.errorPageDTOs.length);
    assertEquals("error page 1", defaultContext.errorPageDTOs[0].name);
}
Also used : FailedErrorPageDTO(org.osgi.service.http.runtime.dto.FailedErrorPageDTO) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) Servlet(javax.servlet.Servlet) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) 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