Example usage for javax.servlet.http HttpServletRequest getAttributeNames

List of usage examples for javax.servlet.http HttpServletRequest getAttributeNames

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest 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:ubic.gemma.web.util.RequestUtil.java

/**
 * Stores request attributes in session//from  ww w.j  a  v  a  2 s  .  c o  m
 *
 * @param aRequest the current request
 */
public static void stowRequestAttributes(HttpServletRequest aRequest) {
    if (aRequest.getSession().getAttribute(RequestUtil.STOWED_REQUEST_ATTRIBS) != null) {
        return;
    }

    Enumeration<String> e = aRequest.getAttributeNames();
    Map<String, Object> map = new HashMap<>();

    while (e.hasMoreElements()) {
        String name = e.nextElement();
        map.put(name, aRequest.getAttribute(name));
    }

    aRequest.getSession().setAttribute(RequestUtil.STOWED_REQUEST_ATTRIBS, map);
}

From source file:com.threewks.thundr.request.servlet.ServletSupport.java

/**
 * Set the request attributes to the given set.
 * //from w  w  w.ja  v  a 2s . c  o m
 * @param request
 * @param attributes
 */
public static void setAttributes(HttpServletRequest request, Map<String, Object> attributes) {
    if (request != null) {
        List<String> existing = list(iterable(request.getAttributeNames()));
        List<String> toRemove = list(existing).removeItems(attributes.keySet());
        for (String remove : toRemove) {
            request.removeAttribute(remove);
        }
        addAttributes(request, attributes);
    }
}

From source file:com.threewks.thundr.request.servlet.ServletSupport.java

/**
 * Get the full set of request attirubtes in the given request.
 * //w  ww . j a v  a  2  s. com
 * @param request
 * @return
 */
public static Map<String, Object> getAttributes(HttpServletRequest request) {
    Map<String, Object> attributes = new HashMap<String, Object>();
    if (request != null) {
        Enumeration<String> names = request.getAttributeNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            attributes.put(name, request.getAttribute(name));
        }
    }
    return attributes;
}

From source file:org.qifu.ui.UIComponentValueUtils.java

public static Object getOgnlProcessObjectFromPageContextOrRequest(PageContext pageContext, String expression) {
    Map<String, Object> ognlRoot = new HashMap<String, Object>();
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    Enumeration<String> pcNames = pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
    Enumeration<String> pNames = request.getParameterNames();
    Enumeration<String> aNames = request.getAttributeNames();
    /**/*from  w  w  w . java 2 s .c om*/
     * ognlRoot , ?: pageContext.getAttribute > request.getParameter > request.getAttribute
     */
    while (pcNames.hasMoreElements()) {
        String key = pcNames.nextElement();
        ognlRoot.put(key, pageContext.getAttribute(key));
    }
    while (pNames.hasMoreElements()) {
        String key = pNames.nextElement();
        if (ognlRoot.get(key) == null) {
            ognlRoot.put(key, request.getParameter(key));
        }
    }
    while (aNames.hasMoreElements()) {
        String key = aNames.nextElement();
        if (ognlRoot.get(key) == null) {
            ognlRoot.put(key, request.getAttribute(key));
        }
    }
    if (ognlRoot.size() == 0) {
        ognlRoot = null;
        return null;
    }
    Object val = null;
    try {
        val = Ognl.getValue(expression, ognlRoot);
    } catch (OgnlException e) {
        //e.printStackTrace();
    }
    ognlRoot.clear();
    ognlRoot = null;
    return val;
}

From source file:org.craftercms.commons.http.HttpUtils.java

/**
 * Creates a map from the request attributes in the specified request.
 *
 * @param request the request//ww w.java2  s.c  o  m
 */
public static Map<String, Object> createRequestAttributesMap(HttpServletRequest request) {
    Map<String, Object> attributesMap = new HashMap<>();
    for (Enumeration attributeNameEnum = request.getAttributeNames(); attributeNameEnum.hasMoreElements();) {
        String attributeName = (String) attributeNameEnum.nextElement();

        attributesMap.put(attributeName, request.getAttribute(attributeName));
    }

    return attributesMap;
}

From source file:org.jahia.services.applications.pluto.JahiaPortletUtil.java

