Example usage for javax.servlet ServletContext getAttributeNames

List of usage examples for javax.servlet ServletContext getAttributeNames

Introduction

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

Prototype

public Enumeration<String> getAttributeNames();

Source Link

Document

Returns an Enumeration containing the attribute names available within this ServletContext.

Usage

From source file:org.smigo.config.WebAppInitializer.java

@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
    super.beforeSpringSecurityFilterChain(servletContext);
    log.info("Starting servlet context");
    log.info("contextName: " + servletContext.getServletContextName());
    log.info("contextPath:" + servletContext.getContextPath());
    log.info("effectiveMajorVersion:" + servletContext.getEffectiveMajorVersion());
    log.info("effectiveMinorVersion:" + servletContext.getEffectiveMinorVersion());
    log.info("majorVersion:" + servletContext.getMajorVersion());
    log.info("minorVersion:" + servletContext.getMinorVersion());
    log.info("serverInfo:" + servletContext.getServerInfo());
    //               ", virtualServerName:" + servletContext.getVirtualServerName() +
    log.info("toString:" + servletContext.toString());

    for (Enumeration<String> e = servletContext.getAttributeNames(); e.hasMoreElements();) {
        log.info("Attribute:" + e.nextElement());
    }//  ww w  .j a v  a 2s.  c  o m
    for (Map.Entry<String, String> env : System.getenv().entrySet()) {
        log.info("System env:" + env.toString());
    }
    for (Map.Entry<Object, Object> prop : System.getProperties().entrySet()) {
        log.info("System prop:" + prop.toString());
    }

    final String profile = System.getProperty("smigoProfile", EnvironmentProfile.PRODUCTION);
    log.info("Starting with profile " + profile);

    WebApplicationContext context = new AnnotationConfigWebApplicationContext() {
        {
            register(WebConfiguration.class);
            setDisplayName("SomeRandomName");
            getEnvironment().setActiveProfiles(profile);
        }
    };

    FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("CharacterEncodingFilter",
            new CharacterEncodingFilter());
    characterEncodingFilter.setInitParameter("encoding", "UTF-8");
    characterEncodingFilter.setInitParameter("forceEncoding", "true");
    characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");

    servletContext.addListener(new RequestContextListener());
    servletContext.addListener(new ContextLoaderListener(context));

    //http://stackoverflow.com/questions/4811877/share-session-data-between-2-subdomains
    //        servletContext.getSessionCookieConfig().setDomain(getDomain());

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(false);
    servletContext.addServlet("dispatcher", dispatcherServlet).addMapping("/");
}

From source file:org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfigurationTests.java

@Test
public void onDifferentPortInWebServer() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.port=" + ports.get().management);
    this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class,
            BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class);
    ServletContext servletContext = mock(ServletContext.class);
    given(servletContext.getInitParameterNames()).willReturn(new Vector<String>().elements());
    given(servletContext.getAttributeNames()).willReturn(new Vector<String>().elements());
    this.applicationContext.setServletContext(servletContext);
    this.applicationContext.refresh();
    assertContent("/controller", ports.get().management, null);
    assertContent("/endpoint", ports.get().management, null);
}

From source file:org.eiichiro.bootleg.AbstractRequest.java

/**
 * Returns the Web endpoint method parameter from Web application 
 * ({@code ServletContext})./* w  ww  .ja  v  a 2s  .  c o  m*/
 * 
 * @param type The parameter type.
 * @param name The parameter name.
 * @return The Web endpoint method parameter from Web application 
 * ({@code ServletContext}).
 */
protected Object application(Type type, String name) {
    return parameter(type, name, new Function<String, Object>() {

        public Object apply(String name) {
            return context.application().getAttribute(name);
        }

    }, new Function<String, Collection<Object>>() {

        @SuppressWarnings("unchecked")
        public Collection<Object> apply(String name) {
            ServletContext servletContext = context.application();
            Object attribute = servletContext.getAttribute(name);

            if (attribute instanceof Collection<?>) {
                return (Collection<Object>) attribute;
            }

            Map<String, Object> map = new TreeMap<String, Object>();

            for (Object object : Collections.list(servletContext.getAttributeNames())) {
                String key = (String) object;

                if (key.startsWith(name + "[")) {
                    map.put(key, servletContext.getAttribute(key));
                }
            }

            return (map.isEmpty()) ? null : map.values();
        }

    });
}

From source file:com.inverse2.ajaxtoaster.AjaxToasterServlet.java

