public InputStream excecute() throws ConfigurationException, HttpException,
IOException, HttpConnectionNotOkException {
if (url == null) {
throw new ConfigurationException("url must be set.");
}
if (bodyEntity == null) {
throw new ConfigurationException("body Entity is not filled");
}
HttpClient client = new HttpClient();
initProxyAuth(client, this.proxyAuthConfig);
initHostAuth(client, this.hostAuthConfig);
PostMethod postMethod = new PostMethod(url);
// wirst du nicht brauchen - Start
Iterator itKey = httpHeader.keySet().iterator();
while (itKey.hasNext()) {
String headerName = itKey.next().toString();
String headerValue = httpHeader.get(headerName).toString();
postMethod.addRequestHeader(headerName, headerValue);
}
postMethod.setRequestEntity(bodyEntity);
// wirst du nicht brauchen ENDE
int ret = client.executeMethod(postMethod); // request abfeuern
if (ret != 200) {
String retAsString = postMethod.getResponseBodyAsString();
throw new HttpConnectionNotOkException("Return code was: " + ret + "\nmsg=" + retAsString);
}
InputStream is = postMethod.getResponseBodyAsStream();
return is;
}
private void initProxyAuth(HttpClient client, ProxyAuthConfig config) {
if (config != null) {
client.getHostConfiguration().setProxy(config.getHost(),
config.getPort());
HttpState state = client.getState();
AuthScope authScope = new AuthScope(config.getHost(), config
.getPort(), AuthScope.ANY_REALM);
state.setProxyCredentials(authScope,
new UsernamePasswordCredentials(config.getUser(), config
.getPwd()));
client.getParams().setAuthenticationPreemptive(true);
}
}
/**
* Authentification for remote service.
*
* @param host
* remote host
* @param port
* remote port
* @param hostUserName
* remote user
* @param hostPwd
* remote password
*/
private void initHostAuth(HttpClient client, HostAuthConfig config) {
if (config != null) {
HttpState state = client.getState();
AuthScope authScope = new AuthScope(config.getHost(), config
.getPort(), AuthScope.ANY_REALM);
state.setCredentials(authScope, new UsernamePasswordCredentials(
config.getUser(), config.getPwd()));
client.getParams().setAuthenticationPreemptive(true);
}
}
/// der aufrufende Code von excecute in einer anderen Klasse.
private Object sendSoap(String body) throws HttpException,
ConfigurationException, IOException, HttpConnectionNotOkException,
JDOMException {
Object ret = null;
String strMsg = "Module " + agtModule + ", sendSoap():";
InputStream is = null;
// 2. soap an Server, response
post = new DoPost();
// Set the SOAP action and security
post.setUrl(SOAPUrlTarget + SOAPUrlParams);
post.setProxyAuth(wsProxyAuth);
post.setHostAuth(wsHostAuth);
// Set the Body, generated for each document
post.setBodyEntity(body);
post.addHttpHeader("Content-Type", "text/xml; charset=utf-8");
post.addHttpHeader("Accept",
"application/soap+xml, application/dime, multipart/related, text/*");
post.addHttpHeader("User-Agent", "Axis/1.4");
post.addHttpHeader("Cache-Control", "no-cache");
post.addHttpHeader("Pragma", "no-cache");
post.addHttpHeader("SOAPAction",
"http://sap.com/xi/WebService/soap1.1");
// SPIErrorHandler.logAction(strMsg + " Post object: \t" +
// post.toString(), dbCurrent);
try {
is = post.excecute();
// 3. soapResponse verarbeiten.
SoapSapToBean parser = new SoapSapToBean();
// SoapToString bean = new SoapToString(); -> for debug purposes
/**
* The Response bean contains the jdom xpath results of the SOAP
* return message
*/
ResponseBean bean = new ResponseBean();
ret = parser.process(is, bean);
is.close();
is = null;
} finally {
/*
* It is very urgent to close the stream in a controlled way, to
* avoid problems on the OS
*/
if (is != null) {
try {
is.close();
} catch (java.io.IOException ioe) {
//ioe.printStackTrace();
SPIErrorHandler.handleError(ioe.toString(), dbCurrent);
}
} // end if
}
// transfer the SOAP result to the next level
return ret;
}