http サービスを呼び出すと、次の例外が発生します。
com.wm.app.b2b.server.ServiceException
Message: com.wm.net.NetException: [ISC.0064.9314] Authorization Required: Unauthorized
ここまでは順調ですね。しかし、人間が読める翻訳ではなく、プログラムでその情報を取得したいと思います。ステータス コード 401 や、問題が 401 であることを 100% 証明する何かを取得するチャンスはないようです。
「根本原因」 (getCause...) を取得しようとするラッパーを作成しても機能しません。他に「原因」はありません...
私が持っているのは解析可能な文字列だけです。何か案が?
アップデート
私はそれを成し遂げる方法を見つけました - 非推奨の方法を使用して...:
...
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);
}
...
より良い解決策は、単純に HTTP-Status-Code をチェックすることですが、http-service によって受信された場合、401 は永久に消えてしまいます... :-|