001/** 002 * 003 * Copyright 2019-2024 Florian Schmaus 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.dox.element; 018 019import java.io.IOException; 020import java.nio.ByteBuffer; 021 022import org.jivesoftware.smack.packet.IQ; 023import org.jivesoftware.smack.util.stringencoder.Base64; 024 025import org.jxmpp.jid.Jid; 026import org.minidns.dnsmessage.DnsMessage; 027 028public class DnsIq extends IQ { 029 030 public static final String ELEMENT = "dns"; 031 public static final String NAMESPACE = "urn:xmpp:dox:0"; 032 033 private final DnsMessage dnsMessage; 034 035 private String base64DnsMessage; 036 037 public DnsIq(String base64DnsMessage) throws IOException { 038 this(Base64.decode(base64DnsMessage)); 039 this.base64DnsMessage = base64DnsMessage; 040 } 041 042 public DnsIq(byte[] dnsMessage) throws IOException { 043 this(new DnsMessage(dnsMessage)); 044 } 045 046 @SuppressWarnings("this-escape") 047 public DnsIq(DnsMessage dnsQuery, Jid to) { 048 this(dnsQuery); 049 setTo(to); 050 setType(Type.get); 051 } 052 053 public DnsIq(DnsMessage dnsMessage) { 054 super(ELEMENT, NAMESPACE); 055 this.dnsMessage = dnsMessage; 056 } 057 058 public DnsMessage getDnsMessage() { 059 return dnsMessage; 060 } 061 062 @SuppressWarnings("ByteBufferBackingArray") 063 public String getDnsMessageBase64Encoded() { 064 if (base64DnsMessage == null) { 065 ByteBuffer byteBuffer = dnsMessage.getInByteBuffer(); 066 byte[] bytes = byteBuffer.array(); 067 base64DnsMessage = Base64.encodeToStringWithoutPadding(bytes); 068 } 069 return base64DnsMessage; 070 } 071 072 @Override 073 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { 074 xml.rightAngleBracket(); 075 076 xml.escape(getDnsMessageBase64Encoded()); 077 078 return xml; 079 } 080 081}