/**
 * Remove from request useless attributes
 *
 * @param portalRequest/*from   w  w  w .  ja  v  a2  s  .  c  om*/
 * @return
 */
public static Map<String, Object> filterJahiaAttributes(HttpServletRequest portalRequest) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    @SuppressWarnings("unchecked")
    List<String> names = EnumerationUtils.toList(portalRequest.getAttributeNames());
    for (String key : names) {
        if (isSpringAttribute(key)) {
            Object value = portalRequest.getAttribute(key);
            map.put(key, value);
            portalRequest.removeAttribute(key);
        }
    }

    return map;
}

From source file:in.xebia.poc.FileUploadUtils.java

public static boolean parseFileUploadRequest(HttpServletRequest request, File outputFile,
        Map<String, String> params) throws Exception {
    log.debug("Request class? " + request.getClass().toString());
    log.debug("Request is multipart? " + ServletFileUpload.isMultipartContent(request));
    log.debug("Request method: " + request.getMethod());
    log.debug("Request params: ");
    for (Object key : request.getParameterMap().keySet()) {
        log.debug((String) key);//  w w  w. j  a v  a 2 s  .c o  m
    }
    log.debug("Request attribute names: ");

    boolean filedataInAttributes = false;
    Enumeration attrNames = request.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attrName = (String) attrNames.nextElement();
        log.debug(attrName);
        if ("filedata".equals(attrName)) {
            filedataInAttributes = true;
        }
    }

    if (filedataInAttributes) {
        log.debug("Found filedata in request attributes, getting it out...");
        log.debug("filedata class? " + request.getAttribute("filedata").getClass().toString());
        FileItem item = (FileItem) request.getAttribute("filedata");
        item.write(outputFile);
        for (Object key : request.getParameterMap().keySet()) {
            params.put((String) key, request.getParameter((String) key));
        }
        return true;
    }

    /*ServletFileUpload upload = new ServletFileUpload();
    //upload.setSizeMax(Globals.MAX_UPLOAD_SIZE);
    FileItemIterator iter = upload.getItemIterator(request);
    while(iter.hasNext()){
       FileItemStream item = iter.next();
       InputStream stream = item.openStream();
               
       //If this item is a file
       if(!item.isFormField()){
             
     log.debug("Found non form field in upload request with field name = " + item.getFieldName());
             
     String name = item.getName();
     if(name == null){
         throw new Exception("File upload did not have filename specified");
     }
             
        // Some browsers, including IE, return the full path so trim off everything but the file name
        name = getFileNameFromPath(name);
                 
    //Enforce required file extension, if present
    if(!name.toLowerCase().endsWith( ".zip" )){
       throw new Exception("File uploaded did not have required extension .zip");
    }
            
      bufferedCopyStream(stream, new FileOutputStream(outputFile));
       }
       else {
    params.put(item.getFieldName(), Streams.asString(stream));
       }
    }
    return true;*/

    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Parse the request
    List /* FileItem */ items = upload.parseRequest(request);

    // Process the uploaded items
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (!item.isFormField()) {
            log.debug("Found non form field in upload request with field name = " + item.getFieldName());

            String name = item.getName();
            if (name == null) {
                throw new Exception("File upload did not have filename specified");
            }

            // Some browsers, including IE, return the full path so trim off everything but the file name
            name = getFileNameFromPath(name);

            item.write(outputFile);
        } else {
            params.put(item.getFieldName(), item.getString());
        }
    }
    return true;
}

From source file:com.bsb.cms.commons.web.MossActionUtils.java

/**
 * ?? ???<s:debug></s:debug> 
 * /*w w w  . jav  a  2 s  .c o m*/
 * @param req
 */
@SuppressWarnings("all")
@Deprecated
public static void print(HttpServletRequest req) {
    // Application
    Map<String, Object> parameters = new WeakHashMap<String, Object>();

    // attributes in scope RequestParameter
    for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        String[] v = req.getParameterValues(name);
        if (v.length == 1) {
            if (v[0].equals(""))
                continue;
            parameters.put(name, v[0]);
        } else
            parameters.put(name, v);
    }

    // attributes in scope Request
    for (Enumeration e = req.getAttributeNames(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        Object v = req.getAttribute(name);
        parameters.put(name, v);
    }

    // attributes in scope Session
    HttpSession session = req.getSession();
    for (Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        Object v = session.getAttribute(name);
        parameters.put(name, v);
    }

    Set keys = parameters.keySet();
    Iterator it = keys.iterator();
    String key;
    Object value;
    while (it.hasNext()) {
        key = (String) it.next();
        value = parameters.get(key);
        System.out.println("key:" + key + ", value:" + value);
    }

}

