org.springframework.web.servlet.FrameworkServlet Java Examples

The following examples show how to use org.springframework.web.servlet.FrameworkServlet. 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: SpringWebMvc_5_x_IT.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequest() throws Exception {
    MockServletConfig config = new MockServletConfig();
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    config.addInitParameter("contextConfigLocation", "classpath:spring-web-test.xml");
    req.setMethod("GET");
    req.setRequestURI("/");
    req.setRemoteAddr("1.2.3.4");
    
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.init(config);
    
    servlet.service(req, res);
    
    Method method = FrameworkServlet.class.getDeclaredMethod("doGet", HttpServletRequest.class, HttpServletResponse.class);
    
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    
    verifier.verifyTrace(Expectations.event(SPRING_MVC, method));
    verifier.verifyTraceCount(0);
}
 
Example #2
Source File: SpringWebMvc_3_x_to_4_x_IT.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequest() throws Exception {
    MockServletConfig config = new MockServletConfig();
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    config.addInitParameter("contextConfigLocation", "classpath:spring-web-test.xml");
    req.setMethod("GET");
    req.setRequestURI("/");
    req.setRemoteAddr("1.2.3.4");
    
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.init(config);
    
    servlet.service(req, res);
    
    Method method = FrameworkServlet.class.getDeclaredMethod("doGet", HttpServletRequest.class, HttpServletResponse.class);
    
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    
    verifier.verifyTrace(Expectations.event(SPRING_MVC, method));
    verifier.verifyTraceCount(0);
}
 
