001/**
002 *
003 * Copyright 2018 Paul Schaub.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.ox.store.abstr;
018
019import java.io.IOException;
020import java.util.Date;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore;
025
026import org.jxmpp.jid.BareJid;
027import org.pgpainless.key.OpenPgpV4Fingerprint;
028
029public abstract class AbstractOpenPgpMetadataStore implements OpenPgpMetadataStore {
030
031    private final Map<BareJid, Map<OpenPgpV4Fingerprint, Date>> announcedFingerprints = new HashMap<>();
032
033    @Override
034    public Map<OpenPgpV4Fingerprint, Date> getAnnouncedFingerprintsOf(BareJid contact) throws IOException {
035        Map<OpenPgpV4Fingerprint, Date> fingerprints = announcedFingerprints.get(contact);
036        if (fingerprints == null) {
037            fingerprints = readAnnouncedFingerprintsOf(contact);
038            announcedFingerprints.put(contact, fingerprints);
039        }
040        return fingerprints;
041    }
042
043    @Override
044    public void setAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> data) throws IOException {
045        announcedFingerprints.put(contact, data);
046        writeAnnouncedFingerprintsOf(contact, data);
047    }
048
049    /**
050     * Read the fingerprints and modification dates of announced keys of a user from local storage.
051     *
052     * @param contact contact
053     * @return contacts announced key fingerprints and latest modification dates
054     *
055     * @throws IOException IO is dangerous
056     */
057    protected abstract Map<OpenPgpV4Fingerprint, Date> readAnnouncedFingerprintsOf(BareJid contact) throws IOException;
058
059    /**
060     * Write the fingerprints and modification dates of announced keys of a user to local storage.
061     *
062     * @param contact contact
063     * @param metadata announced key fingerprints and latest modification dates
064     *
065     * @throws IOException IO is dangerous
066     */
067    protected abstract void writeAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> metadata) throws IOException;
068}