_targetServer.reset();
String urlSuffix = serviceName;
Exception firstException = null;
StringBuffer errorMessages = new StringBuffer();
while (_targetServer.hasNext()) {
Server target = _targetServer.getNextTargetServer();
String serverUrl = target.getFullUrlString(encrypted && !WojUtil.isDevMode());
try {
URL url = new URL(serverUrl+ urlSuffix);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("calling "+url+" encrypted="+encrypted+" devmode="+WojUtil.isDevMode()+" params="+params);
}
String urlContent = URLHelper.getContentPostMethod(url, params, false);
_targetServer.setNewTargetServer(target);
if (urlContent != null && decode) {
Object result = ServiceProvider.getDefault().getEncodingService().decode(urlContent);
//if object is encapsulate in an answer
if (result instanceof Answer) {
Answer a = (Answer) result;
if (a.getNewUak() != null) {
getUnifiedAuthentificationService().setUAK(a.getNewUak());
}
Object object = a.getObject();
switch (a.getStatus()) {
case Answer.STATUS_OK:
return object;
case Answer.STATUS_FAILED:
case Answer.STATUS_INVALID:
case Answer.STATUS_BAD_UAK:
case Answer.STATUS_NOT_AUTHORIZED:
throw new AnswerServiceException(serverUrl, serviceName, params, a, String.valueOf(object));
}
return null;
}
return result;
} else {
return urlContent;
}
} catch(URLRedirectException e) {
String url = e.getUrl();
LOGGER.debug("redirected to url: "+url);
String server = url;
if (server.indexOf('?') != -1) {
server = server.substring(0, server.indexOf('?'));
}
if (server.endsWith(serviceName)) {
server = server.substring(0, server.length() - serviceName.length());
}
urlSuffix = url.substring(server.length());
LOGGER.info("redirected to server: "+server);
try {
_targetServer.setNewTargetServer(new Server(new URL(server)));
} catch (MalformedURLException e1) {
throw new ServiceNotAvailableException("redirected to malformed url: "+server, e1);
}
} catch(ConnectException e) {
LOGGER.info("Server did not respond: "+serverUrl, e);