OSDN Git Service

Theta V に対応させる。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / utils / SimpleHttpClient.java
index 2817e7f..a064758 100644 (file)
@@ -12,8 +12,11 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.List;
+import java.util.Map;
 
 import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 
 /**
  *
@@ -128,9 +131,36 @@ public class SimpleHttpClient
      *
      *
      */
-    public static void httpGetBytes(String url, int timeoutMs, @NonNull IReceivedMessageCallback callback)
+    public static void httpGetBytes(String url, Map<String, String> setProperty, int timeoutMs, @NonNull IReceivedMessageCallback callback)
+    {
+        httpCommandBytes(url, "GET", null, setProperty, null, timeoutMs, callback);
+    }
+
+    /**
+     *
+     *
+     *
+     */
+    public static void httpPostBytes(String url, String postData, Map<String, String> setProperty, int timeoutMs, @NonNull IReceivedMessageCallback callback)
+    {
+        httpCommandBytes(url, "POST", postData, setProperty, null, timeoutMs, callback);
+    }
+
+    /**
+     *
+     *
+     *
+     */
+    public static void httpPostBytesWithHeader(String url, String postData, Map<String, String> setProperty, @Nullable String contentType, int timeoutMs, @NonNull IReceivedMessageCallback callback)
+    {
+        httpCommandBytes(url, "POST", postData, setProperty, contentType, timeoutMs, callback);
+    }
+
+    private static void httpCommandBytes(@NonNull String url, @NonNull String requestMethod, @Nullable String postData, @Nullable Map<String, String> setProperty, @Nullable String contentType, int timeoutMs, @NonNull IReceivedMessageCallback callback)
     {
         HttpURLConnection httpConn = null;
+        OutputStream outputStream = null;
+        OutputStreamWriter writer = null;
         InputStream inputStream = null;
         int timeout = timeoutMs;
         if (timeoutMs < 0)
@@ -138,15 +168,43 @@ public class SimpleHttpClient
             timeout = DEFAULT_TIMEOUT;
         }
 
-        //  HTTP GETメソッドで要求を投げる
+        //  HTTP メソッドで要求を送出
         try
         {
             final URL urlObj = new URL(url);
             httpConn = (HttpURLConnection) urlObj.openConnection();
-            httpConn.setRequestMethod("GET");
+            httpConn.setRequestMethod(requestMethod);
+            if (setProperty != null)
+            {
+                for (String key : setProperty.keySet())
+                {
+                    String value = setProperty.get(key);
+                    httpConn.setRequestProperty(key, value);
+                }
+            }
+            if (contentType != null)
+            {
+                httpConn.setRequestProperty("Content-Type", contentType);
+            }
             httpConn.setConnectTimeout(timeout);
             httpConn.setReadTimeout(timeout);
-            httpConn.connect();
+            if (postData == null)
+            {
+                httpConn.connect();
+            }
+            else
+            {
+                httpConn.setDoInput(true);
+                httpConn.setDoOutput(true);
+                outputStream = httpConn.getOutputStream();
+                writer = new OutputStreamWriter(outputStream, "UTF-8");
+                writer.write(postData);
+                writer.flush();
+                writer.close();
+                writer = null;
+                outputStream.close();
+                outputStream = null;
+            }
 
             int responseCode = httpConn.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK)
