Groovy Sample – ApexRestClient.groovy
package com.oracle.ceal
import java.net.HttpURLConnection;
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSession
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
class ApexRestClient {
private HttpURLConnection connection
private apexUrl
private def proxyHost
private def proxyPort
private def user
private def pwd
private def domain
private def ignoreSSLCertsErrors
public ApexRestClient(apexServerUrl,httpProxyHost, httpProxyPort, identityDomain,username, password, ignoreSSLCertificationPathErrors) {
apexUrl=apexServerUrl
proxyHost=httpProxyHost
proxyPort=httpProxyPort
domain=identityDomain
user=username
pwd=password
ignoreSSLCertsErrors=ignoreSSLCertificationPathErrors
}
def setProxyParams() {
Properties systemProperties = System.getProperties()
systemProperties.setProperty("http.proxyHost",proxyHost)
systemProperties.setProperty("http.proxyPort",proxyPort)
systemProperties.setProperty("https.proxyHost",proxyHost)
systemProperties.setProperty("https.proxyPort",proxyPort)
}
def setSSLParams() {
if (ignoreSSLCertsErrors !=null && ignoreSSLCertsErrors.toUpperCase()=="TRUE") {
println "Ignoring SSL certification path errors"
// Disable SSL cert validation
def hostnameVerifier = [
verify: { hostname, session -> true }
]
def trustManager = [
checkServerTrusted: { chain, authType -> },
checkClientTrusted: { chain, authType -> },
getAcceptedIssuers: { null }
]
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier as HostnameVerifier)
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory())
SSLContext context = SSLContext.getInstance("SSL")
context.init(null, [trustManager as X509TrustManager] as TrustManager[], null)
}
}
def openConnection(restUrl,method,contentType, body) {
println "Opening connection to apex $restUrl with method:$method"
int statusCode
setProxyParams()
setSSLParams()
URL newUrl
newUrl=new URL(restUrl)
connection = (HttpURLConnection) newUrl.openConnection()
connection.setDoOutput(true)
connection.setDoInput(true)
connection.setUseCaches(false)
if (method=="")
connection.setRequestMethod("GET")
else
connection.setRequestMethod(method)
if (contentType.toUpperCase()=="FORM") {
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded")
}
if (contentType.toUpperCase()=="JSON") {
connection.setRequestProperty("Content-Type","application/json")
}
if (contentType.toUpperCase()=="") {
// add no content type
}
String userCredentials = domain +"."+user + ":" + pwd
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userCredentials.getBytes())
connection.setRequestProperty("Authorization", basicAuth)
if (body!=null && body!="") {
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (body);
wr.flush ();
wr.close ();
}
String response=""
try {
statusCode = connection.responseCode
println "Connection status code: $statusCode "
if (statusCode==401 || statusCode==403) {
println "Not authorized"
}
if (statusCode==200) {
println "Authentication succeeded"
println "Server response:"
println "-----"
response=displayServerResponse(connection)
println "-----"
}
if (statusCode==400 || statusCode==500) {
println "Bad request"
println "Server response:"
println "-----"
response=displayServerResponse(connection)
println "-----"
}
} catch (Exception e) {
println "Error connecting to the URL"
println e.getMessage()
} finally {
if (connection != null) {
connection.disconnect();
}
}
return response
}
def displayServerResponse(connection) {
InputStream is;
if (connection.getResponseCode()==200) {
is=connection.getInputStream();
} else {
is=connection.getErrorStream();
}
println "Response Content-Type:"+connection.getContentType()
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
println sb
return sb.toString()
}
def launchProcUsingGET(apexUri) {
println "**Launching PL/SQL in apex**"
def restUrl=apexUrl+"/"+apexUri
/*
* Procedure in apex is defined this way
* RESTful Service Module: bics/
URI Template: plsql/
Method: GET
Source Type: PL/SQL
Requires Secure Access: YES
Source:
DECLARE
prevdeptno number;
deptloc varchar2(30);
deptname varchar2(30);
CURSOR getemps IS select * from emp
where ((select job from emp where ename = :empname) IN ('PRESIDENT', 'MANAGER'))
or deptno = (select deptno from emp where ename = :empname)
order by deptno, ename;
BEGIN
sys.htp.htmlopen;
sys.htp.headopen;
sys.htp.title('Departments');
sys.htp.headclose;
sys.htp.bodyopen;
for emprecs in getemps
loop
if emprecs.deptno != prevdeptno or prevdeptno is null then
select dname, loc into deptname, deptloc
from dept where deptno = (select deptno from emp where ename = emprecs.ename);
if prevdeptno is not null then
sys.htp.print('</ul>');
end if;
sys.htp.print('Department ' || deptname || ' located in ' || deptloc || '<p/>');
sys.htp.print('<ul>');
end if;
sys.htp.print('<li>');
prevdeptno := emprecs.deptno;
end loop;
sys.htp.print('</ul>');
sys.htp.bodyclose;
sys.htp.htmlclose;
END;
URL call will be in the form: https://<SERVER>.oraclecloudapps.com/apex/bics/plsql/
Response will be in following format for this specific plsql example
Response Content-Type:text/html; charset=UTF-8
<HTML>
<HEAD>
<TITLE>Departments</TITLE>
</HEAD>
<BODY>
</ul>
</BODY>
</HTML>
*/
def response
response=openConnection(restUrl,"GET","FORM","")
println "****"
println "****"
}
def launchSQLQueryUsingGETAndVariableOnUrl(apexUri, parameter) {
println "**Launching Sql query in apex**"
/*
* Procedure in apex is defined this way
* RESTful Service Module: bics/
URI Template: test/{ID}
Method: GET
Source Type: Query
Format: JSON
Requires Secure Access: YES
Source: select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO
from EMP where EMPNO = :ID
URL call will be in the form: https://<SERVER>.oraclecloudapps.com/apex/bics/test/7839
Response will be in following format:
Response Content-Type:application/json
{"next":{"$ref":"https://<SERVER>.oraclecloudapps.com/apex/bics/test/7839?page=1"},"items":[{"empno":7839,"ename":"KING","job":"PRESIDENT","hiredate":"1981-11-17T00:00:00Z","sal":5000,"deptno":10}]}
*
*/
def restUrl=apexUrl+"/"+apexUri+"/"+parameter
def response
response=openConnection(restUrl,"GET","FORM","")
println "****"
}
}
Groovy Sample — APEXAutomationParameters.groovy
package com.oracle.ceal
import java.io.File;
import java.util.Properties;
class APEXAutomationParameters {
private Properties props = new Properties()
def apexRestUrl
def APEX_REST_URL='apexRestUrl'
def identityDomain
def APEX_IDENTITY_DOMAIN='apexIdentityDomain'
def username
def APEX_USERNAME='apexUsername'
def password
def APEX_PASSWORD='apexPassword'
def proxyHost
def PROXY_HOST='proxyHost'
def proxyPort
def PROXY_PORT='proxyPort'
def ignoreSSLCertificationPathErrors
def IGNORE_CERT_PATH_ERRORS='ignoreSSLCertificationPathErrors'
public APEXAutomationParameters(propertiesFile) {
def propsFileName=propertiesFile
File propsFile = new File(propsFileName)
try {
props.load(propsFile.newDataInputStream())
} catch ( FileNotFoundException fnfe) {
println "$propsFileName APEX properties file not found in the current directory. Exiting."
System.exit(1);
}
apexRestUrl=props.getProperty(APEX_REST_URL)
identityDomain=props.getProperty(APEX_IDENTITY_DOMAIN)
username=props.getProperty(APEX_USERNAME)
password=props.getProperty(APEX_PASSWORD)
proxyHost=props.getProperty(PROXY_HOST)
proxyPort=props.getProperty(PROXY_PORT)
ignoreSSLCertificationPathErrors=props.getProperty(IGNORE_CERT_PATH_ERRORS)
}
def isConfigValid() {
try {
// Required parameters check
assert apexRestUrl != '' : "$APEX_REST_URL is empty"
assert identityDomain != '' : "$APEX_IDENTITY_DOMAIN is empty"
assert username != '' : "$APEX_USERNAME is empty"
assert password != '' : "$APEX_PASSWORD is empty"
// validate url is correct
URL apexUrl=new URL(apexRestUrl)
// connection test
// ssl check
println "$APEX_REST_URL = $apexRestUrl"
println "$APEX_IDENTITY_DOMAIN = $identityDomain"
println "$APEX_USERNAME = $username"
println "$APEX_PASSWORD = ******"
} catch(AssertionError e) {
println e.getMessage()
return false
} catch (MalformedURLException e) {
println "APEX Rest url is incorrect. Current value:$apexRestUrl , expected format http|https://"
println e.getMessage()
return false
}
return true
}
}
Groovy Sample — BICSAutomationParameters.groovy
package com.oracle.ceal
import java.io.File;
import java.util.Properties;
class BICSAutomationParameters {
private Properties props = new Properties()
def bicsRestUrl
def BICS_REST_URL='bicsRestUrl'
def identityDomain
def BICS_IDENTITY_DOMAIN='bicsIdentityDomain'
def username
def BICS_USERNAME='bicsUsername'
def password
def BICS_PASSWORD='bicsPassword'
def proxyHost
def PROXY_HOST='proxyHost'
def proxyPort
def PROXY_PORT='proxyPort'
def ignoreSSLCertificationPathErrors
def IGNORE_CERT_PATH_ERRORS='ignoreSSLCertificationPathErrors'
public BICSAutomationParameters(propertiesFile) {
def propsFileName=propertiesFile
File propsFile = new File(propsFileName)
try {
props.load(propsFile.newDataInputStream())
} catch ( FileNotFoundException fnfe) {
println "$propsFileName BICS properties file not found in the current directory. Exiting."
System.exit(1);
}
bicsRestUrl=props.getProperty(BICS_REST_URL)
identityDomain=props.getProperty(BICS_IDENTITY_DOMAIN)
username=props.getProperty(BICS_USERNAME)
password=props.getProperty(BICS_PASSWORD)
proxyHost=props.getProperty(PROXY_HOST)
proxyPort=props.getProperty(PROXY_PORT)
ignoreSSLCertificationPathErrors=props.getProperty(IGNORE_CERT_PATH_ERRORS)
}
def isConfigValid() {
try {
// Required parameters check
assert bicsRestUrl != '' : "$BICS_REST_URL is empty"
assert identityDomain != '' : "$BICS_IDENTITY_DOMAIN is empty"
assert username != '' : "$BICS_USERNAME is empty"
assert password != '' : "$BICS_PASSWORD is empty"
// validate url is correct
URL bicsUrl=new URL(bicsRestUrl)
// connection test
// ssl check
println "$BICS_REST_URL = $bicsRestUrl"
println "$BICS_IDENTITY_DOMAIN = $identityDomain"
println "$BICS_USERNAME = $username"
println "$BICS_PASSWORD = ******"
} catch(AssertionError e) {
println e.getMessage()
return false
} catch (MalformedURLException e) {
println "BICS Rest url is incorrect. Current value:$bicsRestUrl , expected format http|https://"
println e.getMessage()
return false
}
return true
}
}
Groovy Sample — PBCSAutomationParameters.groovy
package com.oracle.ceal
class PBCSAutomationParameters {
private Properties props = new Properties()
def planningRestUrl
def PBCS_PLANNING_REST_URL='pbcsPlanningRestUrl'
def interopRestUrl
def PBCS_INTEROP_REST_URL='pbcsInteropRestUrl'
def identityDomain
def PBCS_IDENTITY_DOMAIN='pbcsIdentityDomain'
def username
def PBCS_USERNAME='pbcsUsername'
def password
def PBCS_PASSWORD='pbcsPassword'
def proxyHost
def PROXY_HOST='proxyHost'
def proxyPort
def PROXY_PORT='proxyPort'
def ignoreSSLCertificationPathErrors
def IGNORE_CERT_PATH_ERRORS='ignoreSSLCertificationPathErrors'
public PBCSAutomationParameters(propertiesFile) {
def propsFileName=propertiesFile
File propsFile = new File(propsFileName)
try {
props.load(propsFile.newDataInputStream())
} catch ( FileNotFoundException fnfe) {
println "$propsFileName PBCS properties file not found in the current directory. Exiting."
System.exit(1);
}
planningRestUrl=props.getProperty(PBCS_PLANNING_REST_URL)
interopRestUrl=props.getProperty(PBCS_INTEROP_REST_URL)
identityDomain=props.getProperty(PBCS_IDENTITY_DOMAIN)
username=props.getProperty(PBCS_USERNAME)
password=props.getProperty(PBCS_PASSWORD)
proxyHost=props.getProperty(PROXY_HOST)
proxyPort=props.getProperty(PROXY_PORT)
ignoreSSLCertificationPathErrors=props.getProperty(IGNORE_CERT_PATH_ERRORS)
}
def isConfigValid() {
try {
// Required parameters check
assert planningRestUrl != '' : "$PBCS_PLANNING_REST_URL is empty"
assert interopRestUrl != '' : "$PBCS_INTEROP_REST_URL is empty"
assert identityDomain != '' : "$PBCS_IDENTITY_DOMAIN is empty"
assert username != '' : "$PBCS_USERNAME is empty"
assert password != '' : "$PBCS_PASSWORD is empty"
// validate url is correct
URL planningUrl=new URL(planningRestUrl)
URL interopUrl=new URL(interopRestUrl)
// connection test
// ssl check
println "$PBCS_PLANNING_REST_URL = $planningRestUrl"
println "$PBCS_INTEROP_REST_URL = $interopRestUrl"
println "$PBCS_IDENTITY_DOMAIN = $identityDomain"
println "$PBCS_USERNAME = $username"
println "$PBCS_PASSWORD = ******"
} catch(AssertionError e) {
println e.getMessage()
return false
} catch (MalformedURLException e) {
println "PBCS Rest urls are incorrect. Current value:$planningRestUrl and $interopRestUrl, expected format http|https://<SERVER>/HyperionPlanning/rest/11.1.2.3.xyz http|https://<SERVER>/interop/rest/11.1.2.3.xyz"
println e.getMessage()
return false
}
return true
}
}