public List getJDCBConnectionPoolList() {
    ServletContext context = getServletContext();
    DBConnectionPool pool = null;/*  ww w.  j  a va2 s.  co  m*/
    ArrayList poolList = new ArrayList();

    for (Enumeration e = context.getAttributeNames(); e.hasMoreElements();) {
        String attribName = (String) e.nextElement();
        if (attribName.startsWith(ATTRIB_JDBC_CONN_POOL)) {
            pool = (DBConnectionPool) context.getAttribute(attribName);
            if (pool != null) {
                poolList.add(pool);
            }
        }
    }

    return (poolList);
}

From source file:org.apache.struts.action.ActionServlet.java

/**
 * <p>Saves a String[] of module prefixes in the ServletContext under
 * Globals.MODULE_PREFIXES_KEY.  <strong>NOTE</strong> - the "" prefix for
 * the default module is not included in this list.</p>
 *
 * @param context The servlet context.//www. jav a  2 s  . co m
 * @since Struts 1.2
 */
protected void initModulePrefixes(ServletContext context) {
    ArrayList prefixList = new ArrayList();

    Enumeration names = context.getAttributeNames();

    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();

        if (!name.startsWith(Globals.MODULE_KEY)) {
            continue;
        }

        String prefix = name.substring(Globals.MODULE_KEY.length());

        if (prefix.length() > 0) {
            prefixList.add(prefix);
        }
    }

    String[] prefixes = (String[]) prefixList.toArray(new String[prefixList.size()]);

    context.setAttribute(Globals.MODULE_PREFIXES_KEY, prefixes);
}

From source file:com.jsmartframework.web.manager.BeanHandler.java

void finalizeWebBeans(ServletContext servletContext) {
    List<String> names = Collections.list(servletContext.getAttributeNames());
    for (String name : names) {
        Object bean = servletContext.getAttribute(name);
        if (bean == null) {
            continue;
        }/*from  w  w  w.  j  a va2 s.com*/
        if (bean.getClass().isAnnotationPresent(WebBean.class)) {
            finalizeWebBean(bean, servletContext);
        }
    }
}

From source file:org.apache.tapestry.request.RequestContext.java

/**
 * Writes the state of the context to the writer, typically for inclusion
 * in a HTML page returned to the user. This is useful
 * when debugging.  The Inspector uses this as well.
 *
 **///from   w  ww.j av a2s.c  o  m

