import com.experian.eda.enterprise.core.api.Message;
import com.experian.eda.enterprise.rest.api.cpf.ConnectivityRequestRest;
import com.experian.eda.enterprise.rest.api.cpf.ConnectivityResponseRest;
import com.experian.eda.enterprise.script.groovy.GroovyComponent;
import com.experian.eda.enterprise.startup.InterfaceBrokerProperties
import com.experian.ibutils.http.CallParameters
import com.experian.ibutils.http.CallResponse;
import groovy.xml.XmlSlurper;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;

import java.nio.charset.StandardCharsets
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream;

import static com.experian.eda.enterprise.properties.PropertiesRegistry.getTenantProperties;


import java.text.SimpleDateFormat

public class ProcessRequest implements GroovyComponent<Message> {

    protected static final Logger logger = LoggerFactory.getLogger(this);
    // Proces Flow transitions
    protected static final String _TR_SUCCESS = "success";
    protected static final String _TR_ERROR = "error";
    // REST constants
    protected static final String _REST_REQUEST = "rest.request";
    protected static final String _REST_RESPONSE = "rest.response";
    // REST http status codes
    protected static final Integer _HTTP_STATUS_OK = 200;
    protected static final Integer _HTTP_STATUS_ERROR = 500;
    static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
    private static final String	homePath = InterfaceBrokerProperties.getProperty("client.solution.home")
    private static final String	trustStorePath = (getTenantProperties()?.get("httpclient.truststore"))?.replaceAll(Pattern.quote('${client.solution.home}'), Matcher.quoteReplacement(homePath))
    //private static final truststorePath=getTenantProperties().get("httpclient.truststore");
    //private static final String	trustStorePath="C:\\Program Files\\PowerCurve\\Connectivity\\Connectivity v1.12\\sample\\key\\truststore.jks";

    static final String CACHE_OBJECT_KEY = "data"
    static final String S3_DATE_PATTERN = "yyyyMMdd_HHmmssZ"


    //S3 Accessory class
    S3Client s3;

    //Cache variables
    String env = "";
    String interfaceName = "";
    Integer maxDays = 0;
    //Integer retries = 0;
    String serviceURL = "";
    String accessKey = "";
    String secretKey = "";
    String cacheFound = "";
    String bucket = "";
    List uids = [];
    Map reqHdrs = [:];
    HashMap errorMap = new HashMap();
    int readTimeout;
    int connectTimeout;

    //Connectivity Request/Response Set
    ConnectivityRequestRest request
    ConnectivityResponseRest response

    //CacheEntry
    Map cache;

    //Request-Body
    static final RQ_REQUEST_BODY= "request-body"
    static final RQ_REQUEST_HEADERS="request-headers"
    static final RQ_CACHE_PARAMS = "eda-cache-parameters"
    static final RQ_CACHE_AUTH = "eda-cache-auth"
    static final RQ_SERVICE_URL="service-url"
    static final RQ_READ_TIMEOUT="read-timeout"
    static final RQ_CONNECT_TIMEOUT="connect-timeout"

    //Cache Results Key
    static final String RS_HTTP_CODE = "response-http-code"
    static final String RS_HEADERS= "response-headers"
    static final String RS_BODY = "response-body"
    static final String RS_CACHE_BLOCK = "eda-cache-results"
    static final String RS_MATCH_FOUND = "cache-found"
    static final String RS_MATCH_KEY = "cache-key"
    static final String RS_MATCH_DATE = "cache-date"


