Example usage for org.springframework.web.context.support WebApplicationContextUtils getRequiredWebApplicationContext

List of usage examples for org.springframework.web.context.support WebApplicationContextUtils getRequiredWebApplicationContext

Introduction

In this page you can find the example usage for org.springframework.web.context.support WebApplicationContextUtils getRequiredWebApplicationContext.

Prototype

public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)
        throws IllegalStateException 

Source Link

Document

Find the root WebApplicationContext for this web app, typically loaded via org.springframework.web.context.ContextLoaderListener .

Usage

From source file:alpha.portal.webapp.listener.StartupListener.java

/**
 * {@inheritDoc}/*ww w .  j av  a2  s . c  o m*/
 */
@SuppressWarnings("unchecked")
public void contextInitialized(final ServletContextEvent event) {
    StartupListener.log.debug("Initializing context...");

    final ServletContext context = event.getServletContext();

    // Orion starts Servlets before Listeners, so check if the config
    // object already exists
    Map<String, Object> config = (HashMap<String, Object>) context.getAttribute(Constants.CONFIG);

    if (config == null) {
        config = new HashMap<String, Object>();
    }

    if (context.getInitParameter(Constants.CSS_THEME) != null) {
        config.put(Constants.CSS_THEME, context.getInitParameter(Constants.CSS_THEME));
    }

    final ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    /*
     * String[] beans = ctx.getBeanDefinitionNames(); for (String bean :
     * beans) { log.debug(bean); }
     */

    PasswordEncoder passwordEncoder = null;
    try {
        final ProviderManager provider = (ProviderManager) ctx
                .getBean("org.springframework.security.authentication.ProviderManager#0");
        for (final Object o : provider.getProviders()) {
            final AuthenticationProvider p = (AuthenticationProvider) o;
            if (p instanceof RememberMeAuthenticationProvider) {
                config.put("rememberMeEnabled", Boolean.TRUE);
            } else if (ctx.getBean("passwordEncoder") != null) {
                passwordEncoder = (PasswordEncoder) ctx.getBean("passwordEncoder");
            }
        }
    } catch (final NoSuchBeanDefinitionException n) {
        StartupListener.log.debug("authenticationManager bean not found, assuming test and ignoring...");
        // ignore, should only happen when testing
    }

    context.setAttribute(Constants.CONFIG, config);

    // output the retrieved values for the Init and Context Parameters
    if (StartupListener.log.isDebugEnabled()) {
        StartupListener.log.debug("Remember Me Enabled? " + config.get("rememberMeEnabled"));
        if (passwordEncoder != null) {
            StartupListener.log.debug("Password Encoder: " + passwordEncoder.getClass().getSimpleName());
        }
        StartupListener.log.debug("Populating drop-downs...");
    }

    StartupListener.setupContext(context);
}

From source file:com.sourcesense.alfresco.opensso.AlfrescoFacade.java

public AlfrescoFacade(ServletContext servletContext) {
    this.servletContext = servletContext;
    WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    transactionService = serviceRegistry.getTransactionService();
    nodeService = serviceRegistry.getNodeService();
    authComponent = (AuthenticationComponent) ctx.getBean("AuthenticationComponent");
    authService = (AuthenticationService) ctx.getBean("AuthenticationService");
    personService = (PersonService) ctx.getBean("personService");
    permissionService = (PermissionService) ctx.getBean("permissionService");
    authenticationService = (AuthenticationService) ctx.getBean("authenticationService");
    authorityService = (AuthorityService) ctx.getBean("authorityService");
    transactionalHelper = new TransactionalHelper(transactionService);
    ticketComponent = (TicketComponent) ctx.getBean("ticketComponent");

}

From source file:com.ibm.asset.trails.form.ReportDownload.java