@@ -155,7 +213,7 @@ public class SimpleHttpClient
             }
             if (inputStream == null)
             {
-                Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
+                Log.w(TAG, " http " + requestMethod + " Response Code Error: " + responseCode + ": " + url);
                 callback.onErrorOccurred(new NullPointerException());
                 callback.onCompleted();
                 return;
@@ -163,7 +221,7 @@ public class SimpleHttpClient
         }
         catch (Exception e)
         {
-            Log.w(TAG, "httpGet: " + url + "  " + e.getMessage());
+            Log.w(TAG, "http " + requestMethod + " " + url + "  " + e.getMessage());
             e.printStackTrace();
             if (httpConn != null)
             {
@@ -173,11 +231,72 @@ public class SimpleHttpClient
             callback.onCompleted();
             return;
         }
+        finally
+        {
+            try
+            {
+                if (writer != null)
+                {
+                    writer.close();
+                }
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+            }
+            try
+            {
+                if (outputStream != null)
+                {
+                    outputStream.close();
+                }
+            }
+            catch (IOException e)
+            {
+                e.printStackTrace();
+            }
+        }
 
         // 応答を確認する
         try
         {
             int contentLength = httpConn.getContentLength();
+            if (contentLength < 0)
+            {
+                // コンテンツ長が取れない場合の処理...
+                try
+                {
+                    Map<String, List<String>> headers = httpConn.getHeaderFields();
+
+                    /*
+                    // 応答ヘッダをすべてダンプするロジック...
+                    for (String key : headers.keySet())
+                    {
+                        final List<String> valueList = headers.get(key);
+                        Log.v(TAG, " " + key + " : " + getValue(valueList));
+                    }
+                    */
+
+                    // コンテンツ長さが取れない場合は、HTTP応答ヘッダから取得する
+                    List<String> valueList = headers.get("X-FILE_SIZE");
+                    try
+                    {
+                        if (valueList != null)
+                        {
+                            contentLength = Integer.parseInt(getValue(valueList));
+                        }
+                    }
+                    catch (Exception ee)
+                    {
+                        ee.printStackTrace();
+                    }
+                }
+                catch (Exception e)
+                {
+                    e.printStackTrace();
+                }
+            }
+
             byte[] buffer = new byte[BUFFER_SIZE];
             int readBytes = 0;
             int readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
@@ -210,15 +329,63 @@ public class SimpleHttpClient
         callback.onCompleted();
     }
 
+
+    public static String getValue(List<String> valueList)
+    {
+        // 応答ヘッダの値切り出し用...
+        boolean isFirst = true;
+        final StringBuilder values = new StringBuilder();
+        for (String value : valueList)
+        {
+            values.append(value);
+            if (isFirst)
+            {
+                isFirst = false;
+            }
+            else
+            {
+                values.append(" ");
+            }
+        }
+        return (values.toString());
+    }
+
+    public static Bitmap httpGetBitmap(String url, Map<String, String> setProperty, int timeoutMs)
+    {
+        return (httpCommandBitmap(url, "GET", null, setProperty, null, timeoutMs));
+    }
+
+    /**
+     *
+     *
+     *
+     */
+    public static Bitmap httpPostBitmap(String url, String postData, int timeoutMs)
+    {
+        return (httpCommandBitmap(url, "POST", postData, null, null, timeoutMs));
+    }
+
+    /**
+     *
+     *
+     *
+     */
+    public static Bitmap httpPostBitmapWithHeader(String url, String postData, @Nullable Map<String, String> setProperty, @Nullable String contentType, int timeoutMs)
+    {
+        return (httpCommandBitmap(url, "POST", postData, setProperty, contentType, timeoutMs));
+    }
+
     /**
      *
      *
      *
      */
-    public static Bitmap httpGetBitmap(String url, int timeoutMs)
+    private static Bitmap httpCommandBitmap(@NonNull String url, @NonNull String requestMethod, @Nullable String postData, @Nullable Map<String, String> setProperty, @Nullable String contentType, int timeoutMs)
     {
         HttpURLConnection httpConn = null;
         InputStream inputStream = null;
+        OutputStream outputStream = null;
+        OutputStreamWriter writer = null;
         Bitmap bmp = null;
 
         int timeout = timeoutMs;
@@ -227,15 +394,44 @@ public class SimpleHttpClient
             timeout = DEFAULT_TIMEOUT;
         }
 
-        //  HTTP GETメソッドで要求を投げる
+        //  HTTP メソッドで要求を送出
         try
         {
             final URL urlObj = new URL(url);
             httpConn = (HttpURLConnection) urlObj.openConnection();
-            httpConn.setRequestMethod("GET");
+            httpConn.setRequestMethod(requestMethod);
+            if (setProperty != null)
+            {
+                for (String key : setProperty.keySet())
+                {
+                    String value = setProperty.get(key);
+                    httpConn.setRequestProperty(key, value);
+                }
+            }
+            if (contentType != null)
+            {
+                httpConn.setRequestProperty("Content-Type", contentType);
+            }
             httpConn.setConnectTimeout(timeout);
             httpConn.setReadTimeout(timeout);
-            httpConn.connect();
+
+            if (postData == null)
+            {
+                httpConn.connect();
+            }
+            else
+            {
+                httpConn.setDoInput(true);
+                httpConn.setDoOutput(true);
+                outputStream = httpConn.getOutputStream();
+                writer = new OutputStreamWriter(outputStream, "UTF-8");
+                writer.write(postData);
+                writer.flush();
+                writer.close();
+                writer = null;
+                outputStream.close();
+                outputStream = null;
+            }
 
             int responseCode = httpConn.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK)
@@ -248,14 +444,14 @@ public class SimpleHttpClient
             }
             if (inputStream == null)
             {
-                Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
+                Log.w(TAG, "http: (" + requestMethod + ") Response Code Error: " + responseCode + ": " + url);
                 return (null);
             }
             inputStream.close();
         }
         catch (Exception e)
         {
-            Log.w(TAG, "httpGet: " + url + "  " + e.getMessage());
+            Log.w(TAG, "http: (" + requestMethod + ") " + url + "  " + e.getMessage());
             e.printStackTrace();
             if (httpConn != null)
             {
@@ -263,6 +459,31 @@ public class SimpleHttpClient
             }
             return (null);
         }
