package net.morimekta.providence.rpc.handler;

import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;

import java.io.IOException;
import java.util.Map;

/**
 * Request initializer that set's a set of fixed headers.
 */
public class SetHeadersInitializer implements HttpRequestInitializer {
    private final Map<String, String> headers;

    public SetHeadersInitializer(Map<String, String> headers) {
        this.headers = headers;
    }

    @Override
    public void initialize(HttpRequest request) throws IOException {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            switch (entry.getKey()
                         .toLowerCase()) {
                case "authorization":
                    request.getHeaders()
                           .setAuthorization(entry.getValue());
                    break;
                default:
                    request.getHeaders()
                           .set(entry.getKey(), entry.getValue());
                    break;
            }
        }
    }
}

@@ -36,24 +36,71 @@
 c.handler;%0A%0A
+import com.google.api.client.http.HttpHeaders;%0A
 import com.g
@@ -283,9 +283,8 @@
  set
-'
 s a 
@@ -614,16 +614,65 @@
 ption %7B%0A
+        HttpHeaders http = request.getHeaders();%0A
         
@@ -748,214 +748,629 @@
     
-switch (entry.getKey()%0A                         .toLowerCase()) %7B%0A                case %22authorization%22:%0A                    request.getHeaders()%0A                           .setAuthorization(entry.getV
+String value = entry.getValue();%0A            switch (entry.getKey().toLowerCase()) %7B%0A                case %22accept%22:%0A                    http.setAccept(value);%0A                    break;%0A                case %22accept-encoding%22:%0A                    http.setAcceptEncoding(value);%0A                    break;%0A                case %22authorization%22:%0A                    http.setAuthorization(value);%0A                    break;%0A                case %22content-encoding%22:%0A                    http.setContentEncoding(value);%0A                    break;%0A                case %22content-type%22:%0A                    http.setContentType(v
 alue
-()
 );%0A 
@@ -1415,15 +1415,25 @@
     
-default
+case %22user-agent%22
 :%0A  
@@ -1454,37 +1454,86 @@
     
-request.getHeaders()%0A        
+http.setUserAgent(value);%0A                    break;%0A                default:%0A
     
@@ -1547,16 +1547,21 @@
         
+ http
 .set(ent
@@ -1573,32 +1573,21 @@
 tKey(), 
-entry.getV
+v
 alue
-()
 );%0A     

Handle more special cases for headers.