1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

Rework XMPP Error class design

Introduce AbstractError, change 'Conditions' to enums. Because of
AbstractError, it was necessary that PlainStreamElement and
TopLevelStreamElement becomes an interface. Thus the implementation of
TopLevelStreamElement.toString() had to be removed.

This adds

- policy-violation
- unexpected-request

to XMPPError.Condition, and removes the

- payment-required
- remote-server-error
- unexpected-condition
- request-timeout

Conditions

The file transfer code does now no longer throw XMPPErrorExceptions, but
SmackExceptions.

Fixes SMACK-608. Makes it possible to resolves SMACK-386.
This commit is contained in:
Florian Schmaus 2014-11-25 13:11:24 +01:00
parent cc09192095
commit 9286a1decb
31 changed files with 582 additions and 548 deletions

View file

@ -280,7 +280,7 @@ public class Socks5BytestreamRequest implements BytestreamRequest {
*/
private void cancelRequest() throws XMPPErrorException, NotConnectedException {
String errorMessage = "Could not establish socket with any provided host";
XMPPError error = new XMPPError(XMPPError.Condition.item_not_found, errorMessage);
XMPPError error = XMPPError.from(XMPPError.Condition.item_not_found, errorMessage);
IQ errorIQ = IQ.createErrorResponse(this.bytestreamRequest, error);
this.manager.getConnection().sendPacket(errorIQ);
throw new XMPPErrorException(errorMessage, error);

View file

@ -593,8 +593,7 @@ public class AdHocCommandManager extends Manager {
private void respondError(AdHocCommandData response, XMPPError.Condition condition,
AdHocCommand.SpecificErrorCondition specificCondition) throws NotConnectedException
{
XMPPError error = new XMPPError(condition);
error.addExtension(new AdHocCommandData.SpecificError(specificCondition));
XMPPError error = new XMPPError(condition, new AdHocCommandData.SpecificError(specificCondition));
respondError(response, error);
}

View file

@ -0,0 +1,45 @@
/**
*
* Copyright 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.filetransfer;
import org.jivesoftware.smack.SmackException;
public abstract class FileTransferException extends SmackException {
/**
*
*/
private static final long serialVersionUID = 1L;
public static class NoStreamMethodsOfferedException extends FileTransferException {
/**
*
*/
private static final long serialVersionUID = 1L;
}
public static class NoAcceptableTransferMechanisms extends FileTransferException {
/**
*
*/
private static final long serialVersionUID = 1L;
}
}

View file

@ -37,6 +37,8 @@ import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.filetransfer.FileTransferException.NoAcceptableTransferMechanisms;
import org.jivesoftware.smackx.filetransfer.FileTransferException.NoStreamMethodsOfferedException;
import org.jivesoftware.smackx.si.packet.StreamInitiation;
import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smackx.xdata.FormField;
@ -176,32 +178,32 @@ public class FileTransferNegotiator extends Manager {
*
* @param request The related file transfer request.
* @return The file transfer object that handles the transfer
* @throws XMPPErrorException If there are either no stream methods contained in the packet, or
* @throws NoStreamMethodsOfferedException If there are either no stream methods contained in the packet, or
* there is not an appropriate stream method.
* @throws NotConnectedException
* @throws NoAcceptableTransferMechanisms
*/
public StreamNegotiator selectStreamNegotiator(
FileTransferRequest request) throws XMPPErrorException, NotConnectedException {
FileTransferRequest request) throws NotConnectedException, NoStreamMethodsOfferedException, NoAcceptableTransferMechanisms {
StreamInitiation si = request.getStreamInitiation();
FormField streamMethodField = getStreamMethodField(si
.getFeatureNegotiationForm());
if (streamMethodField == null) {
String errorMessage = "No stream methods contained in packet.";
XMPPError error = new XMPPError(XMPPError.Condition.bad_request, errorMessage);
String errorMessage = "No stream methods contained in stanza.";
XMPPError error = XMPPError.from(XMPPError.Condition.bad_request, errorMessage);
IQ iqPacket = IQ.createErrorResponse(si, error);
connection().sendPacket(iqPacket);
throw new XMPPErrorException(errorMessage, error);
throw new FileTransferException.NoStreamMethodsOfferedException();
}
// select the appropriate protocol
StreamNegotiator selectedStreamNegotiator;
try {
selectedStreamNegotiator = getNegotiator(streamMethodField);
}
catch (XMPPErrorException e) {
IQ iqPacket = IQ.createErrorResponse(si, e.getXMPPError());
catch (NoAcceptableTransferMechanisms e) {
IQ iqPacket = IQ.createErrorResponse(si, XMPPError.from(XMPPError.Condition.bad_request, "No acceptable transfer mechanism"));
connection().sendPacket(iqPacket);
throw e;
}
@ -221,7 +223,7 @@ public class FileTransferNegotiator extends Manager {
}
private StreamNegotiator getNegotiator(final FormField field)
throws XMPPErrorException {
throws NoAcceptableTransferMechanisms {
String variable;
boolean isByteStream = false;
boolean isIBB = false;
@ -236,9 +238,7 @@ public class FileTransferNegotiator extends Manager {
}
if (!isByteStream && !isIBB) {
XMPPError error = new XMPPError(XMPPError.Condition.bad_request,
"No acceptable transfer mechanism");
throw new XMPPErrorException(error);
throw new FileTransferException.NoAcceptableTransferMechanisms();
}
if (isByteStream && isIBB) {
@ -299,10 +299,11 @@ public class FileTransferNegotiator extends Manager {
* @throws XMPPErrorException Thrown if there is an error negotiating the file transfer.
* @throws NotConnectedException
* @throws NoResponseException
* @throws NoAcceptableTransferMechanisms
*/
public StreamNegotiator negotiateOutgoingTransfer(final String userID,
final String streamID, final String fileName, final long size,
final String desc, int responseTimeout) throws XMPPErrorException, NotConnectedException, NoResponseException {
final String desc, int responseTimeout) throws XMPPErrorException, NotConnectedException, NoResponseException, NoAcceptableTransferMechanisms {
StreamInitiation si = new StreamInitiation();
si.setSessionID(streamID);
si.setMimeType(URLConnection.guessContentTypeFromName(fileName));
@ -337,8 +338,7 @@ public class FileTransferNegotiator extends Manager {
}
}
private StreamNegotiator getOutgoingNegotiator(final FormField field)
throws XMPPErrorException {
private StreamNegotiator getOutgoingNegotiator(final FormField field) throws NoAcceptableTransferMechanisms {
boolean isByteStream = false;
boolean isIBB = false;
for (String variable : field.getValues()) {
@ -351,9 +351,7 @@ public class FileTransferNegotiator extends Manager {
}
if (!isByteStream && !isIBB) {
XMPPError error = new XMPPError(XMPPError.Condition.bad_request,
"No acceptable transfer mechanism");
throw new XMPPErrorException(error);
throw new FileTransferException.NoAcceptableTransferMechanisms();
}
if (isByteStream && isIBB) {

View file

@ -341,16 +341,15 @@ public class OutgoingFileTransfer extends FileTransfer {
private void handleXMPPException(XMPPErrorException e) {
XMPPError error = e.getXMPPError();
if (error != null) {
String condition = error.getCondition();
if (XMPPError.Condition.forbidden.equals(condition)) {
switch (error.getCondition()) {
case forbidden:
setStatus(Status.refused);
return;
}
else if (XMPPError.Condition.bad_request.equals(condition)) {
case bad_request:
setStatus(Status.error);
setError(Error.not_acceptable);
}
else {
break;
default:
setStatus(FileTransfer.Status.error);
}
}

View file

@ -73,7 +73,7 @@ public class CloseListenerTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.item_not_found.toString(),
assertEquals(XMPPError.Condition.item_not_found,
argument.getValue().getError().getCondition());
}

View file

@ -75,7 +75,7 @@ public class DataListenerTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.item_not_found.toString(),
assertEquals(XMPPError.Condition.item_not_found,
argument.getValue().getError().getCondition());
}

View file

@ -120,7 +120,7 @@ public class InBandBytestreamManagerTest {
fail("exception should be thrown");
}
catch (XMPPErrorException e) {
assertEquals(XMPPError.Condition.feature_not_implemented.toString(),
assertEquals(XMPPError.Condition.feature_not_implemented,
e.getXMPPError().getCondition());
}

View file

@ -83,7 +83,7 @@ public class InBandBytestreamRequestTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.not_acceptable.toString(),
assertEquals(XMPPError.Condition.not_acceptable,
argument.getValue().getError().getCondition());
}

View file

@ -368,7 +368,7 @@ public class InBandBytestreamSessionTest {
protocol.addResponse(null, Verification.requestTypeERROR, new Verification<IQ, IQ>() {
public void verify(IQ request, IQ response) {
assertEquals(XMPPError.Condition.unexpected_request.toString(),
assertEquals(XMPPError.Condition.unexpected_request,
request.getError().getCondition());
}
@ -406,7 +406,7 @@ public class InBandBytestreamSessionTest {
protocol.addResponse(null, Verification.requestTypeERROR, new Verification<IQ, IQ>() {
public void verify(IQ request, IQ response) {
assertEquals(XMPPError.Condition.bad_request.toString(),
assertEquals(XMPPError.Condition.bad_request,
request.getError().getCondition());
}

View file

@ -91,7 +91,7 @@ public class InitiationListenerTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.not_acceptable.toString(),
assertEquals(XMPPError.Condition.not_acceptable,
argument.getValue().getError().getCondition());
}
@ -119,7 +119,7 @@ public class InitiationListenerTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.resource_constraint.toString(),
assertEquals(XMPPError.Condition.resource_constraint,
argument.getValue().getError().getCondition());
}
@ -209,7 +209,7 @@ public class InitiationListenerTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.not_acceptable.toString(),
assertEquals(XMPPError.Condition.not_acceptable,
argument.getValue().getError().getCondition());
}

View file

@ -98,7 +98,7 @@ public class InitiationListenerTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.not_acceptable.toString(),
assertEquals(XMPPError.Condition.not_acceptable,
argument.getValue().getError().getCondition());
}
@ -188,7 +188,7 @@ public class InitiationListenerTest {
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
assertEquals(IQ.Type.error, argument.getValue().getType());
assertEquals(XMPPError.Condition.not_acceptable.toString(),
assertEquals(XMPPError.Condition.not_acceptable,
argument.getValue().getError().getCondition());
}

View file

@ -113,7 +113,7 @@ public class Socks5ByteStreamRequestTest {
assertTrue(IQ.class.isInstance(targetResponse));
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
assertEquals(XMPPError.Condition.item_not_found.toString(),
assertEquals(XMPPError.Condition.item_not_found,
((IQ) targetResponse).getError().getCondition());
}
@ -157,7 +157,7 @@ public class Socks5ByteStreamRequestTest {
assertTrue(IQ.class.isInstance(targetResponse));
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
assertEquals(XMPPError.Condition.item_not_found.toString(),
assertEquals(XMPPError.Condition.item_not_found,
((IQ) targetResponse).getError().getCondition());
}
@ -205,7 +205,7 @@ public class Socks5ByteStreamRequestTest {
assertTrue(IQ.class.isInstance(targetResponse));
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
assertEquals(XMPPError.Condition.item_not_found.toString(),
assertEquals(XMPPError.Condition.item_not_found,
((IQ) targetResponse).getError().getCondition());
}
@ -297,7 +297,7 @@ public class Socks5ByteStreamRequestTest {
assertTrue(IQ.class.isInstance(targetResponse));
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
assertEquals(XMPPError.Condition.item_not_found.toString(),
assertEquals(XMPPError.Condition.item_not_found,
((IQ) targetResponse).getError().getCondition());
}