package guru.nidi.ramlproxy;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ReponseMetaData {
    private final int code;
    private final Map<String, List<Object>> headers;

    public ReponseMetaData(int code, Map<String, List<Object>> headers) {
        this.code = code;
        this.headers = headers;
    }

    public ReponseMetaData(Map<String, Object> raw) {
        code = raw.get("code") == null ? HttpServletResponse.SC_OK : (Integer) raw.get("code");
        headers = new HashMap<>();
        final Map<String, Object> rawHeaders = (Map<String, Object>) raw.get("headers");
        if (rawHeaders != null) {
            for (Map.Entry<String, Object> entry : rawHeaders.entrySet()) {
                if (entry.getValue() instanceof List) {
                    headers.put(entry.getKey(), (List<Object>) entry.getValue());
                } else {
                    headers.put(entry.getKey(), Collections.singletonList(entry.getValue()));
                }
            }
        }
    }

    public void apply(HttpServletResponse response) {
        response.setStatus(code);
        for (Map.Entry<String, List<Object>> entry : headers.entrySet()) {
            for (Object value : entry.getValue()) {
                response.addHeader(entry.getKey(), "" + value);
            }
        }
    }
}

@@ -1,8 +1,634 @@
+/*%0A * Copyright (C) 2014 Stefan Niederhauser (nidin@gmx.ch)%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *         http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 

add license header