package com.google.apigee.callout.jwtsigned;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class HTTPUtil {

  public static String getResponsePayload(HttpURLConnection conn) throws Exception {

    String content = "";
    try (BufferedReader rd =
        new BufferedReader(
            new InputStreamReader(
                (conn.getResponseCode() >= 400) ? conn.getErrorStream() : conn.getInputStream()))) {
      for (String line = rd.readLine(); line != null; line = rd.readLine()) {
        content += line;
      }
    }
    return content;
  }

  public static Map<String, Object> post(
      String urlToPost, Map<String, String> formdata, Map<String, String> headers)
      throws Exception {
    HashMap<String, Object> result = new HashMap<String, Object>();

    URL siteUrl = new URL(urlToPost);

    HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
    conn.setRequestMethod("POST");

    String content = "";
    if (formdata != null) {
      content =
          formdata.keySet().stream()
              .map(
                  key -> {
                    try {
                      return key + "=" + URLEncoder.encode(formdata.get(key), "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                      throw new RuntimeException(e);
                    }
                  })
              .collect(Collectors.joining("&"));
    }

    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", "" + Integer.toString(content.getBytes().length));
    conn.setRequestProperty("Accept", "*/*");
    conn.setRequestProperty("Accept-Charset", "UTF-8");

    if (headers != null) {
      for (String key : headers.keySet()) {
        conn.setRequestProperty(key, headers.get(key));
      }
    }

    conn.setInstanceFollowRedirects(false);
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.connect();

    try (DataOutputStream out = new DataOutputStream(conn.getOutputStream())) {
      out.writeBytes(content);
    }

    int code = conn.getResponseCode();
    result.put("code", new Integer(code));
    result.put("headers", conn.getHeaderFields()); // Map<String, List<String>>
    result.put("content", getResponsePayload(conn));
    conn.disconnect();
    return result;
  }

  Map<String, Object> get(String urlToRead) throws Exception {
    HashMap<String, Object> result = new HashMap<String, Object>();
    URL url = new URL(urlToRead);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setInstanceFollowRedirects(false);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept-Charset", "UTF-8");
    conn.connect();

    int code = conn.getResponseCode();
    result.put("code", new Integer(code));
    result.put("headers", conn.getHeaderFields()); // Map<String, List<String>>
    result.put("content", getResponsePayload(conn)); // String
    conn.disconnect();
    return result;
  }

}

@@ -2599,16 +2599,33 @@
 %0A  %7D%0A%0A  
+// public static 
 Map%3CStri
@@ -2670,32 +2670,35 @@
 ws Exception %7B%0A 
+ //
    HashMap%3CStrin
@@ -2749,16 +2749,19 @@
 ct%3E();%0A 
+ //
    URL u
@@ -2786,16 +2786,19 @@
 Read);%0A 
+ //
    HttpU
@@ -2852,32 +2852,35 @@
 nConnection();%0A 
+ //
    conn.setInsta
@@ -2899,32 +2899,35 @@
 irects(false);%0A 
+ //
    conn.setReque
@@ -2940,24 +2940,27 @@
 od(%22GET%22);%0A 
+ //
    conn.setR
@@ -2999,24 +2999,27 @@
  %22UTF-8%22);%0A 
+ //
    conn.conn
@@ -3025,18 +3025,25 @@
 nect();%0A
-%0A
  
+ //%0A  //
    int c
@@ -3065,32 +3065,35 @@
 esponseCode();%0A 
+ //
    result.put(%22c
@@ -3111,32 +3111,35 @@
 nteger(code));%0A 
+ //
    result.put(%22h
@@ -3194,32 +3194,35 @@
  List%3CString%3E%3E%0A 
+ //
    result.put(%22c
@@ -3269,18 +3269,21 @@
 tring%0A  
+//
   
+ 
 conn.dis
@@ -3286,32 +3286,35 @@
 .disconnect();%0A 
+ //
    return result
@@ -3312,18 +3312,21 @@
 rn result;%0A 
+ //
  %7D%0A%0A%7D%0A

update HTTPUtil to exclude unused methods