diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-08-07 12:57:57 +0200 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-08-07 12:57:57 +0200 |
commit | a892bb6f1685b84b74ba37e5a6a24ee8fb45eee7 (patch) | |
tree | f6d8036841f961dd987c2fb48e41983b327be049 /src/main/java/de/thedevstack/conversationsplus/xmpp/time | |
parent | be1992ce897d3b0551e545646a3e0f45cc6dd8c2 (diff) |
Simplifying introduction of new XEP implementations, implements FS#250 and FS#251 (Privacy and Security settings), refactoring of location of some interfaces
Diffstat (limited to '')
5 files changed, 105 insertions, 41 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/EntityTime.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/EntityTime.java new file mode 100644 index 00000000..7382f37a --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/EntityTime.java @@ -0,0 +1,8 @@ +package de.thedevstack.conversationsplus.xmpp.time; + +/** + */ +public interface EntityTime { + String NAMESPACE = "urn:xmpp:time"; + String ELEMENT = "time"; +} diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/EntityTimeXep.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/EntityTimeXep.java new file mode 100644 index 00000000..fb721bce --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/EntityTimeXep.java @@ -0,0 +1,51 @@ +package de.thedevstack.conversationsplus.xmpp.time; + +import de.thedevstack.conversationsplus.xmpp.AbstractXep; +import de.thedevstack.conversationsplus.xmpp.IqPacketHandler; + +/** + */ +public class EntityTimeXep extends AbstractXep { + public EntityTimeXep() { + super(); + } + + public EntityTimeXep(boolean enabled) { + super(enabled); + } + + @Override + public String xepNumber() { + return "0202"; + } + + @Override + public String shortName() { + return "time"; + } + + @Override + public String name() { + return "Entity Time"; + } + + @Override + public String namespace() { + return EntityTime.NAMESPACE; + } + + @Override + public String featureNamespace() { + return EntityTime.NAMESPACE; + } + + @Override + public String elementName() { + return EntityTime.ELEMENT; + } + + @Override + public IqPacketHandler handler() { + return new TimeIqPacketHandler(); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimeIqPacketHandler.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimeIqPacketHandler.java new file mode 100644 index 00000000..961c649b --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimeIqPacketHandler.java @@ -0,0 +1,21 @@ +package de.thedevstack.conversationsplus.xmpp.time; + +import de.thedevstack.conversationsplus.entities.Account; +import de.thedevstack.conversationsplus.utils.XmppSendUtil; +import de.thedevstack.conversationsplus.xmpp.IqPacketHandler; +import de.thedevstack.conversationsplus.xmpp.exceptions.IqPacketErrorException; +import de.thedevstack.conversationsplus.xmpp.exceptions.NotAllowedIqException; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; + +/** + */ +public class TimeIqPacketHandler implements IqPacketHandler { + @Override + public void handleIqPacket(Account account, IqPacket packet) throws IqPacketErrorException { + if (packet.getType() == IqPacket.TYPE.GET) { + XmppSendUtil.sendIqPacket(account, TimePacketGenerator.generateResponse(packet)); + } else { + throw new NotAllowedIqException(packet, "only type=get allowed for processing an Entity Time (XEP-0202)"); + } + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacket.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacket.java deleted file mode 100644 index d8756d41..00000000 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacket.java +++ /dev/null @@ -1,35 +0,0 @@ -package de.thedevstack.conversationsplus.xmpp.time; - -import java.util.Locale; -import java.util.TimeZone; - -import de.thedevstack.conversationsplus.generator.AbstractGenerator; -import de.thedevstack.conversationsplus.xml.Element; -import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; - -/** - * Representation of an software version packet as defined in XEP-0202. - * @see <a href="http://xmpp.org/extensions/xep-0202.html">http://xmpp.org/extensions/xep-0202.html</a> - */ -public class TimePacket extends IqPacket { - public static final String NAMESPACE = "urn:xmpp:time"; - - TimePacket() { - super(IqPacket.TYPE.RESULT); - Element time = this.addChild("time", NAMESPACE); - final long now = System.currentTimeMillis(); - time.addChild("utc").setContent(AbstractGenerator.getTimestamp(now)); - TimeZone ourTimezone = TimeZone.getDefault(); - long offsetSeconds = ourTimezone.getOffset(now) / 1000; - long offsetMinutes = Math.abs((offsetSeconds % 3600) / 60); - long offsetHours = offsetSeconds / 3600; - String hours; - if (offsetHours < 0) { - hours = String.format(Locale.US, "%03d", offsetHours); - } else { - hours = String.format(Locale.US, "%02d", offsetHours); - } - String minutes = String.format(Locale.US, "%02d", offsetMinutes); - time.addChild("tzo").setContent(hours + ":" + minutes); - } -} diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java index 344beb9e..ef381d4d 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/time/TimePacketGenerator.java @@ -1,7 +1,12 @@ package de.thedevstack.conversationsplus.xmpp.time; -import de.thedevstack.conversationsplus.xmpp.iqversion.IqVersionPacket; +import java.util.Locale; +import java.util.TimeZone; + +import de.thedevstack.conversationsplus.generator.AbstractGenerator; +import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacketGenerator; /** * Generates the IQ Packets for Entity Time @@ -26,12 +31,26 @@ public final class TimePacketGenerator { * @param packet * @return */ - public static TimePacket generateResponse(IqPacket packet) { - TimePacket timePacket = new TimePacket(); - timePacket.setTo(packet.getFrom()); - timePacket.setId(packet.getId()); + public static IqPacket generateResponse(IqPacket packet) { + IqPacket responsePacket = IqPacketGenerator.generateIqResultResponse(packet); + + Element time = responsePacket.addChild(EntityTime.ELEMENT, EntityTime.NAMESPACE); + final long now = System.currentTimeMillis(); + time.addChild("utc").setContent(AbstractGenerator.getTimestamp(now)); + TimeZone ourTimezone = TimeZone.getDefault(); + long offsetSeconds = ourTimezone.getOffset(now) / 1000; + long offsetMinutes = Math.abs((offsetSeconds % 3600) / 60); + long offsetHours = offsetSeconds / 3600; + String hours; + if (offsetHours < 0) { + hours = String.format(Locale.US, "%03d", offsetHours); + } else { + hours = String.format(Locale.US, "%02d", offsetHours); + } + String minutes = String.format(Locale.US, "%02d", offsetMinutes); + time.addChild("tzo").setContent(hours + ":" + minutes); - return timePacket; + return responsePacket; } private TimePacketGenerator() {} |