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

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

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

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

From source file:br.com.munif.personalsecurity.aplicacao.autorizacao.Autorizador.java

protected void initSpring() {
    System.out.println("---------> Injetando spring na API");
    WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())
            .getAutowireCapableBeanFactory().autowireBean(this);
}

From source file:fr.hoteia.qalingo.core.web.servlet.DispatcherServlet.java

private void initPlatformDevice(HttpServletRequest request) {
    final ServletContext context = getServletContext();
    final ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    final RequestUtil requestUtil = (RequestUtil) ctx.getBean("requestUtil");

    // DEVICE/*from   ww  w .  j  a  va2s.c o  m*/
    try {
        final WURFLHolder wurfl = (WURFLHolder) ctx.getBean("wurflHolder");
        final WURFLManager manager = wurfl.getWURFLManager();
        Device device = manager.getDeviceForRequest(request);
        String deviceFolder = "default";
        if (device != null) {
            boolean isSmartPhone = BooleanUtils.toBoolean(device.getVirtualCapability("is_smartphone"));
            boolean isIPhoneOs = BooleanUtils.toBoolean(device.getVirtualCapability("is_iphone_os"));
            boolean isAndroid = BooleanUtils.toBoolean(device.getVirtualCapability("is_android"));
            if (isSmartPhone || isIPhoneOs || isAndroid) {
                deviceFolder = "mobile";
            }
        }
        requestUtil.updateCurrentDevice(request, deviceFolder);

    } catch (Exception e) {
        LOG.error("", e);
    }
}

From source file:org.beanfuse.security.monitor.SecurityFilter.java

/**
 * /*from w  ww  . j a  v  a 2s.c o m*/
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = ((HttpServletRequest) request);
    String resource = resourceExtractor.extract(httpRequest);
    request.setAttribute("resourceName", resource);
    HttpSession session = httpRequest.getSession(true);
    if (null == monitor) {
        WebApplicationContext wac = WebApplicationContextUtils
                .getRequiredWebApplicationContext(session.getServletContext());
        monitor = (SecurityMonitor) wac.getBean("securityMonitor", SecurityMonitor.class);
    }
    // ??login??
    if (!freeResources.contains(resource) && !monitor.isPublicResource(resource)) {
        OnlineActivity info = monitor.getSessionController().getOnlineActivity(session.getId());
        if (info != null && null != httpRequest.getRemoteUser()
                && !info.getPrincipal().equals(httpRequest.getRemoteUser())) {
            info = null;
        }
        if (null == info) {
            Authentication auth = null;
            // remember me
            if (monitor.enableRememberMe()) {
                auth = monitor.getRememberMeService().autoLogin(httpRequest);
            }
            if (null == auth) {
                auth = new SsoAuthentication(httpRequest);
                auth.setDetails(monitor.getUserDetailsSource().buildDetails(httpRequest));
            }
            try {
                monitor.authenticate(auth);
            } catch (AuthenticationException e) {
                // URL
                session.setAttribute(PREVIOUS_URL,
                        httpRequest.getRequestURL() + "?" + httpRequest.getQueryString());
                redirectTo((HttpServletRequest) request, (HttpServletResponse) response, loginFailPath);
                return;
            }
        } else if (info.isExpired()) {
            monitor.logout(session);
            // URL
            session.setAttribute(PREVIOUS_URL,
                    httpRequest.getRequestURL() + "?" + httpRequest.getQueryString());
            redirectTo((HttpServletRequest) request, (HttpServletResponse) response, expiredPath);
            return;
        } else {
            info.refreshLastRequest();
            boolean pass = monitor.isAuthorized(info.getUserid(), resource);
            if (pass) {
                logger.debug("user {} access {} success", info.getPrincipal(), resource);
            } else {
                logger.info("user {} cannot access resource[{}]", info.getPrincipal(), resource);
                redirectTo((HttpServletRequest) request, (HttpServletResponse) response, noAuthorityPath);
                return;
            }
        }
    } else {
        logger.debug("free or public resource {} was accessed", resource);
    }
    chain.doFilter(request, response);
}

