/**
 * Copyright (c) 2014-2017 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipse.smarthome.binding.hue.internal;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 *
 * @author Q42, standalone Jue library (https://github.com/Q42/Jue)
 * @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding
 */
class HttpClient {
    private int timeout = 1000;

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public Result get(String address) throws IOException {
        return doNetwork(address, "GET", "");
    }

    public Result post(String address, String body) throws IOException {
        return doNetwork(address, "POST", body);
    }

    public Result put(String address, String body) throws IOException {
        return doNetwork(address, "PUT", body);
    }

    public Result delete(String address) throws IOException {
        return doNetwork(address, "DELETE", "");
    }

    protected Result doNetwork(String address, String requestMethod, String body) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) new URL(address).openConnection();
        try {
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setConnectTimeout(timeout);
            conn.setReadTimeout(timeout);

            if (body != null && !body.equals("")) {
                conn.setDoOutput(true);
                OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
                out.write(body);
                out.close();
            }

            InputStream in = new BufferedInputStream(conn.getInputStream());
            String output = convertStreamToString(in);
            return new Result(output, conn.getResponseCode());
        } finally {
            conn.disconnect();
        }
    }

    private static String convertStreamToString(InputStream is) {
        try {
            return new Scanner(is).useDelimiter("\\A").next();
        } catch (NoSuchElementException e) {
            return "";
        }
    }

    public static class Result {
        private String body;
        private int responseCode;

        public Result(String body, int responseCode) {
            this.body = body;
            this.responseCode = responseCode;
        }

        public String getBody() {
            return body;
        }

        public int getResponseCode() {
            return responseCode;
        }
    }
}

@@ -2409,34 +2409,98 @@
 ) %7B%0A        try 
-%7B%0A
+(final Scanner scanner = new Scanner(is)) %7B%0A            try %7B%0A    
             retu
@@ -2502,31 +2502,23 @@
  return 
-new S
+s
 canner
-(is)
 .useDeli
@@ -2534,24 +2534,28 @@
 A%22).next();%0A
+    
         %7D ca
@@ -2583,16 +2583,17 @@
 eption e
+x
 ) %7B%0A    
@@ -2596,24 +2596,28 @@
             
+    
 return %22%22;%0A 
@@ -2615,16 +2615,30 @@
 urn %22%22;%0A
+            %7D%0A
         

fix resource leak (#3920)