FileTooLargeError.java

  1. /**
  2.  *
  3.  * Copyright © 2017 Grigory Fedorov
  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.httpfileupload.element;

  18. import org.jivesoftware.smack.packet.ExtensionElement;
  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.packet.StanzaError;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. /**
  23.  * File Too Large error extension.
  24.  *
  25.  * @author Grigory Fedorov
  26.  * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
  27.  */
  28. public class FileTooLargeError implements ExtensionElement {
  29.     public static final String ELEMENT = "file-too-large";
  30.     public static final String NAMESPACE = SlotRequest.NAMESPACE;

  31.     private final long maxFileSize;
  32.     private final String namespace;

  33.     public FileTooLargeError(long maxFileSize) {
  34.         this(maxFileSize, NAMESPACE);
  35.     }

  36.     protected FileTooLargeError(long maxFileSize, String namespace) {
  37.         this.maxFileSize = maxFileSize;
  38.         this.namespace = namespace;
  39.     }

  40.     public long getMaxFileSize() {
  41.         return maxFileSize;
  42.     }

  43.     @Override
  44.     public String getElementName() {
  45.         return ELEMENT;
  46.     }

  47.     @Override
  48.     public String getNamespace() {
  49.         return namespace;
  50.     }

  51.     @Override
  52.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  53.         XmlStringBuilder xml = new XmlStringBuilder(this);
  54.         xml.rightAngleBracket();
  55.         xml.element("max-file-size", String.valueOf(maxFileSize));
  56.         xml.closeElement(this);
  57.         return xml;
  58.     }

  59.     public static FileTooLargeError from(IQ iq) {
  60.         StanzaError error = iq.getError();
  61.         if (error == null) {
  62.             return null;
  63.         }
  64.         return error.getExtension(ELEMENT, NAMESPACE);
  65.     }
  66. }