/** {@inheritDoc} */ @Override public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { log.debug("Servicing Request"); if (codecFactory == null) { ServletContext ctx = getServletContext(); log.debug("Context path: {}", ctx.getContextPath()); //attempt to lookup the webapp context webAppCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(ctx); //now try to look it up as an attribute if (webAppCtx == null) { log.debug("Webapp context was null, trying lookup as attr."); webAppCtx = (WebApplicationContext) ctx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); } //lookup the server and codec factory if (webAppCtx != null) { server = (IServer) webAppCtx.getBean("red5.server"); codecFactory = (RemotingCodecFactory) webAppCtx.getBean("remotingCodecFactory"); } else { log.debug("No web context"); } } log.debug("Remoting request {} {}", req.getContextPath(), req.getServletPath()); if (APPLICATION_AMF.equals(req.getContentType())) { serviceAMF(req, resp); } else { resp.getWriter().write("Red5 : Remoting Gateway"); } }
WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, servletContext); this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext()); WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext()); if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) { this.requestDataValueProcessor = this.webApplicationContext.getBean( RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
if (wac.containsBean(beanName)) { T endpoint = wac.getBean(beanName, endpointClass); if (logger.isTraceEnabled()) { logger.trace("Using @ServerEndpoint singleton " + endpoint); if (ann != null && wac.containsBean(ann.value())) { T endpoint = wac.getBean(ann.value(), endpointClass); if (logger.isTraceEnabled()) { logger.trace("Using @ServerEndpoint singleton " + endpoint); return (T) wac.getBean(beanName); logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass); return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
@Override @Nullable public Object getAttribute(String name) { if ((this.explicitAttributes == null || !this.explicitAttributes.contains(name)) && (this.exposedContextBeanNames == null || this.exposedContextBeanNames.contains(name)) && this.webApplicationContext.containsBean(name)) { return this.webApplicationContext.getBean(name); } else { return super.getAttribute(name); } }
@Override @Nullable public Class<?> getType(ELContext elContext, @Nullable Object base, Object property) throws ELException { if (base == null) { String beanName = property.toString(); WebApplicationContext wac = getWebApplicationContext(elContext); if (wac.containsBean(beanName)) { elContext.setPropertyResolved(true); return wac.getType(beanName); } } return null; }
@Override protected WebApplicationContext initWebAppContext() { ServletContext servletContext = this.webAppContext.getServletContext(); Assert.state(servletContext != null, "No ServletContext"); ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext); if (rootWac == null) { rootWac = this.webAppContext; ApplicationContext parent = this.webAppContext.getParent(); while (parent != null) { if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { rootWac = parent; break; } parent = parent.getParent(); } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); } return this.webAppContext; }
if (applicationContext == null) { ServletContext ctx = getServletContext(); applicationContext = WebApplicationContextUtils.getWebApplicationContext(ctx); if (applicationContext == null) { applicationContext = (WebApplicationContext) ctx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); manager = (RTMPConnManager) applicationContext.getBean("rtmpConnManager"); if (manager == null) { log.warn("Connection manager was null in context, getting class instance"); log.debug("Request - method: {} content type: {} path: {}", new Object[] { req.getMethod(), req.getContentType(), req.getServletPath() }); if (!REQUEST_METHOD.equals(req.getMethod()) || req.getContentLength() == 0) {
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = request.getPathInfo(); Long emailId = null; LOG.debug("service() => Recording Open for Email[" + emailId + "]"); WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); EmailTrackingManager emailTrackingManager = (EmailTrackingManager) context.getBean("blEmailTrackingManager"); String userAgent = request.getHeader("USER-AGENT"); Map<String, String> extraValues = new HashMap<String, String>(); extraValues.put("userAgent", userAgent);
@Test public void testContextLoaderListenerWithLocalContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString( new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()})); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { if (context instanceof WebApplicationContext) { WebApplicationContext wac = (WebApplicationContext) context; ServletContext sc = wac.getServletContext(); if (sc != null) { sc.setAttribute("javax.websocket.server.ServerContainer", new MockServerContainer()); } } }
/** * Verify that the breaking change introduced in <a * href="https://jira.spring.io/browse/SPR-12553">SPR-12553</a> has been reverted. * * <p>This code has been copied from * {@link org.springframework.test.context.hierarchies.web.ControllerIntegrationTests}. * * @see org.springframework.test.context.hierarchies.web.ControllerIntegrationTests#verifyRootWacSupport() */ private void verifyRootWacSupport() { assertNotNull(personDao); assertNotNull(personController); ApplicationContext parent = wac.getParent(); assertNotNull(parent); assertTrue(parent instanceof WebApplicationContext); WebApplicationContext root = (WebApplicationContext) parent; 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 testApplicationScope() { WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION); assertNull(ac.getServletContext().getAttribute(NAME)); DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class); assertSame(bean, ac.getServletContext().getAttribute(NAME)); assertSame(bean, ac.getBean(NAME)); new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext())); assertTrue(bean.wasDestroyed()); }
@Override protected void initFrameworkServlet() throws ServletException, BeansException { context = getServletContext(); context.log("GSP servlet initialized"); context.setAttribute(SERVLET_INSTANCE, this); final WebApplicationContext webApplicationContext = getWebApplicationContext(); grailsAttributes = GrailsFactoriesLoader.loadFactoriesWithArguments(GrailsApplicationAttributes.class, getClass().getClassLoader(), new Object[]{context}).get(0); webApplicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); groovyPagesTemplateEngine = webApplicationContext.getBean(GroovyPagesTemplateEngine.BEAN_ID, GroovyPagesTemplateEngine.class); }
@Test public void pathVarsOverrideStaticAttributes() throws Exception { WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getServletContext()).willReturn(new MockServletContext()); HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); TestView tv = new TestView(wac); tv.setApplicationContext(wac); Properties p = new Properties(); p.setProperty("one", "bar"); p.setProperty("something", "else"); tv.setAttributes(p); Map<String, Object> pathVars = new HashMap<>(); pathVars.put("one", new HashMap<>()); pathVars.put("two", new Object()); request.setAttribute(View.PATH_VARIABLES, pathVars); tv.render(new HashMap<>(), request, response); checkContainsAll(pathVars, tv.model); assertEquals(3, tv.model.size()); assertEquals("else", tv.model.get("something")); assertTrue(tv.initialized); }
@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)); }
/** * Filter to run for the LegacySecurityRealm is the * ChainServletFilter legacy from /WEB-INF/security/SecurityFilters.groovy. */ @Override public Filter createFilter(FilterConfig filterConfig) { Binding binding = new Binding(); SecurityComponents sc = this.createSecurityComponents(); binding.setVariable("securityComponents", sc); binding.setVariable("securityRealm",this); BeanBuilder builder = new BeanBuilder(); builder.parse(filterConfig.getServletContext().getResourceAsStream("/WEB-INF/security/SecurityFilters.groovy"),binding); WebApplicationContext context = builder.createApplicationContext(); return (Filter) context.getBean("legacy"); }
@Override public int doStartTag() throws JspException { WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()); context.getAutowireCapableBeanFactory().autowireBean(this); return SKIP_BODY; }
@SuppressWarnings("unchecked") private Supplier<C> createContext(HttpServletRequest request) { WebApplicationContext context = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getServletContext()); if (this.contextClass.isInstance(context)) { return () -> (C) context; } return () -> context.getBean(this.contextClass); }
private static CompositeUriComponentsContributor getUriComponentsContributor() { WebApplicationContext wac = getWebApplicationContext(); if (wac != null) { try { return wac.getBean(MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME, CompositeUriComponentsContributor.class); } catch (NoSuchBeanDefinitionException ex) { // Ignore } } return defaultUriComponentsContributor; }
@Test public void testContextLoaderListenerWithDefaultContext() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); listener.contextInitialized(event); String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr); assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext); assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext); LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle"); assertTrue("Has father", context.containsBean("father")); assertTrue("Has rod", context.containsBean("rod")); assertTrue("Has kerry", context.containsBean("kerry")); assertTrue("Not destroyed", !lb.isDestroyed()); assertFalse(context.containsBean("beans1.bean1")); assertFalse(context.containsBean("beans1.bean2")); listener.contextDestroyed(event); assertTrue("Destroyed", lb.isDestroyed()); assertNull(sc.getAttribute(contextAttr)); assertNull(WebApplicationContextUtils.getWebApplicationContext(sc)); }