From source file:com.sun.xml.ws.transport.http.servlet.WSSpringServlet.java

protected WebApplicationContext initWebApplicationContext() {
    if (this.webApplicationContext != null) {
        return this.webApplicationContext;
    } else {//from  w  w  w .  j  a  v  a 2 s  .c  o  m
        return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    }
}

From source file:jp.co.opentone.bsol.framework.web.filter.FeedAuthenticationFilter.java

private void setupAuthenticator(String name, FilterConfig filterConfig) throws ServletException {
    try {// www. j  a  va2 s  .c  om
        factory = (FeedAuthenticatorFactory) Class.forName(name).newInstance();
        factory.setContext(
                WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext()));
    } catch (Exception e) {
        log.error(String.format("Authenticator factory not instanciated. %s", name), e);
        throw new ServletException(e);
    }
}

From source file:org.xmlactions.web.conceal.HttpPager.java

public static ApplicationContext getApplicationContext(ServletContext servletContext) {
    ApplicationContext applicationContext;
    applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    return applicationContext;
}

From source file:jp.terasoluna.fw.web.taglib.WriteCodeCountTag.java

/**
 * ^O]Jn?\bh?B/*from  w ww. ja v a  2s  .  c  o m*/
 *
 * <p>
 *   T?[ubgReLXgR?[hXg??[_?[
 * ??AR?[hXg???AR?[hXgvf?
 * p?B
 * R?[hXg???A0???B
 * </p>
 *
 * @return ???w?B? <code>EVAL_BODY_INCLUDE</code>
 * @throws JspException <code>JSP</code>O
 */
@Override
public int doStartTag() throws JspException {
    if (log.isDebugEnabled()) {
        log.debug("doStartTag() called.");
    }

    JspWriter out = pageContext.getOut();

    try {
        if ("".equals(id)) {
            // id???
            log.error("id is required.");
            throw new JspTagException("id is required.");
        }

        // pageContext?AApplicationContext?B
        ServletContext sc = pageContext.getServletContext();
        ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

        CodeListLoader loader = null;

        try {
            loader = (CodeListLoader) context.getBean(id);
        } catch (ClassCastException e) {
            //BeanCodeListLoaderOX??[
            String errorMessage = "bean id:" + id + " is not instance of CodeListLoader.";
            log.error(errorMessage);
            throw new JspTagException(errorMessage, e);
        }

        // ?P?[
        Locale locale = RequestUtils.getUserLocale((HttpServletRequest) pageContext.getRequest(),
                Globals.LOCALE_KEY);

        CodeBean[] codeBeanList = loader.getCodeBeans(locale);
        if (codeBeanList == null) {
            // codeBeanListnull??0?o?B
            if (log.isWarnEnabled()) {
                log.warn("Codebean is null. CodeListLoader(bean id:" + id + ")");
            }
            out.print(0);
        } else {
            // ????R?[hXg?o?B
            out.print(codeBeanList.length);
        }

        return EVAL_BODY_INCLUDE;
    } catch (IOException ioe) {
        log.error("IOException caused.");
        throw new JspTagException(ioe.toString());
    }
}

From source file:org.musicrecital.webapp.listener.StartupListener.java

/**
 * This method uses the LookupManager to lookup available roles from the data layer.
 *
 * @param context The servlet context//from  w ww. j  a  v  a  2s  .  co  m
 */
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]");

    // Any manager extending GenericManager will do:
    GenericManager manager = (GenericManager) ctx.getBean("userManager");
    doReindexing(manager);
    log.debug("Full text search reindexing complete [OK]");
}

From source file:com.kesdip.license.web.servlet.UpdateServlet.java

/**
 * Initialize {@link HibernateTemplate} and the actualUpdateRoot.
 * //from  w  w  w . ja  v  a 2  s.  com
 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
 */
@Override
public void init(ServletConfig config) throws ServletException {

    servletContext = config.getServletContext();

    WebApplicationContext springCtx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);

    hibernateTemplate = (HibernateTemplate) springCtx.getBean("hibernateTemplate");
    actualUpdateRoot = config.getInitParameter("updateSiteRoot");
}