1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-26 06:07:59 +01:00

Update XMPPError to RFC6120

Fixes SMACK-445
This commit is contained in:
Georg Lukas 2013-06-14 23:08:47 +02:00 committed by Florian Schmaus
parent 67722aea93
commit d5195c4004
4 changed files with 77 additions and 138 deletions

View file

@ -24,50 +24,48 @@ import java.util.Map;
/**
* Represents a XMPP error sub-packet. Typically, a server responds to a request that has
* problems by sending the packet back and including an error packet. Each error has a code, type,
* problems by sending the packet back and including an error packet. Each error has a type,
* error condition as well as as an optional text explanation. Typical errors are:<p>
*
* <table border=1>
* <hr><td><b>Code</b></td><td><b>XMPP Error</b></td><td><b>Type</b></td></hr>
* <tr><td>500</td><td>internal-server-error</td><td>WAIT</td></tr>
* <tr><td>403</td><td>forbidden</td><td>AUTH</td></tr>
* <tr><td>400</td<td>bad-request</td><td>MODIFY</td>></tr>
* <tr><td>404</td><td>item-not-found</td><td>CANCEL</td></tr>
* <tr><td>409</td><td>conflict</td><td>CANCEL</td></tr>
* <tr><td>501</td><td>feature-not-implemented</td><td>CANCEL</td></tr>
* <tr><td>302</td><td>gone</td><td>MODIFY</td></tr>
* <tr><td>400</td><td>jid-malformed</td><td>MODIFY</td></tr>
* <tr><td>406</td><td>no-acceptable</td><td> MODIFY</td></tr>
* <tr><td>405</td><td>not-allowed</td><td>CANCEL</td></tr>
* <tr><td>401</td><td>not-authorized</td><td>AUTH</td></tr>
* <tr><td>402</td><td>payment-required</td><td>AUTH</td></tr>
* <tr><td>404</td><td>recipient-unavailable</td><td>WAIT</td></tr>
* <tr><td>302</td><td>redirect</td><td>MODIFY</td></tr>
* <tr><td>407</td><td>registration-required</td><td>AUTH</td></tr>
* <tr><td>404</td><td>remote-server-not-found</td><td>CANCEL</td></tr>
* <tr><td>504</td><td>remote-server-timeout</td><td>WAIT</td></tr>
* <tr><td>502</td><td>remote-server-error</td><td>CANCEL</td></tr>
* <tr><td>500</td><td>resource-constraint</td><td>WAIT</td></tr>
* <tr><td>503</td><td>service-unavailable</td><td>CANCEL</td></tr>
* <tr><td>407</td><td>subscription-required</td><td>AUTH</td></tr>
* <tr><td>500</td><td>undefined-condition</td><td>WAIT</td></tr>
* <tr><td>400</td><td>unexpected-condition</td><td>WAIT</td></tr>
* <tr><td>408</td><td>request-timeout</td><td>CANCEL</td></tr>
* <hr><td><b>XMPP Error</b></td><td><b>Type</b></td></hr>
* <tr><td>internal-server-error</td><td>WAIT</td></tr>
* <tr><td>forbidden</td><td>AUTH</td></tr>
* <tr><td>bad-request</td><td>MODIFY</td></tr>
* <tr><td>item-not-found</td><td>CANCEL</td></tr>
* <tr><td>conflict</td><td>CANCEL</td></tr>
* <tr><td>feature-not-implemented</td><td>CANCEL</td></tr>
* <tr><td>gone</td><td>MODIFY</td></tr>
* <tr><td>jid-malformed</td><td>MODIFY</td></tr>
* <tr><td>no-acceptable</td><td> MODIFY</td></tr>
* <tr><td>not-allowed</td><td>CANCEL</td></tr>
* <tr><td>not-authorized</td><td>AUTH</td></tr>
* <tr><td>payment-required</td><td>AUTH</td></tr>
* <tr><td>recipient-unavailable</td><td>WAIT</td></tr>
* <tr><td>redirect</td><td>MODIFY</td></tr>
* <tr><td>registration-required</td><td>AUTH</td></tr>
* <tr><td>remote-server-not-found</td><td>CANCEL</td></tr>
* <tr><td>remote-server-timeout</td><td>WAIT</td></tr>
* <tr><td>remote-server-error</td><td>CANCEL</td></tr>
* <tr><td>resource-constraint</td><td>WAIT</td></tr>
* <tr><td>service-unavailable</td><td>CANCEL</td></tr>
* <tr><td>subscription-required</td><td>AUTH</td></tr>
* <tr><td>undefined-condition</td><td>WAIT</td></tr>
* <tr><td>unexpected-condition</td><td>WAIT</td></tr>
* <tr><td>request-timeout</td><td>CANCEL</td></tr>
* </table>
*
* @author Matt Tucker
*/
public class XMPPError {
private int code;
private Type type;
private String condition;
private final Type type;
private final String condition;
private String message;
private List<PacketExtension> applicationExtensions = null;
/**
* Creates a new error with the specified condition infering the type and code.
* Creates a new error with the specified condition inferring the type.
* If the Condition is predefined, client code should be like:
* new XMPPError(XMPPError.Condition.remote_server_timeout);
* If the Condition is not predefined, invocations should be like
@ -76,12 +74,20 @@ public class XMPPError {
* @param condition the error condition.
*/
public XMPPError(Condition condition) {
this.init(condition);
this.message = null;
// Look for the condition and its default type
ErrorSpecification defaultErrorSpecification = ErrorSpecification.specFor(condition);
this.condition = condition.value;
if (defaultErrorSpecification != null) {
// If there is a default error specification for the received condition,
// it get configured with the inferred type.
type = defaultErrorSpecification.getType();
} else {
type = null;
}
}
/**
* Creates a new error with the specified condition and message infering the type and code.
* Creates a new error with the specified condition and message infering the type.
* If the Condition is predefined, client code should be like:
* new XMPPError(XMPPError.Condition.remote_server_timeout, "Error Explanation");
* If the Condition is not predefined, invocations should be like
@ -91,71 +97,29 @@ public class XMPPError {
* @param messageText a message describing the error.
*/
public XMPPError(Condition condition, String messageText) {
this.init(condition);
this(condition);
this.message = messageText;
}
/**
* Creates a new error with the specified code and no message.
*
* @param code the error code.
* @deprecated new errors should be created using the constructor XMPPError(condition)
*/
public XMPPError(int code) {
this.code = code;
this.message = null;
}
/**
* Creates a new error with the specified code and message.
* deprecated
*
* @param code the error code.
* @param message a message describing the error.
* @deprecated new errors should be created using the constructor XMPPError(condition, message)
*/
public XMPPError(int code, String message) {
this.code = code;
this.message = message;
}
/**
* Creates a new error with the specified code, type, condition and message.
* Creates a new error with the specified type, condition and message.
* This constructor is used when the condition is not recognized automatically by XMPPError
* i.e. there is not a defined instance of ErrorCondition or it does not applies the default
* i.e. there is not a defined instance of ErrorCondition or it does not apply the default
* specification.
*
* @param code the error code.
* @param type the error type.
* @param condition the error condition.
* @param message a message describing the error.
* @param extension list of packet extensions
*/
public XMPPError(int code, Type type, String condition, String message,
public XMPPError(Type type, String condition, String message,
List<PacketExtension> extension) {
this.code = code;
this.type = type;
this.condition = condition;
this.message = message;
this.applicationExtensions = extension;
}
/**
* Initialize the error infering the type and code for the received condition.
*
* @param condition the error condition.
*/
private void init(Condition condition) {
// Look for the condition and its default code and type
ErrorSpecification defaultErrorSpecification = ErrorSpecification.specFor(condition);
this.condition = condition.value;
if (defaultErrorSpecification != null) {
// If there is a default error specification for the received condition,
// it get configured with the infered type and code.
this.type = defaultErrorSpecification.getType();
this.code = defaultErrorSpecification.getCode();
}
}
/**
* Returns the error condition.
*
@ -174,15 +138,6 @@ public class XMPPError {
return type;
}
/**
* Returns the error code.
*
* @return the error code.
*/
public int getCode() {
return code;
}
/**
* Returns the message describing the error, or null if there is no message.
*
@ -199,10 +154,10 @@ public class XMPPError {
*/
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<error code=\"").append(code).append("\"");
buf.append("<error");
if (type != null) {
buf.append(" type=\"");
buf.append(type.name());
buf.append(type.name().toLowerCase());
buf.append("\"");
}
buf.append(">");
@ -227,7 +182,6 @@ public class XMPPError {
if (condition != null) {
txt.append(condition);
}
txt.append("(").append(code).append(")");
if (message != null) {
txt.append(" ").append(message);
}
@ -249,7 +203,7 @@ public class XMPPError {
}
/**
* Returns the first patcket extension that matches the specified element name and
* Returns the first packet extension that matches the specified element name and
* namespace, or <tt>null</tt> if it doesn't exist.
*
* @param elementName the XML element name of the packet extension.
@ -356,66 +310,64 @@ public class XMPPError {
private static class ErrorSpecification {
private static Map<Condition, ErrorSpecification> instances = new HashMap<Condition, ErrorSpecification>();
private final int code;
private final Type type;
@SuppressWarnings("unused")
private final Condition condition;
private ErrorSpecification(Condition condition, Type type, int code) {
this.code = code;
private ErrorSpecification(Condition condition, Type type) {
this.type = type;
this.condition = condition;
}
static {
instances.put(Condition.internal_server_error, new ErrorSpecification(
Condition.internal_server_error, Type.WAIT, 500));
Condition.internal_server_error, Type.WAIT));
instances.put(Condition.forbidden, new ErrorSpecification(Condition.forbidden,
Type.AUTH, 403));
Type.AUTH));
instances.put(Condition.bad_request, new XMPPError.ErrorSpecification(
Condition.bad_request, Type.MODIFY, 400));
Condition.bad_request, Type.MODIFY));
instances.put(Condition.item_not_found, new XMPPError.ErrorSpecification(
Condition.item_not_found, Type.CANCEL, 404));
Condition.item_not_found, Type.CANCEL));
instances.put(Condition.conflict, new XMPPError.ErrorSpecification(
Condition.conflict, Type.CANCEL, 409));
Condition.conflict, Type.CANCEL));
instances.put(Condition.feature_not_implemented, new XMPPError.ErrorSpecification(
Condition.feature_not_implemented, Type.CANCEL, 501));
Condition.feature_not_implemented, Type.CANCEL));
instances.put(Condition.gone, new XMPPError.ErrorSpecification(
Condition.gone, Type.MODIFY, 302));
Condition.gone, Type.MODIFY));
instances.put(Condition.jid_malformed, new XMPPError.ErrorSpecification(
Condition.jid_malformed, Type.MODIFY, 400));
Condition.jid_malformed, Type.MODIFY));
instances.put(Condition.no_acceptable, new XMPPError.ErrorSpecification(
Condition.no_acceptable, Type.MODIFY, 406));
Condition.no_acceptable, Type.MODIFY));
instances.put(Condition.not_allowed, new XMPPError.ErrorSpecification(
Condition.not_allowed, Type.CANCEL, 405));
Condition.not_allowed, Type.CANCEL));
instances.put(Condition.not_authorized, new XMPPError.ErrorSpecification(
Condition.not_authorized, Type.AUTH, 401));
Condition.not_authorized, Type.AUTH));
instances.put(Condition.payment_required, new XMPPError.ErrorSpecification(
Condition.payment_required, Type.AUTH, 402));
Condition.payment_required, Type.AUTH));
instances.put(Condition.recipient_unavailable, new XMPPError.ErrorSpecification(
Condition.recipient_unavailable, Type.WAIT, 404));
Condition.recipient_unavailable, Type.WAIT));
instances.put(Condition.redirect, new XMPPError.ErrorSpecification(
Condition.redirect, Type.MODIFY, 302));
Condition.redirect, Type.MODIFY));
instances.put(Condition.registration_required, new XMPPError.ErrorSpecification(
Condition.registration_required, Type.AUTH, 407));
Condition.registration_required, Type.AUTH));
instances.put(Condition.remote_server_not_found, new XMPPError.ErrorSpecification(
Condition.remote_server_not_found, Type.CANCEL, 404));
Condition.remote_server_not_found, Type.CANCEL));
instances.put(Condition.remote_server_timeout, new XMPPError.ErrorSpecification(
Condition.remote_server_timeout, Type.WAIT, 504));
Condition.remote_server_timeout, Type.WAIT));
instances.put(Condition.remote_server_error, new XMPPError.ErrorSpecification(
Condition.remote_server_error, Type.CANCEL, 502));
Condition.remote_server_error, Type.CANCEL));
instances.put(Condition.resource_constraint, new XMPPError.ErrorSpecification(
Condition.resource_constraint, Type.WAIT, 500));
Condition.resource_constraint, Type.WAIT));
instances.put(Condition.service_unavailable, new XMPPError.ErrorSpecification(
Condition.service_unavailable, Type.CANCEL, 503));
Condition.service_unavailable, Type.CANCEL));
instances.put(Condition.subscription_required, new XMPPError.ErrorSpecification(
Condition.subscription_required, Type.AUTH, 407));
Condition.subscription_required, Type.AUTH));
instances.put(Condition.undefined_condition, new XMPPError.ErrorSpecification(
Condition.undefined_condition, Type.WAIT, 500));
Condition.undefined_condition, Type.WAIT));
instances.put(Condition.unexpected_request, new XMPPError.ErrorSpecification(
Condition.unexpected_request, Type.WAIT, 400));
Condition.unexpected_request, Type.WAIT));
instances.put(Condition.request_timeout, new XMPPError.ErrorSpecification(
Condition.request_timeout, Type.CANCEL, 408));
Condition.request_timeout, Type.CANCEL));
}
protected static ErrorSpecification specFor(Condition condition) {
@ -430,14 +382,5 @@ public class XMPPError {
protected Type getType() {
return type;
}
/**
* Returns the error code.
*
* @return the error code.
*/
protected int getCode() {
return code;
}
}
}

View file

@ -703,7 +703,6 @@ public class PacketParserUtils {
*/
public static XMPPError parseError(XmlPullParser parser) throws Exception {
final String errorNamespace = "urn:ietf:params:xml:ns:xmpp-stanzas";
String errorCode = "-1";
String type = null;
String message = null;
String condition = null;
@ -711,9 +710,6 @@ public class PacketParserUtils {
// Parse the error header
for (int i=0; i<parser.getAttributeCount(); i++) {
if (parser.getAttributeName(i).equals("code")) {
errorCode = parser.getAttributeValue("", "code");
}
if (parser.getAttributeName(i).equals("type")) {
type = parser.getAttributeValue("", "type");
}
@ -754,7 +750,7 @@ public class PacketParserUtils {
catch (IllegalArgumentException iae) {
LOGGER.log(Level.SEVERE, "Could not find error type for " + type.toUpperCase(), iae);
}
return new XMPPError(Integer.parseInt(errorCode), errorType, condition, message, extensions);
return new XMPPError(errorType, condition, message, extensions);
}
/**

View file

@ -317,12 +317,12 @@ public class OutgoingFileTransfer extends FileTransfer {
private void handleXMPPException(XMPPException e) {
XMPPError error = e.getXMPPError();
if (error != null) {
int code = error.getCode();
if (code == 403) {
String condition = error.getCondition();
if (XMPPError.Condition.forbidden.equals(condition)) {
setStatus(Status.refused);
return;
}
else if (code == 400) {
else if (XMPPError.Condition.bad_request.equals(condition)) {
setStatus(Status.error);
setError(Error.not_acceptable);
}

View file

@ -1085,7 +1085,7 @@ public class JingleSession extends JingleNegotiator implements MediaReceivedList
List<PacketExtension> extList = new ArrayList<PacketExtension>();
extList.add(jingleError);
XMPPError error = new XMPPError(0, XMPPError.Type.CANCEL, jingleError.toString(), "", extList);
XMPPError error = new XMPPError(XMPPError.Type.CANCEL, jingleError.toString(), "", extList);
// Fill in the fields with the info from the Jingle packet
errorPacket.setPacketID(iq.getPacketID());