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.HashMap;
021import java.util.Map;
022
023import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore;
024
025import org.jxmpp.jid.BareJid;
026import org.pgpainless.key.OpenPgpV4Fingerprint;
027
028public abstract class AbstractOpenPgpTrustStore implements OpenPgpTrustStore {
029
030    private final Map<BareJid, Map<OpenPgpV4Fingerprint, Trust>> trustCache = new HashMap<>();
031
032    /**
033     * Read the trust record for the key with fingerprint {@code fingerprint} of user {@code owner} from local storage.
034     * This method returns {@link Trust#undecided} in case that no trust record has been found.
035     *
036     * @param owner owner of the key
037     * @param fingerprint fingerprint of the key
038     * @return trust state of the key
039     *
040     * @throws IOException IO is dangerous
041     */
042    protected abstract Trust readTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException;
043
044    /**
045     * Write the trust record for the key with fingerprint {@code fingerprint} of user {@code owner} to local storage.
046     *
047     * @param owner owner of the key
048     * @param fingerprint fingerprint of the key
049     * @param trust trust state of the key
050     *
051     * @throws IOException IO is dangerous
052     */
053    protected abstract void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException;
054
055    @Override
056    public Trust getTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException {
057        Trust trust;
058        Map<OpenPgpV4Fingerprint, Trust> trustMap = trustCache.get(owner);
059
060        if (trustMap != null) {
061            trust = trustMap.get(fingerprint);
062            if (trust != null) {
063                return trust;
064            }
065        } else {
066            trustMap = new HashMap<>();
067            trustCache.put(owner, trustMap);
068        }
069
070        trust = readTrust(owner, fingerprint);
071        trustMap.put(fingerprint, trust);
072
073        return trust;
074    }
075
076    @Override
077    public void setTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException {
078        Map<OpenPgpV4Fingerprint, Trust> trustMap = trustCache.get(owner);
079        if (trustMap == null) {
080            trustMap = new HashMap<>();
081            trustCache.put(owner, trustMap);
082        }
083
084        if (trustMap.get(fingerprint) == trust) {
085            return;
086        }
087
088        trustMap.put(fingerprint, trust);
089        writeTrust(owner, fingerprint, trust);
090    }
091}