package com.ais.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import javax.faces.context.FacesContext;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HttpUtils {
private final static Log log = LogFactory.getLog(HttpUtils.class);
public static String doHttpPost(String url, String content) {
//System.out.println("---HttpUtils.doHttpPost--url------>"+url);
//System.out.println("---HttpUtils.doHttpPost--content-->"+content);
log.info("---HttpUtils.doHttpPost--url------>"+url);
log.info("---HttpUtils.doHttpPost--content-->"+content);
InputStream is = null;
ByteArrayOutputStream outStream = null;
OutputStreamWriter out = null;
HttpURLConnection conn = null;
try {
URL console = new URL(url);
if (url.indexOf("https") != -1) {
conn = (HttpsURLConnection) console.openConnection();
//trustAllCerts
SslUtils.ignoreSsl();
} else {
conn = (HttpURLConnection) console.openConnection();
}
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json");
if(url.indexOf("/login") == -1) {
//set the sessionID
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String sessionId = (String)request.getSession().getAttribute("API_SESSION_ID");
conn.setRequestProperty("cookie", sessionId);
}
//System.out.println("---HttpUtils.doHttpPost--connect---begin--");
conn.connect();
//System.out.println("---HttpUtils.doHttpPost--connect---execute--");
if (content != null) {
out = new OutputStreamWriter(conn.getOutputStream());
out.write(content);
out.flush();
}
if(url.indexOf("/login") != -1) {
//get the sessionID
String sessionID = null;
String cookieValue = conn.getHeaderField("set-cookie");
if(cookieValue != null){
sessionID = cookieValue.substring(0, cookieValue.indexOf(";"));
}else{
sessionID = "";
}
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
request.getSession().setAttribute("API_SESSION_ID", sessionID);
}
is = conn.getInputStream();
//System.out.println("---HttpUtils.doHttpPost--connect---end");
if (is != null) {
outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
String sr = new String(outStream.toByteArray());
//System.out.println("---HttpUtils.doHttpPost--return-->"+sr);
log.info("---HttpUtils.doHttpPost--return-->"+sr);
return sr;
}
} catch (MalformedURLException e) {
log.error(e.getMessage());
} catch (ProtocolException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
if (conn != null) {
try {
conn.disconnect();
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
return null;
}
public static void main(String[] args) {
/*
//String url = "http://localhost:8090/opp/api/oppGetReconFile";
String url = "http://localhost:8090/opp/api/oppCreateSessionID";
SessionAPIForm sessionForm = new SessionAPIForm();
sessionForm.setSourceSysCode("ODS");
sessionForm.setMerchantRef("00001");
sessionForm.setEncryptKey("ais123456");
JSONObject jsonObject = JSONObject.fromObject(sessionForm);
String result = doHttpPost(url, jsonObject.toString());
System.out.println(result);
*/
}
}