Closed i7QC2njzxHCc0LYBqCh0HQf30xF2DYuJjqtFbNe closed 4 years ago
Fabric is using build-in Java library HttpURLConnection.
Code Example:
import org.json.JSONObject;
import java.nio.charset.Charset ;
import sun.misc.BASE64Encoder;
import static com.k2view.cdbms.shared.user.UserCode.reportUserMessage; import static com.k2view.cdbms.usercode.common.SharedGlobals.*;
public class ReSTCall{
public static Logger log = LoggerFactory.getLogger(ReSTCall.class); public static SharedGlobals globals = new SharedGlobals(); public String createRequest(String restURL, String jsonStr, String wsMethod) throws Exception{ String loginPassword = WS_LAYER7_USERNAME+ ":" + WS_LAYER7_PASSWORD; HttpsURLConnection con = null; StringBuffer content = new StringBuffer(); try { String encoded = new sun.misc.BASE64Encoder().encode(loginPassword.getBytes()); TrustManager[] defTrustMngr = new TrustManager[] {new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() {return null;} public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} }}; // we can get Instance of TLS - it's more secured SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, defTrustMngr, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // Install the relevant host verifier //HttpsURLConnection.setDefaultHostnameVerifier(verifiedHost); String inputLine=""; BufferedReader in ; URL url = new URL(null, restURL, new sun.net.www.protocol.https.Handler()); //con = (HttpsURLConnection)url.openConnection(); if(restURL.length() > 0 ){ con = (HttpsURLConnection) url.openConnection(); con.setRequestMethod(wsMethod); con.setDoOutput(true); //set request Headers con.setRequestProperty("Content-Type","application/json"); con.setRequestProperty ("Authorization", "Basic " + encoded);; //add JSON input if (jsonStr != null && !"".equals(jsonStr)){ JSONObject jsonObject = new JSONObject(jsonStr); OutputStreamWriter jOut = new OutputStreamWriter(con.getOutputStream()); jOut.write(jsonObject.toString()); jOut.close(); } //execute the rest API int executeStatus = con.getResponseCode(); log.info("con.getResponseCode() "+executeStatus); //reportUserMessage("ResponseCode: "+executeStatus+" / ResponseMessage: "+con.getResponseMessage()); if (executeStatus != 200) { in = new BufferedReader(new InputStreamReader(con.getErrorStream())); content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } log.info("Rest API Response is: "+content.toString()); reportUserMessage("Rest API Response is: "+content.toString()); in.close(); throw new RuntimeException("Failed. The Response code is not 200 : HTTPS error code : "+ con.getResponseCode()); } //read the response of the Get Request API in = new BufferedReader(new InputStreamReader(con.getInputStream())); content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } log.info("Rest API Response is: "+content.toString()); reportUserMessage("Rest API Response is: "+content.toString()); in.close(); con.disconnect(); } else{ log.info("The provided url is empty, it's mandatory so please provide the relevant inputs to create the URL"); throw new RuntimeException("There is no URL provided!"); } } catch (Exception e) { //the printStackTrace logged into k2fabric*.err log reportUserMessage("e.printStackTrace: "); e.printStackTrace(); log.info("ERROR catched:"+e); throw new Exception(); } finally{ if(con!=null) con.disconnect(); return content.toString(); } } public String getParamsString(Map<String, String> params) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { if (entry.getValue() != null && entry.getValue().length()>0){ result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); result.append("&"); } } String resultString = result.toString(); return resultString.length() > 0 ? resultString.substring(0, resultString.length() - 1): resultString; }
}
Fabric is using build-in Java library HttpURLConnection.
Code Example:
import org.json.JSONObject;
import java.nio.charset.Charset ;
import sun.misc.BASE64Encoder;
import static com.k2view.cdbms.shared.user.UserCode.reportUserMessage; import static com.k2view.cdbms.usercode.common.SharedGlobals.*;
public class ReSTCall{
}