summaryrefslogtreecommitdiffstats
blob: ff56fb527a0e28d7e076ef2c7e02987ead49e148 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*  Copyright (c) 2016, Isode Limited, London, England.
 *  All rights reserved.
 *                                                                       
 *  Acquisition and use of this software and related materials for any      
 *  purpose requires a written license agreement from Isode Limited,
 *  or a written license from an organisation licensed by Isode Limited
 *  to grant such a license.
 *
 */
package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.elements.S5BProxyRequest;
import com.isode.stroke.elements.S5BProxyRequest.StreamHost;
import com.isode.stroke.jid.JID;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;

/**
 * S5BProxyRequestParaser
 */
public class S5BProxyRequestParser extends
        GenericPayloadParser<S5BProxyRequest> {

    private boolean parseActivate = false;
    
    private final StringBuilder activeJIDBuilder = new StringBuilder();
    
    /**
     * Constructor
     */
    public S5BProxyRequestParser() {
        super(new S5BProxyRequest());
    }

    @Override
    public void handleStartElement(String element, String ns,
            AttributeMap attributes) {
        if ("streamhost".equals(element)) {
            String hostValue = attributes.getAttributeValue("host");
            String jidValue = attributes.getAttributeValue("jid");
            String portValue = attributes.getAttributeValue("port");
            if (hostValue != null && jidValue != null && portValue != null) {
                int port = -1;
                try {
                    port = Integer.parseInt(portValue);
                }
                catch (NumberFormatException nfe) {
                    port = -1;
                }
                JID jid = new JID(jidValue);
                if (!hostValue.isEmpty() && port != -1 && jid.isValid()) {
                    StreamHost streamHost = new StreamHost();
                    streamHost.host = hostValue;
                    streamHost.port = port;
                    streamHost.jid = jid;
                    getPayloadInternal().setStreamHost(streamHost);
                }
            }
        }
        else if ("active".equals(element)) {
            parseActivate = true;
        }
        else if ("query".equals(element)) {
            String sidValue = attributes.getAttributeValue("sid");
            if (sidValue != null) {
                getPayloadInternal().setSID(sidValue);
            };
        }
        
    }

    @Override
    public void handleEndElement(String element, String ns) {
        if ("activate".equals(element)) {
            JID active = new JID(activeJIDBuilder.toString());
            if (active.isValid()) {
                getPayloadInternal().setActivate(active);
            }
            parseActivate = false;
        }
        
    }

    @Override
    public void handleCharacterData(String data) {
        if (parseActivate) {
            activeJIDBuilder.append(data);
        }
    }

}