javax.servlet.ServletRegistration.Dynamic Java Examples

The following examples show how to use javax.servlet.ServletRegistration.Dynamic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Add the filter.
 *
 * @param filterName the filter name.
 * @param filterClass the filter class.
 * @return the filter dynamic.
 * @see ServletContext#addFilter(java.lang.String, java.lang.Class)
 */
@Override
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) {
    if (status == SERVICING) {
        throw new IllegalStateException("Cannot call this after web application has started");
    }

    if (filterName == null || filterName.trim().equals("")) {
        throw new IllegalArgumentException("Filter name cannot be null or empty");
    }

    DefaultFilterEnvironment filterEnvironment;
    if (filters.containsKey(filterName)) {
        filterEnvironment = filters.get(filterName);
    } else {
        filterEnvironment = new DefaultFilterEnvironment();
        filterEnvironment.setFilterName(filterName);
        filterEnvironment.setWebApplication(this);
        filters.put(filterName, filterEnvironment);
    }
    filterEnvironment.setClassName(filterClass.getCanonicalName());

    return filterEnvironment;
}
 
Example #2
Source File: TestRestServletInjector.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultInjectListen(@Mocked ServletContext servletContext,
    @Mocked Dynamic dynamic) throws UnknownHostException, IOException {
  try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getByName("127.0.0.1"))) {
    int port = ss.getLocalPort();

    new Expectations(ServletConfig.class) {
      {
        ServletConfig.getServletUrlPattern();
        result = "/rest/*";
        ServletConfig.getLocalServerAddress();
        result = "127.0.0.1:" + port;
      }
    };

    Assert.assertEquals(dynamic, RestServletInjector.defaultInject(servletContext));
  }
}
 
Example #3
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new ToolListener());
    servletContext.addListener(new SakaiContextLoaderListener(rootContext));
    
    FilterRegistration requestFilterRegistration = servletContext.addFilter("sakai.request", RequestFilter.class);
    requestFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
    requestFilterRegistration.setInitParameter(RequestFilter.CONFIG_UPLOAD_ENABLED, "true");       

    Dynamic servlet = servletContext.addServlet("sakai-site-group-manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example #4
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new ToolListener());
    servletContext.addListener(new SakaiContextLoaderListener(rootContext));

    servletContext.addFilter("sakai.request", RequestFilter.class)
            .addMappingForUrlPatterns(
                    EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
                    true,
                    "/*");

    Dynamic servlet = servletContext.addServlet("sakai.message.bundle.manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example #5
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new ToolListener());
    servletContext.addListener(new SakaiContextLoaderListener(rootContext));
    
    FilterRegistration requestFilterRegistration = servletContext.addFilter("sakai.request", RequestFilter.class);
    requestFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
    requestFilterRegistration.setInitParameter(RequestFilter.CONFIG_UPLOAD_ENABLED, "true");       

    Dynamic servlet = servletContext.addServlet("sakai-site-group-manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example #6
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new SakaiContextLoaderListener(rootContext));

    servletContext.addFilter("sakai.request", RequestFilter.class)
            .addMappingForUrlPatterns(
                    EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
                    true,
                    "/*");

    Dynamic servlet = servletContext.addServlet("sakai.googledrive", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example #7
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new ToolListener());
    servletContext.addListener(new SakaiContextLoaderListener(rootContext));

    servletContext.addFilter("sakai.request", RequestFilter.class)
            .addMappingForUrlPatterns(
                    EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
                    true,
                    "/*");

    Dynamic servlet = servletContext.addServlet("sakai.message.bundle.manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example #8
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
	AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
	rootContext.setServletContext(servletContext);
	rootContext.register(ThymeleafConfig.class);

	servletContext.addListener(new ToolListener());
	servletContext.addListener(new SakaiContextLoaderListener(rootContext));

	servletContext.addFilter("sakai.request", RequestFilter.class)
		.addMappingForUrlPatterns(
			EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
			true,
			"/*");

	Dynamic servlet = servletContext.addServlet("sakai.datemanager", new DispatcherServlet(rootContext));
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example #9
Source File: RestServletInjector.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public Dynamic inject(ServletContext servletContext, String urlPattern) {
  String[] urlPatterns = splitUrlPattern(urlPattern);
  if (urlPatterns.length == 0) {
    LOGGER.warn("urlPattern is empty, ignore register {}.", SERVLET_NAME);
    return null;
  }

  String listenAddress = ServletConfig.getLocalServerAddress();
  if (!ServletUtils.canPublishEndpoint(listenAddress)) {
    LOGGER.warn("ignore register {}.", SERVLET_NAME);
    return null;
  }

  // dynamic deploy a servlet to handle serviceComb RESTful request
  Dynamic dynamic = servletContext.addServlet(SERVLET_NAME, RestServlet.class);
  dynamic.setAsyncSupported(true);
  dynamic.addMapping(urlPatterns);
  dynamic.setLoadOnStartup(0);
  LOGGER.info("RESTful servlet url pattern: {}.", Arrays.toString(urlPatterns));

  return dynamic;
}
 
Example #10
Source File: TestCseXmlWebApplicationContext.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testInjectServlet(@Mocked ConfigurableListableBeanFactory beanFactory) {
  Holder<Boolean> holder = new Holder<>();
  new MockUp<RestServletInjector>() {
    @Mock
    public Dynamic defaultInject(ServletContext servletContext) {
      holder.value = true;
      return null;
    }
  };

  context.invokeBeanFactoryPostProcessors(beanFactory);

  Assert.assertTrue(holder.value);
}
 
Example #11
Source File: ApplicationContextFacade.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName,
        Class<? extends Servlet> servletClass) {
    if (SecurityUtil.isPackageProtectionEnabled()) {
        return (ServletRegistration.Dynamic) doPrivileged("addServlet",
                new Class[]{String.class, Class.class},
                new Object[]{servletName, servletClass});
    } else {
        return context.addServlet(servletName, servletClass);
    }
}
 
