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:org.sakaiproject.tool.section.filter.AuthnFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpSession session = ((HttpServletRequest) request).getSession(true);
    Authn authnService = (Authn) WebApplicationContextUtils
            .getWebApplicationContext(session.getServletContext()).getBean(authnBean);
    String userUid = null;//from   w ww  .  ja  v a2  s .c  om
    try {
        userUid = authnService.getUserUid(request);
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Could not get user uuid from authn service.");
    }
    if (log.isDebugEnabled())
        log.debug("userUid=" + userUid);
    if (userUid == null) {
        if (authnRedirect != null) {
            if (authnRedirect.equals(((HttpServletRequest) request).getRequestURI())) {
                // Don't redirect to the same spot.
                chain.doFilter(request, response);
            } else {
                ((HttpServletResponse) response).sendRedirect(authnRedirect);
            }
        } else {
            ((HttpServletResponse) response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }
    } else {
        chain.doFilter(request, response);
    }
}

From source file:com.sharksharding.util.web.http.QueryViewServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    /* ? *///  w ww.ja  v  a  2s  .  c om
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    /* Ioc */
    WebApplicationContext context = WebApplicationContextUtils
            .getWebApplicationContext(request.getSession().getServletContext());
    ShardRule shardRule = (ShardRule) context.getBean("shardRule");
    JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
    if (null != shardRule) {
        byte[] viewData = null;
        byte[] responseData = null;
        /* 1???2??? */
        final String TYPE = request.getParameter("type");
        if (null == TYPE) {
            viewData = initView(PATH + "index.html");
            if (null != viewData) {
                write(response, viewData);
            }
        } else {
            switch (Integer.parseInt(TYPE)) {
            case 1:
                String page = request.getParameter("page");
                if (page.equals("index")) {
                    viewData = initView(PATH + "index.html");
                } else if (page.equals("query")) {
                    viewData = initView(PATH + "query.html");
                }
                if (null != viewData) {
                    write(response, viewData);
                }
                break;
            case 2:
                final String SQL = request.getParameter("sql");
                if (null != SQL && 0 < SQL.trim().length()) {
                    JSONObject jsonObj = new JSONObject();
                    ExecuteSql exeSql = new ExecuteSql(jdbcTemplate);
                    try {
                        Map<String, Object> datas = exeSql.queryData(SQL);
                        if (!datas.isEmpty()) {
                            StringBuffer strBuffer = new StringBuffer();
                            Set<String> keys = datas.keySet();
                            for (String key : keys) {
                                strBuffer.append(datas.get(key) + ",");
                            }
                            jsonObj.put("sqlResult", strBuffer.toString());
                            responseData = jsonObj.toJSONString().getBytes();
                        }
                    } catch (Exception e) {
                        Writer write = new StringWriter();
                        e.printStackTrace(new PrintWriter(write));
                        jsonObj.put("error", write.toString());
                        responseData = jsonObj.toString().getBytes("utf-8");
                    }
                } else {
                    responseData = GetIndexData.getData(shardRule).getBytes("utf-8");
                }
                if (null != responseData) {
                    write(response, responseData);
                }
            }
        }
    }
}

From source file:org.vaadin.spring.security.shared.PushSecurityInterceptor.java

private synchronized SecurityContextRepository getSecurityContextRepository(ServletContext servletContext) {
    if (securityContextRepository == null) {
        final WebApplicationContext applicationContext = WebApplicationContextUtils
                .getWebApplicationContext(servletContext);
        try {//  ww  w.  j a  va 2  s  . co m
            securityContextRepository = applicationContext.getBean(SecurityContextRepository.class);
        } catch (BeansException ex) {
            LOGGER.info(
                    "Found no SecurityContextRepository in the application context, using HttpSessionSecurityContextRepository");
            securityContextRepository = new HttpSessionSecurityContextRepository();
        }
    }
    return securityContextRepository;
}

From source file:org.openmeetings.servlet.outputhandler.ActivateUser.java

