1 /*
2  * Jicofo, the Jitsi Conference Focus.
3  *
4  * Copyright @ 2015 Atlassian Pty Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package mock.xmpp;
19 
20 import org.jitsi.xmpp.extensions.jingle.*;
21 import org.jitsi.protocol.xmpp.*;
22 import org.jivesoftware.smack.iqrequest.*;
23 import org.jivesoftware.smack.packet.*;
24 import org.jxmpp.jid.*;
25 import org.jxmpp.jid.impl.*;
26 import org.jxmpp.stringprep.*;
27 
28 import java.util.*;
29 
30 public class XmppPeer
31     implements IQRequestHandler
32 {
33     private final XmppConnection connection;
34 
35     private final List<IQ> iqs = new ArrayList<>();
36 
XmppPeer(String jid)37     public XmppPeer(String jid)
38     {
39         this(jidCreate(jid), new MockXmppConnection(jidCreate(jid)));
40     }
41 
jidCreate(String jid)42     private static Jid jidCreate(String jid)
43     {
44         try
45         {
46             return JidCreate.from(jid);
47         }
48         catch (XmppStringprepException e)
49         {
50             throw new RuntimeException(e);
51         }
52     }
53 
XmppPeer(Jid jid, XmppConnection connection)54     public XmppPeer(Jid jid, XmppConnection connection)
55     {
56         this.connection = connection;
57     }
58 
getConnection()59     public XmppConnection getConnection()
60     {
61         return connection;
62     }
63 
start()64     public void start()
65     {
66         this.connection.registerIQRequestHandler(this);
67     }
68 
stop()69     public void stop()
70     {
71         this.connection.unregisterIQRequestHandler(this);
72     }
73 
getIqCount()74     public int getIqCount()
75     {
76         synchronized (iqs)
77         {
78             return iqs.size();
79         }
80     }
81 
getIq(int idx)82     public IQ getIq(int idx)
83     {
84         synchronized (iqs)
85         {
86             return iqs.get(idx);
87         }
88     }
89 
90     @Override
handleIQRequest(IQ iqRequest)91     public IQ handleIQRequest(IQ iqRequest)
92     {
93         synchronized (iqs)
94         {
95             iqs.add(iqRequest);
96         }
97 
98         return IQ.createErrorResponse(
99                 iqRequest,
100                 XMPPError.Condition.feature_not_implemented);
101     }
102 
103     @Override
getMode()104     public Mode getMode()
105     {
106         return Mode.sync;
107     }
108 
109     @Override
getType()110     public IQ.Type getType()
111     {
112         return IQ.Type.get;
113     }
114 
115     @Override
getElement()116     public String getElement()
117     {
118         return JingleIQ.ELEMENT_NAME;
119     }
120 
121     @Override
getNamespace()122     public String getNamespace()
123     {
124         return JingleIQ.NAMESPACE;
125     }
126 }
127