DnsIq.java

  1. /**
  2.  *
  3.  * Copyright 2019 Florian Schmaus
  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.dox.element;

  18. import java.io.IOException;
  19. import java.nio.ByteBuffer;

  20. import org.jivesoftware.smack.packet.IQ;
  21. import org.jivesoftware.smack.util.stringencoder.Base64;

  22. import org.jxmpp.jid.Jid;
  23. import org.minidns.dnsmessage.DnsMessage;

  24. public class DnsIq extends IQ {

  25.     public static final String ELEMENT = "dns";
  26.     public static final String NAMESPACE = "urn:xmpp:dox:0";

  27.     private final DnsMessage dnsMessage;

  28.     private String base64DnsMessage;

  29.     public DnsIq(String base64DnsMessage) throws IOException {
  30.         this(Base64.decode(base64DnsMessage));
  31.         this.base64DnsMessage = base64DnsMessage;
  32.     }

  33.     public DnsIq(byte[] dnsMessage) throws IOException {
  34.         this(new DnsMessage(dnsMessage));
  35.     }

  36.     public DnsIq(DnsMessage dnsQuery, Jid to) {
  37.         this(dnsQuery);
  38.         setTo(to);
  39.         setType(Type.get);
  40.     }

  41.     public DnsIq(DnsMessage dnsMessage) {
  42.         super(ELEMENT, NAMESPACE);
  43.         this.dnsMessage = dnsMessage;
  44.     }

  45.     public DnsMessage getDnsMessage() {
  46.         return dnsMessage;
  47.     }

  48.     @SuppressWarnings("ByteBufferBackingArray")
  49.     public String getDnsMessageBase64Encoded() {
  50.         if (base64DnsMessage == null) {
  51.             ByteBuffer byteBuffer = dnsMessage.getInByteBuffer();
  52.             byte[] bytes = byteBuffer.array();
  53.             base64DnsMessage = Base64.encodeToStringWithoutPadding(bytes);
  54.         }
  55.         return base64DnsMessage;
  56.     }

  57.     @Override
  58.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  59.         xml.rightAngleBracket();

  60.         xml.escape(getDnsMessageBase64Encoded());

  61.         return xml;
  62.     }

  63. }