001/**
002 *
003 * Copyright 2017 Florian Schmaus, 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.element;
018
019import java.io.ByteArrayInputStream;
020import java.io.InputStream;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.packet.Stanza;
024import org.jivesoftware.smack.util.StringUtils;
025import org.jivesoftware.smack.util.XmlStringBuilder;
026import org.jivesoftware.smackx.ox.util.Util;
027
028/**
029 * Class that represents an OpenPGP message.
030 * The content of this elements text is an base64 encoded , OpenPGP encrypted/signed content element ({@link SignElement},
031 * {@link SigncryptElement}, {@link CryptElement}).
032 *
033 * @see <a href="https://xmpp.org/extensions/xep-0373.html#exchange">
034 *     XEP-0373: ยง3.1 Exchanging OpenPGP Encrypted and Signed Data</a>
035 */
036public class OpenPgpElement implements ExtensionElement {
037
038    public static final String ELEMENT = "openpgp";
039    public static final String NAMESPACE = "urn:xmpp:openpgp:0";
040
041    // Represents the OpenPGP message, but encoded using base64.
042    private final String base64EncodedOpenPgpMessage;
043
044    public OpenPgpElement(String base64EncodedOpenPgpMessage) {
045        this.base64EncodedOpenPgpMessage = StringUtils.requireNotNullNorEmpty(base64EncodedOpenPgpMessage,
046                "base64 encoded message MUST NOT be null nor empty.");
047    }
048
049    public InputStream toInputStream() {
050        return new ByteArrayInputStream(base64EncodedOpenPgpMessage.getBytes(Util.UTF8));
051    }
052
053    /**
054     * Return the OpenPGP encrypted payload.
055     *
056     * @return OpenPGP encrypted payload.
057     */
058    public String getEncryptedBase64MessageContent() {
059        return base64EncodedOpenPgpMessage;
060    }
061
062    @Override
063    public String getElementName() {
064        return ELEMENT;
065    }
066
067    @Override
068    public String getNamespace() {
069        return NAMESPACE;
070    }
071
072    @Override
073    public XmlStringBuilder toXML(String enclosingNamespace) {
074        XmlStringBuilder xml = new XmlStringBuilder(this);
075        xml.rightAngleBracket().append(base64EncodedOpenPgpMessage).closeElement(this);
076        return xml;
077    }
078
079    public static OpenPgpElement fromStanza(Stanza stanza) {
080        return stanza.getExtension(ELEMENT, NAMESPACE);
081    }
082}