diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-07-09 21:49:15 (GMT) |
---|---|---|
committer | Nick Hudson <nick.hudson@isode.com> | 2015-07-22 16:23:29 (GMT) |
commit | 2341df7c72714df988b39cb2019562987ea28cbb (patch) | |
tree | 266eceb8732a75ff5fc78ac20b0ba7b5fa191267 /src | |
parent | 7f96c8b224099f1c0e5c6996984fe79558e0d550 (diff) | |
download | stroke-2341df7c72714df988b39cb2019562987ea28cbb.zip stroke-2341df7c72714df988b39cb2019562987ea28cbb.tar.bz2 |
Complete VCard functionalities.
Updates VCardManager, VCardMemoryStorage and VCardStorage.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Test added for VCardManager, which passes.
Change-Id: I0d4f1dbb647f262ff2a8967807fb798a8181b0b5
Diffstat (limited to 'src')
-rw-r--r-- | src/com/isode/stroke/vcards/VCardManager.java | 19 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardMemoryStorage.java | 14 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardStorage.java | 2 |
3 files changed, 30 insertions, 5 deletions
diff --git a/src/com/isode/stroke/vcards/VCardManager.java b/src/com/isode/stroke/vcards/VCardManager.java index 2a7837c..a5b62e8 100644 --- a/src/com/isode/stroke/vcards/VCardManager.java +++ b/src/com/isode/stroke/vcards/VCardManager.java @@ -14,12 +14,14 @@ import com.isode.stroke.queries.IQRouter; import com.isode.stroke.signals.Signal1; import com.isode.stroke.signals.Signal2; import com.isode.stroke.signals.Slot2; +import java.util.Date; +import java.util.TimeZone; public class VCardManager { - private final JID ownJID; - private final IQRouter iqRouter; - private final VCardStorage storage; - private final Set<JID> requestedVCards = new HashSet<JID>(); + private JID ownJID = new JID(); + private IQRouter iqRouter; + private VCardStorage storage; + private Set<JID> requestedVCards = new HashSet<JID>(); /** * The JID will always be bare. @@ -37,6 +39,7 @@ public class VCardManager { this.ownJID = ownJID; this.iqRouter = iqRouter; this.storage = vcardStorage; + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); } public void delete() { @@ -47,8 +50,14 @@ public class VCardManager { } public VCard getVCardAndRequestWhenNeeded(final JID jid) { + return getVCardAndRequestWhenNeeded(jid, null); + } + + public VCard getVCardAndRequestWhenNeeded(final JID jid, final Date allowedAge) { VCard vcard = storage.getVCard(jid); - if (vcard == null) { + Date vcardFetchedTime = storage.getVCardWriteTime(jid); + boolean vcardTooOld = (vcard != null) && (vcardFetchedTime == null || (allowedAge != null && ((new Date().getTime() - vcardFetchedTime.getTime()) > allowedAge.getTime()))); + if (vcard == null || vcardTooOld) { requestVCard(jid); } return vcard; diff --git a/src/com/isode/stroke/vcards/VCardMemoryStorage.java b/src/com/isode/stroke/vcards/VCardMemoryStorage.java index d666d98..24ca6f4 100644 --- a/src/com/isode/stroke/vcards/VCardMemoryStorage.java +++ b/src/com/isode/stroke/vcards/VCardMemoryStorage.java @@ -10,22 +10,36 @@ import java.util.Map; import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; +import java.util.Date; +import java.util.TimeZone; public class VCardMemoryStorage extends VCardStorage { public VCardMemoryStorage(CryptoProvider crypto) { super(crypto); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); } private Map<JID, VCard> vcards = new HashMap<JID, VCard>(); + private Map<JID, Date> vcardWriteTimes = new HashMap<JID, Date>(); @Override public VCard getVCard(JID jid) { return vcards.get(jid); } + /** + * @param JID jid. + * @return Date, May be Null. + */ + @Override + public Date getVCardWriteTime(final JID jid) { + return vcardWriteTimes.get(jid); + } + @Override public void setVCard(JID jid, VCard vcard) { vcards.put(jid, vcard); + vcardWriteTimes.put(jid, new Date()); } } diff --git a/src/com/isode/stroke/vcards/VCardStorage.java b/src/com/isode/stroke/vcards/VCardStorage.java index 9021599..cb732ea 100644 --- a/src/com/isode/stroke/vcards/VCardStorage.java +++ b/src/com/isode/stroke/vcards/VCardStorage.java @@ -8,11 +8,13 @@ import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; import com.isode.stroke.stringcodecs.Hexify; +import java.util.Date; public abstract class VCardStorage { private CryptoProvider crypto; public abstract VCard getVCard(JID jid); + public abstract Date getVCardWriteTime(JID jid); public abstract void setVCard(JID jid, VCard vcard); public VCardStorage(CryptoProvider crypto) { |