private void getSessionIdFromBuffer( ) {
sessionId = bufferedSessionID;
if ( trace != null)
trace.addInfo("SessionId from Buffer: " + sessionId);
}
private void getSessionId( ) {
long currentTime = System.currentTimeMillis();
if ( (currentTime - lastTimestamp) > timeOut ) {
lastTimestamp = currentTime;
getSessionIdFromSFDC();
bufferedSessionID = sessionId;
} else {
getSessionIdFromBuffer();
}
}
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.lookup.Channel;
import com.sap.aii.mapping.lookup.LookupException;
import com.sap.aii.mapping.lookup.LookupService;
import com.sap.aii.mapping.lookup.Payload;
import com.sap.aii.mapping.lookup.SystemAccessor;
/**
* Java Mapping Parameter:
* lookupChannel (Adapter - SOAP)
* timeout (Simple Type - xsd:integer)
*/
public class SalesForceLogin extends AbstractTransformation {
Channel lookupChannel;
String sessionId = "";
String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:con=\"xmlns:urn=urn:partner.soap.sforce.com\">"
+ "<soapenv:Header> " + "<con:SessionHeader>" + "<con:sessionId>";
String suffix = "</con:sessionId> </con:SessionHeader> </soapenv:Header> <soapenv:Body>";
String envelope = "</soapenv:Body> </soapenv:Envelope>";
String inputLine = "";
String messageID = "";
TransformationInput transInput = null;
TransformationOutput transOutput = null;
public AbstractTrace trace = getTrace();
// Buffer do SessionID
public static String bufferedSessionID = "";
public static long lastTimestamp;
public static long timeOut = 10000;
public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {
trace = (AbstractTrace) getTrace();
timeOut = arg0.getInputParameters().getInt("timeout");
timeOut *= 1000; // convert to milliseconds
trace.addInfo("Timeout: " + timeOut);
lookupChannel = arg0.getInputParameters().getChannel("lookupChannel");
trace.addInfo("Channel: " + lookupChannel);
messageID = arg0.getInputHeader().getMessageId(); // Message ID only available if run from PI.
transInput = arg0;
transOutput = arg1;
this.execute((InputStream) arg0.getInputPayload().getInputStream(),
(OutputStream) arg1.getOutputPayload().getOutputStream());
}
public void execute(InputStream in, OutputStream out) {
try {
getSessionId();
copyPayload(in, out);
} catch (Exception t) {
t.printStackTrace();
}
}
private void copyPayload(InputStream in, OutputStream out) throws StreamTransformationException {
String c = "";
String Newpayload = "";
try {
InputStreamReader inr = new InputStreamReader(in,StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(inr);
String temp = "";
while ((temp = reader.readLine()) != null) {
c = c + temp;
}
int len = c.indexOf(">");
Newpayload = c.substring(len + 1);
out.write(prefix.getBytes());
out.write(sessionId.getBytes());
out.write(suffix.getBytes());
out.write(Newpayload.getBytes(StandardCharsets.UTF_8));
out.write(envelope.getBytes());
} catch (IOException e) {
throw new StreamTransformationException(e.getMessage());
}
}
private void getSessionIdFromSFDC( ) {
try {
trace.addInfo("SessionId from LookUp: ");
SystemAccessor accessor = null;
accessor = LookupService.getSystemAccessor(lookupChannel);
String loginxml = "<urn:login xmlns:urn=\"urn:partner.soap.sforce.com\"></urn:login>";
InputStream inputStream = new ByteArrayInputStream(loginxml.getBytes());
Payload payload = LookupService.getXmlPayload(inputStream);
Payload SOAPOutPayload;
SOAPOutPayload = accessor.call(payload);
InputStream inp = SOAPOutPayload.getContent();
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
Document document = builder.parse(inp);
NodeList sessionId1 = document.getElementsByTagName("sessionId");
Node node = sessionId1.item(0);
if (node != null) {
node = node.getFirstChild();
if (node != null) {
sessionId = node.getNodeValue();
trace.addInfo("SessionId: " + sessionId);
}
}
if (accessor != null)
accessor.close();
} catch (LookupException e) {
getTrace().addDebugMessage(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
getTrace().addDebugMessage(e.toString());
e.printStackTrace();
}
}
private void getSessionIdFromBuffer( ) {
sessionId = bufferedSessionID;
if ( trace != null)
trace.addInfo("SessionId from Buffer: " + sessionId);
}
private void getSessionId( ) {
long currentTime = System.currentTimeMillis();
if ( (currentTime - lastTimestamp) > timeOut ) {
lastTimestamp = currentTime;
getSessionIdFromSFDC();
bufferedSessionID = sessionId;
} else {
getSessionIdFromBuffer();
}
}
public static void main(String[] args) throws Exception {
try {
bufferedSessionID = "temp session id";
SalesForceLogin obj = new SalesForceLogin();
obj.getSessionIdFromBuffer();
FileInputStream in = new FileInputStream("./input.xml");
FileOutputStream out = new FileOutputStream("./output.xml");
obj.copyPayload(in, out);
System.out.println("Test Done!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Channel channel = LookupService.getChannel("BUSINESS COMPONENT","COMMUNICATION CHANNEL");
SystemAccessor accessor = null;
accessor = LookupService.getSystemAccessor(channel);
String loginxml ="<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">+
"<soapenv:Header/>"+
"<soapenv:Body>" +
"<urn:login>"+
"<urn:username>"
+ username
+ "</urn:username>"
+<urn:password>"
+ password
"</urn:password>"+
"</urn:login>"+
"</soapenv:Body>"+
"</soapenv:Envelope>"
Loginxml value string:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<urn:login>
<urn:username>YOUR USER</urn:username>
<urn:password>YOUR PASS</urn:password>
</urn:login>
</soapenv:Body>
</soapenv:Envelope>
InputStream receive the loginxml:
InputStream inputStream = new ByteArrayInputStream(loginxml.getBytes());
String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"xmlns:urn=urn:partner.soap.sforce.com\">"
+ "<soapenv:Header> " + "<urn:SessionHeader>" + "<urn:sessionId> ";
String suffix = "</urn:sessionId> </urn:SessionHeader> </soapenv:Header> <soapenv:Body>";
String envelope = "</soapenv:Body> </soapenv:Envelope>";
Java code:
/* End of remove xml version tag */
out.write(prefix.getBytes());
out.write(sessionId.getBytes());
out.write(suffix.getBytes());
out.write(Newpayload.getBytes());
out.write(envelope.getBytes());
SOAP Envelop sample:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:con="xmlns:urn=urn:partner.soap.sforce.com">
<soapenv:Header>
<con:SessionHeader>
<con:sessionId>temp session id</con:sessionId>
</con:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<!-- input.xml -->
</soapenv:Body>
</soapenv:Envelope>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |