package ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
/** @author ljpyo */
@Component("httpUtil")
public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
public static final String POST = "POST";
public static final String GET = "GET";
public static final String DELETE = "DELETE";
private String makeParams(Map<String, Object> params){
String param = null;
StringBuffer sb = new StringBuffer();
if(params != null){
for ( String key : params.keySet() ){
logger.info(" key : " + key + " / value : " + params.get(key));
sb.append(key).append("=").append((params.get(key)==null?"":params.get(key)).toString().trim()).append("&");
}
}
param = sb.toString().substring(0, sb.toString().length()-1);
return param;
}
private String makeJsonParams(Map<String, Object> params){
String json = "";
if(params != null){
json = new Gson().toJson(params);
}
return json;
}
public String httpUrlConnection(String getpost, String targetUrl, Map<String, Object> params) throws Exception {
String returnText = this.httpUrlConnection(getpost, targetUrl, params, null, false);
return returnText;
}
public String httpUrlConnection(String getpost, String targetUrl, Map<String, Object> params, boolean isJson) throws Exception {
String returnText = this.httpUrlConnection(getpost, targetUrl, params, null, isJson);
return returnText;
}
public String httpUrlConnection(String getpost, String targetUrl, Map<String ,Object> params, Map<String, Object> header, boolean isJson) throws Exception {
URL url = null;
HttpURLConnection conn = null;
String jsonData = "";
BufferedReader br = null;
StringBuffer sb = null;
String returnText = "";
JSONObject jobj = null;
String postParams = "";
try{
if(getpost.equalsIgnoreCase(POST) || getpost.equalsIgnoreCase(DELETE)){
url = new URL(targetUrl);
} else if(getpost.equalsIgnoreCase(GET)){
url = new URL(targetUrl + ((params!=null)?"?"+makeParams(params):""));
}
logger.info("request url : " + url);
conn = (HttpURLConnection) url.openConnection();
// conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// conn.setRequestProperty("Accept", "application/json");
if(header != null){
conn.setRequestProperty(header.get("headerKey").toString(), header.get("headerValue").toString());
logger.info("header : " + header.get("headerKey").toString() + " / headerValue : " +header.get("headerValue").toString());
}
if(isJson){
conn.setRequestProperty("Content-Type", "application/json");
}
conn.setRequestMethod(getpost);
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setDoOutput(true);
if(getpost.equalsIgnoreCase(POST) || getpost.equalsIgnoreCase(DELETE)){
if(params != null){
if(isJson){
postParams = makeJsonParams(params);
} else {
postParams = makeParams(params);
}
logger.info("isJson : " + isJson);
logger.info("postParam.toString() : " + postParams);
logger.info("post param : " + postParams.getBytes("UTF-8").toString());
conn.getOutputStream().flush();
}
}
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
sb = new StringBuffer();
while((jsonData = br.readLine()) != null){
sb.append(jsonData);
}
returnText = sb.toString();
try{
jobj = new JSONObject(returnText);
if( ! jobj.has("responseCode") ){
jobj.put("responseCode", conn.getResponseCode());
}
} catch (JSONException e){
jobj = new JSONObject();
jobj.put("responseCode", conn.getResponseCode());
}
} catch (IOException e){
logger.debug("exception in httpurlconnection ! ", e);
throw new APIException("exception in httpurlconnection !");
} finally {
try {
if (br != null) br.close();
} catch(Exception e){
logger.warn("finally..br.close()", e);
}
br = null;
try {
if(conn!=null)
conn.disconnect();
} catch(Exception e){
logger.warn("finally..conn.disconnect()", e);
}
conn = null;
}
return jobj != null ? jobj.toString() : null;
}
}