    //@Synchronized
    public String processMessage(final Message message, final Map<String, String> dataMap) {

        logger.info("Trust-Store-Path:"+trustStorePath);
        def S3response;
        request = message.get(_REST_REQUEST) as ConnectivityRequestRest;
        logger.debug("\nBody : " + request.getBody());

        String[] pathParams = request.getPathParamArray();
        env = pathParams[1];
        interfaceName = pathParams[2];

        String requestBody = request.getBodyAsJson().toString();

        JSONObject requestObject = new JSONObject(requestBody);
        String requestData = requestObject.get(RQ_REQUEST_BODY);
        JSONObject requestHeader = requestObject.getJSONObject(RQ_REQUEST_HEADERS);
        JSONObject edaCacheParams = requestObject.getJSONObject(RQ_CACHE_PARAMS);
        JSONObject edaCacheAuth = requestObject.getJSONObject(RQ_CACHE_AUTH);

        //Get eda cache params

        Iterator<String> keys = edaCacheParams.keys();

        while (keys.hasNext()) {
            String key = keys.next();
            if (key.toString().toLowerCase().startsWith("uid")) {
                uids.add(edaCacheParams.get(key).toString());
            } else if (key.toString().equalsIgnoreCase(RQ_SERVICE_URL)) {
                serviceURL = edaCacheParams.get(key);
            }else if(key.toString().equalsIgnoreCase(RQ_READ_TIMEOUT)) {
                readTimeout = Integer.parseInt(edaCacheParams.get(key).toString());
            }
            else if(key.toString().equalsIgnoreCase(RQ_CONNECT_TIMEOUT)) {
                connectTimeout = Integer.parseInt(edaCacheParams.get(key).toString());
            }
            else {
                maxDays = Integer.parseInt(edaCacheParams.get(key).toString());
            }
        }

        //Get S3 credentials
        keys = edaCacheAuth.keys();

        while (keys.hasNext()) {
            String key = keys.next();
            if (key.toString().toLowerCase().startsWith("access")) {
                accessKey = edaCacheAuth.get(key);
            } else if (key.toString().toLowerCase().startsWith("secret")) {
                secretKey = edaCacheAuth.get(key);
            } else {
                bucket = edaCacheAuth.get(key);
            }
        }

        //Get original headers
        keys = requestHeader.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            reqHdrs.put(key, requestHeader.get(key));
        }

        logger.info("Request XML : " + requestData);
        logger.info("Content-Type : " + requestHeader.get("Content-Type"));
        logger.info("Service URL : " + serviceURL);
        for (String uid : uids) {
            logger.info("UID : " + uid);
        }
        logger.info("Max days : " + maxDays);
        logger.info("Access ID :" + accessKey);
        logger.info("Secret Key :" + secretKey);
        logger.info("Bucket : " + bucket);
        logger.info("Environment : " + env);
        logger.info("Interface : " + interfaceName);

        AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accessKey, secretKey);

        // Set up S3 client
        s3 = S3Client.builder()
                .region(Region.AP_SOUTHEAST_2) // Replace with your desired region
                .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
                .build();

        boolean cacheEnabled = bucket != "NOCACHE";
        boolean hasUids = false;

        if (cacheEnabled) {
            if (uids.size() > 0) {
                hasUids = true;
            }

            if (hasUids) {
                logger.info("Looking for cache entry...");
            } else {
                logger.info "No cache UID provided, skipping cache lookup";
            }

            cache = hasUids ? download(s3, bucket, env, interfaceName, uids) : null;
            //logger.info("Cache Date:"+cache.created);
            if (cache) {
/*                logger.info("Setting values to response body:\n"+
                        "https_status:"+_HTTP_STATUS_OK+"\n"+
                        "cache-data:"+cache.data.toString()+"\n"+
                        "cache-found:"+cacheFound+"\n"+
                        "cache-key:"+cache.key+"\n"+
                        "cache-date:"+cache.createdDateString());*/
                if (isCurrent(cache.get("created") as Date,maxDays)) {
                    Map headers = ["Content-Type": "application/xml"];
                    cacheFound = "Y";
                    response = new ConnectivityResponseRest();
                    response.setStatusCode(_HTTP_STATUS_OK);
                    logger.info("cache-found:"+cacheFound+"\n"+
                            "cache-key:"+ cache.get("key")+"\n"+
                            "cache-date:"+ createdDateString(cache.get("created") as Date,DATE_PATTERN));
                    HashMap respBody = createResponse(_HTTP_STATUS_OK.toString(),cache.data.toString(),headers,cacheFound,cache.key.toString(),createdDateString((Date)cache.created,DATE_PATTERN));
                    response.setBody(respBody);
                    message.put(_REST_RESPONSE, response);
                    return _TR_SUCCESS;
                } else {
                    logger.info "Cache data expired ${cache.key} | ${cache.version} | ${cache.created}";
                    cacheFound = "E";
                }
            } else {
                logger.info "No cache found for given UIDs";
                cacheFound = "N";
            }
        }
        logger.info "Calling service @ $serviceURL";

        def resp =
                performCall(serviceURL, "POST", requestData, reqHdrs,connectTimeout,readTimeout);
        //logger.info "Service response :$resp"
        if (resp != null){
            if (resp.isError()) {
                if (resp.isConnectTimeout()) {
                    errorMap.put("Error Code", "E003");
                    errorMap.put("Error Message", resp.getErrorText());
                    response = new ConnectivityResponseRest();
                    response.setStatusCode(_HTTP_STATUS_ERROR);
                    response.setBody(errorMap);
                    message.put(_REST_RESPONSE, response);
                    return _TR_ERROR;
                } else if (resp.isReadTimeout()) {
                    errorMap.put("Error Code", "E004");
                    errorMap.put("Error Message", resp.getErrorText());
                    response = new ConnectivityResponseRest();
                    response.setStatusCode(_HTTP_STATUS_ERROR);
                    response.setBody(errorMap);
                    message.put(_REST_RESPONSE, response);
                    return _TR_ERROR;
                }
            }
        }else{
            errorMap.put("Error Code", "E005");
            errorMap.put("Error Message", "Error calling the service URL");
            response = new ConnectivityResponseRest();
            response.setStatusCode(_HTTP_STATUS_ERROR);
            response.setBody(errorMap);
            message.put(_REST_RESPONSE, response);
            return _TR_ERROR;

        }

        if (hasUids && !resp.isError() && resp.returnText && checkRespForErrors(resp.returnText)) {
            Map cache = new HashMap();
            cache.put("data",resp.returnText);
            cache.put("created",new Date());

            logger.info "Uploading new cache entry...";
            upload(s3, bucket, env, interfaceName, uids, cache)
        } else {
            logger.info "Response not eligible for cache entry, skipping";
        }

        s3.close();

        response = new ConnectivityResponseRest();
        response.setStatusCode(_HTTP_STATUS_OK);
        response.setBody(createResponse(resp.httpRetCode.toString(), resp.returnText.toString(), resp.getHeaders(), "", "", ""));
        message.put(_REST_RESPONSE, response);

        return _TR_SUCCESS;

    }

    static String createdDateString(Date created, String format) {
        if (!created) return "-"
        return new SimpleDateFormat(format).format(created)
    }

    static boolean isCurrent(Date created, int maxDays) {
        if (!created) return false
        def now = System.currentTimeMillis()
        def elapsedDays = (now - created.time) / 3600000 / 24
        return (elapsedDays <= maxDays)
    }

    static HashMap createResponse(String httpCode, String responseBody, Map respHeaders, String cacheFound, String cacheKey, String cacheDate)
    {
        HashMap mainResponse = new HashMap();
        Map responseHdrs = new HashMap();
        Map edaCacheResults = new HashMap();
        Map headers = [:]
        mainResponse.put(RS_HTTP_CODE,httpCode);
        mainResponse.put(RS_BODY,responseBody);
        for (def entry:respHeaders.entrySet())
        {
            headers.put(entry.key,entry.value);

        }
        responseHdrs.putAll(headers);
        mainResponse.put(RS_HEADERS,responseHdrs);
        edaCacheResults.put(RS_MATCH_FOUND,cacheFound);
        edaCacheResults.put(RS_MATCH_KEY,cacheKey);
        edaCacheResults.put(RS_MATCH_DATE,cacheDate);
        mainResponse.put(RS_CACHE_BLOCK,edaCacheResults);

        return mainResponse;
    }

    static Map download(S3Client s3, String bucket, String env, String interfaceName, List<String> uids) {

        Map cacheEntry=new HashMap();
        ListObjectsV2Request req;
        def res;

        for (String uid:uids){
            String prefix = "$env/$interfaceName"
            prefix += "/$uid"
            prefix += "/$CACHE_OBJECT_KEY"

            logger.info "Looking for cache entry $prefix";

            req = ListObjectsV2Request.builder()
                    .bucket(bucket)
                    .prefix(prefix)
                    .build();
            res = s3.listObjectsV2(req);
            if (res.contents().size() == 1) {
                break;
            }
        }
        if(res.contents().size() == 0)
        {
            return null;
        }
        def key = res.contents().first().key();
        logger.info "Found potential matching entry with key $key";
        HeadObjectRequest hor = HeadObjectRequest.builder().bucket(bucket).key(key).build();
        HeadObjectResponse ho = s3.headObject(hor);
        def meta = ho.metadata();
        logger.info "Metadata - $meta";

        GetObjectRequest gor = GetObjectRequest.builder().key(key).bucket(bucket).build();
        ResponseBytes go = s3.getObjectAsBytes(gor);
        def bis = new GZIPInputStream(go.asInputStream());
        def response = new String(bis.bytes, StandardCharsets.UTF_8);
        logger.info "Obtained response: $response";
        def dat = meta["metadax-amz-meta-cache-date"] ?: "19010101_000000Z";

        cacheEntry.put("data",response);
        cacheEntry.put("created",new SimpleDateFormat(S3_DATE_PATTERN).parse(dat));
        cacheEntry.put("key",key);
        cacheEntry.put("version",gor.versionId().toString());
        return cacheEntry;
    }


    static void upload(S3Client s3, String bucket, String env, String interfaceName, List<String> uids, Map cache) {

        for(String uid:uids){
            String prefix = "$env/$interfaceName";
            prefix += "/$uid";
            prefix += "/$CACHE_OBJECT_KEY";

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            def gz = new GZIPOutputStream(bos);
            gz.write(cache.get("data").toString().getBytes(StandardCharsets.UTF_8));
            gz.close();
            def bytes = bos.toByteArray();

            Map<String, String> metadata = [:];
            metadata["metadax-amz-meta-cache-date"] = new SimpleDateFormat(S3_DATE_PATTERN).format(cache.get("created"));

            logger.info "Uploading for cache entry $prefix - metadata = $metadata";
            PutObjectRequest req = PutObjectRequest.builder()
                    .bucket(bucket)
                    .key(prefix)
                    .metadata(metadata)
                    .build();

            PutObjectResponse resp = s3.putObject(req, RequestBody.fromBytes(bytes));

            cache.key = prefix;
            cache.version = resp.versionId();
        }

        logger.info "Cache successfully uploaded: $cache";
    }


    CallResponse performCall(String url, String method, String request, Map originalHeaders, int connectTimeout, int readTimeout) {
        def params
        CallResponse response=null;

        try {
        
            params = new CallParameters(
                    url: url,
                    method: method,
                    request: request,
                    trustStoreFile: trustStorePath,
                    connectTimeout: connectTimeout,
                    readTimeout: readTimeout
            );
    
            params.requestHeaders = originalHeaders;
    
            response = params.submit("proxy-${System.currentTimeMillis()}");
        }
        catch(Throwable e)
        {
            logger.error(e.printStackTrace())
        }
        return response;

    }

    static boolean checkRespForErrors (String resp)
    {
        def responseCheck = new XmlSlurper().parseText(resp);
        boolean result = responseCheck.Body.Fault.faultcode.isEmpty();
        return result;
    }
} 

