001/**
002 *
003 * Copyright 2017 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.omemo.exceptions;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
024
025import org.jxmpp.jid.BareJid;
026
027/**
028 * Exception gets thrown when we are unable to establish a session with a device for some reason.
029 *
030 * @author Paul Schaub
031 */
032public class CannotEstablishOmemoSessionException extends Exception {
033
034    private static final long serialVersionUID = 3165844730283295249L;
035    private final HashMap<BareJid, HashMap<OmemoDevice, Throwable>> failures = new HashMap<>();
036    private final HashMap<BareJid, ArrayList<OmemoDevice>> successes = new HashMap<>();
037
038    public CannotEstablishOmemoSessionException(OmemoDevice failed, Throwable reason) {
039        super();
040        getFailsOfContact(failed.getJid()).put(failed, reason);
041    }
042
043    public void addFailures(CannotEstablishOmemoSessionException otherFailures) {
044        for (Map.Entry<BareJid, HashMap<OmemoDevice, Throwable>> entry : otherFailures.getFailures().entrySet()) {
045            getFailsOfContact(entry.getKey()).putAll(entry.getValue());
046        }
047    }
048
049    public void addSuccess(OmemoDevice success) {
050        getSuccessesOfContact(success.getJid()).add(success);
051    }
052
053    public HashMap<BareJid, HashMap<OmemoDevice, Throwable>> getFailures() {
054        return failures;
055    }
056
057    public HashMap<BareJid, ArrayList<OmemoDevice>> getSuccesses() {
058        return successes;
059    }
060
061    private HashMap<OmemoDevice, Throwable> getFailsOfContact(BareJid contact) {
062        HashMap<OmemoDevice, Throwable> h = failures.get(contact);
063        if (h == null) {
064            h = new HashMap<>();
065            failures.put(contact, h);
066        }
067        return h;
068    }
069
070    private ArrayList<OmemoDevice> getSuccessesOfContact(BareJid contact) {
071        ArrayList<OmemoDevice> suc = successes.get(contact);
072        if (suc == null) {
073            suc = new ArrayList<>();
074            successes.put(contact, suc);
075        }
076        return suc;
077    }
078
079    /**
080     * Return true, if there is at least one recipient, which would not be able to decipher the message on any of
081     * their devices.
082     *
083     * @return true if the exception requires to be thrown
084     */
085    public boolean requiresThrowing() {
086        for (Map.Entry<BareJid, HashMap<OmemoDevice, Throwable>> entry : failures.entrySet()) {
087            ArrayList<OmemoDevice> suc = successes.get(entry.getKey());
088            if (suc == null || suc.isEmpty()) {
089                return true;
090            }
091        }
092        return false;
093    }
094}