Thursday, 21 November 2024

Groovy Script to form the dynamic http query for top and skip

When cant use the ODATA adapter if there is limitation on pulling the records from API

For ODATA - Using looping process can achieve it

For HTTP - there is no easy way so you can use this script and use iterating splitter and gather, in between use the request reply to pull 100, 100 records to get all records.

Groovy script


import com.sap.gateway.ip.core.customdev.util.Message;

import groovy.xml.*;

import java.text.SimpleDateFormat;

import groovy.json.*;

import groovy.util.*;

import groovy.xml.MarkupBuilder;

def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String



def headers = message.getHeaders();

def count = headers.get("count");

count = count.toInteger();



// def count = 640;

int chunksize = 100



int iteration = Math.ceil(count / chunksize).toInteger()



StringWriter writer = new StringWriter()

MarkupBuilder xml = new MarkupBuilder(writer)



xml.Nodes {

for(int i=0;i < iteration;i++){

int skip = i * chunksize

println skip

int top = (i == iteration - 1)?(count % chunksize == 0 ? chunksize : count % chunksize): chunksize

//println top

Node {



if (skip == 0) {

query "\$top=${top}"

}

else{

query "\$top=${top}&\$skip=${skip}"

}

}

}
}


def resultXML =writer.toString()

message.setBody(resultXML)

return message;

}

Input => Header => Count =640


Output:


<Nodes>

<Node>

<query>$top=100</query>

</Node>

<Node>

<query>$top=100&amp;$skip=100</query>

</Node>

<Node>

<query>$top=100&amp;$skip=200</query>

</Node>

<Node>

<query>$top=100&amp;$skip=300</query>

</Node>

<Node>

<query>$top=100&amp;$skip=400</query>

</Node>

<Node>

<query>$top=100&amp;$skip=500</query>

</Node>

<Node>

<query>$top=40&amp;$skip=600</query>

</Node>

</Nodes>




Groovy Script to use the mod and divide on the count in sap cpi

Groovy script

import com.sap.gateway.ip.core.customdev.util.Message;

import groovy.xml.*;

import java.text.SimpleDateFormat;

import groovy.json.*;

import groovy.util.*;

import groovy.xml.MarkupBuilder;

def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String



def headers = message.getHeaders();

def count = headers.get("count");

count = count.toInteger()

// def count = 340;

int ct = count / 100

int remain = count % 100



StringWriter writer = new StringWriter()

MarkupBuilder xml = new MarkupBuilder(writer)



xml.Nodes {

(1..ct).each { index ->

Node {



Value(100)

}

}

if(remain > 0 ){

Node{

Value(remain)

}

}

}


def resultXML = writer.toString()

message.setBody(resultXML)

return message;

}

Input:

CM - header

 count = 340;

Output:



<Nodes>

<Node>

<Value>100</Value>

</Node>

<Node>

<Value>100</Value>

</Node>

<Node>

<Value>100</Value>

</Node>

<Node>

<Value>40</Value>

</Node>

</Nodes>





Wednesday, 13 November 2024

Review naming script

  

URL: https://xxx.hana.ondemand.com/api/v1/IntegrationDesigntimeArtifacts(Id='${property.iflowName}',Version=%27active%27)/Configurations

 

Script:

 

/* Refer the link below to learn more about the use cases of script.

https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/148851bf8192412cba1f9d2c17f4bd25.html

 

If you want to know more about the SCRIPT APIs, refer the link below

https://help.sap.com/doc/a56f52e1a58e4e2bac7f7adbf45b2e26/Cloud/en-US/index.html */

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

 

