Example usage for javax.servlet ServletContext setAttribute

List of usage examples for javax.servlet ServletContext setAttribute

Introduction

In this page you can find the example usage for javax.servlet ServletContext setAttribute.

Prototype

public void setAttribute(String name, Object object);

Source Link

Document

Binds an object to a given attribute name in this ServletContext.

Usage

From source file:org.springframework.boot.context.web.SpringBootServletInitializer.java

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
    SpringApplicationBuilder builder = createSpringApplicationBuilder();
    builder.main(getClass());//w  ww .  j a  v a  2 s .  c om
    ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
    if (parent != null) {
        this.logger.info("Root context already created (using as parent).");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
        builder.initializers(new ParentContextApplicationContextInitializer(parent));
    }
    builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
    builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
    builder = configure(builder);
    SpringApplication application = builder.build();
    if (application.getSources().isEmpty()
            && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) {
        application.getSources().add(getClass());
    }
    Assert.state(application.getSources().size() > 0,
            "No SpringApplication sources have been defined. Either override the "
                    + "configure method or add an @Configuration annotation");
    // Ensure error pages are registered
    if (this.registerErrorPageFilter) {
        application.getSources().add(ErrorPageFilter.class);
    }
    return run(application);
}

From source file:org.hdiv.AbstractHDIVTestCase.java

protected final void setUp() throws Exception {

    String[] files = { "/org/hdiv/config/hdiv-core-applicationContext.xml", "/org/hdiv/config/hdiv-config.xml",
            "/org/hdiv/config/hdiv-validations.xml", "/org/hdiv/config/applicationContext-test.xml",
            "/org/hdiv/config/applicationContext-extra.xml" };

    if (this.applicationContext == null) {
        this.applicationContext = new ClassPathXmlApplicationContext(files);
    }/*from  www  .j a va2s . c  om*/

    // Servlet API mock
    HttpServletRequest request = (MockHttpServletRequest) this.applicationContext.getBean("mockRequest");
    HttpSession httpSession = request.getSession();
    ServletContext servletContext = httpSession.getServletContext();
    HDIVUtil.setHttpServletRequest(request);

    // Put Spring context on ServletContext
    StaticWebApplicationContext webApplicationContext = new StaticWebApplicationContext();
    webApplicationContext.setServletContext(servletContext);
    webApplicationContext.setParent(this.applicationContext);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            webApplicationContext);

    // Initialize config
    this.config = (HDIVConfig) this.applicationContext.getBean("config");

    InitListener initListener = new InitListener();
    // Initialize ServletContext
    ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);
    initListener.contextInitialized(servletContextEvent);
    // Initialize HttpSession
    HttpSessionEvent httpSessionEvent = new HttpSessionEvent(httpSession);
    initListener.sessionCreated(httpSessionEvent);
    // Initialize request
    ServletRequestEvent requestEvent = new ServletRequestEvent(servletContext, request);
    initListener.requestInitialized(requestEvent);

    if (log.isDebugEnabled()) {
        log.debug("Hdiv test context initialized");
    }

    onSetUp();
}

From source file:org.hdiv.hateoas.jackson.AbstractHDIVTestCase.java

protected void setUp() throws Exception {

    String[] files = { "/org/hdiv/config/hdiv-core-applicationContext.xml", "/org/hdiv/config/hdiv-config.xml",
            "/org/hdiv/config/hdiv-validations.xml", "/org/hdiv/config/applicationContext-test.xml",
            "/org/hdiv/config/applicationContext-extra.xml" };

    if (this.applicationContext == null) {
        this.applicationContext = new ClassPathXmlApplicationContext(files);
    }/* w w  w . ja  v  a2s . com*/

    // Servlet API mock
    HttpServletRequest request = (MockHttpServletRequest) this.applicationContext.getBean("mockRequest");
    HttpSession httpSession = request.getSession();
    ServletContext servletContext = httpSession.getServletContext();
    HDIVUtil.setHttpServletRequest(request);

    // Put Spring context on ServletContext
    StaticWebApplicationContext webApplicationContext = new StaticWebApplicationContext();
    webApplicationContext.setServletContext(servletContext);
    webApplicationContext.setParent(this.applicationContext);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            webApplicationContext);

    // Initialize config
    this.config = (HDIVConfig) this.applicationContext.getBean("config");

    InitListener initListener = new InitListener();
    // Initialize ServletContext
    ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);
    initListener.contextInitialized(servletContextEvent);
    // Initialize HttpSession
    HttpSessionEvent httpSessionEvent = new HttpSessionEvent(httpSession);
    initListener.sessionCreated(httpSessionEvent);
    // Initialize request
    ServletRequestEvent requestEvent = new ServletRequestEvent(servletContext, request);
    initListener.requestInitialized(requestEvent);

    if (log.isDebugEnabled()) {
        log.debug("Hdiv test context initialized");
    }

    onSetUp();
}

From source file:org.apache.solr.security.GenericHadoopAuthPlugin.java