From source file:com.feilong.servlet.http.RequestUtil.java

/**
 * ??requestattribute, name /attributeValue map(TreeMap).
 * /*  w  w  w.ja  v a  2 s. c om*/
 * 
 * <h3>?:</h3>
 * 
 * <blockquote>
 * <p style="color:red">
 * 1.??,json,?, {@link JsonUtil#formatSimpleMap(Map, Class...) }
 * </p>
 * 
 * <p>
 * 2.??mapremove?,??request Attribute
 * </p>
 * </blockquote>
 * 
 * @param request
 *            the request
 * @return {@link javax.servlet.ServletRequest#getAttributeNames()} nullempty,{@link Collections#emptyMap()}<br>
 */
public static Map<String, Object> getAttributeMap(HttpServletRequest request) {
    Enumeration<String> attributeNames = request.getAttributeNames();
    if (isNullOrEmpty(attributeNames)) {
        return emptyMap();
    }

    Map<String, Object> map = new TreeMap<>();
    while (attributeNames.hasMoreElements()) {
        String name = attributeNames.nextElement();
        map.put(name, getAttribute(request, name));
    }
    return map;
}

From source file:org.apache.qpid.server.management.plugin.HttpManagementUtil.java

public static Subject tryToAuthenticate(HttpServletRequest request,
        HttpManagementConfiguration managementConfig) {
    Subject subject = null;//  www.ja v a2 s  . com
    SocketAddress localAddress = getSocketAddress(request);
    final AuthenticationProvider authenticationProvider = managementConfig
            .getAuthenticationProvider(localAddress);
    SubjectCreator subjectCreator = authenticationProvider.getSubjectCreator(request.isSecure());
    String remoteUser = request.getRemoteUser();

    if (remoteUser != null || authenticationProvider instanceof AnonymousAuthenticationManager) {
        subject = authenticateUser(subjectCreator, remoteUser, null);
    } else if (authenticationProvider instanceof ExternalAuthenticationManager && Collections
            .list(request.getAttributeNames()).contains("javax.servlet.request.X509Certificate")) {
        Principal principal = null;
        X509Certificate[] certificates = (X509Certificate[]) request
                .getAttribute("javax.servlet.request.X509Certificate");
        if (certificates != null && certificates.length != 0) {
            principal = certificates[0].getSubjectX500Principal();

            if (!Boolean.valueOf(String.valueOf(authenticationProvider
                    .getAttribute(ExternalAuthenticationManager.ATTRIBUTE_USE_FULL_DN)))) {
                String username;
                String dn = ((X500Principal) principal).getName(X500Principal.RFC2253);

                username = SSLUtil.getIdFromSubjectDN(dn);
                principal = new UsernamePrincipal(username);
            }

            subject = subjectCreator.createSubjectWithGroups(new AuthenticatedPrincipal(principal));
        }
    } else {
        String header = request.getHeader("Authorization");
        if (header != null) {
            String[] tokens = header.split("\\s");
            if (tokens.length >= 2 && "BASIC".equalsIgnoreCase(tokens[0])) {
                boolean isBasicAuthSupported = false;
                if (request.isSecure()) {
                    isBasicAuthSupported = managementConfig.isHttpsBasicAuthenticationEnabled();
                } else {
                    isBasicAuthSupported = managementConfig.isHttpBasicAuthenticationEnabled();
                }
                if (isBasicAuthSupported) {
                    String base64UsernameAndPassword = tokens[1];
                    String[] credentials = (new String(
                            Base64.decodeBase64(base64UsernameAndPassword.getBytes()))).split(":", 2);
                    if (credentials.length == 2) {
                        subject = authenticateUser(subjectCreator, credentials[0], credentials[1]);
                    }
                }
            }
        }
    }
    return subject;
}