Example #12
Source File: DWAdapter.java    From wiztowar with MIT License 5 votes vote down vote up
/**
 * This method is adapted from ServerFactory.createInternalServlet.
 */
private void createInternalServlet(final ExtendedEnvironment env, final ServletContext context) {
    if (context.getMajorVersion() >= 3) {

        // Add the Task servlet
        final ServletRegistration.Dynamic taskServlet = context.addServlet("TaskServlet", new TaskServlet(env.getTasks()));
        taskServlet.setAsyncSupported(true);
        taskServlet.addMapping("/tasks/*");

        // Add the Admin servlet
        final ServletRegistration.Dynamic adminServlet = context.addServlet("AdminServlet", new AdminServlet());
        adminServlet.setAsyncSupported(true);
        adminServlet.addMapping("/admin/*");
    } else throw new IllegalStateException("The WizToWar adapter doesn't support servlet versions under 3");
}
 
Example #13
Source File: WebInitializer.java    From springMvc4.x-project with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.register(MyMvcConfig.class);
	ctx.setServletContext(servletContext); // ②

	Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example #14
Source File: ApplicationContextFacade.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName,
        Servlet servlet) {
    if (SecurityUtil.isPackageProtectionEnabled()) {
        return (ServletRegistration.Dynamic) doPrivileged("addServlet",
                new Class[]{String.class, Servlet.class},
                new Object[]{servletName, servlet});
    } else {
        return context.addServlet(servletName, servlet);
    }
}
 
Example #15
Source File: ApplicationContextFacade.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public FilterRegistration.Dynamic addFilter(String filterName,
        Class<? extends Filter> filterClass) {
    if (SecurityUtil.isPackageProtectionEnabled()) {
        return (FilterRegistration.Dynamic) doPrivileged("addFilter",
                new Class[]{String.class, Class.class},
                new Object[]{filterName, filterClass});
    } else {
        return context.addFilter(filterName, filterClass);
    }
}
 
Example #16
Source File: WebInitializer.java    From springMvc4.x-project with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.register(MyMvcConfig.class);
	ctx.setServletContext(servletContext); // ②

	Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example #17
Source File: WebInitializer.java    From springMvc4.x-project with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.register(MyMvcConfig.class);
	ctx.setServletContext(servletContext); // ②

	Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // ③
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example #18
Source File: DefaultWebApplicationTest.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test getAsync.
 *
 * @throws Exception
 */
@Test
public void testGetAsync2() throws Exception {
    DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper();
    DefaultWebApplication webApp = new DefaultWebApplication();
    webApp.setWebApplicationRequestMapper(webAppRequestMapper);
    Dynamic registration = webApp.addServlet("Chat", TestChat2Servlet.class);
    registration.setAsyncSupported(true);
    webApp.addServletMapping("Chat", "/chat");
    webApp.initialize();
    webApp.start();
    TestWebApplicationRequest request = new TestWebApplicationRequest();
    request.setWebApplication(webApp);
    request.setServletPath("/chat");
    TestWebApplicationResponse response = new TestWebApplicationResponse();
    webApp.service(request, response);
    assertNotNull(response.getResponseBytes());
    request = new TestWebApplicationRequest();
    request.setWebApplication(webApp);
    request.setServletPath("/chat");
    request.setMethod("POST");
    request.setParameter("action", new String[]{"login"});
    request.setParameter("name", new String[]{"username"});
    response = new TestWebApplicationResponse();
    webApp.service(request, response);
    assertNotNull(response.getResponseBytes());
    request = new TestWebApplicationRequest();
    request.setWebApplication(webApp);
    request.setServletPath("/chat");
    request.setMethod("POST");
    request.setParameter("action", new String[]{"post"});
    request.setParameter("name", new String[]{"username"});
    request.setParameter("message", new String[]{new Date().toString()});
    response = new TestWebApplicationResponse();
    webApp.service(request, response);
    assertNotNull(response.getResponseBytes());
}
 
Example #19
Source File: DefaultWebApplicationTest.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test getAsync.
 *
 * @throws Exception
 */
