Seven Stars

Iterate through globalMap

I am sing TalendESB --> tRestRequest. I am able to see all the content of globalMap.get("restRequest").

 

{URI_ABSOLUTE=http://localhost:8088/data/, PATTERN=/data/, OPERATION=b, ALL_HEADER_PARAMS={a=[123], Accept=[application/json, text/plain, */*], accept-encoding=[gzip, deflate, br], Accept-Language=[en-US,en;q=0.8], connection=[keep-alive], Content-Length=[9], content-type=[application/x-www-form-urlencoded], DNT=[1], h1=[header1], Host=[localhost:8088], User-Agent=[Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36]}, VERB=POST, MESSAGE_CONTEXT=org.apache.cxf.jaxrs.impl.tl.ThreadLocalMessageContext@29b4f161, URI_BASE=http://localhost:8088/, CorrelationID=null, PARAMS={HEADER:h1:id_String=header1, FORM:q1:id_String=mydata}, URI=data/, ALL_QUERY_PARAMS={}, URI_REQUEST=http://localhost:8088/data/}

 

I specifically need to extract the PARAMS object...

PARAMS={HEADER:h1:id_String=header1, FORM:q1:id_String=mydata}

 

How can I capture the HEADERS and FORM data in tJavaRow (after tRestRequest)?

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Twelve Stars

Re: Iterate through globalMap

OK, what I thought would work, does work. You will need a bit of Java experience for this, but with that you should be able to extrapolate from the code below which will print out 

 

//The object with the key "restRequest" is a HashMap
java.util.Map<String, Object> myMap = ((java.util.Map<String, Object>)globalMap.get("restRequest"));

//Print out of what you are seeing already
System.out.println("MyMap = " +myMap.toString());

//Create an iterator over the keyset
java.util.Iterator<String> it = myMap.keySet().iterator();

//Iterate over the keyset and print out the keys
while(it.hasNext()){
	System.out.println(it.next());
}

//Retrieve the PARAMS HashMap
java.util.Map<String, Object> myParams = ((java.util.Map<String, Object>)myMap.get("PARAMS"));

//Create an Iterator for the PARAMS HashMap
java.util.Iterator<String> it2 = myParams.keySet().iterator();

//Iterate over the PARAMS HasMap
while(it2.hasNext()){
	System.out.println(it2.next());
}
Rilhia Solutions
8 REPLIES
Twelve Stars

Re: Iterate through globalMap

EDIT: My reply was not relevant after re-looking at you question. I'll be back after trying something 

Rilhia Solutions
Twelve Stars

Re: Iterate through globalMap

OK, what I thought would work, does work. You will need a bit of Java experience for this, but with that you should be able to extrapolate from the code below which will print out 

 

//The object with the key "restRequest" is a HashMap
java.util.Map<String, Object> myMap = ((java.util.Map<String, Object>)globalMap.get("restRequest"));

//Print out of what you are seeing already
System.out.println("MyMap = " +myMap.toString());

//Create an iterator over the keyset
java.util.Iterator<String> it = myMap.keySet().iterator();

//Iterate over the keyset and print out the keys
while(it.hasNext()){
	System.out.println(it.next());
}

//Retrieve the PARAMS HashMap
java.util.Map<String, Object> myParams = ((java.util.Map<String, Object>)myMap.get("PARAMS"));

//Create an Iterator for the PARAMS HashMap
java.util.Iterator<String> it2 = myParams.keySet().iterator();

//Iterate over the PARAMS HasMap
while(it2.hasNext()){
	System.out.println(it2.next());
}
Rilhia Solutions
Seven Stars

Re: Iterate through globalMap

Wow, this is helpful (with the comments). I will try that out and reply back in a few. Thanks @rhall_2_0
Seven Stars

Re: Iterate through globalMap

Hi, I just tried this code, it looks promising. For some reason, at the PARAMS part, it is not printing any values..
I see:
HEADER:h1:id_String
FORM:q1:id_String

I am trying to play around with .values() and seeing if I can get the values out too.

Thanks for the start!
Seven Stars

Re: Iterate through globalMap

So, this worked for me:

while(it2.hasNext()){

String key = it2.next().toString();
String value = myParams.get(key).toString();

System.out.println(key + " " + value);

}
Twelve Stars

Re: Iterate through globalMap

Glad it worked ...or helped you to get to a working solution :-)

Rilhia Solutions
Eight Stars

Re: Iterate through globalMap

Hi,

 

Do you really need to handle this restRequest global variable ?

 

I did test below:

1-My job looks like:

 

Capture.PNG

 

With:

Capture.PNG

 

In my tJavaRow, I can get these two fields:

System.out.println(globalMap.get("restRequest"));
System.out.println("********************H1******************************");
System.out.println(input_row.h1);
System.out.println("********************Q1******************************");
System.out.println(input_row.q1);

 

2-I call this REST webservice from SOAPUI:

Capture.PNG

 

3-Then I get the values of these two parameters (As they are in the global variable):

{URI_ABSOLUTE=http://localhost:8088/TEST, PATTERN=/, OPERATION=OUT, ALL_HEADER_PARAMS={accept-encoding=[gzip,deflate], connection=[keep-alive], Content-Length=[9], content-type=[application/x-www-form-urlencoded], h1=[header1], Host=[localhost:8088], User-Agent=[Apache-HttpClient/4.1.1 (java 1.5)]}, VERB=POST, MESSAGE_CONTEXT=org.apache.cxf.jaxrs.impl.tl.ThreadLocalMessageContext@1f3cbb32, URI_BASE=http://localhost:8088/TEST, CorrelationID=null, PARAMS={HEADER:h1:id_String=header1, FORM:q1:id_String=mydata}, URI=/, ALL_QUERY_PARAMS={}, URI_REQUEST=http://localhost:8088/TEST}
********************H1******************************
header1
********************Q1******************************
mydata

 

Doesn't it meet your requirements ?

 

Eric

Seven Stars

Re: Iterate through globalMap

I can use these; and I am doing so. But there are some places in my code where I simply need to ignore the calls and log them. I do not want to parse the headers and all. Hence, when I found this restRequest GlobalMap, it struck me as I can simply dump this info directly for now and parse it later as needed. 

 

Thanks!