1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
package BusinessObjectConnector;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class SessionToken {
static HttpHost _target;
public static String get(HttpHost theTarget, String sUserName, String sPassword, String sAuth) {
String sServer = "uneipdeserveur";
String sPort = "6405";
String sLogonToken = "";
// creation of an http host
// specify the host, protocol, and port
_target = new HttpHost(sServer, Integer.parseInt(sPort), "http");
try {
sLogonToken = logonCMS(sUserName, sPassword, sAuth);
} catch (Exception e) {
e.printStackTrace();
}
return sLogonToken;
}
public static Document loadXMLFromString(String xml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
public static void logoffCMS(String sLogonToken) throws Exception {
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
boolean loggedOff = false;
// Post preparation
//
try {
HttpPost httpPostRequest = new HttpPost("/biprws/logoff");
httpPostRequest.addHeader("Accept", "application/xml");
httpPostRequest.addHeader("X-SAP-LogonToken", sLogonToken);
StringEntity myEntity = new StringEntity("");
myEntity.setContentType("application/xml");
httpPostRequest.setEntity(myEntity);
HttpResponse httpResponse = httpClient.execute(_target, httpPostRequest);
} catch (Exception e) {
loggedOff = true;
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
if (loggedOff) {
System.out.println("Business Object : Logged off.");
}
httpClient.close();
}
}
public static String logonCMS(String sUserName, String sPassword, String sAuth) throws Exception {
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
Document xmlResponse = null;
//
// send get and retrieve entity
//
HttpGet httpGetRequest = new HttpGet("/biprws/logon/long");
System.out.println("executing request to " + _target);
HttpResponse httpResponse = httpClient.execute(_target, httpGetRequest);
HttpEntity entity = httpResponse.getEntity();
// If the response does not enclose an entity, there is no need
// to bother about connection release
String sFinal = "";
if (entity != null) {
InputStream inputStream = entity.getContent();
sFinal = recupereStringInputStream(inputStream);
//
// modify the xml for the post
//
try {
xmlResponse = loadXMLFromString(sFinal);
renseigneNode(xmlResponse, "userName", sUserName);
renseigneNode(xmlResponse, "password", sPassword);
renseigneNode(xmlResponse, "auth", sAuth);
} catch (Exception e) {
e.printStackTrace();
}
//
// post preparation
//
HttpPost httpPostRequest = new HttpPost("/biprws/logon/long");
//
// transforms xml in String
//
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(xmlResponse), new StreamResult(writer));
String s = writer.getBuffer().toString().replaceAll("\n|\r", "");
//
// put the xml inentity
//
StringEntity myEntity = new StringEntity(s);
//
// send post
//
myEntity.setContentType("application/xml");
httpPostRequest.setEntity(myEntity);
httpResponse = httpClient.execute(_target, httpPostRequest);
Header hLogonToken = httpResponse.getFirstHeader("X-SAP-LogonToken");
String sReturn = hLogonToken.getValue();
return sReturn;
}
} catch (Exception e) {
// thrown by entity.getContent();
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.close();
;
}
return "";
}
public static String recupereStringInputStream(InputStream inputStream) throws Exception {
byte[] buffer = new byte[1024];
String sFinal = "";
int bytesRead = 0;
BufferedInputStream bis = new BufferedInputStream(inputStream);
while ((bytesRead = bis.read(buffer)) != -1) {
sFinal = sFinal + new String(buffer, 0, bytesRead);
}
return sFinal;
}
public static Document renseigneNode(Document myXmlDoc, String sItemName, String sTextContent) {
NodeList nl = myXmlDoc.getElementsByTagName("attr");
for (int i = 0; i < nl.getLength(); i++) {
Node no = nl.item(i);
if (sItemName.equals(no.getAttributes().getNamedItem("name").getNodeValue())) {
no.setTextContent(sTextContent);
}
}
return myXmlDoc;
}
} |