Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ccb8d1c2633e5abdb990d6527c8eb685dc92259e (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
/****************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others.
 * 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.provider.xmpp.identity;

import java.net.URISyntaxException;
import java.util.Hashtable;
import java.util.Iterator;
import org.eclipse.ecf.core.identity.BaseID;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.util.StringUtils;
import org.eclipse.ecf.internal.provider.xmpp.Messages;
import org.eclipse.ecf.presence.IFQID;
import org.eclipse.ecf.presence.im.IChatID;

public class XMPPID extends BaseID implements IChatID, IFQID {

	private static final long serialVersionUID = 3257289140701049140L;
	public static final char USER_HOST_DELIMITER = '@';
	public static final char PORT_DELIMITER = ':';
	public static final String PATH_DELIMITER = "/";

	static class XMPPEscape {
		StringBuffer buf = new StringBuffer();

		public XMPPEscape(char[] chars) {
			if (chars != null) {
				for (int i = 0; i < chars.length; i++) {
					buf.append(chars[i]);
				}
			}
		}

		public String getAsString() {
			return buf.toString();
		}

	}

	protected static Hashtable escapeTable;

	static {
		escapeTable = new Hashtable(10);
		escapeTable.put("@", new XMPPEscape(new char[] { '\\', '4', '0' }));
		escapeTable.put("\"", new XMPPEscape(new char[] { '\\', '2', '2' }));
		escapeTable.put("&", new XMPPEscape(new char[] { '\\', '2', '6' }));
		escapeTable.put("'", new XMPPEscape(new char[] { '\\', '2', '7' }));
		escapeTable.put("/", new XMPPEscape(new char[] { '\\', '2', 'f' }));
		escapeTable.put(":", new XMPPEscape(new char[] { '\\', '3', 'a' }));
		escapeTable.put("<", new XMPPEscape(new char[] { '\\', '3', 'c' }));
		escapeTable.put(">", new XMPPEscape(new char[] { '\\', '3', 'e' }));
		escapeTable.put("\\", new XMPPEscape(new char[] { '\\', '5', 'c' }));
	}

	// implements JEP-0106 JID escaping:
	// http://www.xmpp.org/extensions/xep-0106.html
	static String fixEscapeInNode(String node) {
		if (node == null)
			return null;
		for (final Iterator i = escapeTable.keySet().iterator(); i.hasNext();) {
			final String key = (String) i.next();
			final XMPPEscape escape = (XMPPEscape) escapeTable.get(key);
			node = StringUtils.replaceAll(node, key, escape.getAsString());
		}
		return node;
	}

	static String fixPercentEscape(String src) {
		if (src == null)
			return null;
		return StringUtils.replaceAll(src, "%", "%25");
	}

	public static String unfixEscapeInNode(String node) {
		if (node == null)
			return null;
		for (final Iterator i = escapeTable.keySet().iterator(); i.hasNext();) {
			final String key = (String) i.next();
			final XMPPEscape escape = (XMPPEscape) escapeTable.get(key);
			node = StringUtils.replaceAll(node, escape.getAsString(), key);
		}
		return node;
	}

	protected String username;
	protected String hostname;
	protected String resourcename;
	protected int port = -1;

	public XMPPID(Namespace namespace, String unamehost)
			throws URISyntaxException {
		super(namespace);
		unamehost = fixPercentEscape(unamehost);
		if (unamehost == null)
			throw new URISyntaxException(unamehost,
					Messages.XMPPID_EXCEPTION_XMPPID_USERNAME_NOT_NULL);
		// Handle parsing of user@host/resource string
		int atIndex = unamehost.lastIndexOf(USER_HOST_DELIMITER);
		if (atIndex == -1)
			throw new URISyntaxException(unamehost,
					Messages.XMPPID_EXCEPTION_HOST_PORT_NOT_VALID);
		username = fixEscapeInNode(unamehost.substring(0, atIndex));
		final String remainder = unamehost.substring(atIndex + 1);
		// Handle parsing of host:port
		atIndex = remainder.lastIndexOf(PORT_DELIMITER);
		if (atIndex != -1) {
			try {
				final int slashLoc = remainder.indexOf(PATH_DELIMITER);
				if (slashLoc != -1)
					port = Integer.parseInt(remainder.substring(atIndex + 1,
							slashLoc));
				else
					port = Integer.parseInt(remainder.substring(atIndex + 1));
			} catch (final NumberFormatException e) {
				throw new URISyntaxException(unamehost,
						Messages.XMPPID_EXCEPTION_INVALID_PORT);
			}
			hostname = remainder.substring(0, atIndex);
		}
		atIndex = remainder.indexOf(PATH_DELIMITER);
		if (atIndex != -1) {
			if (hostname == null)
				hostname = remainder.substring(0, atIndex);
			setResourceName(remainder.substring(atIndex + 1));
		} else {
			setResourceName(null);
		}
		if (hostname == null)
			hostname = remainder;
	}

	protected int namespaceCompareTo(BaseID o) {
		return getName().compareTo(o.getName());
	}

	protected boolean namespaceEquals(BaseID o) {
		if (!(o instanceof XMPPID)) {
			return false;
		}
		final XMPPID other = (XMPPID) o;
		// Get resources from this and other
		String thisResourceName = getResourceName();
		String otherResourceName = other.getResourceName();
		// The resources are considered equal if either one is null (not known
		// yet), or they are equal by
		// string comparison
		boolean resourceEquals = false;
		if (thisResourceName == null) {
			resourceEquals = (otherResourceName == null) ? true : false;
		} else {
			resourceEquals = thisResourceName.equals(otherResourceName);
		}
		return resourceEquals
				&& getUsernameAtHost().equals(other.getUsernameAtHost());
	}

	protected String namespaceGetName() {
		return getUsernameAtHost();
	}

	protected int namespaceHashCode() {
		return getUsernameAtHost().hashCode();
	}

	public String getNodename() {
		return username;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.BaseID#namespaceToExternalForm()
	 */
	protected String namespaceToExternalForm() {
		return getNamespace().getScheme() + "://" + getFQName();
	}

	public String getUsername() {
		return unfixEscapeInNode(username);
	}

	public String getHostname() {
		return hostname;
	}

	public String getResourceName() {
		return resourcename;
	}

	public void setResourceName(String resourceName) {
		this.resourcename = resourceName;
	}

	public int getPort() {
		return port;
	}

	public String getUsernameAtHost() {
		String username = getUsername();
		String hostname = getHostname();
		int semiColonIdx = hostname.indexOf(';');
		if (semiColonIdx != -1) {
			hostname = hostname.substring(0, semiColonIdx);
		}
		return username + USER_HOST_DELIMITER + hostname
				+ ((getPort() == -1) ? "" : ":" + getPort());
	}

	public String getFQName() {
		String rn = getResourceName();
		return getUsernameAtHost() + PATH_DELIMITER + ((rn == null) ? "" : rn);
	}

	public String toString() {
		final StringBuffer sb = new StringBuffer("XMPPID["); //$NON-NLS-1$
		sb.append(toExternalForm()).append("]");
		return sb.toString();
	}

	public Object getAdapter(Class clazz) {
		if (clazz.isInstance(this)) {
			return this;
		} else
			return super.getAdapter(clazz);
	}
}

Back to the top