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

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

Introduction

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

Prototype

@Nullable
public static WebApplicationContext getWebApplicationContext(ServletContext sc) 

Source Link

Document

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

Usage

From source file:dk.teachus.frontend.TeachUsApplication.java

protected ApplicationContext getApplicationContext() {
    return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
}

From source file:net.webpasswordsafe.server.report.JasperReportServlet.java

private boolean isAuthorizedReport(HttpServletRequest req, String reportName) {
    boolean isAuthorized = isAuthorized(req, Constants.VIEW_REPORT_PREFIX + reportName);
    User user = new User();
    user.setUsername((String) req.getSession().getAttribute(Constants.SESSION_KEY_USERNAME));
    AuditLogger auditLogger = (AuditLogger) WebApplicationContextUtils
            .getWebApplicationContext(getServletContext()).getBean("auditLogger");
    auditLogger.log(new Date(), user.getUsername(), req.getRemoteAddr(), "view report", reportName,
            isAuthorized, (isAuthorized ? "" : "not authorized"));
    return isAuthorized;
}

From source file:net.mlw.vlh.web.tag.ValueListSpaceTag.java

/**
 * @param configName The config to set.//  w  ww  . j  av a 2 s. co m
 */
public void setConfigName(String configName) {
    try {
        WebApplicationContext context = WebApplicationContextUtils
                .getWebApplicationContext(pageContext.getServletContext());
        config = (ValueListConfigBean) context.getBean(configName, ValueListConfigBean.class);
    } catch (BeansException e) {
        config = null;
        LOGGER.error("Check spelling of the config='" + configName + "' Error: " + e.getMessage());
    }
}

From source file:net.webpasswordsafe.server.report.JasperReportServlet.java

@SuppressWarnings("unchecked")
private boolean isAuthorized(HttpServletRequest req, String action) {
    boolean isAuthorized = false;
    Authorizer authorizer = (Authorizer) WebApplicationContextUtils
            .getWebApplicationContext(getServletContext()).getBean("authorizer");
    User user = new User();
    user.setUsername((String) req.getSession().getAttribute(Constants.SESSION_KEY_USERNAME));
    user.setRoles((Set<Constants.Role>) req.getSession().getAttribute(Constants.SESSION_KEY_ROLES));
    try {//from   w  w w .  java  2  s.  c  o m
        isAuthorized = authorizer.isAuthorized(user, action);
    } catch (Exception e) {
        isAuthorized = false;
    }
    return isAuthorized;
}

From source file:ro.nextreports.server.web.NextServerApplication.java

public Object getSpringBean(String beanName) {
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(getServletContext());
    if (!applicationContext.containsBean(beanName)) {
        return null;
    }//from   w w  w  . ja  v  a 2  s  .c o m

    return applicationContext.getBean(beanName);
}

From source file:edu.duke.cabig.c3pr.web.security.SecureWebServiceHandler.java

/**
 * @param servletContext//from   w  w w.j a v a2  s .c om
 * @param samlAssertion
 * @throws RuntimeException
 * @throws BeansException
 * @throws UsernameNotFoundException
 * @throws DataAccessException
 */
private void authenticateSubject(ServletContext servletContext, SAMLAssertion samlAssertion)
        throws RuntimeException, BeansException, UsernameNotFoundException, DataAccessException {

    String loginId = extractLoginId(samlAssertion);
    if (StringUtils.isBlank(loginId)) {
        throw new RuntimeException("Unable to determine login ID from the SAML assertion.");
    }

    ApplicationContext springCtx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    UserDetailsService userDetailsService = (UserDetailsService) springCtx.getBean(CSM_USER_DETAILS_SERVICE);
    UserDetails user = loadUserDetails(loginId, userDetailsService, springCtx);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user,
            user.getPassword(), user.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(token);
}

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