@SuppressWarnings("unchecked")
protected FilterConfig getInitFilterConfig(Map<String, Object> pluginConfig) {
    Map<String, String> params = new HashMap<>();

    String type = (String) Objects.requireNonNull(pluginConfig.get(HADOOP_AUTH_TYPE));
    params.put(HADOOP_AUTH_TYPE, type);//from  w w  w. ja va  2  s.  c om

    String sysPropPrefix = (String) pluginConfig.getOrDefault(SYSPROP_PREFIX_PROPERTY, "solr.");
    Collection<String> authConfigNames = (Collection<String>) pluginConfig
            .getOrDefault(AUTH_CONFIG_NAMES_PROPERTY, Collections.emptyList());
    Map<String, String> authConfigDefaults = (Map<String, String>) pluginConfig
            .getOrDefault(DEFAULT_AUTH_CONFIGS_PROPERTY, Collections.emptyMap());
    Map<String, String> proxyUserConfigs = (Map<String, String>) pluginConfig.getOrDefault(PROXY_USER_CONFIGS,
            Collections.emptyMap());

    for (String configName : authConfigNames) {
        String systemProperty = sysPropPrefix + configName;
        String defaultConfigVal = authConfigDefaults.get(configName);
        String configVal = System.getProperty(systemProperty, defaultConfigVal);
        if (configVal != null) {
            params.put(configName, configVal);
        }
    }

    // Configure proxy user settings.
    params.putAll(proxyUserConfigs);

    final ServletContext servletContext = new AttributeOnlyServletContext();
    log.info("Params: " + params);

    ZkController controller = coreContainer.getZkController();
    if (controller != null) {
        servletContext.setAttribute(DELEGATION_TOKEN_ZK_CLIENT, controller.getZkClient());
    }

    FilterConfig conf = new FilterConfig() {
        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return new IteratorEnumeration(params.keySet().iterator());
        }

        @Override
        public String getInitParameter(String param) {
            return params.get(param);
        }

        @Override
        public String getFilterName() {
            return "HadoopAuthFilter";
        }
    };

    return conf;
}

From source file:com.ikon.servlet.admin.OmrServlet.java

/**
 * editAscFile// ww w  . j a v  a2  s .c o  m
 */
private void editAscFile(String userId, HttpServletRequest request, HttpServletResponse response)
        throws DatabaseException, ServletException, IOException {
    ServletContext sc = getServletContext();
    long omId = WebUtils.getLong(request, "om_id");
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("om", OmrDAO.getInstance().findByPk(omId));
    sc.getRequestDispatcher("/admin/omr_edit_asc.jsp").forward(request, response);
    log.debug("editAscFile: void");
}

From source file:com.ikon.servlet.admin.OmrServlet.java

/**
 * editFieldsFile/*from   w ww  .  ja  v a 2 s .  co m*/
 */
private void editFieldsFile(String userId, HttpServletRequest request, HttpServletResponse response)
        throws DatabaseException, ServletException, IOException {
    ServletContext sc = getServletContext();
    long omId = WebUtils.getLong(request, "om_id");
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("om", OmrDAO.getInstance().findByPk(omId));
    sc.getRequestDispatcher("/admin/omr_edit_fields.jsp").forward(request, response);
    log.debug("editFieldsFile: void");
}

From source file:com.ikon.servlet.admin.OmrServlet.java

/**
 * List omr templates//  w ww  . j  a  v  a  2s  . co  m
 */
private void list(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("list({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    List<Omr> list = OmrDAO.getInstance().findAll();
    sc.setAttribute("omr", list);
    sc.getRequestDispatcher("/admin/omr_list.jsp").forward(request, response);
    log.debug("list: void");
}

From source file:com.ikon.servlet.admin.OmrServlet.java

/**
 * delete type record/*from   ww  w .  ja v  a 2s  .com*/
 */
private void delete(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("delete({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    long omId = WebUtils.getLong(request, "om_id");
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("om", OmrDAO.getInstance().findByPk(omId));
    sc.getRequestDispatcher("/admin/omr_edit.jsp").forward(request, response);
    log.debug("delete: void");
}

From source file:com.ikon.servlet.admin.OmrServlet.java

/**
 * check/*from  www .  j a  v  a  2s  .  c  o m*/
 */
private void check(String userId, HttpServletRequest request, HttpServletResponse response)
        throws DatabaseException, ServletException, IOException {
    ServletContext sc = getServletContext();
    long omId = WebUtils.getLong(request, "om_id");
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("om", OmrDAO.getInstance().findByPk(omId));
    sc.setAttribute("results", null);
    sc.getRequestDispatcher("/admin/omr_check.jsp").forward(request, response);
    log.debug("check: void");
}

From source file:com.ikon.servlet.admin.OmrServlet.java

/**
 * edit type record/*from   w  ww .j a v  a  2 s.  c  o  m*/
 */
private void edit(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException, ParseException, RepositoryException {
    log.debug("edit({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    long omId = WebUtils.getLong(request, "om_id");
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("om", OmrDAO.getInstance().findByPk(omId));
    sc.setAttribute("properties", PropertyGroupUtils.getAllGroupsProperties());
    sc.getRequestDispatcher("/admin/omr_edit.jsp").forward(request, response);
    log.debug("edit: void");
}