I get the following exception when calling the http service:
com.wm.app.b2b.server.ServiceException
Message: com.wm.net.NetException: [ISC.0064.9314] Authorization Required: Unauthorized
So far, so good. But I would like to get that information programatically - not the human readable translation. I seem to have no chance to get the status code 401 or something that is a 100% proof that the problem is a 401.
Writing a wrapper that tries to get the "root cause" (getCause...) does not work. There is no other "cause"...
All I have are parsable strings. Any idea?
UPDATE
I found a way to get it done - using a deprecated method...:
...
try {
output =
Service.doInvoke( "my.package.authentication", "checkAuthentication", input );
} catch( final ServiceException sEx ) {
// if this is deprecated: how to we have to handle this in future?
final Throwable wrappedEx = sEx.getWrappedException();
// return early
if(null == wrappedEx || !NetException.class.isInstance(wrappedEx) ) {
throw sEx;
}
// process the net exception
final NetException nEx = (NetException)wrappedEx;
final String responseBody = convertStreamToString(nEx.getResponseInputStream());
// process the returned body wrapped by the net exception
final Gson gson = new Gson();
final ErrorData errorData = gson.fromJson(responseBody, ErrorData.class);
// check if the problem is an invalid token
tokenIsInvalid = errorData.code.equals(INVALID_TOKEN_EXCEPTION__CODE_STRING);
} catch( Exception e ) {
// wrap the exception in a service exception and throw it
throw new ServiceException(e);
}
...
A better solution would simply check the HTTP-Status-Code - but a 401 is gone forever if recieved by the http-service... :-|