Search in sources :

Example 31 with ServletContextDTO

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

the class HttpServiceRuntimeTest method patternAndErrorPageSpecified.

// As specified in OSGi Compendium Release 6, Chapter 140.4.1
@Test
public void patternAndErrorPageSpecified() throws InterruptedException {
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet", HTTP_WHITEBOARD_SERVLET_NAME, "servlet", HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, asList("400"));
    registrations.add(m_context.registerService(Servlet.class.getName(), new TestServlet(), properties));
    awaitServiceRegistration();
    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(0, runtimeDTO.failedServletDTOs.length);
    assertEquals(1, defaultContext.servletDTOs.length);
    assertEquals(1, defaultContext.errorPageDTOs.length);
    assertEquals("servlet", defaultContext.servletDTOs[0].name);
    assertEquals("servlet", defaultContext.errorPageDTOs[0].name);
    assertArrayEquals(new String[] { "/servlet" }, defaultContext.servletDTOs[0].patterns);
    assertArrayEquals(new long[] { 400 }, defaultContext.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) Test(org.junit.Test)

Example 32 with ServletContextDTO

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

the class HttpServiceRuntimeTest method dtosAreIndependentCopies.

@Test
public void dtosAreIndependentCopies() throws Exception {
    // register first servlet
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/test", HTTP_WHITEBOARD_SERVLET_NAME, "servlet 1", HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + "test", "testValue");
    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 runtimeDTOWithFirstSerlvet = serviceRuntime.getRuntimeDTO();
    // register second servlet
    registerServlet("testServlet 2", "/servlet_2");
    RuntimeDTO runtimeDTOWithTwoSerlvets = serviceRuntime.getRuntimeDTO();
    assertNotSame(runtimeDTOWithFirstSerlvet, runtimeDTOWithTwoSerlvets);
    ServletContextDTO defaultContextFirstServlet = assertDefaultContext(runtimeDTOWithFirstSerlvet);
    ServletContextDTO defaultContextTwoServlets = assertDefaultContext(runtimeDTOWithTwoSerlvets);
    assertNotSame(defaultContextFirstServlet.servletDTOs[0].patterns, defaultContextTwoServlets.servletDTOs[0].patterns);
    boolean mapsModifiable = true;
    try {
        defaultContextTwoServlets.servletDTOs[0].initParams.clear();
    } catch (UnsupportedOperationException e) {
        mapsModifiable = false;
    }
    if (mapsModifiable) {
        assertNotSame(defaultContextFirstServlet.servletDTOs[0].initParams, defaultContextTwoServlets.servletDTOs[0].initParams);
    }
}
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 33 with ServletContextDTO

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

the class HttpServiceRuntimeTest method httpRuntimeServiceIsAvailableAfterBundleActivation.

@Test
public void httpRuntimeServiceIsAvailableAfterBundleActivation() throws Exception {
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    ServiceReferenceDTO serviceDTO = runtimeDTO.serviceDTO;
    assertNotNull(serviceDTO);
    assertNotNull(serviceDTO.properties);
    assertTrue(serviceDTO.properties.containsKey(HTTP_SERVICE_ID));
    assertTrue(serviceDTO.properties.containsKey(HTTP_SERVICE_ENDPOINT));
    assertTrue(serviceDTO.properties.get(HTTP_SERVICE_ID) instanceof Collection);
    final Collection ids = (Collection) serviceDTO.properties.get(HTTP_SERVICE_ID);
    assertTrue(ids.size() == 1);
    assertTrue(ids.iterator().next() instanceof Long);
    assertTrue(0 < (Long) ids.iterator().next());
    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);
    ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
    assertEquals(0, defaultContext.attributes.size());
    // TODO The default context should have a negative service Id
    // assertTrue(0 > runtimeDTO.servletContextDTOs[0].serviceId);
    // TODO Should be "/" ?
    assertEquals("", defaultContext.contextPath);
    assertEquals(0, defaultContext.initParams.size());
    assertEquals(0, defaultContext.filterDTOs.length);
    assertEquals(0, defaultContext.servletDTOs.length);
    assertEquals(0, defaultContext.resourceDTOs.length);
    assertEquals(0, defaultContext.errorPageDTOs.length);
    assertEquals(0, defaultContext.listenerDTOs.length);
}
Also used : ServiceReferenceDTO(org.osgi.framework.dto.ServiceReferenceDTO) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) Collection(java.util.Collection) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) Test(org.junit.Test)

Example 34 with ServletContextDTO

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

the class ServletContextDTOBuilder method build.

public static ServletContextDTO build(final ServletContextHelperInfo info, final ServletContext context, final int reason) {
    final ServletContextDTO dto = (reason == -1 ? new ServletContextDTO() : new FailedServletContextDTO());
    dto.attributes = getAttributes(context);
    dto.contextPath = context != null ? context.getContextPath() : info.getPath();
    dto.initParams = info.getInitParameters();
    dto.name = info.getName();
    dto.serviceId = info.getServiceId();
    dto.errorPageDTOs = BuilderConstants.ERROR_PAGE_DTO_ARRAY;
    dto.filterDTOs = BuilderConstants.FILTER_FAILURE_DTO_ARRAY;
    dto.listenerDTOs = BuilderConstants.LISTENER_DTO_ARRAY;
    dto.resourceDTOs = BuilderConstants.RESOURCE_DTO_ARRAY;
    dto.servletDTOs = BuilderConstants.SERVLET_DTO_ARRAY;
    if (reason != -1) {
        ((FailedServletContextDTO) dto).failureReason = reason;
    }
    return dto;
}
Also used : ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) FailedServletContextDTO(org.osgi.service.http.runtime.dto.FailedServletContextDTO) FailedServletContextDTO(org.osgi.service.http.runtime.dto.FailedServletContextDTO)

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