/*
 * Copyright 2013 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.appengine.tck.taskqueue.support;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;


/**
 * @author <a href="mailto:mluksa@redhat.com">Marko Luksa</a>
 */
public class RequestData implements Serializable {

    private byte[] body;
    private Map<String, String> headers = new HashMap<String, String>();

    public RequestData(HttpServletRequest req) throws IOException {
        storeHeaders(req);
        storeBody(req);
    }

    private void storeHeaders(HttpServletRequest req) {
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            headers.put(header, req.getHeader(header));
        }
    }

    private void storeBody(HttpServletRequest req) throws IOException {
        ServletInputStream in = req.getInputStream();
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            copyStream(in, baos);
            body = baos.toByteArray();
        } finally {
            in.close();
        }
    }

    public String getHeader(String name) {
        return headers.get(name);
    }

    public byte[] getBody() {
        return body;
    }

    /**
     * Copy stream.
     *
     * @param in  the input stream
     * @param out the output stream
     * @throws IOException for any IO error
     */
    private void copyStream(final InputStream in, final OutputStream out) throws IOException {
        final byte[] bytes = new byte[8192];
        int cnt;
        while ((cnt = in.read(bytes)) != -1) {
            out.write(bytes, 0, cnt);
        }
    }
}

@@ -857,20 +857,16 @@
 va.util.
-Hash
 Map;%0Aimp
@@ -875,24 +875,28 @@
 t java.util.
+Tree
 Map;%0A%0Aimport
@@ -1099,17 +1099,16 @@
 zable %7B%0A
-%0A
     priv
@@ -1174,32 +1174,47 @@
 new 
-Hash
+Tree
 Map%3C
+%3E(
 String
-, String%3E(
+.CASE_INSENSITIVE_ORDER
 );%0A%0A
@@ -1331,32 +1331,67 @@
 dy(req);%0A    %7D%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     private void
@@ -1627,16 +1627,30 @@
 t(header
+.toLowerCase()
 , req.ge
@@ -1764,16 +1764,21 @@
         
+try (
 ServletI
@@ -1817,21 +1817,9 @@
 am()
-;%0A        try
+)
  %7B%0A 
@@ -1921,16 +1921,16 @@
  baos);%0A
+
         
@@ -1964,52 +1964,8 @@
 ();%0A
-        %7D finally %7B%0A            in.close();%0A
     

Normalize headers; ignore case.