public void write(IMarkupWriter writer) {
    // Create a box around all of this stuff ...

    writer.begin("table");
    writer.attribute("class", "request-context-border");
    writer.begin("tr");
    writer.begin("td");

    // Get the session, if it exists, and display it.

    HttpSession session = getSession();

    if (session != null) {
        object(writer, "Session");
        writer.begin("table");
        writer.attribute("class", "request-context-object");

        section(writer, "Properties");
        header(writer, "Name", "Value");

        pair(writer, "id", session.getId());
        datePair(writer, "creationTime", session.getCreationTime());
        datePair(writer, "lastAccessedTime", session.getLastAccessedTime());
        pair(writer, "maxInactiveInterval", session.getMaxInactiveInterval());
        pair(writer, "new", session.isNew());

        List names = getSorted(session.getAttributeNames());
        int count = names.size();

        for (int i = 0; i < count; i++) {
            if (i == 0) {
                section(writer, "Attributes");
                header(writer, "Name", "Value");
            }

            String name = (String) names.get(i);
            pair(writer, name, session.getAttribute(name));
        }

        writer.end(); // Session

    }

    object(writer, "Request");
    writer.begin("table");
    writer.attribute("class", "request-context-object");

    // Parameters ...

    List parameters = getSorted(_request.getParameterNames());
    int count = parameters.size();

    for (int i = 0; i < count; i++) {

        if (i == 0) {
            section(writer, "Parameters");
            header(writer, "Name", "Value(s)");
        }

        String name = (String) parameters.get(i);
        String[] values = _request.getParameterValues(name);

        writer.begin("tr");
        writer.attribute("class", getRowClass());
        writer.begin("th");
        writer.print(name);
        writer.end();
        writer.begin("td");

        if (values.length > 1)
            writer.begin("ul");

        for (int j = 0; j < values.length; j++) {
            if (values.length > 1)
                writer.beginEmpty("li");

            writer.print(values[j]);

        }

        writer.end("tr");
    }

    section(writer, "Properties");
    header(writer, "Name", "Value");

    pair(writer, "authType", _request.getAuthType());
    pair(writer, "characterEncoding", _request.getCharacterEncoding());
    pair(writer, "contentLength", _request.getContentLength());
    pair(writer, "contentType", _request.getContentType());
    pair(writer, "method", _request.getMethod());
    pair(writer, "pathInfo", _request.getPathInfo());
    pair(writer, "pathTranslated", _request.getPathTranslated());
    pair(writer, "protocol", _request.getProtocol());
    pair(writer, "queryString", _request.getQueryString());
    pair(writer, "remoteAddr", _request.getRemoteAddr());
    pair(writer, "remoteHost", _request.getRemoteHost());
    pair(writer, "remoteUser", _request.getRemoteUser());
    pair(writer, "requestedSessionId", _request.getRequestedSessionId());
    pair(writer, "requestedSessionIdFromCookie", _request.isRequestedSessionIdFromCookie());
    pair(writer, "requestedSessionIdFromURL", _request.isRequestedSessionIdFromURL());
    pair(writer, "requestedSessionIdValid", _request.isRequestedSessionIdValid());
    pair(writer, "requestURI", _request.getRequestURI());
    pair(writer, "scheme", _request.getScheme());
    pair(writer, "serverName", _request.getServerName());
    pair(writer, "serverPort", _request.getServerPort());
    pair(writer, "contextPath", _request.getContextPath());
    pair(writer, "servletPath", _request.getServletPath());

    // Now deal with any headers

    List headers = getSorted(_request.getHeaderNames());
    count = headers.size();

    for (int i = 0; i < count; i++) {
        if (i == 0) {
            section(writer, "Headers");
            header(writer, "Name", "Value");
        }

        String name = (String) headers.get(i);
        String value = _request.getHeader(name);

        pair(writer, name, value);
    }

    // Attributes

    List attributes = getSorted(_request.getAttributeNames());
    count = attributes.size();

    for (int i = 0; i < count; i++) {
        if (i == 0) {
            section(writer, "Attributes");
            header(writer, "Name", "Value");
        }

        String name = (String) attributes.get(i);

        pair(writer, name, _request.getAttribute(name));
    }

    // Cookies ...

    Cookie[] cookies = _request.getCookies();

    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {

            if (i == 0) {
                section(writer, "Cookies");
                header(writer, "Name", "Value");
            }

            Cookie cookie = cookies[i];

            pair(writer, cookie.getName(), cookie.getValue());

        } // Cookies loop
    }

    writer.end(); // Request

    object(writer, "Servlet");
    writer.begin("table");
    writer.attribute("class", "request-context-object");

    section(writer, "Properties");
    header(writer, "Name", "Value");

    pair(writer, "servlet", _servlet);
    pair(writer, "name", _servlet.getServletName());
    pair(writer, "servletInfo", _servlet.getServletInfo());

    ServletConfig config = _servlet.getServletConfig();

    List names = getSorted(config.getInitParameterNames());
    count = names.size();

    for (int i = 0; i < count; i++) {

        if (i == 0) {
            section(writer, "Init Parameters");
            header(writer, "Name", "Value");
        }

        String name = (String) names.get(i);
        ;
        pair(writer, name, config.getInitParameter(name));

    }

    writer.end(); // Servlet

    ServletContext context = config.getServletContext();

    object(writer, "Servlet Context");
    writer.begin("table");
    writer.attribute("class", "request-context-object");

    section(writer, "Properties");
    header(writer, "Name", "Value");

    pair(writer, "majorVersion", context.getMajorVersion());
    pair(writer, "minorVersion", context.getMinorVersion());
    pair(writer, "serverInfo", context.getServerInfo());

    names = getSorted(context.getInitParameterNames());
    count = names.size();
    for (int i = 0; i < count; i++) {
        if (i == 0) {
            section(writer, "Initial Parameters");
            header(writer, "Name", "Value");
        }

        String name = (String) names.get(i);
        pair(writer, name, context.getInitParameter(name));
    }

    names = getSorted(context.getAttributeNames());
    count = names.size();
    for (int i = 0; i < count; i++) {
        if (i == 0) {
            section(writer, "Attributes");
            header(writer, "Name", "Value");
        }

        String name = (String) names.get(i);
        pair(writer, name, context.getAttribute(name));
    }

    writer.end(); // Servlet Context

    writeSystemProperties(writer);

    writer.end("table"); // The enclosing border
}