Search in sources :

Example 1 with ServletRegistration

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

the class JerseyServletContainerInitializer method addServletWithExistingRegistration.

/**
     * Enhance existing servlet configuration.
     */
private static void addServletWithExistingRegistration(final ServletContext context, ServletRegistration registration, final Class<? extends Application> clazz, final Set<Class<?>> classes) throws ServletException {
    // create a new servlet container for a given app.
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, classes).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);
        final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet);
        dynamicRegistration.setAsyncSupported(true);
        dynamicRegistration.setLoadOnStartup(1);
        registration = dynamicRegistration;
    }
    if (registration.getMappings().isEmpty()) {
        final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
        if (ap != null) {
            final String mapping = createMappingPath(ap);
            if (!mappingExists(context, mapping)) {
                registration.addMapping(mapping);
                LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
            } else {
                LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
            }
        } else {
            // Error
            LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING_OR_ANNOTATION(clazz.getName(), ApplicationPath.class.getSimpleName()));
        }
    } else {
        LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_APPLICATION(clazz.getName()));
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ApplicationPath(javax.ws.rs.ApplicationPath)

Example 2 with ServletRegistration

use of javax.servlet.ServletRegistration in project spring-boot by spring-projects.

the class JerseyAutoConfiguration method setServletContext.

@Override
public void setServletContext(ServletContext servletContext) {
    String servletRegistrationName = getServletRegistrationName();
    ServletRegistration registration = servletContext.getServletRegistration(servletRegistrationName);
    if (registration != null) {
        if (logger.isInfoEnabled()) {
            logger.info("Configuring existing registration for Jersey servlet '" + servletRegistrationName + "'");
        }
        registration.setInitParameters(this.jersey.getInit());
        registration.setInitParameter(CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE, Boolean.TRUE.toString());
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration)

Example 3 with ServletRegistration

use of javax.servlet.ServletRegistration in project Axe by DongyuCai.

the class DispatcherServlet method init.

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    //获取 ServletContext 对象(用于注册servlet)
    ServletContext servletContext = servletConfig.getServletContext();
    //初始化框架相关 helper 类
    try {
        HelperLoader.init(servletContext);
        //注册处理JSP的Servlet
        String appJspPath = ConfigHelper.getAppJspPath();
        if (StringUtil.isNotEmpty(appJspPath)) {
            appJspPath = appJspPath.endsWith("/") ? appJspPath : appJspPath + "/";
            ServletRegistration jspServlet = servletContext.getServletRegistration("jsp");
            jspServlet.addMapping(appJspPath + "*");
        }
        //注册处理静态资源的默认Servlet
        String appAssetPath = ConfigHelper.getAppAssetPath();
        if (StringUtil.isNotEmpty(appAssetPath)) {
            appAssetPath = appAssetPath.endsWith("/") ? appAssetPath : appAssetPath + "/";
            ServletRegistration defaultServlet = servletContext.getServletRegistration("default");
            defaultServlet.addMapping(appAssetPath + "*");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) ServletContext(javax.servlet.ServletContext) ServletException(javax.servlet.ServletException) RestException(org.axe.exception.RestException) IOException(java.io.IOException)

Example 4 with ServletRegistration

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

the class DefaultReplacingServletContextListener method contextInitialized.

public void contextInitialized(ServletContextEvent sce) {
    ServletRegistration registration = sce.getServletContext().addServlet("ReplacementServlet", new ReplacementServlet());
    registration.addMapping("/");
}
Also used : ServletRegistration(javax.servlet.ServletRegistration)

Example 5 with ServletRegistration

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

the class AdminEnvironmentTest method addsATaskServlet.

@Test
public void addsATaskServlet() throws Exception {
    final Task task = new Task("thing") {

        @Override
        public void execute(ImmutableMultimap<String, String> parameters, PrintWriter output) throws Exception {
        }
    };
    env.addTask(task);
    handler.setServer(new Server());
    handler.start();
    final ServletRegistration registration = handler.getServletHandler().getServletContext().getServletRegistration("tasks");
    assertThat(registration.getMappings()).containsOnly("/tasks/*");
}
Also used : Task(io.dropwizard.servlets.tasks.Task) ServletRegistration(javax.servlet.ServletRegistration) Server(org.eclipse.jetty.server.Server) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

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