/**
 * Process GET request//from   ww  w . ja va 2s  .  c o  m
 * 
 * @param request HTTP request
 * @param response HTTP response
 * @throws javax.servlet.ServletException
 * @throws java.io.IOException
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // Initial log
    log.info("URI='" + request.getRequestURI() + "'");

    // Descramble request URI
    final String reportPath = getReportPath(request.getRequestURI());
    final String reportPrefix = getReportPrefix(reportPath);
    final String reportName = getReportName(request.getRequestURI());
    final String reportExtension = getReportExtension(request.getRequestURI());
    final JasperReportsService.Format format = JasperReportsService.Format
            .valueOf(reportExtension.toUpperCase());

    // Show result
    if (log.isDebugEnabled()) {
        log.debug("doGet - reportPath='" + reportPath + "'");
        log.debug("doGet - reportPrefix='" + reportPrefix + "'");
        log.debug("reportName='" + reportName + "'");
        log.debug("reportExtension='" + reportExtension + "'");
        log.debug("format='" + format + "'");
    }

    // getting the jasperReportsService from Spring context
    final JasperReportsService jasperReportsService;
    if (SOURCE_TYPE.equals(SourceType.DATASOURCE)) {
        jasperReportsService = (JasperReportsService) WebApplicationContextUtils
                .getWebApplicationContext(getServletContext()).getBean("jasperReportsService");
    } else {
        jasperReportsService = (JasperReportsService) WebApplicationContextUtils
                .getWebApplicationContext(getServletContext()).getBean("jasperReportsBeanService");
    }

    final JasperReport report;
    final File file;

    try {
        // loading the master report to get the parameters of it
        // if the cache option is active will be compiled and cached
        report = jasperReportsService
                .compileReport(reportName.split(JasperReportsService.REPORT_NAMES_SEPARATOR)[0]);
        final Map<String, Object> params = loadParameters(report, request);
        file = jasperReportsService.generateReport(reportName, format, params);
        file.deleteOnExit();

    } catch (Exception e) {
        final String msg = "Exception generating the report.";
        log.error(msg, e);
        throw new ServletException(msg, e);
    }

    // writing the report in the servlet response
    int read = 0;
    final byte[] bytes = new byte[1024];
    final FileInputStream in = new FileInputStream(file);
    while ((read = in.read(bytes)) != -1) {
        response.getOutputStream().write(bytes, 0, read);
    }
    in.close();

    // setting the contentType depending on the format
    switch (format) {
    case CSV:
        response.setContentType("application/vnd.ms-excel");
        break;

    case RTF:
        response.setContentType("application/rtf");
        break;

    case XLS:
        response.setContentType("application/vnd.ms-excel");
        break;

    case ODT:
        response.setContentType("application/odt");
        break;

    case PDF:
        response.setContentType("application/pdf");
        break;
    }

    // prevents problems with the browser's cache
    response.setHeader("Cache-Control", "no-cache");

    // flush and close the response
    response.getOutputStream().flush();
    response.getOutputStream().close();

    if (AUTOCLEAN_SESSION) {
        autoCleanSession(report, request);
    }

    file.delete();
    log.trace("File='" + file + "' deleted.");
}

From source file:mvc.parent.WebController.java

private boolean hasCommonRights(String url, HttpServletRequest request) {
    WebInvocationPrivilegeEvaluator wipe = (WebInvocationPrivilegeEvaluator) WebApplicationContextUtils
            .getWebApplicationContext(request.getServletContext())
            .getBean(WebInvocationPrivilegeEvaluator.class);
    return wipe.isAllowed(url, SecurityContextHolder.getContext().getAuthentication());
}

From source file:de.iteratec.turm.servlets.TurmServlet.java

/**
 * @return//  ww w  . j av a 2 s .c  om
 * @throws DaoException
 */
protected UserDao getUserDao() throws DaoException {
    WebApplicationContext springCtx = WebApplicationContextUtils
            .getWebApplicationContext(this.getServletContext());
    return springCtx.getBean(UserDao.class);
}