cancel
Showing results for 
Search instead for 
Did you mean: 

SAP PI 7.31 - HTTP Java Mapping

arkesh_sharma
Active Participant
0 Kudos

Hi Experts,

I need to write a java mapping to send a http request to remote server, capture its response and then format the response for further processing.

I have searched on Google and I could only find HTTP request and response codes. When I search for PI Java mapping for HTTP request then I hardly find anything.

I only wish to know that if I use the normal java code (found on the internet) to send HTTP request and receive response in my java mapping, will that work? Or, do I need to import some jars and APIs before executing that code?

Thanks,

Arkesh

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Have a look at SAP help "Using lookup API from Java and follow the steps there, for your case especially consider the link under 1.2.

For calls with all other adapters, use the generic lookup API (see: Implementing Lookups Using SystemAccessor).

You have configuration steps explained and java example code.

I implemented lookup with a SOAP adapter according to this and it worked like a charm

arkesh_sharma
Active Participant
0 Kudos

Hi Martin,

Thank You for your response and link.

I am not so sure on the request message structure that is being sent out via the HTTP Receiver Adapter. All I have been given is a http link and the client has asked me to pass the Date as an argument in MMDDYYYY format.

In the code that you provided me, it says:

3. Create a payload according to the data type. // To get a payload object, pass the payload // as java.io.InputStream to one of the following methods: // LookupService.getBinaryPayload() for binary payload, // or LookupService.getTextPayload() for text payloads. Payload payload = LookupService.getXmlPayload(inputStream);

So, it confuses me as I am not sure what should I pass to the receiver HTTP Adapter. Can you please guide me a bit more in this regard ?

Thanks,

Arkesh

Former Member
0 Kudos

First of all, in order to ease testing your HTTP receiver, create a dedicated test flow/integrated configuration for it, you will have better monitoring capabilities of the calls in message monitoring

I guess you need to pass the Date MMDDYYYY as URL parameter, meaning http://host:port?Date=MMDDYYY?

I can think of 2 approaches regarding message protocol setting in HTTP channel:

1) GET

  • Have a look at and set the Main Payload Parameter Name to generate your Date parameter as described there.
  • As message payload, pass MMDDYYY. I am not sure if the mapping runtime/adapter engine likes that.


2) POST

  • Have a look at , instead of HeaderFieldOne, set the Date as UrlParamOne with dynamic configuration in message mapping and configure adapter spec. message attr. in the channel - I hope dynamic conf. works within the Lookup API!
  • Regarding the message payload you could try with an empty message, again I am not sure if the mapping runtime/adapter engine likes that.

Answers (1)

Answers (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Arkesh,

                  The normal code available in web will work in PI too. Before using it in PI you need to format the code in form of java mapping classes accepted by PI. You can find example of java mapping in following blogs

http://techplay.plozzle.com/?p=21

http://www.objectbiz.com/post/5249313454/sap-pi-java-mapping-from-7-0-to-7-1

In normal java code, to send HTTP request the program code starts execution from "main()" function.

Within SAP PI the java mapping codes are not initiated by "main()" function but other methods like "execute()" (PI 7.0 and lower versions) or "transform()" (PI.7.1 and higher versions). Thus codes from internet needs modification before being used in mapping. Then you can send and receive response from your java mapping within PI server. You need jar files to be imported as external libraries within your java project. You can find list of all such library files in this link

  http://wiki.sdn.sap.com/wiki/display/XI/Where+to+get+the+libraries+for+XI+development

Finally you need to work with network team to open the ports to which you need to send and receive response. Generally firewall will prevent you communicate with external servser.

Regards

Anupam

arkesh_sharma
Active Participant
0 Kudos

Thank You so much for your response Anupam.

I had written a small java code based on the link: http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ inside the Execute method as you mentioned.

But now I have ran into trouble. If I run the code on client machine then I get the error as :

java.net.ConnectException: Connection timed out: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)

at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)

at java.net.Socket.connect(Socket.java:529)

at java.net.Socket.connect(Socket.java:478)

at sun.net.NetworkClient.doConnect(NetworkClient.java:163)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:523)

at sun.net.www.http.HttpClient.<init>(HttpClient.java:227)

at sun.net.www.http.HttpClient.New(HttpClient.java:300)

at sun.net.www.http.HttpClient.New(HttpClient.java:317)

at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)

at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)

at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)

at com.sap.pi.http.HTTPRequestAndResponse.checkResponseCode(HTTPRequestAndResponse.java:150)

at com.sap.pi.http.HTTPRequestAndResponse.execute(HTTPRequestAndResponse.java:111)

at com.sap.pi.http.HTTPRequestAndResponse.main(HTTPRequestAndResponse.java:226)

Call to MAIN Complete...

I have used a MAIN Method that calls the Execute method so that I am able to run my code locally on the client machine.

When I run a similar code on my personal system, I get an error saying "403 Forbidden" along with some additional Java Error as shown above.

So, this scenario is confusing me. I am unable to understand which HTTP Link should I call that can give me a proper HTTP Response ?

Thanks,

Arkesh

anupam_ghosh2
Active Contributor
0 Kudos

Hi Arkesh,

                You are working in PI 7.31 system thus finally you need to change the code to include "transform" method in the java class(i.e call "execute" method you wrote from "transform" method.

I have gone through the code you are referring to. Try this obtain the string URL and try to ping the server you are sending the request to

Thus if the URL is "http://www.google.com/search?q=mkyong" check if you are able to telnet or ping google.com from your system. If you do not get a response consult the network team.

Regards

Anupam

arkesh_sharma
Active Participant
0 Kudos

Hi Anupam,

Thank You for your guidance.

I have one small doubt.

Now that I have written my code and I call the execute method inside the Transform method, how can I test my code locally on NWDS ?

Earlier what I used to do to test my code in local machine, I used to create a MAIN method (public static void main() ), create the file streams, pass the file location to those streams and call the execute method inside the MAIN method. That way I used to know what was happening in my code but now I am not sure how to call the transform method in my MAIN method. A little guidance and help in this regard would be really helpful.

P.S.: I checked the TelNet and Ping on my client network. When I type "telnet www.google.com" it gives a connection timeout error and when I type "ping www.google.com" it gives a response. So, I am confused what the problem is. According to the mykong's code, the connection should work and it should give me a response back. 

Thanks,

Arkesh