Groovy online compiler

Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.

Read inputs from stdin

OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.

def name = System.in.newReader().readLine()
println "Hello " + name

About Groovy

Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.

Key Features

  • It's not a replacement for java but it's an enhancer to Java with extra features like DSL support, dynamic typing, closures etc.
  • Accepts Java code as it extends JDK
  • Greater flexibility
  • Concise and much simpler compared to Java
  • Can be used as both programming language and scripting language.

Syntax help

Data Types

Data typeDescriptionRange
StringTo represent text literalsNA
charTo represent single character literalNA
intTo represent whole numbers-2,147,483,648 to 2,147,483,647
shortTo represent short numbers-32,768 to 32,767
longTo represent long numbers-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
doubleTo represent 64 bit floating point numbers4.94065645841246544e-324d to 1.79769313486231570e+308d
floatTo represent 32 bit floating point numbers1.40129846432481707e-45 to 3.40282346638528860e+38
byteTo represent byte value-128 to 127
booleanTo represent boolean values either true or falseTrue or False

Variables

You can define variables in two ways

Syntax:

data-type variable-name;

[or]

def variable-name;

Loops

0.upto(n) {println "$it"}

or

n.times{println "$it"}

where n is the number of loops and 0 specifies the starting index

Decision-Making

1. If / Nested-If / If-Else:

When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.

if(conditional-expression) {
  // code
} else {
  // code
}

2. Switch:

Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

List

List allows you to store ordered collection of data values.

Example:

def mylist = [1,2,3,4,5];
List MethodsDescription
size()To find size of elements
sort()To sort the elements
add()To append new value at the end
contains()Returns true if this List contains requested value.
get()Returns the element of the list at the definite position
pop()To remove the last item from the List
isEmpty()Returns true if List contains no elements
minus()This allows you to exclude few specified elements from the elements of the original
plus()This allows you to add few specified elements to the elements of the original
remove()To remove the element present at the specific position
reverse()To reverse the elements of the original List and creates new list