+        finally
+        {
+            try
+            {
+                if (writer != null)
+                {
+                    writer.close();
+                }
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+            }
+            try
+            {
+                if (outputStream != null)
+                {
+                    outputStream.close();
+                }
+            }
+            catch (IOException e)
+            {
+                e.printStackTrace();
+            }
+        }
         return (bmp);
     }
 
@@ -273,7 +494,27 @@ public class SimpleHttpClient
      */
     public static String httpPost(String url, String postData, int timeoutMs)
     {
-        return (httpCommand(url, "POST", postData, null, null, null, timeoutMs));
+        return (httpCommand(url, "POST", postData, null, null, timeoutMs));
+    }
+
+    /**
+     *
+     *
+     *
+     */
+    public static String httpGetWithHeader(String url, Map<String, String> headerMap, String contentType, int timeoutMs)
+    {
+        return (httpCommand(url, "GET", null, headerMap, contentType, timeoutMs));
+    }
+
+    /**
+     *
+     *
+     *
+     */
+    public static String httpPostWithHeader(String url, String postData, Map<String, String> headerMap, String contentType, int timeoutMs)
+    {
+        return (httpCommand(url, "POST", postData, headerMap, contentType, timeoutMs));
     }
 
     /**
@@ -281,9 +522,9 @@ public class SimpleHttpClient
      *
      *
      */
-    public static String httpPostWithHeader(String url, String postData, String headerKey, String headerValue, String contentType, int timeoutMs)
+    public static String httpPutWithHeader(String url, String putData, Map<String, String> headerMap, String contentType, int timeoutMs)
     {
-        return (httpCommand(url, "POST", postData, headerKey, headerValue, contentType, timeoutMs));
+        return (httpCommand(url, "PUT", putData, headerMap, contentType, timeoutMs));
     }
 
     /**
@@ -293,7 +534,17 @@ public class SimpleHttpClient
      */
     public static String httpPut(String url, String postData, int timeoutMs)
     {
-        return (httpCommand(url, "PUT", postData, null, null, null, timeoutMs));
+        return (httpCommand(url, "PUT", postData, null, null, timeoutMs));
+    }
+
+    /**
+     *
+     *
+     *
+     */
+    public static String httpOptions(String url, String optionsData, int timeoutMs)
+    {
+        return (httpCommand(url, "OPTIONS", optionsData, null, null, timeoutMs));
     }
 
     /**
@@ -301,7 +552,7 @@ public class SimpleHttpClient
      *
      *
      */
