0% found this document useful (0 votes)
255 views

Interview Groovy Script

The document contains code snippets for common Groovy scripting tasks in SAP API Management including: 1. Reading a JSON message body and modifying the structure. 2. Removing whitespace from a message body. 3. Parsing error responses and extracting error messages. 4. Retrieving credentials from a secure store. 5. Counting the number of lines in a message body. 6. Logging message payloads and properties.

Uploaded by

harika kaviti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views

Interview Groovy Script

The document contains code snippets for common Groovy scripting tasks in SAP API Management including: 1. Reading a JSON message body and modifying the structure. 2. Removing whitespace from a message body. 3. Parsing error responses and extracting error messages. 4. Retrieving credentials from a secure store. 5. Counting the number of lines in a message body. 6. Logging message payloads and properties.

Uploaded by

harika kaviti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Interview Groovy Script

1)Reading Json message and creating property:::

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

import java.util.HashMap;

import groovy.json.*

def Message processData(Message message) {

//Body

def jsonOP = message.getBody(String.class);

jsonOP=jsonOP.toString()

def json_to_str=jsonOP.substring(1, jsonOP.length()- 1);

json_to_str="{\"Root\": [{\"Element\":["+json_to_str+"]}]}"

message.setBody(json_to_str);

return message;

2) Space removal Script:

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

import java.util.HashMap;

def Message processData(Message message) {

//Body

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

body = body.replaceAll("\\s","")

// body = body.replaceAll(">\\s*<", "><")

message.setBody(body);

return message;

}
Http Error 400

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

import groovy.json.JsonSlurper

def Message processData(Message message) {

// get a map o2Ef properties

def map = message.getProperties();

// get an exception java class instance

def ex = map.get("CamelExceptionCaught");

if (ex!=null) {

// an http adapter throws an instance of


org.apache.camel.component.ahc.AhcOperationFailedException

if
(ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedEx
ception")) {

def jsonSlurper = new JsonSlurper();

def respayload = jsonSlurper.parseText(ex.getResponseBody());

message.setProperty("errormsg",respayload.message);

return message;

}
Credentials to get

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

import com.sap.it.api.securestore.SecureStoreService;
import com.sap.it.api.securestore.UserCredential;
import com.sap.it.api.securestore.exception.SecureStoreException;
import com.sap.it.api.ITApiFactory;

def Message processData(Message message) {

// Read from properties to make it more dynamic


def mapProperties = message.getProperties();
def credentialName = mapProperties.get("credential_name_property_key");

// Credential specific code


SecureStoreService secureStoreService =
ITApiFactory.getService(SecureStoreService.class, null);
UserCredential userCredential =
secureStoreService.getUserCredential(credentialName);

def user = userCredential.getUsername().toString()


def pass = userCredential.getPassword().toString()

message.setProperty("user", user);
message.setProperty("pass", pass);
return message;
}

Count No lines or records

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

def Message processData(Message message) {

def lineNo = 0;
def lines = message.getBody(java.lang.String) as String;
    lines.eachLine {
    lineNo++} 

message.setProperty("count1",lineNo);
return message;
}

Content Modifier:

Add this in body to print the count of the lines

${property.count1}
Logging of the message:
import com.sap.gateway.ip.core.customdev.util.Message;

def Message processData(Message message) {


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

def messageLog = messageLogFactory.getMessageLog(message);


if(messageLog != null){

messageLog.setStringProperty("Logging", "Printing Payload As Attachment");


messageLog.addAttachmentAsString("Message#1", body, "text/plain");

}
return message;
}

You might also like