protected HandlerExecutionChain getHandlerExecution(HttpServletRequest request) throws Exception { WebApplicationContext appContext = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getServletContext()); HandlerMapping bean = appContext.getBean(RequestMappingHandlerMapping.class); HandlerExecutionChain handler = bean.getHandler(request); if (handler == null) { ServletContext servletContext = request.getServletContext(); Enumeration<?> attrNameEnum = servletContext.getAttributeNames(); while (attrNameEnum.hasMoreElements()) { String attrName = (String) attrNameEnum.nextElement(); if (attrName.startsWith(FrameworkServlet.SERVLET_CONTEXT_PREFIX)) { appContext = (WebApplicationContext) servletContext.getAttribute(attrName); bean = appContext.getBean(RequestMappingHandlerMapping.class); handler = bean.getHandler(request); if (handler != null) { break; } } } } return handler; }
@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); }
/** * Gets the {@link BeanFactory} from the given {@link JspContext}. The default * implementation automagically finds a {@link BeanFactory} that was previously set by * a {@link FrameworkServlet}. The result is cached. * * @param jspContext * {@link JspContext} to be used * @return {@link BeanFactory} found */ @SuppressWarnings("unchecked") protected BeanFactory getBeanFactory(JspContext jspContext) { Object bfCache = jspContext.getAttribute(TAGPROXY_BEANFACTORY_CACHE, PageContext.APPLICATION_SCOPE); if (bfCache != null && bfCache instanceof BeanFactory) { return (BeanFactory) bfCache; } Enumeration<String> en = jspContext.getAttributeNamesInScope(PageContext.APPLICATION_SCOPE); while (en.hasMoreElements()) { String attribute = en.nextElement(); if (attribute.startsWith(FrameworkServlet.SERVLET_CONTEXT_PREFIX)) { Object bf = jspContext.getAttribute(attribute, PageContext.APPLICATION_SCOPE); if (bf != null && bf instanceof BeanFactory) { BeanFactory bfBean = (BeanFactory) bf; jspContext.setAttribute(TAGPROXY_BEANFACTORY_CACHE, bfBean, PageContext.APPLICATION_SCOPE); return bfBean; } } } throw new IllegalStateException("Could not find a BeanFactory. Use a FrameworkServlet or @BeanFactoryReference."); }
/** * 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); }
public void init(ServletConfig config) throws ServletException { super.init(config); String servletName = config.getServletName(); urlPattern = config.getInitParameter("uri-pattern"); if (StringUtils.isEmpty(urlPattern)) { throw new ServletException("servlet=" + servletName + " init-param=uri-prefix cannot be null"); } urlPattern = urlPattern.trim(); springServletName = config.getInitParameter("spring-servlet-name"); /* * if(urlPattern.endsWith("*")){ urlPattern = urlPattern.substring(0,urlPattern.length()-1); } * if(urlPattern.endsWith("/")){ urlPattern = urlPattern.substring(0,urlPattern.length()-1); } */ servicePattern = Pattern.compile(urlPattern); ApplicationContext context = null; if(springServletName != null){ String name = FrameworkServlet.SERVLET_CONTEXT_PREFIX+springServletName; WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext(), name); if(wac != null){ context = wac; } }else{ context = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); } serviceManager = context.getBean(ServiceManager.class); try { venusExceptionFactory = context.getBean(VenusExceptionFactory.class); } catch (BeansException e) { } }
@Bean public FrameworkServlet dispatcherServlet() { return new FrameworkServlet() { @Override protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception { } }; }
@Before public void init() throws Exception { tester = new ServletTester(); XmlWebApplicationContext wac; tester.setAttribute(Servlet.springCtxAttrName, wac = (XmlWebApplicationContext) FrameworkServlet.DEFAULT_CONTEXT_CLASS.newInstance()); wac.setConfigLocation("classpath:/com/griddynamics/banshun/controllers-test/parent-context.xml"); wac.refresh(); tester.addServlet(Servlet.class, "*.html"); tester.start(); }
@Before public void init() throws Exception { tester = new ServletTester(); XmlWebApplicationContext wac; tester.setAttribute(Servlet.springCtxAttrName, wac = (XmlWebApplicationContext) FrameworkServlet.DEFAULT_CONTEXT_CLASS.newInstance()); wac.setConfigLocation("classpath:/com/griddynamics/banshun/scan-test/parent-context.xml"); wac.refresh(); tester.addServlet(Servlet.class, "*.html"); tester.start(); }
@Override protected FrameworkServlet createDispatcherServlet(final WebApplicationContext servletAppContext) { final DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); return dispatcherServlet; }
/** * 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); }