@Test
public void testGetAsync() throws Exception {
    DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper();
    DefaultWebApplication webApp = new DefaultWebApplication();
    webApp.setWebApplicationRequestMapper(webAppRequestMapper);
    Dynamic registration = webApp.addServlet("Chat", TestChat1Servlet.class);
    registration.setAsyncSupported(true);
    webApp.addServletMapping("Chat", "/chat");
    webApp.initialize();
    webApp.start();
    TestWebApplicationRequest request = new TestWebApplicationRequest();
    request.setWebApplication(webApp);
    request.setServletPath("/chat");
    TestWebApplicationResponse response = new TestWebApplicationResponse();
    webApp.service(request, response);
    assertNotNull(response.getResponseBytes());
    request = new TestWebApplicationRequest();
    request.setWebApplication(webApp);
    request.setAsyncSupported(true);
    request.setServletPath("/chat");
    request.setMethod("POST");
    request.setParameter("action", new String[]{"login"});
    request.setParameter("name", new String[]{"username"});
    response = new TestWebApplicationResponse();
    webApp.service(request, response);
    assertNotNull(response.getResponseBytes());
    request = new TestWebApplicationRequest();
    request.setWebApplication(webApp);
    request.setServletPath("/chat");
    request.setMethod("POST");
    request.setParameter("action", new String[]{"post"});
    request.setParameter("name", new String[]{"username"});
    request.setParameter("message", new String[]{new Date().toString()});
    response = new TestWebApplicationResponse();
    webApp.service(request, response);
    assertNotNull(response.getResponseBytes());
}
 
Example #20
Source File: MCRUploadServletDeployer.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void startUp(ServletContext servletContext) {
    if (servletContext != null) {
        String servletName = "MCRUploadViaFormServlet";
        MultipartConfigElement multipartConfig = getMultipartConfig();
        try {
            checkTempStoragePath(multipartConfig.getLocation());
        } catch (IOException e) {
            throw new MCRConfigurationException("Could not setup " + servletName + "!", e);
        }
        Dynamic uploadServlet = servletContext.addServlet(servletName, MCRUploadViaFormServlet.class);
        uploadServlet.addMapping("/servlets/MCRUploadViaFormServlet");
        uploadServlet.setMultipartConfig(multipartConfig);
    }
}
 
Example #21
Source File: DefaultWebApplicationTest.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test addMapping method (verify the # of mappings > 0).
 */
@Test
public void testAddMapping() {
    DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper();
    DefaultWebApplication webApp = new DefaultWebApplication();
    webApp.setWebApplicationRequestMapper(webAppRequestMapper);
    ServletRegistration.Dynamic dynamic
            = webApp.addServlet("echo", "servlet.EchoServlet");
    assertNotNull(dynamic);
    dynamic.addMapping("/echo");
    assertTrue(dynamic.getMappings().size() > 0);
}
 
Example #22
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add the filter.
 *
 * @param filterName the filter name.
 * @param filter the filter.
 * @return the filter dynamic registration.
 */
@Override
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) {
    if (status == SERVICING) {
        throw new IllegalStateException("Cannot call this after web application has started");
    }

    DefaultFilterEnvironment filterEnvironment = new DefaultFilterEnvironment(this, filterName, filter);
    filters.put(filterName, filterEnvironment);

    return filterEnvironment;

}
 
Example #23
Source File: ServletContextSimulator.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public javax.servlet.FilterRegistration.Dynamic addFilter(
		String filterName, Class<? extends Filter> filterClass )
{
	// TODO Auto-generated method stub
	return null;
}
 
Example #24
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add the servlet.
 *
 * @param servletName the servlet name.
 * @param className the class name.
 * @return the servlet dynamic.
 */
@Override
public Dynamic addServlet(String servletName, String className) {
    DefaultServletEnvironment result = servlets.get("servletName");
    if (result == null) {
        result = new DefaultServletEnvironment(this, servletName);
        result.setClassName(className);
        servlets.put(servletName, result);
    } else {
        result.setClassName(className);
    }

    return result;
}
 
Example #25
Source File: TesterServletContext.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public Dynamic addServlet(String servletName,
        Class<? extends Servlet> servletClass) {
    throw new RuntimeException("Not implemented");
}
 
Example #26
Source File: ParserServletContext.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Dynamic addServlet(String string, String string1) {
    System.err.println("** addServlet(string,string)");
    return null;
}
 
Example #27
Source File: ParserServletContext.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Dynamic addServlet(String string, Servlet srvlt) {
    System.err.println("** addServlet(string,srvlt)");
    return null;
}
 
Example #28
Source File: ParserServletContext.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public FilterRegistration.Dynamic addFilter(String string, Class<? extends Filter> type) {
    return null;
}
 
Example #29
Source File: RestServletInjector.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public static Dynamic defaultInject(ServletContext servletContext) {
  RestServletInjector injector = new RestServletInjector();

  String urlPattern = ServletConfig.getServletUrlPattern();
  return injector.inject(servletContext, urlPattern);
}
 
Example #30
Source File: MockServletContext.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName,
        String className) {
    return null;
}