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