Search in sources :

Example 21 with RuntimeDTO

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

the class HttpServiceRuntimeTest method invalidContextHelperPathAppearsAsFailure.

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

Example 22 with RuntimeDTO

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

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

Example 24 with RuntimeDTO

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

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

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