aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/filetransfer/http/upload/SlotPacketParser.java
blob: e2d629e4953da78ad824565e3fd8b44c0289831c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload;

import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.IqPacketParser;
import de.thedevstack.conversationsplus.xmpp.exceptions.MissingRequiredElementException;
import de.thedevstack.conversationsplus.xmpp.exceptions.UnexpectedIqPacketTypeException;
import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException;
import de.thedevstack.conversationsplus.xmpp.filetransfer.http.FileTransferHttp;
import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket;
import de.thedevstack.conversationsplus.xmpp.utils.ErrorIqPacketExceptionHelper;

/**
 *
 */
public final class SlotPacketParser extends IqPacketParser {
    private static final String SLOT_ELEMENT_NAME = "slot";

    public static HttpUploadSlot parseGetAndPutUrl(IqPacket packet) throws XmppException {
        HttpUploadSlot httpUploadSlot = null;
        if (packet.getType() == IqPacket.TYPE.RESULT) {
            Element slot = findChild(packet, SLOT_ELEMENT_NAME, FileTransferHttp.NAMESPACE);
            if (null == slot) {
                slot = findChild(packet, SLOT_ELEMENT_NAME, HttpUpload.NAMESPACE);
            }
            if (null == slot) {
                throw new MissingRequiredElementException(SLOT_ELEMENT_NAME, "neither " + FileTransferHttp.NAMESPACE + " nor " + HttpUpload.NAMESPACE, packet);
            }

            String getUrl = findRequiredChildContent(slot, "get");
            String putUrl = findRequiredChildContent(slot, "put");

            httpUploadSlot = new HttpUploadSlot(getUrl, putUrl);
        } else if (packet.getType() == IqPacket.TYPE.ERROR) {
            ErrorIqPacketExceptionHelper.throwIqErrorException(packet);
        } else {
            throw new UnexpectedIqPacketTypeException(packet, packet.getType(), IqPacket.TYPE.RESULT, IqPacket.TYPE.ERROR);
        }
        return httpUploadSlot;
    }
}