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

Object getBean(String name) throws BeansException;

Source Link

Document

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

Usage

From source file:org.ncbo.stanford.util.security.filter.AuthorizationFilter.java

@SuppressWarnings("unchecked")
public AuthorizationFilter(Context context, WebApplicationContext springAppContext, Router router) {
    super(context, springAppContext, router);
    ontologyService = (OntologyService) springAppContext.getBean("ontologyService");
    addExceptionPaths((TreeSet<String>) springAppContext.getBean("authorizationExceptionPaths", TreeSet.class));
}

From source file:eu.earthobservatory.org.StrabonEndpoint.BrowseBean.java

public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);

    // get the context of the servlet
    context = getServletContext();/*from   www . ja v  a  2 s  . c o  m*/

    // get the context of the application
    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);

    // the the strabon wrapper
    strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean");

}

From source file:org.openxdata.server.servlet.FormDownloadServlet.java

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);

    ServletContext sctx = this.getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sctx);

    // Manual Injection
    formsServer = (FormsServer) ctx.getBean("formsServer");
    userService = (UserService) ctx.getBean("userService");
    formDownloadService = (FormDownloadService) ctx.getBean("formDownloadService");
    serializationService = (SerializationService) ctx.getBean("serializationService");
    authenticationService = (AuthenticationService) ctx.getBean("authenticationService");
}

From source file:org.openxdata.server.servlet.ResetPasswordServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    WebApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(this.getServletContext());
    mailSender = (org.springframework.mail.javamail.JavaMailSenderImpl) ctx.getBean("mailSender");
    userService = (UserService) ctx.getBean("userService");
    userDetailsService = (UserDetailsService) ctx.getBean("userDetailsService");
    messageSource = (ResourceBundleMessageSource) ctx.getBean("messageSource");
    userLocale = new Locale((String) request.getSession().getAttribute("locale")); //new AcceptHeaderLocaleResolver().resolveLocale(request);
    log.debug("userLocale=" + userLocale.getLanguage());

    String email = request.getParameter("email");
    if (email == null) {
        //ajax response reference text
        out.println("noEmailSuppliedError");
    }// w  w w  .j a  v a 2s.c  o m

    try {
        User user = userService.findUserByEmail(email);
        /*
          * Valid User with the correct e-mail.
          */
        insertUserInSecurityContext(user); // this is so that it is possible to reset their password (security checks)

        Properties props = propertyPlaceholder.getResolvedProps();
        String from = props.getProperty("mailSender.from");

        try {
            //Reset the User password and send an email
            userService.resetPassword(user, 6);
            SimpleMailMessage msg = new SimpleMailMessage();
            msg.setTo(email);
            msg.setSubject(messageSource.getMessage("resetPasswordEmailSubject",
                    new Object[] { user.getFullName() }, userLocale));
            msg.setText(messageSource.getMessage("resetPasswordEmail",
                    new Object[] { user.getName(), user.getClearTextPassword() }, userLocale));
            msg.setFrom(from);

            try {
                mailSender.send(msg);
                //ajax response reference text
                out.println("passwordResetSuccessful");
            } catch (MailException ex) {
                log.error("Error while sending an email to the user " + user.getName()
                        + " in order to reset their password.", ex);
                log.error("Password reset email:" + msg.toString());
                //ajax response reference text
                out.println("emailSendError");
            }
        } catch (Exception e) {
            log.error("Error while resetting the user " + user.getName() + "'s password", e);
            //ajax response reference text
            out.println("passwordResetError");
        }
    } catch (UserNotFoundException userNotFound) {
        /*
        * Invalid User or incorrect Email.
        */
        //ajax response reference text
        out.println("validationError");
    }
}

From source file:net.naijatek.myalumni.framework.spring.MyAlumniStartupServlet.java

