package com.brettonw.bag;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class SourceAdapterHttp extends SourceAdapter {
    private static final Logger log = LogManager.getLogger (SourceAdapterHttp.class);

    private static final String UTF_8 = StandardCharsets.UTF_8.name ();

    public SourceAdapterHttp (String urlString) throws IOException {
        this (new URL (urlString));
    }

    public SourceAdapterHttp (URL url) throws IOException {
        this (url, null, null);
    }

    public SourceAdapterHttp (String urlString, Bag postData, String postDataMimeType) throws IOException {
        this (new URL (urlString), postData, postDataMimeType);
    }

    public SourceAdapterHttp (URL url, Bag postData, String postDataMimeType) throws IOException {
        // create the connection, see if it was successful
        HttpURLConnection connection = (HttpURLConnection) url.openConnection ();
        if (connection != null) {
            // don't use the caches
            connection.setUseCaches(false);

            // set up the request, POST if there is post data, otherwise, GET
            if (postData != null) {
                // prepare the post data
                String postDataString = postData.toString (postDataMimeType);
                byte[] postDataBytes = postDataString.getBytes ();

                // setup the headers
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", postDataMimeType + ";charset=UTF-8"); // "application/json"
                connection.setRequestProperty("Content-Length", Integer.toString(postDataBytes.length));

                // write out the request data
                connection.setDoOutput (true);
                OutputStream outputStream = connection.getOutputStream();
                DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                dataOutputStream.write(postDataBytes);
                dataOutputStream.close();
            } else {
                // setup the header
                connection.setRequestMethod("GET");
            }

            // get the response type, tease out the response type and the charset (if given) -
            // default to UTF-8 if it's not given, because that's what it will be in Java
            String[] contentType = connection.getHeaderField("Content-Type").replace(" ", "").split(";");
            mimeType = contentType[0];
            String charset = (contentType.length > 1) ? (contentType[1].split ("=", 2)[1]) : UTF_8;

            // get the response data
            InputStream inputStream = connection.getInputStream();
            Reader inputStreamReader = new InputStreamReader (inputStream, charset);
            stringData = readString (inputStreamReader);
            connection.disconnect();
        }
    }
}

@@ -20,16 +20,58 @@
 w.bag;%0A%0A
+import com.brettonw.bag.formats.MimeType;%0A
 import o
@@ -2359,37 +2359,130 @@
 type
-, tease out the response type
+ (this will trigger the actual fetch), then tease out the%0A            // response type (use a default if it's not present)
  and
@@ -2507,11 +2507,9 @@
 iven
-) -
+,
 %0A   
@@ -2519,16 +2519,26 @@
       //
+ otherwise
  default
@@ -2550,26 +2550,8 @@
 TF-8
- if it's not given
 , be
@@ -2586,16 +2586,17 @@
  in Java
+)
 %0A       
@@ -2606,18 +2606,16 @@
   String
-%5B%5D
  content
@@ -2614,24 +2614,30 @@
  contentType
+Header
  = connectio
@@ -2672,16 +2672,196 @@
 pe%22)
+;%0A            String charset = UTF_8;%0A            mimeType = MimeType.DEFAULT;%0A            if (contentTypeHeader != null) %7B%0A                String%5B%5D contentType = contentTypeHeader
 .replace
 (%22 %22
@@ -2856,16 +2856,17 @@
 .replace
+ 
 (%22 %22, %22%22
@@ -2876,15 +2876,20 @@
 plit
+ 
 (%22;%22);%0A
+    
     
@@ -2935,32 +2935,22 @@
         
-String charset =
+    if
  (conten
@@ -2971,53 +2971,105 @@
  1) 
-? (contentType%5B1%5D.split (%22=%22, 2)%5B1%5D) : UTF_8;
+%7B%0A                    charset = contentType%5B1%5D.split (%22=%22, 2)%5B1%5D;%0A                %7D%0A            %7D
 %0A%0A  

use defaults on the mime type of a web server response if the website fails to set them (I'm looking at you Dominick).