/*
 * Copyright (c) 2015 Spotify AB.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.spotify.heroic.http;

import lombok.Data;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;

@Data
public class CorsResponseFilter implements ContainerResponseFilter {
    private final String corsAllowOrigin;

    public void filter(
        ContainerRequestContext requestContext, ContainerResponseContext responseContext
    ) throws IOException {
        final MultivaluedMap<String, Object> headers = responseContext.getHeaders();
        headers.add("Access-Control-Allow-Origin", corsAllowOrigin);
        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        headers.add("Access-Control-Allow-Headers", "Content-Type");
    }
}

@@ -1469,16 +1469,384 @@
 ders();%0A
+%0A        if (requestContext.getMethod().equalsIgnoreCase(%22OPTIONS%22)) %7B%0A            final String request = requestContext.getHeaderString(%22Access-Control-Request-Headers%22);%0A            final String method = requestContext.getHeaderString(%22Access-Control-Request-Method%22);%0A%0A            if (request == null %7C%7C method == null) %7B%0A                return;%0A            %7D%0A%0A    
         
@@ -1906,32 +1906,36 @@
 rigin);%0A        
+ 
+   
 headers.add(%22Acc
@@ -1966,35 +1966,21 @@
 s%22, 
-%22GET, POST, DELETE, PUT%22);%0A
+method);%0A    
     
@@ -2031,24 +2031,117 @@
 s%22, 
-%22Content-Type%22);
+request);%0A        %7D else %7B%0A            headers.add(%22Access-Control-Allow-Origin%22, corsAllowOrigin);%0A        %7D
 %0A   

make CORS filter respond with what was requested