Example #3
Source File: AbstractDispatcherServletInitializer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a {@link DispatcherServlet} against the given servlet context.
 * <p>This method will create a {@code DispatcherServlet} with the name returned by
 * {@link #getServletName()}, initializing it with the application context returned
 * from {@link #createServletApplicationContext()}, and mapping it to the patterns
 * returned from {@link #getServletMappings()}.
 * <p>Further customization can be achieved by overriding {@link
 * #customizeRegistration(ServletRegistration.Dynamic)} or
 * {@link #createDispatcherServlet(WebApplicationContext)}.
 * @param servletContext the context to register the servlet against
 */
protected void registerDispatcherServlet(ServletContext servletContext) {
	String servletName = getServletName();
	Assert.hasLength(servletName, "getServletName() must not return null or empty");

	WebApplicationContext servletAppContext = createServletApplicationContext();
	Assert.notNull(servletAppContext, "createServletApplicationContext() must not return null");

	FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
	Assert.notNull(dispatcherServlet, "createDispatcherServlet(WebApplicationContext) must not return null");
	dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());

	ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
	if (registration == null) {
		throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
				"Check if there is another servlet registered under the same name.");
	}

	registration.setLoadOnStartup(1);
	registration.addMapping(getServletMappings());
	registration.setAsyncSupported(isAsyncSupported());

	Filter[] filters = getServletFilters();
	if (!ObjectUtils.isEmpty(filters)) {
		for (Filter filter : filters) {
			registerServletFilter(servletContext, filter);
		}
	}

	customizeRegistration(registration);
}
 
Example #4
Source File: AbstractDispatcherServletInitializer.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Register a {@link DispatcherServlet} against the given servlet context.
 * <p>This method will create a {@code DispatcherServlet} with the name returned by
 * {@link #getServletName()}, initializing it with the application context returned
 * from {@link #createServletApplicationContext()}, and mapping it to the patterns
 * returned from {@link #getServletMappings()}.
 * <p>Further customization can be achieved by overriding {@link
 * #customizeRegistration(ServletRegistration.Dynamic)} or
 * {@link #createDispatcherServlet(WebApplicationContext)}.
 * @param servletContext the context to register the servlet against
 */
protected void registerDispatcherServlet(ServletContext servletContext) {
	String servletName = getServletName();
	Assert.hasLength(servletName, "getServletName() must not return null or empty");

	WebApplicationContext servletAppContext = createServletApplicationContext();
	Assert.notNull(servletAppContext, "createServletApplicationContext() must not return null");

	FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
	Assert.notNull(dispatcherServlet, "createDispatcherServlet(WebApplicationContext) must not return null");
	dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());

	ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
	if (registration == null) {
		throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
				"Check if there is another servlet registered under the same name.");
	}

	registration.setLoadOnStartup(1);
	registration.addMapping(getServletMappings());
	registration.setAsyncSupported(isAsyncSupported());

	Filter[] filters = getServletFilters();
	if (!ObjectUtils.isEmpty(filters)) {
		for (Filter filter : filters) {
			registerServletFilter(servletContext, filter);
		}
	}

	customizeRegistration(registration);
}
 
Example #5
Source File: WebMvcUtils.java    From spring-webmvc-support with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Append initialized parameter for {@link ApplicationContextInitializer Context Initializer Class} into {@link
 * FrameworkServlet}
 *
 * @param servletContext          {@link ServletContext}
 * @param contextInitializerClass the class of {@link ApplicationContextInitializer}
 * @see FrameworkServlet#applyInitializers(ConfigurableApplicationContext)
 */
public static void appendFrameworkServletContextInitializerClassInitParameter(
        ServletContext servletContext,
        Class<? extends ApplicationContextInitializer> contextInitializerClass) {

    Collection<? extends ServletRegistration> servletRegistrations =
            WebUtils.findServletRegistrations(servletContext, FrameworkServlet.class).values();

    for (ServletRegistration servletRegistration : servletRegistrations) {
        String contextInitializerClassName = servletRegistration.getInitParameter(CONTEXT_INITIALIZER_CLASSES_PARAM);
        String newContextInitializerClassName = appendInitParameter(contextInitializerClassName, contextInitializerClass.getName());
        servletRegistration.setInitParameter(CONTEXT_INITIALIZER_CLASSES_PARAM, newContextInitializerClassName);
    }

}
 
Example #6
Source File: AbstractDispatcherServletInitializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Register a {@link DispatcherServlet} against the given servlet context.
 * <p>This method will create a {@code DispatcherServlet} with the name returned by
 * {@link #getServletName()}, initializing it with the application context returned
 * from {@link #createServletApplicationContext()}, and mapping it to the patterns
 * returned from {@link #getServletMappings()}.
 * <p>Further customization can be achieved by overriding {@link
 * #customizeRegistration(ServletRegistration.Dynamic)} or
 * {@link #createDispatcherServlet(WebApplicationContext)}.
 * @param servletContext the context to register the servlet against
 */
protected void registerDispatcherServlet(ServletContext servletContext) {
	String servletName = getServletName();
	Assert.hasLength(servletName, "getServletName() must not return empty or null");

	WebApplicationContext servletAppContext = createServletApplicationContext();
	Assert.notNull(servletAppContext,
			"createServletApplicationContext() did not return an application " +
			"context for servlet [" + servletName + "]");

	FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
	dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());

	ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
	Assert.notNull(registration,
			"Failed to register servlet with name '" + servletName + "'." +
			"Check if there is another servlet registered under the same name.");

	registration.setLoadOnStartup(1);
	registration.addMapping(getServletMappings());
	registration.setAsyncSupported(isAsyncSupported());

	Filter[] filters = getServletFilters();
	if (!ObjectUtils.isEmpty(filters)) {
		for (Filter filter : filters) {
			registerServletFilter(servletContext, filter);
		}
	}

	customizeRegistration(registration);
}
 
Example #7
Source File: AbstractDispatcherServletInitializer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Register a {@link DispatcherServlet} against the given servlet context.
 * <p>This method will create a {@code DispatcherServlet} with the name returned by
 * {@link #getServletName()}, initializing it with the application context returned
 * from {@link #createServletApplicationContext()}, and mapping it to the patterns
 * returned from {@link #getServletMappings()}.
 * <p>Further customization can be achieved by overriding {@link
 * #customizeRegistration(ServletRegistration.Dynamic)} or
 * {@link #createDispatcherServlet(WebApplicationContext)}.
 * @param servletContext the context to register the servlet against
 */
protected void registerDispatcherServlet(ServletContext servletContext) {
	String servletName = getServletName();
	Assert.hasLength(servletName, "getServletName() must not return empty or null");

	WebApplicationContext servletAppContext = createServletApplicationContext();
	Assert.notNull(servletAppContext,
			"createServletApplicationContext() did not return an application " +
			"context for servlet [" + servletName + "]");

	FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
	dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());

	ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
	Assert.notNull(registration,
			"Failed to register servlet with name '" + servletName + "'." +
			"Check if there is another servlet registered under the same name.");

	registration.setLoadOnStartup(1);
	registration.addMapping(getServletMappings());
	registration.setAsyncSupported(isAsyncSupported());

	Filter[] filters = getServletFilters();
	if (!ObjectUtils.isEmpty(filters)) {
		for (Filter filter : filters) {
			registerServletFilter(servletContext, filter);
		}
	}

	customizeRegistration(registration);
}
 
Example #8
Source File: AbstractDispatcherServletInitializer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a {@link DispatcherServlet} (or other kind of {@link FrameworkServlet}-derived
 * dispatcher) with the specified {@link WebApplicationContext}.
 * <p>Note: This allows for any {@link FrameworkServlet} subclass as of 4.2.3.
 * Previously, it insisted on returning a {@link DispatcherServlet} or subclass thereof.
 */
protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
	return new DispatcherServlet(servletAppContext);
}
 
Example #9
Source File: AbstractDispatcherServletInitializer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Create a {@link DispatcherServlet} (or other kind of {@link FrameworkServlet}-derived
 * dispatcher) with the specified {@link WebApplicationContext}.
 * <p>Note: This allows for any {@link FrameworkServlet} subclass as of 4.2.3.
 * Previously, it insisted on returning a {@link DispatcherServlet} or subclass thereof.
 */
protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
	return new DispatcherServlet(servletAppContext);
}
 
Example #10
Source File: AbstractDispatcherServletInitializer.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a {@link DispatcherServlet} (or other kind of {@link FrameworkServlet}-derived
 * dispatcher) with the specified {@link WebApplicationContext}.
 * <p>Note: This allows for any {@link FrameworkServlet} subclass as of 4.2.3.
 * Previously, it insisted on returning a {@link DispatcherServlet} or subclass thereof.
 */
protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
	return new DispatcherServlet(servletAppContext);
}
 
Example #11
Source File: AbstractDispatcherServletInitializer.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@link DispatcherServlet} (or other kind of {@link FrameworkServlet}-derived
 * dispatcher) with the specified {@link WebApplicationContext}.
 * <p>Note: This allows for any {@link FrameworkServlet} subclass as of 4.2.3.
 * Previously, it insisted on returning a {@link DispatcherServlet} or subclass thereof.
 */
protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
	return new DispatcherServlet(servletAppContext);
}