Example usage for org.springframework.web.context WebApplicationContext getBean

List of usage examples for org.springframework.web.context WebApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getBean.

Prototype

<T> T getBean(String name, Class<T> requiredType) throws BeansException;

Source Link

Document

Return an instance, which may be shared or independent, of the specified bean.

Usage

From source file:net.siegmar.japtproxy.JaptProxyServlet.java

/**
 * Initializes the Japt-Proxy configuration.
 *
 * @throws ServletException is thrown if the initialization fails.
 *//*from w w  w  .  jav  a2s.  c  o m*/
@Override
public void init() throws ServletException {
    LOG.info("Starting Japt-Proxy servlet {}", Util.VERSION);

    if (japtProxy == null) {
        final WebApplicationContext appCtx = WebApplicationContextUtils
                .getRequiredWebApplicationContext(getServletContext());
        japtProxy = appCtx.getBean("japtProxy", JaptProxy.class);
    }

    LOG.info("Japt-Proxy servlet initialization complete");
}

From source file:de.acosix.alfresco.mtsupport.repo.integration.AuthenticationTest.java

@Test
public void checkUserEagerSynchAndLoginMMustermannDefaultTenant() {
    final WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    final PersonService personService = context.getBean("PersonService", PersonService.class);

    AuthenticationUtil.setFullyAuthenticatedUser("admin");
    Assert.assertTrue("User mmustermann should have been eagerly synchronized",
            personService.personExists("mmustermann"));

    final NodeRef personNodeRef = personService.getPerson("mmustermann");
    final PersonInfo personInfo = personService.getPerson(personNodeRef);

    Assert.assertEquals("First name of mustermann should have been synchronized", "Max",
            personInfo.getFirstName());/*from w  ww.j a v  a2s .co m*/
    Assert.assertEquals("Last name of mmustermann should have been synchronized", "Mustermann",
            personInfo.getLastName());

    final AuthenticationService authenticationService = context.getBean("AuthenticationService",
            AuthenticationService.class);

    authenticationService.authenticate("mmustermann", "mmustermann".toCharArray());
}

From source file:de.acosix.alfresco.mtsupport.repo.integration.AuthenticationTest.java

@Test
public void loginExistingUserAFaustDefaultTenant() {
    final WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    final AuthenticationService authenticationService = context.getBean("AuthenticationService",
            AuthenticationService.class);

    authenticationService.authenticate("afaust", "afaust".toCharArray());
}

From source file:de.acosix.alfresco.mtsupport.repo.integration.AuthenticationTest.java

@Test
public void loginNonExistingUserPMaierDefaultTenant() {
    final WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    final AuthenticationService authenticationService = context.getBean("AuthenticationService",
            AuthenticationService.class);

    this.thrown.expect(AuthenticationException.class);
    this.thrown.expectMessage("Failed to authenticate");
    authenticationService.authenticate("pmaier", "pmaier".toCharArray());
}

From source file:de.acosix.alfresco.mtsupport.repo.integration.AuthenticationTest.java

@Test
public void loginExistingUserAFaustTenantBetaAndNoSynch() {
    final WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    final TenantAdminService tenantAdminService = context.getBean("TenantAdminService",
            TenantAdminService.class);

    AuthenticationUtil.setFullyAuthenticatedUser("admin");
    if (!tenantAdminService.existsTenant("tenantbeta")) {
        tenantAdminService.createTenant("tenantbeta", "admin".toCharArray());
    }// w w  w .ja  v  a  2s.  c o m
    AuthenticationUtil.clearCurrentSecurityContext();

    final AuthenticationService authenticationService = context.getBean("AuthenticationService",
            AuthenticationService.class);

    authenticationService.authenticate("afaust@tenantbeta", "afaust".toCharArray());

    final PersonService personService = context.getBean("PersonService", PersonService.class);
    Assert.assertTrue("User afaust should have been lazily created",
            personService.personExists("afaust@tenantbeta"));

    final NodeRef personNodeRef = personService.getPerson("afaust@tenantbeta");
    final PersonInfo personInfo = personService.getPerson(personNodeRef);

    Assert.assertEquals("First name of afaust@tenantbeta should not have been synchronized", "afaust",
            personInfo.getFirstName());
    Assert.assertEquals("Last name of afaust@tenantbeta should not have been synchronized", "",
            personInfo.getLastName());

    Assert.assertFalse("User mmustermann@tenantbeta should not have been eagerly synchronized",
            personService.personExists("mmustermann@tenantbeta"));
}

From source file:de.acosix.alfresco.mtsupport.repo.integration.AuthenticationTest.java

