001/**
002 *
003 * Copyright © 2017 Grigory Fedorov
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.httpfileupload.element;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.IQ;
021import org.jivesoftware.smack.packet.StanzaError;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024/**
025 * File Too Large error extension.
026 *
027 * @author Grigory Fedorov
028 * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
029 */
030public class FileTooLargeError implements ExtensionElement {
031    public static final String ELEMENT = "file-too-large";
032    public static final String NAMESPACE = SlotRequest.NAMESPACE;
033
034    private final long maxFileSize;
035    private final String namespace;
036
037    public FileTooLargeError(long maxFileSize) {
038        this(maxFileSize, NAMESPACE);
039    }
040
041    protected FileTooLargeError(long maxFileSize, String namespace) {
042        this.maxFileSize = maxFileSize;
043        this.namespace = namespace;
044    }
045
046    public long getMaxFileSize() {
047        return maxFileSize;
048    }
049
050    @Override
051    public String getElementName() {
052        return ELEMENT;
053    }
054
055    @Override
056    public String getNamespace() {
057        return namespace;
058    }
059
060    @Override
061    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
062        XmlStringBuilder xml = new XmlStringBuilder(this);
063        xml.rightAngleBracket();
064        xml.element("max-file-size", String.valueOf(maxFileSize));
065        xml.closeElement(this);
066        return xml;
067    }
068
069    public static FileTooLargeError from(IQ iq) {
070        StanzaError error = iq.getError();
071        if (error == null) {
072            return null;
073        }
074        return error.getExtension(ELEMENT, NAMESPACE);
075    }
076}