def Message processData(Message message) {

    //Body

    def body = message.getBody(java.io.Reader);

    def ConfigurationValues        = new XmlSlurper().parse(body)

 

 

def externalizedParameters =  ["HTTP_Sender_Address",

                              "HTTP_Sender_Authorization",

                              "HTTP_Sender_UserRole",

                              "HTTP_Sender_CSRFProtected",

                              "HTTP_Sender_BodySize",

                              "HTTP_Receiver_Address",

                              "HTTP_Receiver_Query",

                              "HTTP_Receiver_ProxyType",

                              "HTTP_Receiver_LocationID",

                              "HTTP_Receiver_Method",

                              "HTTP_Receiver_SendBody",

                              "HTTP_Receiver_Expression",

                              "HTTP_Receiver_Authentication",

                              "HTTP_Receiver_CredentialName",

                              "HTTP_Receiver_PrivateKeyAlias",

                              "HTTP_Receiver_Timeout",

                              "HTTP_Receiver_ThrowException",

                              "HTTP_Receiver_RequestHeader",

                              "HTTP_Receiver_ResponseHeader",

                              "SOAP_Sender_Address",

                              "SOAP_Sender_URL_To_WSDL",

                              "SOAP_Sender_Authorization",

                              "SOAP_Sender_UserRole",

                              "SOAP_Sender_BodySize",

                              "SOAP_Sender_AttachmentSize",

                              "SOAP_Sender_MessageExchangePattern",

                              "SOAP_Receiver_Address",

                              "SOAP_Receiver_URL_To_WSDL",

                              "SOAP_Receiver_Service",

                              "SOAP_Receiver_EndPoint",

                              "SOAP_Receiver_OperationName",

                              "SOAP_Receiver_ProxyType",

                              "SOAP_Receiver_LocationID",

                              "SOAP_Receiver_Authentication",

                              "SOAP_Receiver_CredentialName",

                              "SOAP_Receiver_PrivateKeyAlias",

                              "SOAP_Receiver_Timeout",

                              "SOAP_Receiver_KeepAlive",

                              "SOAP_Receiver_CompressMessage",

                              "SOAP_Receiver_AllowChunking",

                              "SOAP_Receiver_ReturnHTTPResponseCode",

                              "SOAP_Receiver_CleanUpRequestHeader",

                              "SOAP_Receiver_SAPRMMessageIdDetermination",

                              "AMQP_Sender_Host",

                              "AMQP_Sender_Port",

                              "AMQP_Sender_Path",

                              "AMQP_Sender_ProxyType",

                              "AMQP_Sender_LocationID",

                              "AMQP_Sender_ConnectWithTLS",

                              "AMQP_Sender_Authentication",

                              "AMQP_Sender_CredentialName",

                              "AMQP_Sender_QueueName",

                              "AMQP_Sender_NumberOfConcurrent",

                              "AMQP_Sender_MaxNumberOfPrefetchedMessage",

                              "AMQP_Sender_ConsumeExpiredMessage",

                              "AMQP_Sender_MaxNumberOfRetries",

                              "AMQP_Sender_DeliveryStatusAfterMaxRetries",

                              "AMQP_Receiver_Host",

                              "AMQP_Receiver_Port",

                              "AMQP_Receiver_ProxyType",

                              "AMQP_Receiver_Path",

                              "AMQP_Receiver_ConnectWithTLS",

                              "AMQP_Receiver_LocationID",

                              "AMQP_Receiver_Authentication",

                              "AMQP_Receiver_PrivateKeyAlias",

                              "AMQP_Receiver_CredentialName",

                              "AMQP_Receiver_ConnectWithTLS",

                              "AMQP_Receiver_DestinationType",

                              "AMQP_Receiver_DestinationName",

                              "AMQP_Receiver_ExpirationPeriod",

                              "AMQP_Receiver_Delivery",

                              "AMQP_Receiver_MessageType",

                              "AMQP_Receiver_HeaderFormat",

                              "IDOC_Sender_Address",

                              "IDOC_Sender_Authorization",

                              "IDOC_Sender_UserRole",

                              "IDOC_Sender_InvalidXmlCharacterHandling",

                              "IDOC_Sender_BodySize",

                              "IDOC_Sender_AttachmentSize",

                              "IDOC_Receiver_Address",

                              "IDOC_Receiver_ProxyType",

                              "IDOC_Receiver_LocationID",

                              "IDOC_Receiver_IDocContentType",

                              "IDOC_Receiver_Authentication",

                              "IDOC_Receiver_TimeOut",

                              "IDOC_Receiver_CompressMessage",

                              "IDOC_Receiver_AllowChunking",

                              "IDOC_Receiver_ReturnHTTPResponseCodeAsHeader",

                              "IDOC_Receiver_CleanupRequestHeaders",

                              "IDOC_Receiver_KeepAlive",

                              "IDOC_Receiver_CredentialName",

                              "JDBC_Receiver_JDBCDataSourceAlias",

                              "JDBC_Receiver_ConnectionTimeout",

                              "JDBC_Receiver_QueryResponseTimeout",

                              "JDBC_Receiver_MaximumRecords",

                              "JDBC_Receiver_BatchMode",

                              "JDBC_Receiver_BatchOperation",

                             

                              "RFC_Receiver_Destination",

                              "RFC_Receiver_SendConfirmTransaction",

                              "RFC_Receiver_CreateNewConnection",

                              "JMS_Sender_QueueName",

                              "JMS_Sender_NumberOfConcurrentProcesses",

                              "JMS_Sender_RetryInterval",

                              "JMS_Sender_ExponentialBackoff",

                              "JMS_Sender_MaximumRetryInterval",

                              "JMS_Sender_DeadLetterQueue",

                              "JMS_Receiver_QueueName",

                              "JMS_Receiver_RetentionThresholdForAlerting",

                              "JMS_Receiver_ExpirationPeriod",

                              "JMS_Receiver_CompressStoredMessage",

                              "JMS_Receiver_EncryptStoredMessage",

                              "JMS_Receiver_TransferExchangeProperties",

                              "SFTP_Sender_Directory",

                              "SFTP_Sender_FileName",

                              "SFTP_Sender_Address",

                              "SFTP_Sender_ProxyType",

                              "SFTP_Sender_LocationID",

                              "SFTP_Sender_Authentication",

                              "SFTP_Sender_CredentialName",

                              "SFTP_Sender_UserName",

                              "SFTP_Sender_PrivateKeyAlias",

                              "SFTP_Sender_Timeout",

                              "SFTP_Sender_MaximumReconnectAttempts",

                              "SFTP_Sender_ReconnectDelay",

                              "SFTP_Sender_AutomaticallyDisconnect",

                              "SFTP_Sender_EnableSupporDeprecatedAlgorithms",

                              "SFTP_Sender_ReadLockStrategy",

                              "SFTP_Sender_Sorting",

                              "SFTP_Sender_SortingOrder",

                              "SFTP_Sender_MaxMessagesPerPoll",

                              "SFTP_Sender_LockTimeout",

                              "SFTP_Sender_ChangeDirectoriesStepwise",

                              "SFTP_Sender_IncludeSubdirectories",

                              "SFTP_Sender_FlattenFileNames",

                              "SFTP_Sender_UseFastExistsCheck",

                              "SFTP_Sender_PostProcessing",

                              "SFTP_Sender_IdempotentRepository",

                              "SFTP_Sender_ArchiveDirectory",

                              "SFTP_Sender_Scheduler",

                              "SFTP_Poll_Directory",

                              "SFTP_Poll_FileName",

                              "SFTP_Poll_Address",

                              "SFTP_Poll_ProxyType",

                              "SFTP_Poll_LocationID",

                              "SFTP_Poll_Authentication",

                              "SFTP_Poll_CredentialName",

                              "SFTP_Poll_UserName",

                              "SFTP_Poll_PrivateKeyAlias",

                              "SFTP_Poll_Timeout",

                              "SFTP_Poll_MaximumReconnectAttempts",

                              "SFTP_Poll_ReconnectDelay",

                              "SFTP_Poll_AutomaticallyDisconnect",

                              "SFTP_Poll_ReadLockStrategy",

                              "SFTP_Poll_ChangeDirectoriesStepwise",

                              "SFTP_Poll_IncludeSubdirectories",

                              "SFTP_Poll_UseFastExistsCheck",

                              "SFTP_Poll_PostProcessing",

                              "SFTP_Poll_ArchiveDirectory",

                              "SFTP_Poll_ArchiveDirectory",

                              "SFTP_Receiver_Directory",

                              "SFTP_Receiver_FileName",

                              "SFTP_Receiver_AppendTimestamp",

                              "SFTP_Receiver_Address",

                              "SFTP_Receiver_ProxyType",

                              "SFTP_Receiver_LocationID",

                              "SFTP_Receiver_Authentication",

                              "SFTP_Receiver_CredentialName",

                              "SFTP_Receiver_UserName",

                              "SFTP_Receiver_PrivateKeyAlias",

                              "SFTP_Receiver_Timeout",

                              "SFTP_Receiver_MaximumReconnectAttempts",

                              "SFTP_Receiver_ReconnectDelay",

                              "SFTP_Receiver_AutomaticallyDisconnect",

                              "SFTP_Receiver_EnableSupporDeprecatedAlgorithms",

                              "SFTP_Receiver_ChangeDirectoriesStepwise",

                              "SFTP_Receiver_CreateDirectories",

                              "SFTP_Receiver_FlattenFileNames",

                              "SFTP_Receiver_PreventDirectoryTraversal",

                              "SFTP_Receiver_UseFastExistsTest",

                              "SFTP_Receiver_HandlingForExistingFiles",

                              "SFTP_Receiver_UseTemporaryFile",

                              "SFTP_Receiver_TemporaryFileName",

                              "PD_Sender_Address",

                              "PD_Receiver_Address",

                              "AS2_Sender_Address",

                              "AS2_Sender_Authorization",

                              "AS2_Sender_UserRole",

                              "AS2_Sender_ClientCertificateAuthorization",

                              "AS2_Sender_Source",

                              "AS2_Sender_MessageIDLeftPart",

                              "AS2_Sender_MessageIDRightPart",

                              "AS2_Sender_PartnerAS2ID",

                              "AS2_Sender_OwnAS2ID",

                              "AS2_Sender_MessageSubject",

                              "AS2_Sender_MessageSettings",

                              "AS2_Sender_MandatoryFileName",

                              "AS2_Sender_DuplicateMessageID",

                              "AS2_Sender_DuplicateFileName",

                              "AS2_Sender_DecryptionConfigurationType",

                              "AS2_Sender_DecryptMessage",

                              "AS2_Sender_PrivateKeyAlias",

                              "AS2_Sender_VerifySignature",

                              "AS2_Sender_PublicKeyAlias",

                              "AS2_Sender_PublicKeyAliases",

                              "AS2_Sender_PrivateKeyAliasforSignature",

                              "AS2_Sender_SignatureEncoding",

                              "AS2_Sender_PropagateMDNDetailstoExchange",

                              "AS2_Sender_ProxyType",

                              "AS2_Sender_ProxyType",

                              "AS2_Sender_Authentication",

                              "AS2_Sender_CredentialName",

                              "AS2_Sender_CredentialName",

                              "AS2_Sender_Timeout",

                              "AS2_Sender_QualityofService",

                              "AS2_Sender_RetryInterval",

                              "AS2_Sender_ExponentialBackoff",

                              "AS2_Sender_MaximumRetryInterval",

                              "AS2_Sender_DeadLetterQueue",

                              "AS2_Sender_EncryptMessageDuringPersistence",

                              "AS2_Sender_BodySize",

                              "AS2_Sender_AttachmentsSize",

                              "AS2_Sender_Address",

                              "AS2_Sender_Authorization",

                              "AS2_Sender_UserRole",

                              "AS2_Sender_ClientCertificateAuthorization",

                              "AS2_Sender_Source",

                              "AS2_Sender_PartnerAS2ID",

                              "AS2_Sender_OwnAS2ID",

                              "AS2_Sender_NumberofConcurrentProcesses",

                              "AS2_Sender_VerifySignature",

                              "AS2_Sender_PulicKeyAlias",

                              "AS2_Sender_VerifyMIC",

                              "AS2_Sender_MessageFailureonNegativeMDN",

                              "AS2_Sender_RetryInterval",

                              "AS2_Sender_ExponentialBackoff",

                              "AS2_Sender_MaximumRetryInterval",

                              "AS2_Sender_DeadLetterQueue",

                              "AS2_Sender_EncryptMessageDuringPersistence",

                              "AS2_Receiver_ReceipientURL",

                              "AS2_Receiver_URLParametersPattern",

                              "AS2_Receiver_ProxyType",

                              "AS2_Receiver_LocationID",

                              "AS2_Receiver_AuthenticationType",

                              "AS2_Receiver_PrivateKeyAlias",

                              "AS2_Receiver_CredentialName",

                              "AS2_Receiver_Timeout",

                              "AS2_Receiver_FileName",

                              "AS2_Receiver_MessageIDLeftPart",

                              "AS2_Receiver_MessageIDRightPart",

                              "AS2_Receiver_OwnAS2ID",

                              "AS2_Receiver_PartnerAS2ID",

                              "AS2_Receiver_MessageSubject",

                              "AS2_Receiver_OwnEmailAddress",

                              "AS2_Receiver_ContentType",

                              "AS2_Receiver_CustomHeadersPattern",

                              "AS2_Receiver_ContentTransferEncoding",

                              "AS2_Receiver_CompressMessage",

                              "AS2_Receiver_SignMessage",

                              "AS2_Receiver_Algorithm",

                              "AS2_Receiver_PrivateKeyAlias",

                              "AS2_Receiver_EncryptMessage",

                              "AS2_Receiver_Algorithm",

                              "AS2_Receiver_PublicKeyAlias",

                              "AS2_Receiver_KeyLength",

                              "AS2_Receiver_Type",

                              "AS2_Receiver_TargetURL",

                              "AS2_Receiver_RequestSigning",

                              "AS2_Receiver_Algorithm",

                              "AS2_Receiver_VerifySignature",

                              "AS2_Receiver_PublicKeyAlias",

                              "AS2_Receiver_RequestMIC",

                              "AS2_Receiver_VerifyMIC",

                              "AS2_Receiver_MessageFailureonNegativeMDN",

                              "ODATA_Sender_Authorization",

                                "ODATA_Sender_UserRole",

                                "ODATA_Sender_ClientCertificate",

                                "ODATA_Sender_EDMX",

                                "ODATA_Sender_Operation",

                                "ODATA_Sender_EntitySet",

                                "ODATA_Receiver_Address",

                                "ODATA_Receiver_ProxyType",

                                "ODATA_Receiver_LocationID",

                                "ODATA_Receiver_Authentication",

                                "ODATA_Receiver_CredentialName",

                                "ODATA_Receiver_PrivateKeyAlias",

                                "ODATA_Receiver_CSRFProtected",

                                "ODATA_Receiver_ReuseConnection",

                                "ODATA_Receiver_OperationDetails",

                                "ODATA_Receiver_ResourcePath",

                                "ODATA_Receiver_QueryOptions",

                                "ODATA_Receiver_Fields",

                                "ODATA_Receiver_EnableBatchProcessing",

                                "ODATA_Receiver_CustomQueryOptions",

                                "ODATA_Receiver_ContentType",

                                "ODATA_Receiver_ContentTypeEncoding",

                                "ODATA_Receiver_PageSize",

                                "ODATA_Receiver_ProcessinPage",

                                "ODATA_Receiver_Timeout",

                                "ODATA_Receiver_HEADERDETAILS",

                                "ODATA_Receiver_MetadataDetails"

 

]

 

        def configureParameter = ["MPL_Sender",

                "MPL_Receiver",

                "MPL_MessageType",

                "MPL_CustHeader",

                "DefaultCustMPLHeader1",

                "DefaultCustMPLHeader2",

                "LogPayload",

                "IntegrationPackageName",

                "Severity",

                "BusinessGroup",

                "SensitiveData",

                "MaxJMSRetryCount",

                "Query",

                "IDOC_RCVPRN",

                "IDOC_SNDPRN",

                "IDOC_SNDPOR",

                "IDOC_RCVPOR",

              "QueryLimit",

              "QueryOffset",

                "Environment",

                "LogOnException"]

                   

def mandatoryExternalizedParameter = ["MPL_Sender",

                                      "MPL_Receiver",

                                      "MPL_MessageType",

                                      "MPL_CustHeader",

                                      "DefaultCustMPLHeader1",

                                      "DefaultCustMPLHeader2",

                                      "LogPayload",

                                      "LogOnException",

                                      "IntegrationPackageName",

                                      "Severity",

                                      "BusinessGroup",

                                      "SensitiveData",

                                      "Environment"]

 

 

StringBuilder incorrectNamingConvention = new StringBuilder()

ConfigurationValues.entry.each{ r ->

    def ParameterKey = r.content.properties.ParameterKey.text()

    def takeCount = ParameterKey.toString().count("_")

 

    if(ParameterKey.toString().startsWith("Cust_") == false ){

 

        if (takeCount >= 3 ){

            def ParameterKeySPlit = ParameterKey.toString().split("_").getAt(0)+"_"+ParameterKey.toString().split("_").getAt(1)+"_"+ParameterKey.toString().split("_").getAt(2)

 

            if (externalizedParameters.contains(ParameterKeySPlit) == false){

 

                incorrectNamingConvention.append('Externalized Parameter "').append(ParameterKey).append('" is not as per KCC Naming convention').append("\n")

 

            }

 

 

        }

        else if (configureParameter.contains(ParameterKey) == false){

            incorrectNamingConvention.append('Externalized Parameter "').append(ParameterKey).append('" is not as per KCC Naming convention').append("\n")

 

        }

 

        // else {

 

        //     incorrectNamingConvention.append('Externalized Parameter "').append(ParameterKey).append('" is not as per KCC Naming convention').append("\n")

 

        // }

 

    }

 

}

 

ConfigurationValuesMaps = []

ConfigurationValues.entry.each{ r ->

    def ParameterKey = r.content.properties.ParameterKey.text()

    ConfigurationValuesMaps.add(ParameterKey)

}

 

 

mandatoryExternalizedParameter.each { r->

    if(ConfigurationValuesMaps.contains(r) == false){

 

        incorrectNamingConvention.append('Mandatory Externalize parameter "').append(r).append('" is missing').append("\n")

 

    }

}

   

    message.setBody(incorrectNamingConvention);

    return message;

}