Example usage for org.springframework.mock.web MockHttpServletRequest getAttributeNames

List of usage examples for org.springframework.mock.web MockHttpServletRequest getAttributeNames

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest getAttributeNames.

Prototype

@Override
    public Enumeration<String> getAttributeNames() 

Source Link

Usage

From source file:org.cateproject.test.functional.mockmvc.HtmlUnitRequestBuilder.java

private void parent(MockHttpServletRequest result, RequestBuilder parent) {
    if (parent == null) {
        return;//from   www  .j  a v  a 2s .co m
    }
    MockHttpServletRequest parentRequest = parent.buildRequest(result.getServletContext());

    // session
    HttpSession parentSession = parentRequest.getSession(false);
    if (parentSession != null) {
        Enumeration<String> attrNames = parentSession.getAttributeNames();
        while (attrNames.hasMoreElements()) {
            String attrName = attrNames.nextElement();
            Object attrValue = parentSession.getAttribute(attrName);
            result.getSession().setAttribute(attrName, attrValue);
        }
    }

    // header
    Enumeration<String> headerNames = parentRequest.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String attrName = headerNames.nextElement();
        Enumeration<String> attrValues = parentRequest.getHeaders(attrName);
        while (attrValues.hasMoreElements()) {
            String attrValue = attrValues.nextElement();
            result.addHeader(attrName, attrValue);
        }
    }

    // parameter
    Map<String, String[]> parentParams = parentRequest.getParameterMap();
    for (Map.Entry<String, String[]> parentParam : parentParams.entrySet()) {
        String paramName = parentParam.getKey();
        String[] paramValues = parentParam.getValue();
        result.addParameter(paramName, paramValues);
    }

    // cookie
    Cookie[] parentCookies = parentRequest.getCookies();
    if (parentCookies != null) {
        result.setCookies(parentCookies);
    }

    // request attribute
    Enumeration<String> parentAttrNames = parentRequest.getAttributeNames();
    while (parentAttrNames.hasMoreElements()) {
        String parentAttrName = parentAttrNames.nextElement();
        result.setAttribute(parentAttrName, parentRequest.getAttribute(parentAttrName));
    }
}