package com.blueland.bpm.manage.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

@Service
public class HttpUtil {
	private final static String APPLICATION_JSON = "application/json";

	public static String get(String url) {
		String retvalue = "";
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpGet httpget = new HttpGet(url);
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(950000000)
					.setConnectTimeout(950000000).build();
			httpget.setConfig(requestConfig);
			httpget.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
			ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

				public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						HttpEntity entity = (HttpEntity) response.getEntity();
						return entity != null ? EntityUtils.toString((org.apache.http.HttpEntity) entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}

			};
			retvalue = httpclient.execute(httpget, responseHandler);
		} catch (Exception ex) {
			throw new java.lang.IllegalArgumentException("get url error: " + ex);
		} finally {
			try {
				httpclient.close();
			} catch (Exception e) {
				throw new java.lang.IllegalArgumentException("httpclient.close(): " + e);
			}
		}
		return retvalue;
	}
	
	public static String post(String url,String body){
		String retvalue = "";
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpPost httppost = new HttpPost(url);
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(950000000)
					.setConnectTimeout(950000000).build();
			httppost.setConfig(requestConfig);
			httppost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
			StringEntity entity = new StringEntity(body, "UTF-8");
			entity.setContentType("application/json;charset=UTF-8");
			entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8"));
			httppost.setEntity(entity);
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				retvalue = EntityUtils.toString(response.getEntity());
			} catch (Exception ex) {
				throw new java.lang.Exception("post url error: " + ex);
			} finally {
				try {
					response.close();
				} catch (IOException ex) {
					throw new java.io.IOException("post url error: " + ex);
				}
			}
		} catch (Exception ex) {
			throw new java.lang.IllegalArgumentException("post url error: " + ex);
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				throw new java.lang.IllegalArgumentException("httpclient.close(): " + e);
			}
		}
		return retvalue;
	}
}
