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

Make XMPPError imutable and add stanza reference

This commit is contained in:
Florian Schmaus 2015-12-08 08:22:50 +01:00
parent 83eda4c58d
commit 45feaecdf7
30 changed files with 308 additions and 94 deletions

View file

@ -444,8 +444,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
* @throws InterruptedException
*/
protected void replyRejectPacket(IQ request) throws NotConnectedException, InterruptedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.not_acceptable);
IQ error = IQ.createErrorResponse(request, xmppError);
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.not_acceptable);
this.connection.sendStanza(error);
}
@ -458,8 +457,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
* @throws InterruptedException
*/
protected void replyResourceConstraintPacket(IQ request) throws NotConnectedException, InterruptedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.resource_constraint);
IQ error = IQ.createErrorResponse(request, xmppError);
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.resource_constraint);
this.connection.sendStanza(error);
}
@ -472,8 +470,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
* @throws InterruptedException
*/
protected void replyItemNotFoundPacket(IQ request) throws NotConnectedException, InterruptedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.item_not_found);
IQ error = IQ.createErrorResponse(request, xmppError);
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.item_not_found);
this.connection.sendStanza(error);
}

View file

@ -457,8 +457,8 @@ public class InBandBytestreamSession implements BytestreamSession {
* check if sequence was not used already (see XEP-0047 Section 2.2)
*/
if (data.getSeq() <= this.lastSequence) {
IQ unexpectedRequest = IQ.createErrorResponse((IQ) packet, new XMPPError(
XMPPError.Condition.unexpected_request));
IQ unexpectedRequest = IQ.createErrorResponse((IQ) packet,
XMPPError.Condition.unexpected_request);
connection.sendStanza(unexpectedRequest);
return;
@ -467,8 +467,8 @@ public class InBandBytestreamSession implements BytestreamSession {
// check if encoded data is valid (see XEP-0047 Section 2.2)
if (data.getDecodedData() == null) {
// data is invalid; respond with bad-request error
IQ badRequest = IQ.createErrorResponse((IQ) packet, new XMPPError(
XMPPError.Condition.bad_request));
IQ badRequest = IQ.createErrorResponse((IQ) packet,
XMPPError.Condition.bad_request);
connection.sendStanza(badRequest);
return;
}

View file

@ -691,7 +691,7 @@ public final class Socks5BytestreamManager extends Manager implements Bytestream
* @throws InterruptedException
*/
protected void replyRejectPacket(IQ packet) throws NotConnectedException, InterruptedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.not_acceptable);
XMPPError.Builder xmppError = XMPPError.getBuilder(XMPPError.Condition.not_acceptable);
IQ errorIQ = IQ.createErrorResponse(packet, xmppError);
connection().sendStanza(errorIQ);
}

View file

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

View file

@ -451,7 +451,7 @@ public final class AdHocCommandManager extends Manager {
response.setStatus(Status.canceled);
executingCommands.remove(sessionId);
}
return respondError(response, error);
return respondError(response, XMPPError.getBuilder(error));
}
}
else {
@ -561,7 +561,7 @@ public final class AdHocCommandManager extends Manager {
response.setStatus(Status.canceled);
executingCommands.remove(sessionId);
}
return respondError(response, error);
return respondError(response, XMPPError.getBuilder(error));
}
}
}
@ -576,7 +576,7 @@ public final class AdHocCommandManager extends Manager {
*/
private static IQ respondError(AdHocCommandData response,
XMPPError.Condition condition) {
return respondError(response, new XMPPError(condition));
return respondError(response, XMPPError.getBuilder(condition));
}
/**
@ -590,7 +590,7 @@ public final class AdHocCommandManager extends Manager {
private static IQ respondError(AdHocCommandData response, XMPPError.Condition condition,
AdHocCommand.SpecificErrorCondition specificCondition)
{
XMPPError error = new XMPPError(condition, new AdHocCommandData.SpecificError(specificCondition));
XMPPError.Builder error = XMPPError.getBuilder(condition).addExtension(new AdHocCommandData.SpecificError(specificCondition));
return respondError(response, error);
}
@ -601,7 +601,7 @@ public final class AdHocCommandManager extends Manager {
* @param error the error to send.
* @throws NotConnectedException
*/
private static IQ respondError(AdHocCommandData response, XMPPError error) {
private static IQ respondError(AdHocCommandData response, XMPPError.Builder error) {
response.setType(IQ.Type.error);
response.setError(error);
return response;
@ -626,11 +626,11 @@ public final class AdHocCommandManager extends Manager {
command.setNode(commandInfo.getNode());
}
catch (InstantiationException e) {
throw new XMPPErrorException(new XMPPError(
throw new XMPPErrorException(XMPPError.getBuilder(
XMPPError.Condition.internal_server_error));
}
catch (IllegalAccessException e) {
throw new XMPPErrorException(new XMPPError(
throw new XMPPErrorException(XMPPError.getBuilder(
XMPPError.Condition.internal_server_error));
}
return command;

View file

@ -101,7 +101,7 @@ public class AdHocCommandDataProvider extends IQProvider<AdHocCommandData> {
adHocCommandData.addNote(new AdHocCommandNote(type, value));
}
else if (parser.getName().equals("error")) {
XMPPError error = PacketParserUtils.parseError(parser);
XMPPError.Builder error = PacketParserUtils.parseError(parser);
adHocCommandData.setError(error);
}
}

View file

@ -143,7 +143,7 @@ public final class ServiceDiscoveryManager extends Manager {
// Return <item-not-found/> error since client doesn't contain
// the specified node
response.setType(IQ.Type.error);
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
response.setError(XMPPError.getBuilder(XMPPError.Condition.item_not_found));
}
return response;
}
@ -181,7 +181,7 @@ public final class ServiceDiscoveryManager extends Manager {
} else {
// Return <item-not-found/> error since specified node was not found
response.setType(IQ.Type.error);
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
response.setError(XMPPError.getBuilder(XMPPError.Condition.item_not_found));
}
}
return response;

View file

@ -169,7 +169,7 @@ public final class FileTransferManager extends Manager {
// Bytestream rejection as specified in XEP-65 5.3.1 Example 13, which says that
// 'not-acceptable' should be returned. This is done by Smack in
// Socks5BytestreamManager.replyRejectPacket(IQ).
IQ rejection = IQ.createErrorResponse(initiation, new XMPPError(
IQ rejection = IQ.createErrorResponse(initiation, XMPPError.getBuilder(
XMPPError.Condition.forbidden));
connection().sendStanza(rejection);
}

View file

@ -192,7 +192,7 @@ public final class FileTransferNegotiator extends Manager {
if (streamMethodField == null) {
String errorMessage = "No stream methods contained in stanza.";
XMPPError error = XMPPError.from(XMPPError.Condition.bad_request, errorMessage);
XMPPError.Builder error = XMPPError.from(XMPPError.Condition.bad_request, errorMessage);
IQ iqPacket = IQ.createErrorResponse(si, error);
connection().sendStanza(iqPacket);
throw new FileTransferException.NoStreamMethodsOfferedException();

View file

@ -36,7 +36,6 @@ import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.iqlast.packet.LastActivity;
@ -169,7 +168,7 @@ public final class LastActivityManager extends Manager {
@Override
public IQ handleIQRequest(IQ iqRequest) {
if (!enabled)
return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable));
return IQ.createErrorResponse(iqRequest, Condition.not_acceptable);
LastActivity message = new LastActivity();
message.setType(IQ.Type.result);
message.setTo(iqRequest.getFrom());

View file

@ -31,7 +31,6 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.iqversion.packet.Version;
@ -89,7 +88,7 @@ public final class VersionManager extends Manager {
@Override
public IQ handleIQRequest(IQ iqRequest) {
if (ourVersion == null) {
return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable));
return IQ.createErrorResponse(iqRequest, Condition.not_acceptable);
}
return Version.createResultFor(iqRequest, ourVersion);

View file

@ -30,7 +30,6 @@ import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.time.packet.Time;
@ -78,7 +77,7 @@ public final class EntityTimeManager extends Manager {
return Time.createResponse(iqRequest);
}
else {
return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable));
return IQ.createErrorResponse(iqRequest, Condition.not_acceptable);
}
}
});

View file

@ -34,10 +34,11 @@ public class IBBPacketUtils {
*
* @param from the senders JID
* @param to the recipients JID
* @param xmppError the XMPP error
* @param condition the XMPP error condition
* @return an error IQ
*/
public static IQ createErrorIQ(Jid from, Jid to, XMPPError xmppError) {
public static IQ createErrorIQ(Jid from, Jid to, XMPPError.Condition condition) {
XMPPError.Builder xmppError = XMPPError.getBuilder(condition);
IQ errorIQ = new ErrorIQ(xmppError);
errorIQ.setType(IQ.Type.error);
errorIQ.setFrom(from);

View file

@ -115,9 +115,8 @@ public class InBandBytestreamManagerTest extends InitExtensions {
InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);
try {
XMPPError xmppError = new XMPPError(
IQ errorIQ = IBBPacketUtils.createErrorIQ(targetJID, initiatorJID,
XMPPError.Condition.feature_not_implemented);
IQ errorIQ = IBBPacketUtils.createErrorIQ(targetJID, initiatorJID, xmppError);
protocol.addResponse(errorIQ);
// start In-Band Bytestream

View file

@ -431,11 +431,10 @@ public class Socks5ByteStreamManagerTest {
Verification.requestTypeGET);
// build error packet to reject SOCKS5 Bytestream
XMPPError xmppError = new XMPPError(XMPPError.Condition.not_acceptable);
IQ rejectPacket = new ErrorIQ(xmppError);
XMPPError.Builder builder = XMPPError.getBuilder(XMPPError.Condition.not_acceptable);
IQ rejectPacket = new ErrorIQ(builder);
rejectPacket.setFrom(targetJID);
rejectPacket.setTo(initiatorJID);
rejectPacket.setError(xmppError);
// return error packet as response to the bytestream initiation
protocol.addResponse(rejectPacket, Verification.correspondingSenderReceiver,
@ -450,7 +449,7 @@ public class Socks5ByteStreamManagerTest {
}
catch (XMPPErrorException e) {
protocol.verifyAll();
assertEquals(xmppError, e.getXMPPError());
assertEquals(rejectPacket.getError(), e.getXMPPError());
}
catch (Exception e) {
fail(e.getMessage());

View file

@ -109,7 +109,7 @@ public class Socks5ByteStreamRequestTest {
fail("exception should be thrown");
}
catch (XMPPErrorException e) {
assertTrue(e.getMessage().contains("Could not establish socket with any provided host"));
assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host"));
}
// verify targets response
@ -153,7 +153,7 @@ public class Socks5ByteStreamRequestTest {
fail("exception should be thrown");
}
catch (XMPPErrorException e) {
assertTrue(e.getMessage().contains("Could not establish socket with any provided host"));
assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host"));
}
// verify targets response
@ -200,7 +200,7 @@ public class Socks5ByteStreamRequestTest {
fail("exception should be thrown");
}
catch (XMPPErrorException e) {
assertTrue(e.getMessage().contains(
assertTrue(e.getXMPPError().getDescriptiveText("en").contains(
"Could not establish socket with any provided host"));
}
@ -291,8 +291,8 @@ public class Socks5ByteStreamRequestTest {
fail("exception should be thrown");
}
catch (XMPPException e) {
assertTrue(e.getMessage().contains(
catch (XMPPErrorException e) {
assertTrue(e.getXMPPError().getDescriptiveText("en").contains(
"Could not establish socket with any provided host"));
}

View file

@ -201,8 +201,7 @@ public class Socks5ClientForInitiatorTest {
public void shouldFailIfActivateSocks5ProxyFails() throws Exception {
// build error response as reply to the stream activation
XMPPError xmppError = new XMPPError(XMPPError.Condition.internal_server_error);
IQ error = new ErrorIQ(xmppError);
IQ error = new ErrorIQ(XMPPError.getBuilder(XMPPError.Condition.internal_server_error));
error.setFrom(proxyJID);
error.setTo(initiatorJID);

View file

@ -62,7 +62,7 @@ public class ConfigureFormTest
Node node = mgr.getNode("princely_musings");
PubSub errorIq = new PubSub();
XMPPError error = new XMPPError(Condition.forbidden);
XMPPError.Builder error = XMPPError.getBuilder(Condition.forbidden);
errorIq.setError(error);
con.addIQReply(errorIq);