on 04-07-2015 10:50 AM
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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");
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
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 |
} |
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.