OmemoElement.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.element;

  18. import org.jivesoftware.smack.packet.ExtensionElement;
  19. import org.jivesoftware.smack.util.Objects;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;
  21. import org.jivesoftware.smack.util.stringencoder.Base64;

  22. /**
  23.  * Class that represents an OmemoElement.
  24.  *
  25.  * @author Paul Schaub
  26.  */
  27. public abstract class OmemoElement implements ExtensionElement {

  28.     public static final int TYPE_OMEMO_PREKEY_MESSAGE = 1;
  29.     public static final int TYPE_OMEMO_MESSAGE = 0;

  30.     public static final String NAME_ENCRYPTED = "encrypted";
  31.     public static final String ATTR_PAYLOAD = "payload";

  32.     private final OmemoHeaderElement header;
  33.     private final byte[] payload;

  34.     /**
  35.      * Create a new OmemoMessageElement from a header and a payload.
  36.      *
  37.      * @param header  header of the message
  38.      * @param payload payload
  39.      */
  40.     public OmemoElement(OmemoHeaderElement header, byte[] payload) {
  41.         this.header = Objects.requireNonNull(header);
  42.         this.payload = payload;
  43.     }

  44.     public OmemoHeaderElement getHeader() {
  45.         return header;
  46.     }

  47.     /**
  48.      * Return the payload of the message.
  49.      *
  50.      * @return encrypted payload of the message.
  51.      */
  52.     public byte[] getPayload() {
  53.         if (payload == null) {
  54.             return null;
  55.         }
  56.         return payload.clone();
  57.     }

  58.     public boolean isKeyTransportElement() {
  59.         return payload == null;
  60.     }

  61.     public boolean isMessageElement() {
  62.         return payload != null;
  63.     }

  64.     @Override
  65.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  66.         XmlStringBuilder sb = new XmlStringBuilder(this, enclosingNamespace).rightAngleBracket();

  67.         sb.append(header);

  68.         if (payload != null) {
  69.             sb.openElement(ATTR_PAYLOAD).append(Base64.encodeToString(payload)).closeElement(ATTR_PAYLOAD);
  70.         }

  71.         sb.closeElement(this);
  72.         return sb;
  73.     }

  74.     @Override
  75.     public String getElementName() {
  76.         return NAME_ENCRYPTED;
  77.     }
  78. }