Search in sources :

Example 21 with HttpServiceRuntime

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

use of org.osgi.service.http.runtime.HttpServiceRuntime 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 23 with HttpServiceRuntime

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

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

the class HttpServiceRuntimeTest method invalidContextHelperNameAppearsAsFailure.

// As specified in OSGi Compendium Release 6, Chapter 140.1
@Test
public void invalidContextHelperNameAppearsAsFailure() throws InterruptedException {
    registerContext("context A", "");
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
    assertEquals("context A", runtimeDTO.failedServletContextDTOs[0].name);
    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 25 with HttpServiceRuntime

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

Aggregations

HttpServiceRuntime (org.osgi.service.http.runtime.HttpServiceRuntime)34 Test (org.junit.Test)32 RuntimeDTO (org.osgi.service.http.runtime.dto.RuntimeDTO)31 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)20 CountDownLatch (java.util.concurrent.CountDownLatch)12 ServletException (javax.servlet.ServletException)5 Servlet (javax.servlet.Servlet)4 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 ServletRequestListener (javax.servlet.ServletRequestListener)2 ServletContextHelper (org.osgi.service.http.context.ServletContextHelper)2 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)2 RequestInfoDTO (org.osgi.service.http.runtime.dto.RequestInfoDTO)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Dictionary (java.util.Dictionary)1 Filter (javax.servlet.Filter)1 FilterConfig (javax.servlet.FilterConfig)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1