private void setupCache() throws Exception {

    // @see:/*w ww . j av  a2s.  c om*/
    // net.naijatek.myalumni.util.date.DateConverterUtil::getDatePattern()
    getServletContext().setAttribute(BaseConstants.LOOKUP_DATE_FORMAT_PATTERN,
            SystemConfigConstants.DATE_FORMAT_JSP);
    getServletContext().setAttribute(BaseConstants.LOOKUP_DATE_TIME_FORMAT_PATTERN,
            SystemConfigConstants.DATE_TIME_FORMAT_JSP);

    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    ISystemTaskService startupService = (ISystemTaskService) wac
            .getBean(BaseConstants.SERVICE_SYSTEM_TASK_LOOKUP);
    ISystemConfigService sysConfigService = (ISystemConfigService) wac
            .getBean(BaseConstants.SERVICE_SYSTEM_CONFIG);

    SystemConfigVO sysConfigVO = sysConfigService.getSystemConfig();
    getServletContext().setAttribute(BaseConstants.ORGANIZATION_NAME, sysConfigVO.getOrganizationName());
    getServletContext().setAttribute(BaseConstants.ORGANIZATION_SHORT_NAME,
            sysConfigVO.getOrganizationShortName());
    getServletContext().setAttribute(BaseConstants.ALBUM_URL, sysConfigVO.getAlbumUrl());
    getServletContext().setAttribute(BaseConstants.FORUM_URL, sysConfigVO.getForumUrl());
    getServletContext().setAttribute(BaseConstants.SERVER_URL, sysConfigVO.getServerUrl());
    getServletContext().setAttribute(BaseConstants.ORG_EMAIL, sysConfigVO.getOrgEmail());
    getServletContext().setAttribute(BaseConstants.LOGO_NAME, sysConfigVO.getLogoFileName());
    getServletContext().setAttribute(BaseConstants.CSS_TYPE, "default");

    String hasDorm = sysConfigVO.getHasDormitory();

    setupDatabaseCache(startupService, sysConfigService, hasDorm);
    setupOtherCache(hasDorm);

    if (sysConfigVO.getSystemConfigId() == null) {
        getServletContext().setAttribute(BaseConstants.FIRST_STARTUP, BaseConstants.BOOLEAN_YES);
    } else {
        getServletContext().setAttribute(BaseConstants.FIRST_STARTUP, BaseConstants.BOOLEAN_NO);
    }

}

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

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    /* ? *//*from   w  ww  .j av a 2  s.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.hdiv.filter.ValidatorFilter.java

/**
 * Init required dependencies/*from   w w  w.  jav  a  2 s .c om*/
 */
protected void initDependencies() {

    if (this.hdivConfig == null) {
        ServletContext servletContext = getServletContext();
        WebApplicationContext context = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletContext);

        this.hdivConfig = (HDIVConfig) context.getBean("config");
        this.validationHelper = (IValidationHelper) context.getBean("validatorHelper");
        if (context.containsBean("multipartConfig")) {
            // For applications without Multipart requests
            this.multipartConfig = (IMultipartConfig) context.getBean("multipartConfig");
        }

        this.errorHandler = (ValidatorErrorHandler) context.getBean("validatorErrorHandler");
    }

}

From source file:ch.ralscha.extdirectspring.bean.ExtDirectResponseBuilder.java

/**
 * Creates an "exception" response. Calls {@link ExtDirectResponse#setType(String)}
 * with a value of "exception". Calls {@link ExtDirectResponse#setMessage(String)} and
 * {@link ExtDirectResponse#setWhere(String)} according to the {@link Configuration}.
 *
 * This is a method primarily used for implementations of
 * {@link HandlerExceptionResolver}.//from ww  w .  j a v  a 2s .  co m
 *
 * @param exception the exception that was thrown.
 * @return this instance
 */
public ExtDirectResponseBuilder setException(Exception exception) {
    unsuccessful();

    WebApplicationContext ctx = RequestContextUtils.findWebApplicationContext(this.request);
    Configuration configuration;
    try {
        configuration = ctx.getBean(Configuration.class);
    } catch (NoSuchBeanDefinitionException e) {
        configuration = new Configuration();
    }

    this.extDirectResponse.setType("exception");
    this.extDirectResponse.setMessage(configuration.getMessage(exception));

    if (configuration.isSendStacktrace()) {
        this.extDirectResponse.setWhere(ExtDirectSpringUtil.getStackTrace(exception));
    } else {
        this.extDirectResponse.setWhere(null);
    }

    return this;
}