@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 initWebAppContext(WebApplicationContext cxt) { StubWebApplicationContext mockCxt = (StubWebApplicationContext) cxt; registerMvcSingletons(mockCxt); cxt.getServletContext() .setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, mockCxt); }
protected String parseValue( HttpServletRequest request, HttpServletResponse response, String control) { Map<String, Object> model = new HashMap<String, Object>(); model.put("user", SecurityUtils.getLoginUser()); model.putAll(SecurityUtils.getShiroUser().getAttributes()); WebApplicationContext context = RequestContextUtils.getWebApplicationContext(request); SimpleHash simpleHash = freeMarkerParse.buildTemplateModel(model, context.getServletContext(), request, response); return freeMarkerParse.renderString(control, simpleHash); }
/** * Binds a Mock implementation of a GrailsWebRequest object to the current thread. The mock * version uses instances of the Spring MockHttpServletRequest, MockHttpServletResponse and * MockServletContext classes. * * @param ctx The WebApplicationContext to use * @see org.springframework.mock.web.MockHttpServletRequest * @see org.springframework.mock.web.MockHttpServletResponse * @see org.springframework.mock.web.MockServletContext * @return The GrailsWebRequest instance */ public static GrailsWebRequest bindMockWebRequest(WebApplicationContext ctx) { MockHttpServletRequest request = new MockHttpServletRequest(); GrailsWebRequest webRequest = new GrailsWebRequest(request, new MockHttpServletResponse(), ctx.getServletContext()); request.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, webRequest); String[] paramListenerBeans = ctx.getBeanNamesForType(ParameterCreationListener.class); for (String paramListenerBean : paramListenerBeans) { ParameterCreationListener creationListenerBean = (ParameterCreationListener) ctx.getBean(paramListenerBean); webRequest.addParameterListener(creationListenerBean); } RequestContextHolder.setRequestAttributes(webRequest); return webRequest; }
public static String getCache() { WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext(); cache = servletContext.getRealPath("/").replace('\\', '/'); // cache = Option.class.getClassLoader().getResource("").getPath() + "/svg"; // cache = // "C:/Development/JAVA/Tomacat/apache-tomcat-8.0.28-windows-x64/apache-tomcat-8.0.28/me-webapps/gridCPProject"; // cache = "D:/tongyuan/works/configuration/syslink/Cache"; // cache = Option.class.getClassLoader().getResource("").getPath() + "svg"; // cache = // "C:/Development/JAVA/Tomacat/apache-tomcat-8.0.28-windows-x64/apache-tomcat-8.0.28/me-webapps/gridCPProject/static/svg"; // cache = "D:/tongyuan/works/configuration/syslink/Cache"; // System.out.println("getCache: " + cache); return cache; }
@SuppressWarnings("unchecked") @Override public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { // this is servlet container application context WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); if (wac == null) { String message = "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?"; logger.error(message); throw new IllegalStateException(message); } // obtain spring application context WebApplicationContext springWac = WebApplicationContextUtils.getWebApplicationContext( wac.getServletContext(), FrameworkServlet.SERVLET_CONTEXT_PREFIX + "appServlet"); String beanName = ClassUtils.getShortNameAsProperty(endpointClass); if (springWac.containsBean(beanName)) { T endpoint = springWac.getBean(beanName, endpointClass); if (logger.isTraceEnabled()) { logger.trace("Using @ServerEndpoint singleton " + endpoint); } return endpoint; } Component annot = AnnotationUtils.findAnnotation(endpointClass, Component.class); if ((annot != null) && springWac.containsBean(annot.value())) { T endpoint = springWac.getBean(annot.value(), endpointClass); if (logger.isTraceEnabled()) { logger.trace("Using @ServerEndpoint singleton " + endpoint); } return endpoint; } beanName = getBeanNameByType(springWac, endpointClass); if (beanName != null) { return (T) springWac.getBean(beanName); } if (logger.isTraceEnabled()) { logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass); } return springWac.getAutowireCapableBeanFactory().createBean(endpointClass); }
@RequestMapping(value = "download/{file_name}", method = RequestMethod.GET) public ModelAndView downloadFile(@PathVariable String $file_name, HttpServletResponse response) { String dir = context.getServletContext().getRealPath(DOWNLOAD_PATH); File file = new File(dir, $file_name); if (file == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } return new ModelAndView(new DownloadView(), "file", file); /* return ResponseEntity .ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"") .body(file); */ }
public File getRootFolder() { String realPath = defaultUploadFolder; if (uploadFolderRelativePath) { realPath = webApplicationContext.getServletContext().getRealPath(defaultUploadFolder); } File file = new File(realPath); // create if non existent if (!file.exists()) { file.mkdirs(); } // if existent but a file, runtime exception else { if (file.isFile()) { throw new RuntimeException( file.getAbsolutePath() + " is a file. The default root folder provider uses this path to store the files. Consider using a specific root folder provider or delete this file."); } } return file; }
public DefaultUrlMappingEvaluator(WebApplicationContext applicationContext) { this.servletContext = applicationContext.getServletContext(); this.applicationContext = applicationContext; }