CannotEstablishOmemoSessionException.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.exceptions;

  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import org.jivesoftware.smackx.omemo.internal.OmemoDevice;

  22. import org.jxmpp.jid.BareJid;

  23. /**
  24.  * Exception gets thrown when we are unable to establish a session with a device for some reason.
  25.  *
  26.  * @author Paul Schaub
  27.  */
  28. public class CannotEstablishOmemoSessionException extends Exception {

  29.     private static final long serialVersionUID = 3165844730283295249L;
  30.     private final HashMap<BareJid, HashMap<OmemoDevice, Throwable>> failures = new HashMap<>();
  31.     private final HashMap<BareJid, ArrayList<OmemoDevice>> successes = new HashMap<>();

  32.     public CannotEstablishOmemoSessionException(OmemoDevice failed, Throwable reason) {
  33.         super();
  34.         getFailsOfContact(failed.getJid()).put(failed, reason);
  35.     }

  36.     public void addFailures(CannotEstablishOmemoSessionException otherFailures) {
  37.         for (Map.Entry<BareJid, HashMap<OmemoDevice, Throwable>> entry : otherFailures.getFailures().entrySet()) {
  38.             getFailsOfContact(entry.getKey()).putAll(entry.getValue());
  39.         }
  40.     }

  41.     public void addSuccess(OmemoDevice success) {
  42.         getSuccessesOfContact(success.getJid()).add(success);
  43.     }

  44.     public HashMap<BareJid, HashMap<OmemoDevice, Throwable>> getFailures() {
  45.         return failures;
  46.     }

  47.     public HashMap<BareJid, ArrayList<OmemoDevice>> getSuccesses() {
  48.         return successes;
  49.     }

  50.     private HashMap<OmemoDevice, Throwable> getFailsOfContact(BareJid contact) {
  51.         HashMap<OmemoDevice, Throwable> h = failures.get(contact);
  52.         if (h == null) {
  53.             h = new HashMap<>();
  54.             failures.put(contact, h);
  55.         }
  56.         return h;
  57.     }

  58.     private ArrayList<OmemoDevice> getSuccessesOfContact(BareJid contact) {
  59.         ArrayList<OmemoDevice> suc = successes.get(contact);
  60.         if (suc == null) {
  61.             suc = new ArrayList<>();
  62.             successes.put(contact, suc);
  63.         }
  64.         return suc;
  65.     }

  66.     /**
  67.      * Return true, if there is at least one recipient, which would not be able to decipher the message on any of
  68.      * their devices.
  69.      *
  70.      * @return true if the exception requires to be thrown
  71.      */
  72.     public boolean requiresThrowing() {
  73.         for (Map.Entry<BareJid, HashMap<OmemoDevice, Throwable>> entry : failures.entrySet()) {
  74.             ArrayList<OmemoDevice> suc = successes.get(entry.getKey());
  75.             if (suc == null || suc.isEmpty()) {
  76.                 return true;
  77.             }
  78.         }
  79.         return false;
  80.     }
  81. }