mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-10 02:39:42 +02:00
Improve Exceptions (SMACK-426)
This commit is contained in:
parent
9c8d7af3bf
commit
c592b4f046
9 changed files with 103 additions and 57 deletions
|
@ -18,6 +18,7 @@
|
|||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||
import org.jivesoftware.smack.SmackException.ResourceBindingNotOfferedException;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.packet.Bind;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
@ -210,9 +211,10 @@ public class SASLAuthentication {
|
|||
* @throws XMPPErrorException
|
||||
* @throws NoResponseException
|
||||
* @throws SASLErrorException
|
||||
* @throws ResourceBindingNotOfferedException
|
||||
*/
|
||||
public String authenticate(String resource, CallbackHandler cbh) throws IOException,
|
||||
NoResponseException, XMPPErrorException, SASLErrorException {
|
||||
NoResponseException, XMPPErrorException, SASLErrorException, ResourceBindingNotOfferedException {
|
||||
// Locate the SASLMechanism to use
|
||||
String selectedMechanism = null;
|
||||
for (String mechanism : mechanismsPreferences) {
|
||||
|
@ -265,8 +267,7 @@ public class SASLAuthentication {
|
|||
return bindResourceAndEstablishSession(resource);
|
||||
}
|
||||
else {
|
||||
// SASL authentication failed
|
||||
throw new SaslException();
|
||||
throw new NoResponseException();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -352,8 +353,7 @@ public class SASLAuthentication {
|
|||
return bindResourceAndEstablishSession(resource);
|
||||
}
|
||||
else {
|
||||
// SASL authentication failed
|
||||
throw new SaslException();
|
||||
throw new NoResponseException();
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -404,11 +404,12 @@ public class SASLAuthentication {
|
|||
return bindResourceAndEstablishSession(null);
|
||||
}
|
||||
else {
|
||||
throw new SaslException();
|
||||
throw new NoResponseException();
|
||||
}
|
||||
}
|
||||
|
||||
private String bindResourceAndEstablishSession(String resource) throws NoResponseException, XMPPErrorException {
|
||||
private String bindResourceAndEstablishSession(String resource) throws XMPPErrorException,
|
||||
ResourceBindingNotOfferedException, NoResponseException {
|
||||
// Wait until server sends response containing the <bind> element
|
||||
synchronized (this) {
|
||||
if (!resourceBinded) {
|
||||
|
@ -424,7 +425,7 @@ public class SASLAuthentication {
|
|||
if (!resourceBinded) {
|
||||
// Server never offered resource binding, which is REQURIED in XMPP client and server
|
||||
// implementations as per RFC6120 7.2
|
||||
throw new IllegalStateException("Resource binding not offered by server");
|
||||
throw new ResourceBindingNotOfferedException();
|
||||
}
|
||||
|
||||
Bind bindResource = new Bind();
|
||||
|
|
|
@ -152,8 +152,45 @@ public class SmackException extends Exception {
|
|||
*/
|
||||
private static final long serialVersionUID = 4713404802621452016L;
|
||||
|
||||
public FeatureNotSupportedException(String message) {
|
||||
super(message);
|
||||
private final String feature;
|
||||
private final String jid;
|
||||
|
||||
public FeatureNotSupportedException(String feature) {
|
||||
this(feature, null);
|
||||
}
|
||||
|
||||
public FeatureNotSupportedException(String feature, String jid) {
|
||||
super(feature + " not supported" + (jid == null ? "" : " by '" + jid + "'"));
|
||||
this.jid = jid;
|
||||
this.feature = feature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the feature which is not supported.
|
||||
*
|
||||
* @return the feature which is not supported
|
||||
*/
|
||||
public String getFeature() {
|
||||
return feature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get JID which does not support the feature. The JID can be null in cases when there are
|
||||
* multiple JIDs queried for this feature.
|
||||
*
|
||||
* @return the JID which does not support the feature, or null
|
||||
*/
|
||||
public String getJid() {
|
||||
return jid;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ResourceBindingNotOfferedException extends SmackException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 2346934138253437571L;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.sasl;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public enum SASLError {
|
||||
|
||||
aborted,
|
||||
|
@ -29,6 +32,7 @@ public enum SASLError {
|
|||
not_authorized,
|
||||
temporary_auth_failure;
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(SASLError.class.getName());
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name().replace('_', '-');
|
||||
|
@ -36,10 +40,12 @@ public enum SASLError {
|
|||
|
||||
public static SASLError fromString(String string) {
|
||||
string = string.replace('-', '_');
|
||||
for (SASLError error : SASLError.values()) {
|
||||
if (error.name().equals(string))
|
||||
return error;
|
||||
SASLError saslError = null;
|
||||
try {
|
||||
saslError = SASLError.valueOf(string);
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.WARNING, "Could not transform string '" + string + "' to SASLError", e);
|
||||
}
|
||||
return null;
|
||||
return saslError;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,12 +34,14 @@ public class SASLErrorException extends XMPPException {
|
|||
private final Map<String,String> texts;
|
||||
|
||||
public SASLErrorException(String mechanism, SASLFailure saslFailure) {
|
||||
super("SASLError using " + mechanism + ": " + saslFailure);
|
||||
this.mechanism = mechanism;
|
||||
this.saslFailure = saslFailure;
|
||||
this.texts = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
public SASLErrorException(String mechanism, SASLFailure saslFailure, Map<String,String> texts) {
|
||||
super("SASLError using " + mechanism + ": " + saslFailure);
|
||||
this.mechanism = mechanism;
|
||||
this.saslFailure = saslFailure;
|
||||
this.texts = texts;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue