1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.alert.agent;
21 import org.jabber.webb.*;
22 import org.jabber.webb.packet.*;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.backsource.alert.PropertyConstants;
28 import org.backsource.alert.AlertError;
29 /***
30 * Check that is possible to log into jabber.
31 *
32 * <p>.It uses thre properties {@link org.backsource.alert.PropertyConstants#JABBER_SERVER},{@link org.backsource.alert.PropertyConstants#JABBER_USER} and {@link org.backsource.alert.PropertyConstants#JABBER_PWD}.</p>
33 *
34 *
35 *
36 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
37 * @version $Revision: 1.1.1.1 $
38 */
39
40 public class JabberAliveTest extends BaseCase{
41 static final Log log = LogFactory.getLog(JabberAliveTest.class);
42 String server;
43 String user;
44 String pwd;
45 String resource ="JabberAliveTest";
46 boolean error = false;
47 int prio = 6;
48
49
50 public JabberAliveTest (String name){
51 super(name);
52 }
53 protected void setUp() throws Exception{
54 super.setUp();
55 server = prop.getProperty(PropertyConstants.JABBER_SERVER);
56 user =prop.getProperty(PropertyConstants.JABBER_USER);
57 pwd = prop.getProperty(PropertyConstants.JABBER_PWD);
58
59 }
60
61 /***
62 * Agent/test method.
63 *
64 * <p>Check if its possible to log into the jabber server given in {@link org.backsource.alert.PropertyConstants#JABBER_SERVER}.</p>
65 */
66 public void testLogin() {
67 JabberConnection con = null;
68 try {
69
70 con = new JabberConnection();
71 con.addListener( new JabberEventHandler(con) );
72 con.connect(server);
73
74
75 AuthenticationPacket auth = new AuthenticationPacket(user, pwd, resource);
76 try {
77 log.debug( "Loggin in with auth packet: " + auth.formatAsXML() );
78 } catch(Exception ex) {
79
80 }
81 con.sendPacket(auth);
82
83
84 PresencePacket pre = new PresencePacket(null, null, null);
85 try {
86 log.debug( "Sending presence: " + pre.formatAsXML() );
87 } catch(Exception ex) {
88
89 }
90
91 con.sendPacket(pre);
92
93
94 Thread.sleep(4000);
95
96
97 if ( error) {
98 throw new AlertError("Error in contacting jabber server " + server + ": ");
99 }
100
101
102
103 }catch (Exception e) {
104 throw new AlertError("Error in contacting jabber server " + server + ": " + e);
105 }
106 finally {
107 try {
108
109
110 if ( con != null) {
111 con.disconnect();
112 }
113
114 } catch (Throwable e) {
115
116 }
117
118 }
119
120
121
122 }
123
124
125 public static void main(String[] args){
126 try {
127 System.out.println("Staring jabber");
128
129 JabberAliveTest t = new JabberAliveTest("main");
130 t.server ="linutv3.tim.se";
131 t.user = "mf";
132 t.pwd = "mypass";
133 t.testLogin();
134 } catch (Exception e) {
135 e.printStackTrace();
136 }
137
138 }
139
140 public class JabberEventHandler extends JabberConnectionEventAdapter {
141 private JabberConnection con = null;
142 private JabberID[] presence;
143 private String name;
144
145 public JabberEventHandler(JabberConnection con) {
146 this.con = con;
147 }
148
149 public JabberEventHandler(JabberConnection con, JabberID[] presence) {
150 this.con = con;
151 this.presence = presence;
152 }
153 public JabberEventHandler(JabberConnection con, String name) {
154 this.con = con;
155 this.name = name;
156 }
157
158
159
160
161
162 public void connected(JabberConnectionEvent evt){
163 log.debug( "Connected");
164 }
165 public void connectionError(ConnectionErrorEvent cee){
166 log.error( "Connect ERROR " + cee.getMessage());
167 error = true;
168
169 }
170 public void disconnected(JabberConnectionEvent evt) {
171 log.debug( "Disconnected");
172
173 }
174 public void endOfData(JabberConnectionEvent evt) {
175
176 }
177 public void newPacket(PacketEvent pe) {
178 try {
179 log.debug( "Packet: " + pe.getPacket().formatAsXML());
180 Packet pkt = pe.getPacket();
181 if( pkt instanceof AuthenticationPacket) {
182 AuthenticationPacket msg = (AuthenticationPacket)pkt;
183
184 if (msg.isFailure() ) {
185 log.debug("AUTH ERROR");
186 error = true;
187 }
188
189
190 }
191 } catch(Exception ex) {
192
193 }
194 }
195
196 }
197 }