/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

package com.persagy.server.http

import org.slf4j.LoggerFactory
import java.io.*
import java.net.HttpURLConnection
import java.net.URL
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


/**
 * http工具类
 *
 * @author  PLX
 */
object HttpUntil {
    private val logger = LoggerFactory.getLogger(HttpUntil::class.java)

    /**
     * GET请求重定向
     */
    fun redirectGet(url: URL, request: HttpServletRequest, resp: HttpServletResponse) {
        logger.debug("request=$request")
        logger.debug(url.toString())
        val conn = url.openConnection() as HttpURLConnection
        conn.requestMethod = "GET"
        // 设置使用输入流
        conn.doInput = true
        // 设置有输出流
        conn.doOutput = true
        // 不使用缓冲
        conn.useCaches = false
        // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
        conn.instanceFollowRedirects = true
        // 设置使用标准编码格式编码参数的名-值对
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8")

        // 连接
        conn.connect()

        logger.debug("code==${conn.responseCode}：：：${HttpURLConnection.HTTP_OK}")
        // 如果正常返回
        if (HttpURLConnection.HTTP_OK == conn.responseCode) {
            resp.setHeader("Content-type", "text/html;charset=UTF-8")
            conn.inputStream.copyTo(resp.outputStream, 4096)
            conn.inputStream.close()
            resp.outputStream.close()
        }

        conn.disconnect()

        return
    } // Function postRedirect()

    /**
     * GET请求
     *
     * @param   url     请求地址
     * @return  json字符串
     */
    @Throws(Exception::class)
    fun get(url: URL) : String {
        logger.debug("url=$url")
        var result = ""
        val conn = url.openConnection() as HttpURLConnection
        conn.requestMethod = "GET"
        // 不使用缓冲
        conn.useCaches = false
        // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
        conn.instanceFollowRedirects = true

        // 连接
        conn.connect()

        // 如果正常返回
        if (HttpURLConnection.HTTP_OK == conn.responseCode) {
            DataInputStream(conn.inputStream)
            result = is2string(conn.inputStream)
        }

        conn.disconnect()
        logger.debug(result)
        return result
    } // Function post()

    /**
     * Get请求
     *
     * @param   url     请求地址
     * @param   clazz   生成的Java对象
     * @return  Java对象
     */
    @Throws(Exception::class)
    fun <T> get(url: URL, clazz: Class<T>): T {
        val jsonString = get(url)
        logger.debug(jsonString)
        return JacksonUtil.json2object(jsonString, clazz)
    } // Function json2object()

    /**
     * POST请求
     *
     * @param   url     请求地址
     * @return  json字符串
     */
    @Throws(Exception::class)
    fun postJson(url: URL, json: String) : String {
        logger.debug("url=$url\njson=$json")
        var result = ""
        val conn = url.openConnection() as HttpURLConnection
        conn.requestMethod = "POST"
        // 设置使用输入流
        conn.doInput = true
        // 设置有输出流
        conn.doOutput = true
        // 不使用缓冲
        conn.useCaches = false
        // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
        conn.instanceFollowRedirects = true
        // 设置使用标准编码格式编码参数的名-值对
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8")

        // 连接
        conn.connect()

        // 写入参数到请求中
        conn.outputStream.write(json.toByteArray(Charsets.UTF_8))
        conn.outputStream.flush()
        conn.outputStream.close()

        // 如果正常返回
        if (HttpURLConnection.HTTP_OK == conn.responseCode) {
            DataInputStream(conn.inputStream)
            result = is2string(conn.inputStream)
        }

        conn.disconnect()
        logger.debug(result)
        return result
    } // Function post()

    /**
     * POST请求
     *
     * @param   url     请求地址
     * @param   json    请求json字符串
     * @param   clazz   生成的Java对象
     * @return  Java对象
     */
    @Throws(Exception::class)
    fun <T> postJson(url: URL, json: String, clazz: Class<T>): T {
        val jsonString = postJson(url, json)
        logger.debug(jsonString)
        return JacksonUtil.json2object(jsonString, clazz)
    } // Function json2object()

    /**
     * 输入流转换为字符串
     *
     * @param   input   输入流
     * @return  字符串
     */
    private fun is2string(input: InputStream): String {
        val reader = BufferedReader(InputStreamReader(input))
        val sb = StringBuilder()
        try {
            var line: String? = reader.readLine()
            while (line != null) {
                sb.append(line + "\n")
                line = reader.readLine()
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                input.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return sb.toString()
    } // Function is2string()

} // Object HttpUntil