Example usage for javax.servlet ServletRequest getAttributeNames

List of usage examples for javax.servlet ServletRequest getAttributeNames

Introduction

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

Prototype

public Enumeration<String> getAttributeNames();

Source Link

Document

Returns an Enumeration containing the names of the attributes available to this request.

Usage

From source file:com.xsw.utils.Servlets.java

/**
 * ???Request Parameters, copy from spring WebUtils.
 * /* w  w  w.  ja v a  2 s .  c om*/
 * Parameter???.
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
    Validate.notNull(request, "Request must not be null");
    Map<String, Object> params = new TreeMap<String, Object>();
    if (prefix == null) {
        prefix = "";
    }
    // process Attribute
    Enumeration<String> paramNames = request.getAttributeNames();
    while (paramNames != null && paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        if ("".equals(prefix) || paramName.startsWith(prefix)) {
            String unprefixed = paramName.substring(prefix.length());
            Object attobj = request.getAttribute(paramName);
            String[] values = null;
            if (attobj instanceof String[]) {
                values = (String[]) attobj;
            } else {
                values = new String[1];
                values[0] = String.valueOf(attobj);
            }
            if (values == null || values.length == 0) {
                // Do nothing, no values found at all.
            } else if (values.length > 1) {
                params.put(unprefixed, values);
            } else {
                params.put(unprefixed, values[0]);
            }
        }
    }
    // process parameter
    paramNames = request.getParameterNames();
    while (paramNames != null && paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        if ("".equals(prefix) || paramName.startsWith(prefix)) {
            String unprefixed = paramName.substring(prefix.length());
            String[] values = request.getParameterValues(paramName);
            if (values == null || values.length == 0) {
                // Do nothing, no values found at all.
            } else if (values.length > 1) {
                params.put(unprefixed, values);
            } else {
                params.put(unprefixed, values[0]);
            }
        }
    }
    return params;
}

From source file:com.jaspersoft.jasperserver.war.tags.BaseTagSupport.java

protected Map setRequestAttributes(Map attributes) {
    ServletRequest request = pageContext.getRequest();
    Set attributeNames = new HashSet();
    for (Enumeration it = request.getAttributeNames(); it.hasMoreElements();) {
        String attribute = (String) it.nextElement();
        attributeNames.add(attribute);//  ww w . j a v a2 s.  co m
    }

    Map restoreMap = new HashMap();
    for (Iterator it = attributes.entrySet().iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        String attribute = (String) entry.getKey();
        Object value = entry.getValue();

        Object restoreValue;
        if (attributeNames.contains(attribute)) {
            restoreValue = request.getAttribute(attribute);
        } else {
            restoreValue = NO_ATTRIBUTE;
        }
        restoreMap.put(attribute, restoreValue);

        request.setAttribute(attribute, value);
    }
    return restoreMap;
}

From source file:com.sinosoft.one.mvc.web.impl.thread.RootEngine.java

/**
 * Keep a snapshot of the request attributes in case of an include, to
 * be able to restore the original attributes after the include.
 * /*from   w  w w .  ja  va  2s .  co m*/
 * @param inv
 */
private void saveAttributesBeforeInclude(final Invocation inv) {
    ServletRequest request = inv.getRequest();
    logger.debug("Taking snapshot of request attributes before include");
    Map<String, Object> attributesSnapshot = new HashMap<String, Object>();
    Enumeration<?> attrNames = request.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attrName = (String) attrNames.nextElement();
        attributesSnapshot.put(attrName, request.getAttribute(attrName));
    }
    inv.setAttribute("$$one-mvc.attributesBeforeInclude", attributesSnapshot);
}

From source file:net.paoding.rose.web.impl.thread.RootEngine.java

/**
 * Keep a snapshot of the request attributes in case of an include, to
 * be able to restore the original attributes after the include.
 * // w ww  . ja v a2  s .  c om
 * @param inv
 */
private void saveAttributesBeforeInclude(final Invocation inv) {
    ServletRequest request = inv.getRequest();
    logger.debug("Taking snapshot of request attributes before include");
    Map<String, Object> attributesSnapshot = new HashMap<String, Object>();
    Enumeration<?> attrNames = request.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attrName = (String) attrNames.nextElement();
        attributesSnapshot.put(attrName, request.getAttribute(attrName));
    }
    inv.setAttribute("$$paoding-rose.attributesBeforeInclude", attributesSnapshot);
}

From source file:com.laxser.blitz.web.impl.thread.RootEngine.java

/**
 * Keep a snapshot of the request attributes in case of an include, to
 * be able to restore the original attributes after the include.
 * //from  w ww .  j  av a 2  s  .  co  m
 * @param inv
 */
private void saveAttributesBeforeInclude(final Invocation inv) {
    ServletRequest request = inv.getRequest();
    logger.debug("Taking snapshot of request attributes before include");
    Map<String, Object> attributesSnapshot = new HashMap<String, Object>();
    Enumeration<?> attrNames = request.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attrName = (String) attrNames.nextElement();
        attributesSnapshot.put(attrName, request.getAttribute(attrName));
    }
    inv.setAttribute("$$paoding-blitz.attributesBeforeInclude", attributesSnapshot);
}

From source file:org.opencms.util.CmsRequestUtil.java

/**
 * Returns a map with all request attributes.<p>
 * //from   w w w.j a va 2s .c  o m
 * @param req the request
 * 
 * @return the attribute map
 */
public static Map<String, Object> getAtrributeMap(ServletRequest req) {

    if (req instanceof CmsFlexRequest) {
        return ((CmsFlexRequest) req).getAttributeMap();
    }
    Map<String, Object> attrs = new HashMap<String, Object>();
    Enumeration<String> atrrEnum = CmsCollectionsGenericWrapper.enumeration(req.getAttributeNames());
    while (atrrEnum.hasMoreElements()) {
        String key = atrrEnum.nextElement();
        Object value = req.getAttribute(key);
        attrs.put(key, value);
    }
    return attrs;
}

From source file:org.rhq.enterprise.gui.legacy.util.RequestUtils.java

public static String dumpRequestAttributesToString(ServletRequest request) {
    StringBuffer output = new StringBuffer();
    for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        Object value = request.getAttribute(name);
        output.append("name = '" + name + "'\n");
        output.append("type = '" + value.getClass().getCanonicalName() + "'\n");
        output.append("value = '" + value.toString() + "'\n\n");
    }/*from   ww w  . ja  v  a  2  s  .c o  m*/

    return output.toString();
}

From source file:org.yes.cart.web.support.util.HttpUtil.java

private static void dumpParamsAndAttrs(final ServletRequest req, final StringBuilder stringBuilder) {
    final Enumeration parameterNames = req.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        final String key = (String) parameterNames.nextElement();
        stringBuilder.append(MessageFormat.format("\nParameter {0}={1}", key, req.getParameter(key)));
    }//  w  ww. j av  a 2  s  .  co m
    Enumeration attributeNames = req.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        final String key = (String) attributeNames.nextElement();
        stringBuilder.append(MessageFormat.format("\nAttribute {0}={1}", key, req.getAttribute(key)));
    }
}