/*
 * Copyright 2016 Luca Capra <lcapra@create-net.org>.
 *
 * 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 org.createnet.raptor.http.filter;

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

/**
 *
 * @author Luca Capra <lcapra@create-net.org>
 */
public class CORSResponseFilter implements ContainerResponseFilter {

  @Override
  public void filter(ContainerRequestContext request, ContainerResponseContext response)
          throws IOException {

    response.getHeaders().add("X-Powered-By", "Raptor");    
    
    response.getHeaders().add("Access-Control-Allow-Origin", "*");
    response.getHeaders().add("Access-Control-Allow-Headers",
            "origin, content-type, accept, authorization");
    response.getHeaders().add("Access-Control-Allow-Credentials", "true");
    response.getHeaders().add("Access-Control-Allow-Methods",
            "GET, POST, PUT, DELETE, OPTIONS, HEAD");

    String reqHead = request.getHeaderString("Access-Control-Request-Headers");
    if (null != reqHead && reqHead.length() > 0) {
      response.getHeaders().add("Access-Control-Allow-Headers", reqHead);
    }

  }
}

@@ -1164,21 +1164,83 @@
 aptor%22);
+%0A%0A
     
-%0A
+if (request.getMethod().toUpperCase().equals(%22OPTIONS%22)) %7B%0A  
     %0A   
@@ -1236,24 +1236,26 @@
 %0A      %0A    
+ 
+ 
 response.get
@@ -1301,32 +1301,34 @@
 gin%22, %22*%22);%0A    
+  
 response.getHead
@@ -1372,69 +1372,17 @@
 rs%22,
-%0A            %22origin, content-type, accept, authorization
+ %22*
 %22);%0A
+  
     
@@ -1452,24 +1452,26 @@
 true%22);%0A    
+ 
+ 
 response.get
@@ -1519,20 +1519,8 @@
 ds%22,
-%0A           
  %22GE
@@ -1562,37 +1562,22 @@
 %22);%0A
-%0A
     
-String reqHead = request
+  response
 .get
@@ -1586,14 +1586,15 @@
 ader
-String
+s().add
 (%22Ac
@@ -1610,110 +1610,24 @@
 rol-
-Request-Headers%22);%0A    if (null != reqHead && reqHead.length() %3E 0) %7B%0A      response.getHeaders().add(
+Allow-Headers%22, 
 %22Acc
@@ -1656,20 +1656,12 @@
 ers%22
-, reqHead
 );%0A
+%0A
     

improve on cors req