public static EchoResponse response(HttpServletRequest request, boolean withBody) {
        EchoResponse response = new EchoResponse();
        response.setMethod(request.getMethod());
        if (request.getQueryString() != null) {
            String[] normalisedQueryString = request.getQueryString().split("&");
            Arrays.sort(normalisedQueryString);
            response.setResource(request.getRequestURI() + "?" + SimpleStringUtils.join("&", normalisedQueryString));
        } else {
            response.setResource(request.getRequestURI());
        }
        response.setUri(request.getRequestURI());
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String value = request.getHeader(name);
            response.getHeaders().put(name, value);
        }
        if (withBody) {
            long totalBytes = 0;
            InputStream is = null;
            try {
                is = request.getInputStream();
                MessageDigest sha1 = MessageDigest.getInstance("SHA1");
                byte[] data = new byte[1024];
                int read;
                while ((read = is.read(data)) != -1) {
                    sha1.update(data, 0, read);
                    totalBytes += read;
                };

                byte[] hashBytes = sha1.digest();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.length; i++) {
                  sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
                }
                String fileHash = sb.toString();

                response.setBodyLength(totalBytes);
                response.setBodySha1(fileHash);
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
        return response;
    }
Create an echo response from the inbound information in the http server
request.
@param request the request
@param withBody if request is with body
@return a new echo response
test/common/src/main/java/io/apiman/test/common/mock/EchoServlet.java