cancel
Showing results for 
Search instead for 
Did you mean: 

any adapter to connect to 3rd party via Socket TCP/IP in P7.4is ?

Former Member
0 Kudos

Hello,

Anybody know how to connect to 3rd party system via socket tcp/ip.

is there any adapter to implement it via configuration?

i searched SCN ,but only one solution with Java code is fine with my requirement

Thanks ahead

Kevin Gao

Accepted Solutions (1)

Accepted Solutions (1)

former_member182412
Active Contributor
0 Kudos

Hi Kevin,

Please check Michal Krawczyk answer in below thread.

>>>>What are the different ways to integrate these systems with SAP PI?

there's also a seeburger adapter as far as I remember to communicate over TCP,

Regards,

Praveen.

Former Member
0 Kudos

Hi Praveen,

i already go through this solution , but i don't know how to complete it,

saying Seeburger, customer won't pay for the third adapter

any other solution is available?

Thanks

Kevin

Former Member
0 Kudos

Hello Praveen,

i tried the solution as you said, roughly make it out. but i have doubt about the code segment in last part

Writer outWriter = new BufferedWriter(new OutputStreamWriter(

  out.getOutputPayload().getOutputStream()));

  outWriter.write(respFromSocket, 0, respFromSocket.length());

  outWriter.flush();


i am using PI7.4 to realize this transform method, but ccBPM response

<?xml version="1.0" encoding="GB18030"?> only to sender.


could you please finger out how i should write the code about how to output result

Thanks a lot

Kevin Gao

RaghuVamseedhar
Active Contributor
0 Kudos

Kevin,

Please try below code. It will send input of Java Mapping to Socket and give output of Socket as result of Java Mapping.


package javaapplication1;

import com.sap.aii.mapping.api.*;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.util.Arrays;

class NewClass4 extends AbstractTransformation {

    @Override

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {

        try {

            //Connect to socket using IP and port. Then get streams comming from and going into Socket.

            Socket socket = new Socket("hostNameIP", 8080);

            InputStream fromSocket = socket.getInputStream();

            OutputStream toSocket = socket.getOutputStream();

            //Get steams comming into and going out of Java Mapping.         

            InputStream intoMapping = transformationInput.getInputPayload().getInputStream();

            OutputStream outMapping = transformationOutput.getOutputPayload().getOutputStream();

            //Copy Input of Java Mapping into byte array and Send it to Socket.

            byte[] b = new byte[intoMapping.available()];

            intoMapping.read(b);

            toSocket.write(b);

            //Get the response from Socket and Sent it to Output of Java Mapping.

            byte[] bb = new byte[fromSocket.available()];

            fromSocket.read(bb);

            outMapping.write(bb);

        } catch (Exception exception) {

            getTrace().addDebugMessage(exception.getMessage());

            throw new StreamTransformationException(exception.toString());

        }

    }

}

FYI, To handle encoding

byte[] by = new byte[inputstream.available()];

inputstream.read(by);

String strUTF = new String(by, "UTF-8");

Former Member
0 Kudos

Hi Raghu,

thanks for your coding, i am going to test it, but i already found out the bug in previous solution.

by the way, how to paste code in this thread like yours. i 'd like to share code to reader, but i don't know paste code here with good format.

Thanks

Kevin Gao

former_member182412
Active Contributor
0 Kudos

Hi Kevin,

Please find below steps.

Paste your code below.

Regards,

Praveen.

Former Member
0 Kudos

Hello All,

following is transformation part based on


public void transform(TransformationInput in, TransformationOutput out) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

AbstractTrace absTraceLog = this.getTrace();
absTraceLog.addDebugMessage("\n   JavaMapping - Interface -");

try {

// create DOM structure from input XML 
DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse(in.getInputPayload().getInputStream());

// check if the document is null 
if (document != null) {
// extract server, port & request string details from input
// document  
parseInputDocument();
// connect to the server socket and get the response XML as a
// String  

String respFromSocket = getRespFromServerSocket();

// out.getOutputPayload().getOutputStream().write(respFromSocket.getBytes());

// ************************Code To Generate The XML Parsing
// Objects*****************************//
// docout = builder.parse(respFromSocket);
// TransformerFactory tFactory =
// TransformerFactory.newInstance();
// Transformer transformer = tFactory.newTransformer();
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// DOMSource source = new DOMSource(docout);
// transformer.transform(source, new
// StreamResult(out.getOutputPayload().getOutputStream()));

Writer outWriter = new BufferedWriter(new OutputStreamWriter(
out.getOutputPayload().getOutputStream()));
outWriter.write(respFromSocket, 0, respFromSocket.length());
outWriter.flush();

}
} catch (Exception t) {
t.printStackTrace();
}
}

// end execute method
public String getRespFromServerSocket() {
String respFromSocket = new String();
Socket sock = null;
PrintWriter out = null;
BufferedReader in = null;
try {
// Destination HostName 
InetAddress destAddr = InetAddress.getByName(verifyServer);
// Destination port 
int destPort = Integer.parseInt(verifyPort);
// create a client Socket 
sock = new Socket(destAddr, destPort);
out = new PrintWriter(sock.getOutputStream(), true);
in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
// write the request string 
out.println(requestString + "\r\n");
// wait for response from Socket Server 
String line = "";
StringBuffer buffer = new StringBuffer();

while ((line = in.readLine()) != null) {
buffer.append(line);
}
respFromSocket = buffer.toString();
sock.close();
} catch (Exception gen) {
// Implementation not shown, will vary depending on how you wish to
// handle 
// these exceptions. Ideal way will be to create an error XML
// structure 
// createErrorXMLResponse();
return "";
}
return respFromSocket;
}

// Implementation of this method will vary depend on the structure of your
// input xml
public void parseInputDocument() {
try {
Element root = (Element) document.getDocumentElement();
NodeList rc = ((org.w3c.dom.Node) root).getChildNodes();
for (int i = 0; i < rc.getLength(); i++) {
Node detailsNode = (Node) rc.item(i);
NodeList fcList = detailsNode.getChildNodes();
if (fcList.toString().indexOf("Server") != -1)
verifyServer = fcList.item(0).getNodeValue();
if (fcList.toString().indexOf("Port") != -1)
verifyPort = fcList.item(0).getNodeValue();
if (fcList.toString().indexOf("stringinp") != -1)
requestString = fcList.item(0).getNodeValue();
}
} catch (Exception err) {
// Implementation not shown, will vary depending on how you wish to
// handle 
// these exceptions. Ideal way will be to create an error XML
// structure 
// createErrorXMLResponse(); }
}

// end class

}

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello All,

i am encountering new problem, the return message by socket from 3rd party system includes

Chinese character, so system got the messy code . anybody know how to convert the encoding in implementation code.

thanks

Kevin Gao

former_member184720
Active Contributor
0 Kudos

Did you check the blog shared by Raghu which talks about changing encoding using java mapping?