private static String getPathPrefix(Class<?> controllerType) { WebApplicationContext wac = getWebApplicationContext(); if (wac != null) { Map<String, RequestMappingHandlerMapping> map = wac.getBeansOfType(RequestMappingHandlerMapping.class); for (RequestMappingHandlerMapping mapping : map.values()) { if (mapping.isHandler(controllerType)) { String prefix = mapping.getPathPrefix(controllerType); if (prefix != null) { return prefix; } } } } return ""; }
WebApplicationContext wac = getWebApplicationContext(); Assert.notNull(wac, "No WebApplicationContext. "); Map<String, RequestMappingInfoHandlerMapping> map = wac.getBeansOfType(RequestMappingInfoHandlerMapping.class); List<HandlerMethod> handlerMethods = null; for (RequestMappingInfoHandlerMapping mapping : map.values()) {
private static String getPathPrefix(Class<?> controllerType) { WebApplicationContext wac = getWebApplicationContext(); if (wac != null) { Map<String, RequestMappingHandlerMapping> map = wac.getBeansOfType(RequestMappingHandlerMapping.class); for (RequestMappingHandlerMapping mapping : map.values()) { if (mapping.isHandler(controllerType)) { String prefix = mapping.getPathPrefix(controllerType); if (prefix != null) { return prefix; } } } } return ""; }
WebApplicationContext wac = getWebApplicationContext(); Assert.notNull(wac, "No WebApplicationContext. "); Map<String, RequestMappingInfoHandlerMapping> map = wac.getBeansOfType(RequestMappingInfoHandlerMapping.class); List<HandlerMethod> handlerMethods = null; for (RequestMappingInfoHandlerMapping mapping : map.values()) {
@BeforeEach void setUp() { Map<String, SystemDeletable> deleteMe = webApplicationContext.getBeansOfType(SystemDeletable.class); webApplicationContext.getBean(JdbcIdentityZoneProvisioning.class) .retrieveAll() .stream() .filter(zone -> !IdentityZone.getUaa().getId().equals(zone.getId())) .forEach(zone -> { deleteMe.values().stream().forEach(deletable -> deletable.deleteByIdentityZone(zone.getId())); }); }
@Test public void missingGroovyMarkupConfig() throws Exception { GroovyMarkupView view = new GroovyMarkupView(); given(this.webAppContext.getBeansOfType(GroovyMarkupConfig.class, true, false)) .willReturn(new HashMap<>()); view.setUrl("sampleView"); try { view.setApplicationContext(this.webAppContext); fail(); } catch (ApplicationContextException ex) { assertTrue(ex.getMessage().contains("GroovyMarkupConfig")); } }
@Test public void noFreeMarkerConfig() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(new HashMap<>()); given(wac.getServletContext()).willReturn(new MockServletContext()); fv.setUrl("anythingButNull"); exception.expect(ApplicationContextException.class); exception.expectMessage(containsString("FreeMarkerConfig")); fv.setApplicationContext(wac); }
@Test public void verifyRootWacSupport() { assertEquals("foo", foo); assertEquals("bar", bar); ApplicationContext parent = wac.getParent(); assertNotNull(parent); assertTrue(parent instanceof WebApplicationContext); WebApplicationContext root = (WebApplicationContext) parent; assertFalse(root.getBeansOfType(String.class).containsKey("bar")); ServletContext childServletContext = wac.getServletContext(); assertNotNull(childServletContext); ServletContext rootServletContext = root.getServletContext(); assertNotNull(rootServletContext); assertSame(childServletContext, rootServletContext); assertSame(root, rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); assertSame(root, childServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); }
@Test public void validTemplateName() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); MockServletContext sc = new MockServletContext(); Map<String, FreeMarkerConfig> configs = new HashMap<>(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configurer.setServletContext(sc); configs.put("configurer", configurer); given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs); given(wac.getServletContext()).willReturn(sc); fv.setUrl("templateName"); fv.setApplicationContext(wac); MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.US); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); HttpServletResponse response = new MockHttpServletResponse(); Map<String, Object> model = new HashMap<>(); model.put("myattr", "myvalue"); fv.render(model, request, response); assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType()); }
@Test public void keepExistingContentType() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); MockServletContext sc = new MockServletContext(); Map<String, FreeMarkerConfig> configs = new HashMap<>(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configurer.setServletContext(sc); configs.put("configurer", configurer); given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs); given(wac.getServletContext()).willReturn(sc); fv.setUrl("templateName"); fv.setApplicationContext(wac); MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.US); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); HttpServletResponse response = new MockHttpServletResponse(); response.setContentType("myContentType"); Map<String, Object> model = new HashMap<>(); model.put("myattr", "myvalue"); fv.render(model, request, response); assertEquals("myContentType", response.getContentType()); }
private Map<String, DispatcherServlet> determineDispatcherServlets( WebApplicationContext context) { Map<String, DispatcherServlet> dispatcherServlets = new LinkedHashMap<>(); context.getBeansOfType(ServletRegistrationBean.class).values() .forEach((registration) -> { Servlet servlet = registration.getServlet(); if (servlet instanceof DispatcherServlet && !dispatcherServlets.containsValue(servlet)) { dispatcherServlets.put(registration.getServletName(), (DispatcherServlet) servlet); } }); context.getBeansOfType(DispatcherServlet.class) .forEach((name, dispatcherServlet) -> { if (!dispatcherServlets.containsValue(dispatcherServlet)) { dispatcherServlets.put(name, dispatcherServlet); } }); return dispatcherServlets; }
private ServletConfigWrapper(ServletConfig delegate) { this.delegate = delegate; final WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(delegate.getServletContext()); customInitParameterProviders = new HashSet<CustomInitParameterProvider>( applicationContext.getBeansOfType(CustomInitParameterProvider.class).values()); LOGGER.info("Found {} custom init parameter provider(s)", customInitParameterProviders.size()); }
private ServletConfigWrapper(ServletConfig delegate) { this.delegate = delegate; final WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(delegate.getServletContext()); customInitParameterProviders = new HashSet<CustomInitParameterProvider>( applicationContext.getBeansOfType(CustomInitParameterProvider.class).values()); LOGGER.info("Found {} custom init parameter provider(s)", customInitParameterProviders.size()); }
private String rewrite(HttpServletRequest request, String content) { String out = content; if(contentRewriters == null){ final WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext); final Map<String, ContentRewriter> map = wac.getBeansOfType(ContentRewriter.class); contentRewriters = map.values(); } for(ContentRewriter rewriter : contentRewriters) { out = rewriter.rewriteContent(request, out); } return out; }
@Override public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) { logger.trace("Invoking VaadinRequestEndListeners"); for (VaadinRequestEndListener listener : applicationContext.getBeansOfType(VaadinRequestEndListener.class).values()) { try { listener.onRequestEnd(request, response, session); } catch (Exception ex) { logger.error("VaadinRequestEndListener threw an exception, ignoring", ex); } } logger.trace("Finished invoking VaadinRequestEndListener"); super.requestEnd(request, response, session); } }
@Override public void init(ServletConfig config) throws ServletException { super.init(config); WebApplicationContext applicationContext = getWebApplicationContext(config); Collection<ServletAction> handlers = applicationContext.getBeansOfType(ServletAction.class).values(); for (ServletAction handler : handlers) { String url = handler.url(); if (actionMap.containsKey(url)) { throw new RuntimeException("Handler [" + url + "] already exist."); } actionMap.put(url, handler); } }
@Override public void requestStart(VaadinRequest request, VaadinResponse response) { super.requestStart(request, response); logger.trace("Invoking VaadinRequestStartListeners"); for (VaadinRequestStartListener listener : applicationContext.getBeansOfType(VaadinRequestStartListener.class).values()) { try { listener.onRequestStart(request, response); } catch (Exception ex) { logger.error("VaadinRequestStartListener threw an exception, ignoring", ex); } } logger.trace("Finished invoking VaadinRequestStartListeners"); }
@Override public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) { logger.trace("Invoking VaadinRequestEndListeners"); for (VaadinRequestEndListener listener : applicationContext.getBeansOfType(VaadinRequestEndListener.class).values()) { try { listener.onRequestEnd(request, response, session); } catch (Exception ex) { logger.error("VaadinRequestEndListener threw an exception, ignoring", ex); } } logger.trace("Finished invoking VaadinRequestEndListener"); super.requestEnd(request, response, session); } }
@Override public void requestStart(VaadinRequest request, VaadinResponse response) { super.requestStart(request, response); logger.trace("Invoking VaadinRequestStartListeners"); for (VaadinRequestStartListener listener : applicationContext.getBeansOfType(VaadinRequestStartListener.class).values()) { try { listener.onRequestStart(request, response); } catch (Exception ex) { logger.error("VaadinRequestStartListener threw an exception, ignoring", ex); } } logger.trace("Finished invoking VaadinRequestStartListeners"); }
@Override public void setPageContext(PageContext pageContext) { super.setPageContext(pageContext); Map<String, OrganizationManager> organizationManagerMap = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).getBeansOfType(OrganizationManager.class); organizationManagers = organizationManagerMap.values(); }