-    private static String httpCommand(String url, String requestMethod, String postData, String setPropertyKey, String setPropertyValue, String contentType, int timeoutMs)
+    private static String httpCommand(String url, String requestMethod, String postData, Map<String, String> setProperty, String contentType, int timeoutMs)
     {
         HttpURLConnection httpConn = null;
         OutputStream outputStream = null;
@@ -320,9 +571,13 @@ public class SimpleHttpClient
             final URL urlObj = new URL(url);
             httpConn = (HttpURLConnection) urlObj.openConnection();
             httpConn.setRequestMethod(requestMethod);
-            if ((setPropertyKey != null)&&(setPropertyValue != null))
+            if (setProperty != null)
             {
-                httpConn.setRequestProperty(setPropertyKey, setPropertyValue);
+                for (String key : setProperty.keySet())
+                {
+                    String value = setProperty.get(key);
+                    httpConn.setRequestProperty(key, value);
+                }
             }
             if (contentType != null)
             {
@@ -330,18 +585,24 @@ public class SimpleHttpClient
             }
             httpConn.setConnectTimeout(timeout);
             httpConn.setReadTimeout(timeout);
-            httpConn.setDoInput(true);
-            httpConn.setDoOutput(true);
-
-            outputStream = httpConn.getOutputStream();
-            writer = new OutputStreamWriter(outputStream, "UTF-8");
-            writer.write(postData);
-            writer.flush();
-            writer.close();
-            writer = null;
-            outputStream.close();
-            outputStream = null;
 
+            if (postData == null)
+            {
+                httpConn.connect();
+            }
+            else
+            {
+                httpConn.setDoInput(true);
+                httpConn.setDoOutput(true);
+                outputStream = httpConn.getOutputStream();
+                writer = new OutputStreamWriter(outputStream, "UTF-8");
+                writer.write(postData);
+                writer.flush();
+                writer.close();
+                writer = null;
+                outputStream.close();
+                outputStream = null;
+            }
             int responseCode = httpConn.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK)
             {
@@ -350,6 +611,14 @@ public class SimpleHttpClient
             if (inputStream == null)
             {
                 Log.w(TAG, "http " + requestMethod + " : Response Code Error: " + responseCode + ": " + url);
+/*
+                inputStream = httpConn.getInputStream();
+                if (inputStream != null)
+                {
+                    // DUMP RESPONSE DETAIL
+                    Log.w(TAG, " RESP : " + readFromInputStream(inputStream));
+                }
+*/
                 return ("");
             }
         }
@@ -390,8 +659,52 @@ public class SimpleHttpClient
         }
 
         // 応答の読み出し
+        return (readFromInputStream(inputStream));
+/*
+        BufferedReader reader = null;
+        String replyString = "";
+        try
+        {
+            StringBuilder responseBuf = new StringBuilder();
+            reader = new BufferedReader(new InputStreamReader(inputStream));
+
+            int c;
+            while ((c = reader.read()) != -1)
+            {
+                responseBuf.append((char) c);
+            }
+            replyString = responseBuf.toString();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        finally
+        {
+            try
+            {
+                if (reader != null)
+                {
+                    reader.close();
+                }
+            }
+            catch (IOException e)
+            {
+                e.printStackTrace();
+            }
+        }
+        return (replyString);
+*/
+    }
+
+    private static String readFromInputStream(InputStream inputStream)
+    {
         BufferedReader reader = null;
         String replyString = "";
+        if (inputStream == null)
+        {
+            return ("");
+        }
         try
         {
             StringBuilder responseBuf = new StringBuilder();
@@ -425,6 +738,7 @@ public class SimpleHttpClient
         return (replyString);
     }
 
+
     public interface IReceivedMessageCallback
     {
         void onCompleted();