@Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter("UTF-8")); DispatcherServlet dispatcherServlet = new DispatcherServlet(context); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("Dispatcher", dispatcherServlet); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); CXFServlet cxf = new CXFServlet(); BusFactory.setDefaultBus(cxf.getBus()); ServletRegistration.Dynamic cxfServlet = servletContext.addServlet("CXFServlet", cxf); cxfServlet.setLoadOnStartup(1); cxfServlet.addMapping("/services/*"); servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")).addMappingForUrlPatterns(null, false, "/*"); servletContext.getSessionCookieConfig().setSecure(cookieSecure); }
public void init(ServletConfig config) throws ServletException { try { WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()); final AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory(); beanFactory.autowireBean(this); } catch (Exception e) { logger.error("Error initializing ShibbolethExtAuthnHandler", e); } }
/** * Process {@code @Autowired} injection for the given target object, * based on the current web application context. * <p>Intended for use as a delegate. * @param target the target object to process * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext() */ public static void processInjectionBasedOnCurrentContext(Object target) { Assert.notNull(target, "Target object must not be null"); WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext(); if (cc != null) { AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(cc.getAutowireCapableBeanFactory()); bpp.processInjection(target); } else { if (logger.isDebugEnabled()) { logger.debug("Current WebApplicationContext is not available for processing of " + ClassUtils.getShortName(target.getClass()) + ": " + "Make sure this class gets constructed in a Spring web application. Proceeding without injection."); } } }
/** * See {@link IEventNotificationService#trigger(String, String, Long, String, String) */ private void trigger(Event event, String subject, String message) { final String subjectToSend = subject == null ? event.getSubject() : subject; final String messageToSend = message == null ? event.getMessage() : message; // create a new thread to send the messages as it can take some time new Thread(() -> { try { HibernateSessionManager.openSession(); // use proxy bean instead of concrete implementation of service // otherwise there is no transaction for the new session WebApplicationContext ctx = WebApplicationContextUtils .getWebApplicationContext(SessionManager.getServletContext()); IEventNotificationService eventNotificationService = (IEventNotificationService) ctx .getBean("eventNotificationService"); eventNotificationService.triggerInternal(event, subjectToSend, messageToSend); } finally { HibernateSessionManager.closeSession(); } }).start(); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException { // Lazily initialize the delegate if necessary. Filter delegateToUse = this.delegate; if (delegateToUse == null) { synchronized (this.delegateMonitor) { if (this.delegate == null) { WebApplicationContext wac = findWebApplicationContext(); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?"); } this.delegate = initDelegate(wac); } delegateToUse = this.delegate; } } // Let the delegate perform the actual doFilter operation. invokeDelegate(delegateToUse, request, response, filterChain); }
@Override protected void before() throws Throwable { final SpringApplicationBuilder builder = new SpringApplicationBuilder(LocalTestSkipperServer.class); if (this.configLocations != null && this.configLocations.length > 0) { builder.properties( String.format("spring.config.location:%s", StringUtils.arrayToCommaDelimitedString(this.configLocations)) ); } if (this.configNames != null && this.configNames.length > 0) { builder.properties( String.format("spring.config.name:%s", StringUtils.arrayToCommaDelimitedString(this.configNames)) ); } this.app = builder.build(); configurableApplicationContext = app.run( new String[] { "--server.port=0" }); Collection<Filter> filters = configurableApplicationContext.getBeansOfType(Filter.class).values(); mockMvc = MockMvcBuilders.webAppContextSetup((WebApplicationContext) configurableApplicationContext) .addFilters(filters.toArray(new Filter[filters.size()])).build(); skipperPort = configurableApplicationContext.getEnvironment().resolvePlaceholders("${server.port}"); }
private IForumService getForumManager() { if (forumService == null) { WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); forumService = (IForumService) wac.getBean(ForumConstants.FORUM_SERVICE); } return forumService; }
private ILessonService getLessonService() { if (OrganisationGroupAction.lessonService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); OrganisationGroupAction.lessonService = (ILessonService) ctx.getBean("lessonService"); } return OrganisationGroupAction.lessonService; }
@Override public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession ss = SessionManager.getSession(); UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER); long toolSessionId = WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_SESSION_ID); WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); ILearnerService learnerService = (ILearnerService) wac.getBean("learnerService"); String finishURL = learnerService.completeToolSession(toolSessionId, user.getUserID().longValue()); return new RedirectingActionForward(finishURL); }
private IUserManagementService getUserManagementService() { if (HomeAction.userManagementService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); HomeAction.userManagementService = (IUserManagementService) ctx.getBean("userManagementService"); } return HomeAction.userManagementService; }
private ILearningDesignService getLearningDesignService() { if (HomeAction.learningDesignService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); HomeAction.learningDesignService = (ILearningDesignService) ctx.getBean("learningDesignService"); } return HomeAction.learningDesignService; }
private IUserManagementService getUserManagementService() { if (AuthoringAction.userManagementService == null) { WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); AuthoringAction.userManagementService = (IUserManagementService) wac .getBean(CentralConstants.USER_MANAGEMENT_SERVICE_BEAN_NAME); } return AuthoringAction.userManagementService; }
/** * Sets up and runs server. * @param args */ public static void main(String[] args) { final Server server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(8080); server.addConnector(connector); Context htmlContext = new Context(server, "/", Context.SESSIONS); ResourceHandler htmlHandler = new ResourceHandler(); htmlHandler.setResourceBase("web"); htmlContext.setHandler(htmlHandler); Context servletContext = new Context(server, "/", Context.SESSIONS); GenericWebApplicationContext springContext = new GenericWebApplicationContext(); springContext.setParent(new ClassPathXmlApplicationContext("org/getahead/dwrdemo/cli/spring.xml")); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, springContext); ServletHolder holder = new ServletHolder(new DwrSpringServlet()); holder.setInitParameter("pollAndCometEnabled", "true"); holder.setInitParameter("debug", "true"); servletContext.addServlet(holder, "/dwr/*"); try { JettyShutdown.addShutdownHook(server); server.start(); server.join(); } catch (Exception ex) { ex.printStackTrace(); } }
/** * Finds activity position within Learning Design and stores it as request attribute. */ public static ActivityPositionDTO putActivityPositionInRequestByToolSessionId(Long toolSessionId, HttpServletRequest request, ServletContext context) { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context); ILearnerService learnerService = (ILearnerService) wac.getBean("learnerService"); if (learnerService == null) { LearningWebUtil.log.warn("Can not set activity position, no Learner service in servlet context."); return null; } ActivityPositionDTO positionDTO = learnerService.getActivityPositionByToolSessionId(toolSessionId); if (positionDTO != null) { request.setAttribute(AttributeNames.ATTR_ACTIVITY_POSITION, positionDTO); } return positionDTO; }
/** * Look up the SessionFactory that this filter should use. * <p>The default implementation looks for a bean with the specified name * in Spring's root application context. * @return the SessionFactory to use * @see #getSessionFactoryBeanName */ protected SessionFactory lookupSessionFactory() { if (logger.isDebugEnabled()) { logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter"); } WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class); }
public TestSpringVaadinServletService(VaadinServlet servlet, WebApplicationContext applicationContext) throws ServiceException { super(servlet, new DefaultDeploymentConfiguration(TestSpringVaadinServletService.class, new Properties()), ""); this.appContext = applicationContext; init(); }
private void startJetty(int port) throws Exception { Server server = new Server(port); WebApplicationContext webApplicationContext = createWebApplicationContext(); ServletContextHandler handler = createHandlerWith(webApplicationContext); server.setHandler(handler); server.start(); server.join(); }
private ServletContextHandler createHandlerWith(WebApplicationContext webApplicationContext) { ServletContextHandler servletContextHandler = new ServletContextHandler(); servletContextHandler.setContextPath(CONTEXT_PATH_RESPOND_TO_ALL_REQUESTS); ServletHolder dispatcherServletHolder = new ServletHolder(new DispatcherServlet(webApplicationContext)); servletContextHandler.addServlet(dispatcherServletHolder, CONTEXT_PATH_RESPOND_TO_ALL_REQUESTS); return servletContextHandler; }
public void afterPropertiesSet() throws Exception { String mode = applicationContext instanceof WebApplicationContext ? "Java Servlet Mode" : "Java Application Mode"; logger.info(String.format("application initializing start in %s", mode)); logger.info("application initializing begin ..."); if (!CollectionUtils.isEmpty(applicationInitializers)) { for (ApplicationInitializer applicationInitializer : applicationInitializers) { applicationInitializer.initialize(applicationContext); } } logger.info("application initializing end ..."); }
private IUserManagementService getUserManagementService() { if (FindUserLessonsAction.userManagementService == null) { WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); FindUserLessonsAction.userManagementService = (IUserManagementService) wac .getBean(CentralConstants.USER_MANAGEMENT_SERVICE_BEAN_NAME); } return FindUserLessonsAction.userManagementService; }
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); IUserManagementService service = (IUserManagementService) ctx.getBean("userManagementService"); MessageService messageService = (MessageService) ctx.getBean("centralMessageService"); String login = WebUtil.readStrParam(request, "login", false); if (service.isUserSysAdmin()) { if ((login != null) && (login.trim().length() > 0)) { User user = service.getUserByLogin(login); if (user != null) { // audit log when loginas UserDTO sysadmin = (UserDTO) SessionManager.getSession().getAttribute(AttributeNames.USER); IAuditService auditService = (IAuditService) ctx.getBean("auditService"); String[] args = new String[] { sysadmin.getLogin() + "(" + sysadmin.getUserID() + ")", login }; String message = messageService.getMessage("audit.admin.loginas", args); auditService.log(CentralConstants.MODULE_NAME, message); // login.jsp knows what to do with these request.setAttribute("login", login); String token = "#LAMS" + RandomPasswordGenerator.nextPassword(10); request.setAttribute("password", token); // notify the login module that the user has been authenticated correctly UniversalLoginModule.setAuthenticationToken(token); // redirect to login page return (new ActionForward("/login.jsp?redirectURL=/lams/index.jsp")); } } } else { request.setAttribute("errorName", "LoginAsAction"); request.setAttribute("errorMessage", messageService.getMessage("error.authorisation")); return mapping.findForward("error"); } return mapping.findForward("usersearch"); }
public MockMvcContext createWACWithCustomGETRequestBuilder(final WebApplicationContext wac, final String subdomain, final String resourceURI) throws URISyntaxException { MockMvcContext result = new MockMvcContext(); result.setBuilder(MockMvcRequestBuilders.get(new URI("http://" + subdomain + ".localhost/" + resourceURI)) .accept(MediaType.APPLICATION_JSON)); result.setMockMvc(MockMvcBuilders.webAppContextSetup(wac).defaultRequest(result.getBuilder()).build()); return result; }
@Override public void init(FilterConfig fConfig) throws ServletException { ServletContext sc = fConfig.getServletContext(); WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc); if (cxt != null) { cookieUtil = cxt.getBean(CookieUtil.class); sessionUtil = cxt.getBean(SessionUtil.class); } System.out.println("init finished"); }
private ILessonService getLessonService() { if (lessonService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); lessonService = (ILessonService) ctx.getBean("lessonService"); } return lessonService; }
private MessageService getMessageService() { if (EmailUserAction.messageService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); EmailUserAction.messageService = (MessageService) ctx .getBean(CentralConstants.CENTRAL_MESSAGE_SERVICE_BEAN_NAME); } return EmailUserAction.messageService; }
@Override public void init() throws ServletException { WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); RepopulateProgressMarksServlet.auditService = (IAuditService) ctx.getBean("auditService"); RepopulateProgressMarksServlet.lessonService = (ILessonService) ctx.getBean("lessonService"); RepopulateProgressMarksServlet.learnerService = (ICoreLearnerService) ctx.getBean("learnerService"); }
private IUserManagementService getUserManagementService() { if (userManagementService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); userManagementService = (IUserManagementService) ctx .getBean(CentralConstants.USER_MANAGEMENT_SERVICE_BEAN_NAME); } return userManagementService; }
private static IUserManagementService getService() { if (LanguageUtil.service == null) { WebApplicationContext ctx = WebApplicationContextUtils .getWebApplicationContext(SessionManager.getServletContext()); LanguageUtil.service = (IUserManagementService) ctx.getBean("userManagementService"); } return LanguageUtil.service; }
@Override public void init() throws ServletException { WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); auditService = (IAuditService) ctx.getBean("auditService"); lessonService = (ILessonService) ctx.getBean("lessonService"); gradebookService = (IGradebookService) ctx.getBean("gradebookService"); }
private IUserManagementService getUserService() { if (GradebookLearningAction.userService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); GradebookLearningAction.userService = (IUserManagementService) ctx.getBean("userManagementService"); } return GradebookLearningAction.userService; }
private ISecurityService getSecurityService() { if (GradebookLearningAction.securityService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); GradebookLearningAction.securityService = (ISecurityService) ctx.getBean("securityService"); } return GradebookLearningAction.securityService; }
private ILessonService getLessonService() { if (HomeAction.lessonService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); HomeAction.lessonService = (ILessonService) ctx.getBean("lessonService"); } return HomeAction.lessonService; }
private IMonitoringService getMonitoringService() { if (monitoringService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); monitoringService = (IMonitoringService) ctx.getBean(CentralConstants.MONITORING_SERVICE_BEAN_NAME); } return monitoringService; }
@Override public void init(FilterConfig fConfig) throws ServletException { ServletContext sc = fConfig.getServletContext(); WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc); if (cxt != null) { } System.out.println("init finished"); }
private ILessonService getLessonService() { if (GradebookAction.lessonService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); GradebookAction.lessonService = (ILessonService) ctx.getBean("lessonService"); } return GradebookAction.lessonService; }
protected IToolDAO getToolDAO() { if (RestServlet.toolDAO == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext()); RestServlet.toolDAO = (IToolDAO) ctx.getBean("toolDAO"); } return RestServlet.toolDAO; }
private IUserManagementService getUserManagementService() { if (OrganisationGroupAction.userManagementService == null) { WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(getServlet().getServletContext()); OrganisationGroupAction.userManagementService = (IUserManagementService) ctx .getBean("userManagementService"); } return OrganisationGroupAction.userManagementService; }
/** * Create a new ContextExposingHttpServletRequest for the given request. * @param originalRequest the original HttpServletRequest * @param context the WebApplicationContext that this request runs in * @param exposedContextBeanNames the names of beans in the context which * are supposed to be exposed (if this is non-null, only the beans in this * Set are eligible for exposure as attributes) */ public ContextExposingHttpServletRequest( HttpServletRequest originalRequest, WebApplicationContext context, Set<String> exposedContextBeanNames) { super(originalRequest); Assert.notNull(context, "WebApplicationContext must not be null"); this.webApplicationContext = context; this.exposedContextBeanNames = exposedContextBeanNames; }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (ApplicationProperties.FALSE_WHILE_RUNNING_DB_TESTS) { if (applicationContext instanceof WebApplicationContext) ((WebApplicationContext) applicationContext).getServletContext().addListener(this); else throw new RuntimeException("Must be inside a web application context"); } }
@Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext applicationContext = (AnnotationConfigWebApplicationContext) super.createServletApplicationContext(); if (applicationContext != null) applicationContext.setDisplayName("DispatcherServlet WebMvcApplicationConfig"); return applicationContext; }