@Test
public void loginExistingUserAFaustTenantAlphaAndEagerSynch() {
    final WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    final TenantAdminService tenantAdminService = context.getBean("TenantAdminService",
            TenantAdminService.class);

    AuthenticationUtil.setFullyAuthenticatedUser("admin");
    if (!tenantAdminService.existsTenant("tenantalpha")) {
        tenantAdminService.createTenant("tenantalpha", "admin".toCharArray());
    }//  www  .jav  a2  s .com
    AuthenticationUtil.clearCurrentSecurityContext();

    final AuthenticationService authenticationService = context.getBean("AuthenticationService",
            AuthenticationService.class);

    authenticationService.authenticate("afaust@tenantalpha", "afaust".toCharArray());

    final PersonService personService = context.getBean("PersonService", PersonService.class);
    Assert.assertTrue("User afaust@tenantalpha should have been lazily created",
            personService.personExists("afaust@tenantalpha"));

    NodeRef personNodeRef = personService.getPerson("afaust@tenantalpha");
    PersonInfo personInfo = personService.getPerson(personNodeRef);

    Assert.assertEquals("First name of afaust@tenantalpha should have been synchronized", "Axel",
            personInfo.getFirstName());
    Assert.assertEquals("Last name of afaust@tenantalpha should have been synchronized", "Faust",
            personInfo.getLastName());

    Assert.assertTrue("User mmustermann@tenantalpha should not have been eagerly synchronized",
            personService.personExists("mmustermann@tenantalpha"));

    personNodeRef = personService.getPerson("mmustermann@tenantalpha");
    personInfo = personService.getPerson(personNodeRef);

    Assert.assertEquals("First name of mustermann should have been synchronized", "Max",
            personInfo.getFirstName());
    Assert.assertEquals("Last name of mmustermann should have been synchronized", "Mustermann",
            personInfo.getLastName());
}

From source file:com.qut.middleware.esoe.sso.servlet.SSOServlet.java

@Override
public void init() throws ServletException {
    try {//from   ww  w  .  j  ava  2 s.  c o  m
        /* Spring integration to make our servlet aware of IoC */
        WebApplicationContext webAppContext = WebApplicationContextUtils
                .getRequiredWebApplicationContext(this.getServletContext());

        this.ssoProcessor = (SSOProcessor) webAppContext.getBean(ConfigurationConstants.SSO_PROCESSOR,
                com.qut.middleware.esoe.sso.SSOProcessor.class);

        if (this.ssoProcessor == null)
            throw new IllegalArgumentException(
                    "Unable to acquire SSO Processor bean from web application context. Missing bean for name: "
                            + ConfigurationConstants.SSO_PROCESSOR);
    } catch (BeansException e) {
        this.logger.error(
                "Unable to acquire SSO Processor bean from web application context. Error retrieving bean for name: "
                        + e.getLocalizedMessage());
        throw new ServletException(
                "Unable to acquire SSO Processor bean from web application context. Error retrieving bean for name: "
                        + ConfigurationConstants.SSO_PROCESSOR,
                e);
    } catch (IllegalStateException e) {
        this.logger.error(
                "Unable to acquire SSO Processor bean from web application context. Currently in an illegal state for retrieving beans.",
                e);
        throw new ServletException(
                "Unable to acquire SSO Processor bean from web application context. Currently in an illegal state for retrieving beans.",
                e);
    }
}

From source file:ar.com.zauber.commons.web.uri.assets.AbstractSpringTag.java

/**
 *  @return the {@link AssetRepository} 
 *  Search path://  ww  w .j  a v  a  2s  . c o m
 *     - If there is one configured  in the context we use that
 *     - if not we create one and put it in the PageContext.
 */
@SuppressWarnings("unchecked")
protected final <T> T resolve(final Class<T> clazz, final String beanName, final String requestBean,
        final String requestWarn, final ServiceFactory<T> factory, final int scope) {
    final WebApplicationContext appCtx = getApplicationContext();
    T service = null;

    service = (T) pageContext.getAttribute(requestBean, scope);
    if (service == null) {
        try {
            service = appCtx.getBean(beanName, clazz);
        } catch (final NoSuchBeanDefinitionException e) {
            service = factory.create(appCtx);
            pageContext.setAttribute(requestBean, service, scope);

            final Boolean warned = (Boolean) pageContext.getAttribute(requestWarn,
                    PageContext.APPLICATION_SCOPE);
            if (warned == null) {
                LoggerFactory.getLogger(getClass()).warn("Bean  " + beanName + " wasn't found. "
                        + "We will use " + service.getClass().getName());
                pageContext.setAttribute(requestWarn, true, PageContext.APPLICATION_SCOPE);
            }
        }
    }
    return service;
}

From source file:org.imsglobal.basiclti.provider.servlet.filter.BasicLTISecurityFilter.java

protected ConsumerSecretService getConsumerSecretService() throws ServletException {
    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    ConsumerSecretService bean = (ConsumerSecretService) context.getBean(ConsumerSecretService.class.getName(),
            ConsumerSecretService.class);
    if (bean == null) {
        Map<String, ConsumerSecretService> beansOfType = context.getBeansOfType(ConsumerSecretService.class);
        if (beansOfType.size() == 1) {
            bean = beansOfType.values().iterator().next();
        } else if (beansOfType.size() > 1) {
            throw new ServletException(
                    "More than one bean found of type: " + ConsumerSecretService.class.getName());
        }//from ww w .j av a 2  s .  c  o  m
    }
    if (bean != null)
        return bean;
    throw new ServletException("Not able to locate a suitable bean to act as "
            + ConsumerSecretService.class.getName() + " please register an implementation of the "
            + ConsumerSecretService.class.getName() + "!");
}

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

/**
 * /*from w w w.  j av  a 2s.  c  om*/
 */
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);
}