@Override
public void doGet(HttpServletRequest pHttpServletRequest, HttpServletResponse pHttpServletResponse)
        throws ServletException, IOException {
    String lsName = this.getParameter(pHttpServletRequest, "name");
    String lsCode = this.getParameter(pHttpServletRequest, "code");
    UserSession lUserSession = ((UserSession) pHttpServletRequest.getSession().getAttribute("userSession"));

    if (lUserSession != null || lsName.equalsIgnoreCase(REPORT_NAME_NON_WORKSTATION_ACCOUNTS)
            || lsName.equalsIgnoreCase(REPORT_NAME_WORKSTATION_ACCOUNTS)) {
        Account lAccount = null;// w ww.  j a  v a  2 s.c om
        String remoteUser = null;
        ReportBase lReportBase = null;
        ServletContext lServletContext = getServletContext();
        WebApplicationContext lWebApplicationContext = WebApplicationContextUtils
                .getRequiredWebApplicationContext(lServletContext);
        ReportService lReportService = (ReportService) lWebApplicationContext.getBean("reportService");

        if (lUserSession != null) {
            lAccount = ((UserSession) pHttpServletRequest.getSession().getAttribute("userSession"))
                    .getAccount();
            remoteUser = ((UserSession) pHttpServletRequest.getSession().getAttribute("userSession"))
                    .getRemoteUser();
        }

        try {
            pHttpServletResponse.setContentType("application/vnd.ms-excel");
            HSSFWorkbook hwb = new HSSFWorkbook();
            if (lsCode != null) {
                pHttpServletResponse.setHeader("Content-Disposition",
                        new StringBuffer("filename=").append(lsName).append(lsCode)
                                .append(lAccount != null ? lAccount.getAccount().toString() : "").append(".xls")
                                .toString());
            } else {
                pHttpServletResponse.setHeader("Content-Disposition",
                        new StringBuffer("filename=").append(lsName)
                                .append(lAccount != null ? lAccount.getAccount().toString() : "").append(".xls")
                                .toString());
            }
            lReportBase = getReport(lsName, lsCode, lReportService, pHttpServletResponse.getOutputStream(),
                    pHttpServletRequest, hwb);

            if (lReportBase != null) {
                lReportBase.execute(pHttpServletRequest, lAccount, remoteUser, lsName);
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    } else {
        pHttpServletResponse.sendRedirect("/TRAILS");
    }
}

From source file:com.trailmagic.image.ui.ImageTag.java

public int doStartTag() throws JspException {
    StringBuffer html = new StringBuffer();
    ImageManifestation mf;/* w w w.j a  v a  2s .  c  o  m*/

    try {
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        // XXX: maybe this should be a parameter of the tag instead?

        // XXX: this is sort of a kludge for setting a default label
        String defaultLabel = req.getParameter(DEFAULT_LABEL_ATTR);
        HttpSession session = req.getSession();
        if (defaultLabel != null) {
            session.setAttribute(DEFAULT_LABEL_ATTR, defaultLabel);
        }

        // XXX: end kludge

        String size = req.getParameter(SIZE_ATTR);
        String originalp = req.getParameter("original");
        if (size != null) {
            mf = WebSupport.getMFBySize(m_image, Integer.parseInt(size), Boolean.parseBoolean(originalp));
        } else {
            // get label by precedence: req param, tag spec, sess attr
            String label = req.getParameter(LABEL_ATTR);
            if (label == null) {
                label = m_sizeLabel;
            }
            if (label == null) {
                label = (String) session.getAttribute(DEFAULT_LABEL_ATTR);
            }
            if (label != null) {
                mf = WebSupport.getMFByLabel(m_image, label);
            } else {
                mf = WebSupport.getDefaultMF((User) pageContext.findAttribute(USER_ATTR), m_image);
            }
        }

        if (mf != null) {
            // XXX: resume kludge
            pageContext.setAttribute("currentLabel", getLabel(mf));
            // XXX: end kludge
            // XXX: end kludge
            html.append("<img src=\"");

            //XXX: yeek?
            /*
            LinkHelper helper =
            new LinkHelper((HttpServletRequest)pageContext.getRequest());
            */
            WebApplicationContext ctx = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(pageContext.getServletContext());
            LinkHelper helper = (LinkHelper) ctx.getBean("linkHelper");

            html.append(helper.getImageMFUrl((HttpServletRequest) pageContext.getRequest(), mf));
            html.append("\" height=\"");
            html.append(mf.getHeight());
            html.append("\" width=\"");
            html.append(mf.getWidth());
            html.append("\" alt=\"");
            if (m_alt != null) {
                html.append(m_alt);
            } else if (m_image.getCaption() != null) {
                html.append(Util.escapeXml(m_image.getCaption()));
            } else {
                html.append(Util.escapeXml(m_image.getDisplayName()));
            }

            html.append("\" class=\"");
            if (mf.getWidth() > mf.getHeight()) {
                html.append("landscape");
            } else {
                html.append("portrait");
            }
            if (m_cssClass != null) {
                html.append(" ");
                html.append(m_cssClass);
            }

            html.append("\"/>");
        } else {
            html.append("No manifestations found for the specified " + "image.");
        }
        pageContext.getOut().write(html.toString());
        return SKIP_BODY;
    } catch (IOException e) {
        throw new JspException(e);
    }
}

From source file:org.openmrs.contrib.metadatarepository.webapp.listener.StartupListener.java

/**
 * This method uses the LookupManager to lookup available roles from the data layer.
 *
 * @param context The servlet context/* w ww  . j  a  v a 2s.c om*/
 */
public static void setupContext(ServletContext context) {
    ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");

    // get list of possible roles
    context.setAttribute(Constants.AVAILABLE_ROLES, mgr.getAllRoles());
    log.debug("Drop-down initialization complete [OK]");

    CompassGps compassGps = ctx.getBean(CompassGps.class);
    compassGps.index();
}

From source file:com.dianping.lion.api.http.AbstractLionServlet.java

@Override
public void init() throws ServletException {
    applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    projectService = getBean(ProjectService.class);
    environmentService = getBean(EnvironmentService.class);
    configService = getBean(ConfigService.class);
    configReleaseService = getBean(ConfigRelaseService.class);
    operationLogService = getBean(OperationLogService.class);
    systemSettingService = getBean(SystemSettingService.class);
    serviceService = getBean(ServiceService.class);
    userService = getBean(UserService.class);
    productService = getBean(ProductService.class);
    teamService = getBean(TeamService.class);
    configReleaseDao = getBean(ConfigReleaseDao.class);
}

From source file:com.edgenius.wiki.integration.rest.ShellValidatorServlet.java

private PageService getPageServiceBean() {
    if (pageService == null) {
        ServletContext context = this.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        pageService = (PageService) ctx.getBean(PageService.SERVICE_NAME);
    }/*from   w w  w.j  a v  a  2 s . com*/
    return pageService;
}

From source file:com.autentia.wuija.web.LocaleContextFilter.java

public void init(FilterConfig config) throws ServletException {
    final WebApplicationContext wac = WebApplicationContextUtils
            .getRequiredWebApplicationContext(config.getServletContext());
    try {// www  . ja  v  a2  s  .  c o m
        localeResolver = (LocaleResolver) wac.getBean("localeResolver", LocaleResolver.class);
        log.info("LocaleResolver defined: " + localeResolver.getClass().getSimpleName());

    } catch (NoSuchBeanDefinitionException e) {
        log.info("No LocaleResolver defined. Using " + CookieLocaleResolver.class.getSimpleName()
                + " by default");
        localeResolver = new CookieLocaleResolver();
    }
}