001/**
002 *
003 * Copyright 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.nio.charset.Charset;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.util.Objects;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025/**
026 * This class represents a secretkey element which contains a users OpenPGP secret key.
027 *
028 * @see <a href="https://xmpp.org/extensions/xep-0373.html#client-receives-secret-response">
029 *     XEP-0373: ยง5.2.2 PEP Service Success Response</a>
030 */
031public class SecretkeyElement implements ExtensionElement {
032
033    public static final String NAMESPACE = OpenPgpElement.NAMESPACE;
034    public static final String ELEMENT = "secretkey";
035
036    private final byte[] b64Data;
037
038    public SecretkeyElement(byte[] b64Data) {
039        this.b64Data = Objects.requireNonNull(b64Data);
040    }
041
042    public byte[] getB64Data() {
043        return b64Data;
044    }
045
046    @Override
047    public String getNamespace() {
048        return NAMESPACE;
049    }
050
051    @Override
052    public String getElementName() {
053        return ELEMENT;
054    }
055
056    @Override
057    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
058        XmlStringBuilder xml = new XmlStringBuilder(this)
059                .rightAngleBracket()
060                .append(new String(b64Data, Charset.forName("UTF-8")))
061                .closeElement(this);
062        return xml;
063    }
064}