Skip to main content
summaryrefslogtreecommitdiffstats
blob: 530223591f0705a463783274b7cfc327e2bf0cf0 (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*******************************************************************************
 * Copyright (c) 2008 Marcelo Mayworm. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 	Marcelo Mayworm - initial API and implementation
 *
 ******************************************************************************/
package org.eclipse.ecf.internal.provider.xmpp.search;

import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.user.IUser;
import org.eclipse.ecf.core.user.User;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.internal.provider.xmpp.Messages;
import org.eclipse.ecf.internal.provider.xmpp.XmppPlugin;
import org.eclipse.ecf.internal.provider.xmpp.smack.ECFConnection;
import org.eclipse.ecf.presence.search.AbstractUserSearchManager;
import org.eclipse.ecf.presence.search.ICriteria;
import org.eclipse.ecf.presence.search.ICriterion;
import org.eclipse.ecf.presence.search.IRestriction;
import org.eclipse.ecf.presence.search.IResultList;
import org.eclipse.ecf.presence.search.ISearch;
import org.eclipse.ecf.presence.search.IUserSearchEvent;
import org.eclipse.ecf.presence.search.IUserSearchListener;
import org.eclipse.ecf.presence.search.IUserSearchManager;
import org.eclipse.ecf.presence.search.ResultList;
import org.eclipse.ecf.presence.search.UserSearchCompleteEvent;
import org.eclipse.ecf.presence.search.UserSearchException;
import org.eclipse.ecf.provider.xmpp.identity.XMPPID;
import org.eclipse.osgi.util.NLS;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.ReportedData.Row;
import org.jivesoftware.smackx.search.UserSearch;

/**
 * A specific implementation for XMPP provider. XEP-0055:
 * http://www.xmpp.org/extensions/xep-0055.html
 * 
 * @see IUserSearchManager
 * @since 3.0
 */
public class XMPPUserSearchManager extends AbstractUserSearchManager {

	/** Search service name on XMPP server */
	protected static final String SERVICE_SEARCH = "search."; //$NON-NLS-1$

	/** Wrapper for XMPP connection */
	protected ECFConnection ecfConnection;

	protected Namespace connectNamespace;

	protected ID connectedID;

	protected Form form;

	protected UserSearch manager;

	protected boolean enabled;

	protected ICriteria criteria;

	protected static final String FORM_TYPE = "FORM_TYPE"; //$NON-NLS-1$

	protected static final String NAME = "name"; //$NON-NLS-1$

	protected static final String JID = "jid"; //$NON-NLS-1$

	protected static final String SEARCH_ACTION = "search"; //$NON-NLS-1$

	public XMPPUserSearchManager() {
		manager = new UserSearch();
	}

	/**
	 * Create a specific {@link ICriteria} for XMPP
	 */
	public ICriteria createCriteria() {
		return new XMPPCriteria();
	}

	/**
	 * Create a specific {@link IRestriction} for XMPP
	 */
	public IRestriction createRestriction() {
		return new XMPPSelection();
	}

	/**
	 * Specific implementation for XMPP
	 * 
	 * @see IUserSearchManager#search(ICriteria).
	 */
	public ISearch search(ICriteria criteria) throws UserSearchException {

		ResultList resultList = new ResultList();
		try {
			// initialize the form by chance it is null
			if (form == null)
				form = manager.getSearchForm(ecfConnection.getXMPPConnection(),
						SERVICE_SEARCH
								+ ecfConnection.getXMPPConnection()
										.getServiceName());

			/*
			 * For XMPP criterion is considered. The XMPP server will deal with
			 * the search.
			 */
			List criterions = criteria.getCriterions();
			// add the fields for the search dynamically
			// consider just the fields used on the search
			// fields checked by user

			String fields[] = getUserPropertiesFields();
			for (int i = 0; i < fields.length; i++) {
				Iterator criterionsIterator = criterions.iterator();
				// for each user properties field check if it
				// was added by user for the criteria
				// for each field, a search is performed, and
				// the partial result is added to the result list
				while (criterionsIterator.hasNext()) {
					ICriterion criterion = (ICriterion) criterionsIterator
							.next();
					if (criterion.equals(fields[i])) {
						Form answerForm = form.createAnswerForm();
						answerForm.setAnswer(fields[i], true);
						answerForm.setAnswer(SEARCH_ACTION, criterion
								.toExpression());
						ReportedData data = manager.sendSearchForm(
								ecfConnection.getXMPPConnection(), answerForm,
								SERVICE_SEARCH
										+ ecfConnection.getXMPPConnection()
												.getServiceName());
						// create a result list from ReportedData
						IResultList partialResultList = createResultList(data);
						resultList.addAll(partialResultList.geResults());
					}
				}
			}

			return new XMPPSearch(resultList);

		} catch (final XMPPException e) {
			String message = null;
			if (e.getXMPPError() != null && e.getXMPPError().getCode() == 404) {
				message = Messages.XMPPContainer_UNRECOGONIZED_SEARCH_SERVICE;
			} else {
				message = e.getLocalizedMessage();
			}
			throw new UserSearchException(message, e, criteria);
		} catch (ECFException e) {
			throw new UserSearchException(e, criteria);
		}

	}

	/**
	 * Create a result list from ReportedData. Identify dynamically columns and
	 * rows and create users adding it to a {@link IResultList}
	 * 
	 * @param data
	 *            ReportedData
	 * @return {@link IResultList} a list of users
	 * @throws
	 */
	protected IResultList createResultList(ReportedData data) {
		ResultList result = new ResultList();
		Iterator rows = data.getRows();
		while (rows.hasNext()) {
			Row row = (Row) rows.next();
			Iterator jids = row.getValues(JID);
			Iterator names = row.getValues(NAME);
			String jid = null;
			String name = null;
			// XMPP server returns the same length for both
			while (jids.hasNext() && names.hasNext()) {
				try {
					jid = (String) jids.next();
					name = (String) names.next();
					IUser user = new User(new XMPPID(connectNamespace, jid),
							name);
					result.add(new XMPPResultItem(user));
				} catch (URISyntaxException e) {
					throw new RuntimeException(
							"cannot create connect id for client " + jid //$NON-NLS-1$
									+ " , name = " + name, e); //$NON-NLS-1$
				}
			}
		}
		return result;
	}

	/**
	 * Specific implementation for XMPP
	 * 
	 * @see IUserSearchManager#search(ICriteria, IUserSearchListener)
	 */
	public void search(final ICriteria criteria, final IUserSearchListener listener) {
		Assert.isNotNull(criteria);
		Assert.isNotNull(listener);
		Job job = new Job(Messages.XMPPUserSearchManager_JOB) {
			protected IStatus run(IProgressMonitor monitor) {
				try {
					ISearch search = search(criteria);
					UserSearchCompleteEvent complete = new UserSearchCompleteEvent(
							search);
					listener.handleUserSearchEvent(complete);
				} catch (UserSearchException e) {
					log(e.getLocalizedMessage(), e);
					return Status.CANCEL_STATUS;
				}
				return Status.OK_STATUS;
			}
		};
		job.schedule();
	}

	/**
	 * These parameters must be not null
	 * 
	 * @param connectNamespace
	 * @param connectedID
	 * @param connection
	 */
	public void setConnection(Namespace connectNamespace, ID connectedID,
			ECFConnection connection) {
		Assert.isNotNull(connectNamespace);
		Assert.isNotNull(connectedID);
		Assert.isNotNull(connection);
		this.connectNamespace = connectNamespace;
		this.connectedID = connectedID;
		this.ecfConnection = connection;
	}

	/**
	 * Returns the user properties fields available on the XMPP server
	 * 
	 * @param form
	 * @return String[] fields for form
	 * @throws ECFException
	 */
	public String[] getUserPropertiesFields() throws ECFException {
		try {
			if (form == null)
				form = manager.getSearchForm(ecfConnection.getXMPPConnection(),
						SERVICE_SEARCH
								+ ecfConnection.getXMPPConnection()
										.getServiceName());

			Set fields = new HashSet();
			Iterator userProperties = form.getFields();
			while (userProperties.hasNext()) {
				FormField field = (FormField) userProperties.next();
				String variable = field.getVariable();
				// ignore these fields
				if (!variable.equalsIgnoreCase(FORM_TYPE)
						&& !variable.equalsIgnoreCase(SEARCH_ACTION))
					fields.add(variable);
			}
			return (String[]) fields.toArray(new String[0]);
		} catch (final XMPPException e) {
			String message = null;
			if (e.getXMPPError() != null && e.getXMPPError().getCode() == 404) {
				message = Messages.XMPPContainer_UNRECOGONIZED_SEARCH_SERVICE;
			} else {
				message = e.getLocalizedMessage();
			}
			throw new ECFException(message, e);
		}

	}

	/**
	 * Notify that user search for XMPP is enabled
	 */
	public boolean isEnabled() {
		return this.enabled;
	}

	/**
	 * For gtalk it should be not enabled
	 * 
	 * @param enabled
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	/**
	 * @see AbstractUserSearchManager#createUserSearchListener
	 */
	public IUserSearchListener createUserSearchListener() {
		return new UserListener();

	}

	// utility methods
	protected void log(String msg, Throwable e) {
		XmppPlugin.log(msg, e);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ecf.presence.search.AbstractUserSearchManager#fireUserSearchEvent
	 * (org.eclipse.ecf.presence.search.IUserSearchEvent)
	 */
	public void fireUserSearchEvent(IUserSearchEvent event) {
		super.fireUserSearchEvent(event);
	}

	/**
	 * Returns a default implementation for IUserSearchListner
	 * 
	 * @return IUserSearchListener
	 */

	class UserListener implements IUserSearchListener {

		public void handleUserSearchEvent(IUserSearchEvent event) {
			try {

				ISearch searchResult = search(criteria);
				fireUserSearchEvent(new UserSearchCompleteEvent(searchResult));

			} catch (UserSearchException e) {
				log(NLS.bind(
						Messages.XMPPContainer_EXCEPTION_HANDLING_ASYCH_EVENT,
						e), e);
			}

		}

	}

}

Back to the top