private Configurationmanagement getConfigurationmanagement() {
    try {/*from w  w  w.j  a  v  a 2s.co  m*/
        if (!ScopeApplicationAdapter.initComplete) {
            return null;
        }
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        return (Configurationmanagement) context.getBean("cfgManagement");
    } catch (Exception err) {
        log.error("[getConfigurationmanagement]", err);
    }
    return null;
}

From source file:io.github.benas.todolist.web.servlet.todo.DeleteTodoServlet.java

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(servletConfig.getServletContext());
    todoService = applicationContext.getBean(TodoService.class);
    resourceBundle = ResourceBundle.getBundle("todolist");
}

From source file:com.redoute.datamap.servlet.datamap.CreateDatamap.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  ww  . jav  a  2 s.c  om
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);

    try {
        String stream = policy.sanitize(request.getParameter("stream"));
        String application = policy.sanitize(request.getParameter("application"));
        String page = policy.sanitize(request.getParameter("page"));
        String locationType = policy.sanitize(request.getParameter("locationType"));
        String locationValue = policy.sanitize(request.getParameter("locationValue"));
        String implemented = policy.sanitize(request.getParameter("implemented"));
        String zone = policy.sanitize(request.getParameter("zone"));
        String picture = policy.sanitize(request.getParameter("picture"));
        String comment = policy.sanitize(request.getParameter("comment"));

        ApplicationContext appContext = WebApplicationContextUtils
                .getWebApplicationContext(this.getServletContext());
        IDatamapService datamapService = appContext.getBean(IDatamapService.class);
        IFactoryDatamap factoryDatamap = appContext.getBean(IFactoryDatamap.class);

        Datamap datamap = factoryDatamap.create(0, stream, application, page, locationType, locationValue,
                implemented, zone, picture, comment);
        datamapService.createDatamap(datamap);

        response.sendRedirect("Datamap.jsp");
    } finally {
        out.close();
    }
}

From source file:org.dms.sys.web.app.ContextListener.java

/**
 * Context initialized./*from  w  ww.ja  v  a 2 s .com*/
 *
 * @param event the event
 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
 */
public void contextInitialized(ServletContextEvent event) {
    // make sure that the spaces store in the repository exists
    this.servletContext = event.getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

    // If no context has been initialised, exit silently so config changes can be made
    if (ctx == null) {
        return;
    }

    synchronized (this) {
        findEnterpriseListener();
        if (enterpriseListener != null) {
            // Perform any extra context initialisation required for enterprise.
            enterpriseListener.contextInitialized(event);
        }
    }
}

From source file:org.ws13.vaadin.osgi.dm.app.SpringApplicationServlet.java

/**
 * Get the application bean in Spring's context.
 * /* w  ww . ja v a2s  .c  o  m*/
 * @see AbstractApplicationServlet#getNewApplication(HttpServletRequest)
 */
@Override
protected Application getNewApplication(HttpServletRequest request) throws ServletException {

    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

    if (wac == null) {
        throw new ServletException("Cannot get an handle on Spring's context. Is Spring running?"
                + "Check there's an org.springframework.web.context.ContextLoaderListener configured.");
    }

    String[] beanDefinitionNames = wac.getBeanDefinitionNames();
    for (String string : beanDefinitionNames) {
        System.out.println("SpringApplicationServlet.getNewApplication() ->" + string);
    }

    Application bean = wac.getBean(name, Application.class);

    if (!(bean instanceof Application)) {

        throw new ServletException("Bean " + name + " is not of expected class Application");
    }

    return bean;
}

From source file:io.github.benas.todolist.web.servlet.user.account.UpdateAccountServlet.java

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(servletConfig.getServletContext());
    userService = applicationContext.getBean(UserService.class);
    resourceBundle = ResourceBundle.getBundle("todolist");
}

From source file:com.qzgf.datacollection.timer.Yd12580TimerTask.java

public Yd12580TimerTask(ServletContext servletContext) {
    ///*ww w  .ja v  a2 s .co  m*/
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    //?
    baseSqlMapDAO = (BaseSqlMapDAO) context.getBean("baseSqlMapDAO");
    //???
    communicateFacade = (CommunicateFacade) context.getBean("communicateFacade");

    if (log.isDebugEnabled())
        log.debug("12580?");
}