package de.superx.bin;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.Authenticator.RequestorType;
import java.nio.charset.Charset;
import java.util.logging.Logger;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import de.memtext.util.DateUtils;
import de.memtext.util.StringUtils;
import de.memtext.util.XMLUtils;
public class AbstractWebserviceClient {
int pause = 0;
boolean isDeleteTmpXmlFileWanted = true;
public AbstractWebserviceClient() {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
String prot = getRequestingProtocol().toLowerCase();
String host = System.getProperty(prot + ".proxyHost", "");
String port = System.getProperty(prot + ".proxyPort", "");
String user = System.getProperty(prot + ".proxyUser", "");
String password = System.getProperty(prot
+ ".proxyPassword", "");
if (getRequestingHost().toLowerCase().equals(
host.toLowerCase())) {
if (Integer.parseInt(port) == getRequestingPort()) {
// Seems to be OK.
return new PasswordAuthentication(user, password
.toCharArray());
}
}
}
return null;
}
});
}
/**
* macht bei Netzwerkproblemen vier Versuche
*
* @param soapxml
* @param url
* @return
* @throws Exception
*/
protected StringBuffer readSOAP(String soapxml, String url)
throws Exception {
int attempt = 1;
boolean allDone = false;
StringWriter sw = null;
Exception exception = null;
while (allDone == false && attempt < 4) {
try {
sw = new StringWriter();
if (pause > 0) {
Thread.sleep(pause * 1000);
// System.out.println("pause "+pause);
}
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection soapConnection = soapConnectionFactory
.createConnection();
SOAPMessage sr = getSoapMessageFromString(soapxml);
sr.getMimeHeaders().addHeader("SOAPAction",
"http://sap.com/xi/WebService/soap1.1");
SOAPMessage soapResponse = soapConnection.call(sr, url);
// soapResponse.writeTo(System.out);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
// optional indenting
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
// File tmpF = File.createTempFile("soapdata", ".xml");
transformer.transform(
new DOMSource(soapResponse.getSOAPPart()),
new StreamResult(sw));
soapConnection.close();
System.gc();
// if (attempt < 3) throw new RuntimeException("RUntime");
// StringBuffer result=readFile(tmpF);
// if (isDeleteTmpXmlFileWanted) tmpF.delete();
// else System.out.println(" tmp File "+tmpF);
allDone = true;
} catch (Exception e) {
exception = e;
System.out
.println(DateUtils.getNowString()
+ " Aufruf fehlgeschlagen (s. WebserviceLog) - versuche erneut");
StringWriter swException = new StringWriter();
PrintWriter pw = new PrintWriter(swException);
e.printStackTrace(pw);
Logger.getLogger("wc").severe(
" Problem bei Aufruf von " + url + "\n" + soapxml
+ "\n" + swException.toString());
attempt++;
}
}
if (allDone == false)
throw exception;
StringBuffer result = new StringBuffer(sw.toString());
return result;
}
protected File createSoapFile(String soapxml, String url) throws Exception {
int attempt = 1;
boolean allDone = false;
Exception exception = null;
File tmpF = File.createTempFile("soapdata", ".xml");
while (allDone == false && attempt < 4) {
try {
if (tmpF.exists())
tmpF.delete();
if (pause > 0) {
Thread.sleep(pause * 1000);
// System.out.println("pause "+pause);
}
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection soapConnection = soapConnectionFactory
.createConnection();
SOAPMessage sr = getSoapMessageFromString(soapxml);
sr.getMimeHeaders().addHeader("SOAPAction",
"http://sap.com/xi/WebService/soap1.1");
SOAPMessage soapResponse = soapConnection.call(sr, url);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
// optional indenting
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(
new DOMSource(soapResponse.getSOAPPart()),
new StreamResult(tmpF));
soapConnection.close();
System.gc();
// if (attempt<3) throw new RuntimeException("runtime");
allDone = true;
} catch (Exception e) {
exception = e;
System.out
.println("\n"
+ DateUtils.getNowString()
+ " Aufruf fehlgeschlagen (s. WebserviceLog) - versuche erneut");
StringWriter swException = new StringWriter();
PrintWriter pw = new PrintWriter(swException);
e.printStackTrace(pw);
Logger.getLogger("wc").severe(
" Problem bei Aufruf von " + url + "\n" + soapxml
+ "\n" + swException.toString());
attempt++;
}
}
if (allDone == false)
throw exception;
return tmpF;
}
private SOAPMessage getSoapMessageFromString(String xml)
throws SOAPException, IOException {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory
.createMessage(
new MimeHeaders(),
new ByteArrayInputStream(xml.getBytes(Charset
.forName("UTF-8"))));
return message;
}
/**
* Entferne unnötige Zeichen und EX_JEST Block
*
* @param data
* @return
*/
static String purge(String data) {
data = XMLUtils.removeTroublesomeCharacters(data);
data = data.replaceAll("\u20AC", "EUR");
data = data.replaceAll("\u2013", "-");// dash
data = data.replaceAll("\u2026", "...");// ...
data = data.replace('^', ' ');
data = data.replaceAll("(?s).*", "");
data = data.replaceAll("", "");
data = data.replaceAll("", "");
return data;
}
String adaptURL(String url) {
String result = url;
if (System.getProperty("WS_HOST") != null)
result = url.replaceAll("WS_HOST", System.getProperty("WS_HOST"));
return result;
}
private StringBuffer readFile(File f) throws IOException {
FileReader fr = new FileReader(f);
BufferedReader bfr = new BufferedReader(fr);
String line;
StringBuffer result = new StringBuffer();
while ((line = bfr.readLine()) != null) {
result.append(line + "\n");
}
bfr.close();
fr.close();
return result;
}
}