Search in sources :

Example 6 with ServletRegistration

use of javax.servlet.ServletRegistration in project jetty.project by eclipse.

the class HolderTest method testInitParams.

@Test
public void testInitParams() throws Exception {
    ServletHolder holder = new ServletHolder(Source.JAVAX_API);
    ServletRegistration reg = holder.getRegistration();
    try {
        reg.setInitParameter(null, "foo");
        fail("null name accepted");
    } catch (IllegalArgumentException e) {
    }
    try {
        reg.setInitParameter("foo", null);
        fail("null value accepted");
    } catch (IllegalArgumentException e) {
    }
    reg.setInitParameter("foo", "bar");
    assertFalse(reg.setInitParameter("foo", "foo"));
    Set<String> clash = reg.setInitParameters(Collections.singletonMap("foo", "bax"));
    assertTrue("should be one clash", clash != null && clash.size() == 1);
    try {
        reg.setInitParameters(Collections.singletonMap((String) null, "bax"));
        fail("null name in map accepted");
    } catch (IllegalArgumentException e) {
    }
    try {
        reg.setInitParameters(Collections.singletonMap("foo", (String) null));
        fail("null value in map accepted");
    } catch (IllegalArgumentException e) {
    }
    Set<String> clash2 = reg.setInitParameters(Collections.singletonMap("FOO", "bax"));
    assertTrue("should be no clash", clash2.isEmpty());
    assertEquals("setInitParameters should not replace existing non-clashing init parameters", 2, reg.getInitParameters().size());
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) Test(org.junit.Test)

Example 7 with ServletRegistration

use of javax.servlet.ServletRegistration in project jersey by jersey.

the class JerseyServletContainerInitializer method addServletWithDefaultConfiguration.

/**
     * Enhance default servlet (named {@link Application}) configuration.
     */
private static void addServletWithDefaultConfiguration(final ServletContext context, final Set<Class<?>> classes) throws ServletException {
    ServletRegistration registration = context.getServletRegistration(Application.class.getName());
    if (registration != null) {
        final Set<Class<?>> appClasses = getRootResourceAndProviderClasses(classes);
        final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(ResourceConfig.class, appClasses).addProperties(getInitParams(registration)).addProperties(Utils.getContextParams(context));
        if (registration.getClassName() != null) {
            // class name present - complete servlet registration from container point of view
            Utils.store(resourceConfig, context, registration.getName());
        } else {
            // no class name - no complete servlet registration from container point of view
            final ServletContainer servlet = new ServletContainer(resourceConfig);
            registration = context.addServlet(registration.getName(), servlet);
            ((ServletRegistration.Dynamic) registration).setLoadOnStartup(1);
            if (registration.getMappings().isEmpty()) {
                // Error
                LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING(registration.getName()));
            } else {
                LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_CLASSES(registration.getName(), appClasses));
            }
        }
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Application(javax.ws.rs.core.Application)

Example 8 with ServletRegistration

use of javax.servlet.ServletRegistration in project tomee by apache.

the class TomEEWebConfigProvider method getFacesServletMappings.

@Override
public List<ServletMapping> getFacesServletMappings(final ExternalContext externalContext) {
    final List<ServletMapping> facesServletMappings = super.getFacesServletMappings(externalContext);
    try {
        // getContext() is a runtime object where getServletRegistrations() is forbidden so unwrap
        final ServletContext sc = ServletContext.class.cast(Reflections.get(externalContext.getContext(), "sc"));
        if (sc != null && sc.getServletRegistrations() != null) {
            for (final Map.Entry<String, ? extends ServletRegistration> reg : sc.getServletRegistrations().entrySet()) {
                final ServletRegistration value = reg.getValue();
                if ("javax.faces.webapp.FacesServlet".equals(value.getClassName())) {
                    for (final String mapping : value.getMappings()) {
                        final Class<?> clazz = sc.getClassLoader().loadClass(value.getClassName());
                        final org.apache.myfaces.shared_impl.webapp.webxml.ServletMapping mappingImpl = new org.apache.myfaces.shared_impl.webapp.webxml.ServletMapping(value.getName(), clazz, mapping);
                        facesServletMappings.add(new ServletMappingImpl(mappingImpl));
                    }
                }
            }
        } else {
            facesServletMappings.addAll(super.getFacesServletMappings(externalContext));
        }
    } catch (final Exception e) {
        // don't fail cause our cast failed
        facesServletMappings.clear();
        facesServletMappings.addAll(super.getFacesServletMappings(externalContext));
    }
    return facesServletMappings;
}
Also used : ServletMapping(org.apache.myfaces.spi.ServletMapping) ServletMappingImpl(org.apache.myfaces.spi.impl.ServletMappingImpl) ServletRegistration(javax.servlet.ServletRegistration) ServletContext(javax.servlet.ServletContext) Map(java.util.Map)

Aggregations

ServletRegistration (javax.servlet.ServletRegistration)8 ServletContext (javax.servlet.ServletContext)2 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)2 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)2 Test (org.junit.Test)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Task (io.dropwizard.servlets.tasks.Task)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 ApplicationPath (javax.ws.rs.ApplicationPath)1 Application (javax.ws.rs.core.Application)1 ServletMapping (org.apache.myfaces.spi.ServletMapping)1 ServletMappingImpl (org.apache.myfaces.spi.impl.ServletMappingImpl)1 RestException (org.axe.exception.RestException)1 Server (org.eclipse.jetty.server.Server)1