package com.ibeetl.admin.flow.utils;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLUtil {

	/**
	 * 解读流
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static String readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = null;
		try {
			outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}
			byte[] data = outStream.toByteArray();
			return new String(data, "utf-8");
		} finally{
			if(outStream != null)
				outStream.close();
			if(inStream != null)
				inStream.close();
		}
	}
	
	/**
	 * 以get方式获取链接的输出
	 * @param href
	 * @return
	 * @throws Exception
	 */
	public static String get(String href) throws Exception {
		URL url = new URL(href);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.addRequestProperty("content-type", "text/plain");
		conn.setUseCaches(false);
		conn.setConnectTimeout(30000);
		conn.setDoOutput(false);
		return readInputStream(conn.getInputStream());
	}
	
	/**
	 * 以post方式获取链接的输出
	 * @param href
	 * @return
	 * @throws Exception
	 */
	public static String post(String href, String data) throws Exception {
		HttpURLConnection conn = null;
		if(data == null || data.length() == 0) {
			return get(href);
		} else {
			try {
				URL url = new URL(href);
				conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod("POST");
				conn.addRequestProperty("content-type", "text/plain");
				conn.setDoOutput(true);
				conn.setUseCaches(false);
				OutputStream os = conn.getOutputStream();
				os.write(data.getBytes("utf-8"));
				os.flush();
			} finally {
				if(conn != null) {
					conn.getOutputStream().close();
				}
			}
			if(conn != null) {
				return readInputStream(conn.getInputStream());
			}
			return null;
		}
	}

	/**
	 * 以httpspost方式获取链接的输出
	 * @param href
	 * @return
	 * @throws Exception
	 */
	public static String httpsPost(String href, String data) throws Exception {
		HttpsURLConnection conn = null;
		if(data == null || data.length() == 0) {
			return get(href);
		} else {
			try {
				//创建sslContext对象，并使指定的信任管理器初始化
				TrustManager[] tm={new MyX509TrustManager()};
				SSLContext sslContext=SSLContext.getInstance("SSL");
				sslContext.init(null, tm, new java.security.SecureRandom());
				//得到factory对象
				SSLSocketFactory ssf=sslContext.getSocketFactory();
				URL url = new URL(href);
				conn = (HttpsURLConnection) url.openConnection();
				conn.setSSLSocketFactory(ssf);
				conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
				conn.setRequestMethod("POST");
				conn.addRequestProperty("content-type", "text/plain");
				conn.setDoOutput(true);
				conn.setUseCaches(false);
				OutputStream os = conn.getOutputStream();
				os.write(data.getBytes("utf-8"));
				os.flush();
			} finally {
				if(conn != null) {
					conn.getOutputStream().close();
				}
			}
			if(conn != null) {
				return readInputStream(conn.getInputStream());
			}
			return null;
		}
	}
}
