Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9e114f26ab10d5361ed565b01f3a714170b74117 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*******************************************************************************
 * Copyright (c) 2006 - 2006 Mylar eclipse.org project 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:
 *     Mylar project committers - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.internal.trac.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.security.auth.login.LoginException;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.mylar.context.core.MylarStatusHandler;
import org.eclipse.mylar.internal.tasks.core.HtmlStreamTokenizer;
import org.eclipse.mylar.internal.tasks.core.HtmlTag;
import org.eclipse.mylar.internal.tasks.core.WebClientUtil;
import org.eclipse.mylar.internal.tasks.core.HtmlStreamTokenizer.Token;
import org.eclipse.mylar.internal.trac.core.model.TracComponent;
import org.eclipse.mylar.internal.trac.core.model.TracMilestone;
import org.eclipse.mylar.internal.trac.core.model.TracPriority;
import org.eclipse.mylar.internal.trac.core.model.TracSearch;
import org.eclipse.mylar.internal.trac.core.model.TracSearchFilter;
import org.eclipse.mylar.internal.trac.core.model.TracSeverity;
import org.eclipse.mylar.internal.trac.core.model.TracTicket;
import org.eclipse.mylar.internal.trac.core.model.TracTicketResolution;
import org.eclipse.mylar.internal.trac.core.model.TracTicketStatus;
import org.eclipse.mylar.internal.trac.core.model.TracTicketType;
import org.eclipse.mylar.internal.trac.core.model.TracVersion;
import org.eclipse.mylar.internal.trac.core.model.TracSearchFilter.CompareOperator;
import org.eclipse.mylar.internal.trac.core.model.TracTicket.Key;
import org.eclipse.mylar.internal.trac.core.util.TracHttpClientTransportFactory.TracHttpException;

/**
 * Represents a Trac repository that is accessed through the Trac's query script
 * and web interface.
 * 
 * @author Steffen Pingel
 */
public class Trac09Client extends AbstractTracClient {

	private HttpClient httpClient = new HttpClient();

	private boolean authenticated;

	public Trac09Client(URL url, Version version, String username, String password) {
		super(url, version, username, password);
	}

	private GetMethod connect(String serverURL) throws TracException {
		try {
			return connectInternal(serverURL);
		} catch (TracException e) {
			throw e;
		} catch (Exception e) {
			throw new TracException(e);
		}
	}

	private GetMethod connectInternal(String serverURL) throws TracLoginException, IOException, TracHttpException {
		WebClientUtil.setupHttpClient(httpClient, proxy, serverURL);

		for (int attempt = 0; attempt < 2; attempt++) {
			// force authentication
			if (!authenticated && hasAuthenticationCredentials()) {
				authenticate();
			}

			GetMethod method = new GetMethod(WebClientUtil.getRequestPath(serverURL));
			method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
			int code;
			try {
				code = httpClient.executeMethod(method);
			} catch (IOException e) {
				method.releaseConnection();
				throw e;
			}

			if (code == HttpURLConnection.HTTP_OK) {
				return method;
			} else if (code == HttpURLConnection.HTTP_UNAUTHORIZED || code == HttpURLConnection.HTTP_FORBIDDEN) {
				// login or reauthenticate due to an expired session
				method.releaseConnection();
				authenticated = false;
				authenticate();
			} else {
				throw new TracHttpException(code);
			}
		}

		throw new TracLoginException();
	}

	private void authenticate() throws TracLoginException, IOException {
		if (!hasAuthenticationCredentials()) {
			throw new TracLoginException();
		}

		Credentials credentials = new UsernamePasswordCredentials(username, password);
		httpClient.getState().setCredentials(
				new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), credentials);

		GetMethod method = new GetMethod(WebClientUtil.getRequestPath(repositoryUrl + LOGIN_URL));
		method.setFollowRedirects(false);

		try {
			httpClient.getParams().setAuthenticationPreemptive(true);
			int code = httpClient.executeMethod(method);
			if (code == HttpURLConnection.HTTP_UNAUTHORIZED || code == HttpURLConnection.HTTP_FORBIDDEN) {
				throw new TracLoginException();
			}
		} finally {
			method.releaseConnection();
			httpClient.getParams().setAuthenticationPreemptive(false);
		}

		authenticated = true;
	}

	/**
	 * Fetches the web site of a single ticket and returns the Trac ticket.
	 * 
	 * @param id
	 *            Trac id of ticket
	 * @throws LoginException
	 */
	public TracTicket getTicket(int id) throws TracException {
		GetMethod method = connect(repositoryUrl + ITracClient.TICKET_URL + id);
		try {
			TracTicket ticket = new TracTicket(id);

			BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),
					ITracClient.CHARSET));
			HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(reader, null);
			for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {
				if (token.getType() == Token.TAG) {
					HtmlTag tag = (HtmlTag) token.getValue();
					if (tag.getTagType() == HtmlTag.Type.TD) {
						String headers = tag.getAttribute("headers");
						if ("h_component".equals(headers)) {
							ticket.putBuiltinValue(Key.COMPONENT, getText(tokenizer));
						} else if ("h_milestone".equals(headers)) {
							ticket.putBuiltinValue(Key.MILESTONE, getText(tokenizer));
						} else if ("h_priority".equals(headers)) {
							ticket.putBuiltinValue(Key.PRIORITY, getText(tokenizer));
						} else if ("h_severity".equals(headers)) {
							ticket.putBuiltinValue(Key.SEVERITY, getText(tokenizer));
						} else if ("h_version".equals(headers)) {
							ticket.putBuiltinValue(Key.VERSION, getText(tokenizer));
						} else if ("h_keywords".equals(headers)) {
							ticket.putBuiltinValue(Key.KEYWORDS, getText(tokenizer));
						} else if ("h_cc".equals(headers)) {
							ticket.putBuiltinValue(Key.CC, getText(tokenizer));
						} else if ("h_owner".equals(headers)) {
							ticket.putBuiltinValue(Key.OWNER, getText(tokenizer));
						} else if ("h_reporter".equals(headers)) {
							ticket.putBuiltinValue(Key.REPORTER, getText(tokenizer));
						}
						// TODO handle custom fields
					} else if (tag.getTagType() == HtmlTag.Type.H2 && "summary".equals(tag.getAttribute("class"))) {
						ticket.putBuiltinValue(Key.SUMMARY, getText(tokenizer));
					} else if (tag.getTagType() == HtmlTag.Type.H3 && "status".equals(tag.getAttribute("class"))) {
						String text = getStrongText(tokenizer);
						if (text.length() > 0) {
							int i = text.indexOf(" (");
							if (i != -1) {
								// status contains resolution as well
								ticket.putBuiltinValue(Key.STATUS, text.substring(0, i));
								ticket.putBuiltinValue(Key.RESOLUTION, text.substring(i, text.length() - 1));
							} else {
								ticket.putBuiltinValue(Key.STATUS, text);
							}
						}
					}
					// TODO parse description
				}
			}

			if (ticket.isValid() && ticket.getValue(Key.SUMMARY) != null) {
				return ticket;
			}

			throw new InvalidTicketException();
		} catch (IOException e) {
			throw new TracException(e);
		} catch (ParseException e) {
			throw new TracException(e);
		} finally {
			method.releaseConnection();
		}
	}

	public void search(TracSearch query, List<TracTicket> tickets) throws TracException {
		GetMethod method = connect(repositoryUrl + ITracClient.QUERY_URL + query.toUrl());
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),
					ITracClient.CHARSET));
			String line;

			Map<String, String> constantValues = getExactMatchValues(query);

			// first line contains names of returned ticket fields
			line = reader.readLine();
			if (line == null) {
				throw new InvalidTicketException();
			}
			StringTokenizer t = new StringTokenizer(line, "\t");
			Key[] fields = new Key[t.countTokens()];
			for (int i = 0; i < fields.length; i++) {
				fields[i] = Key.fromKey(t.nextToken());
			}

			// create a ticket for each following line of output
			while ((line = reader.readLine()) != null) {
				t = new StringTokenizer(line, "\t");
				TracTicket ticket = new TracTicket();
				for (int i = 0; i < fields.length && t.hasMoreTokens(); i++) {
					if (fields[i] != null) {
						try {
							if (fields[i] == Key.ID) {
								ticket.setId(Integer.parseInt(t.nextToken()));
							} else if (fields[i] == Key.TIME) {
								ticket.setCreated(Integer.parseInt(t.nextToken()));
							} else if (fields[i] == Key.CHANGE_TIME) {
								ticket.setLastChanged(Integer.parseInt(t.nextToken()));
							} else {
								ticket.putBuiltinValue(fields[i], parseTicketValue(t.nextToken()));
							}
						} catch (NumberFormatException e) {
							MylarStatusHandler.log(e, "Error parsing repsonse: " + line);
						}
					}
				}

				if (ticket.isValid()) {
					for (String key : constantValues.keySet()) {
						ticket.putValue(key, parseTicketValue(constantValues.get(key)));
					}

					tickets.add(ticket);
				}
			}
		} catch (IOException e) {
			throw new TracException(e);
		} finally {
			method.releaseConnection();
		}
	}

	/**
	 * Trac has sepcial encoding rules for the returned output: None is
	 * represented by "--".
	 */
	private String parseTicketValue(String value) {
		if ("--".equals(value)) {
			return "";
		}
		return value;
	}

	/**
	 * Extracts constant values from <code>query</code>. The Trac query
	 * script does not return fields that matched exactly againt a single value.
	 */
	private Map<String, String> getExactMatchValues(TracSearch query) {
		Map<String, String> values = new HashMap<String, String>();
		List<TracSearchFilter> filters = query.getFilters();
		for (TracSearchFilter filter : filters) {
			if (filter.getOperator() == CompareOperator.IS && filter.getValues().size() == 1) {
				values.put(filter.getFieldName(), filter.getValues().get(0));
			}
		}
		return values;
	}

	public void validate() throws TracException {
		GetMethod method = connect(repositoryUrl + "/");
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),
					ITracClient.CHARSET));
			HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(reader, null);
			for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {
				if (token.getType() == Token.TAG) {
					HtmlTag tag = (HtmlTag) token.getValue();
					if (tag.getTagType() == HtmlTag.Type.A) {
						String id = tag.getAttribute("id");
						if ("tracpowered".equals(id)) {
							return;
						}
					}
				}
			}

			throw new TracException("Not a valid Trac repository");
		} catch (IOException e) {
			throw new TracException(e);
		} catch (ParseException e) {
			throw new TracException(e);
		} finally {
			method.releaseConnection();
		}
	}

	public void updateAttributes(IProgressMonitor monitor) throws TracException {
		monitor.beginTask("Updating attributes", IProgressMonitor.UNKNOWN);

		GetMethod method = connect(repositoryUrl + ITracClient.NEW_TICKET_URL);
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),
					ITracClient.CHARSET));
			HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(reader, null);
			for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {
				if (monitor.isCanceled()) {
					throw new OperationCanceledException();
				}

				if (token.getType() == Token.TAG) {
					HtmlTag tag = (HtmlTag) token.getValue();
					if (tag.getTagType() == HtmlTag.Type.SELECT) {
						String name = tag.getAttribute("id");
						if ("component".equals(name)) {
							List<String> values = getOptionValues(tokenizer);
							data.components = new ArrayList<TracComponent>(values.size());
							for (String value : values) {
								data.components.add(new TracComponent(value));
							}
						} else if ("milestone".equals(name)) {
							List<String> values = getOptionValues(tokenizer);
							data.milestones = new ArrayList<TracMilestone>(values.size());
							for (String value : values) {
								data.milestones.add(new TracMilestone(value));
							}
						} else if ("priority".equals(name)) {
							List<String> values = getOptionValues(tokenizer);
							data.priorities = new ArrayList<TracPriority>(values.size());
							for (int i = 0; i < values.size(); i++) {
								data.priorities.add(new TracPriority(values.get(i), i + 1));
							}
						} else if ("severity".equals(name)) {
							List<String> values = getOptionValues(tokenizer);
							data.severities = new ArrayList<TracSeverity>(values.size());
							for (int i = 0; i < values.size(); i++) {
								data.severities.add(new TracSeverity(values.get(i), i + 1));
							}
						} else if ("type".equals(name)) {
							List<String> values = getOptionValues(tokenizer);
							data.ticketTypes = new ArrayList<TracTicketType>(values.size());
							for (int i = 0; i < values.size(); i++) {
								data.ticketTypes.add(new TracTicketType(values.get(i), i + 1));
							}
						} else if ("version".equals(name)) {
							List<String> values = getOptionValues(tokenizer);
							data.versions = new ArrayList<TracVersion>(values.size());
							for (String value : values) {
								data.versions.add(new TracVersion(value));
							}
						}
					}
				}
			}

			data.ticketResolutions = new ArrayList<TracTicketResolution>(5);
			data.ticketResolutions.add(new TracTicketResolution("fixed", 1));
			data.ticketResolutions.add(new TracTicketResolution("invalid", 2));
			data.ticketResolutions.add(new TracTicketResolution("wontfix", 3));
			data.ticketResolutions.add(new TracTicketResolution("duplicate", 4));
			data.ticketResolutions.add(new TracTicketResolution("worksforme", 5));

			data.ticketStatus = new ArrayList<TracTicketStatus>(4);
			data.ticketStatus.add(new TracTicketStatus("new", 1));
			data.ticketStatus.add(new TracTicketStatus("assigned", 2));
			data.ticketStatus.add(new TracTicketStatus("reopened", 3));
			data.ticketStatus.add(new TracTicketStatus("closed", 4));
		} catch (IOException e) {
			throw new TracException(e);
		} catch (ParseException e) {
			throw new TracException(e);
		} finally {
			method.releaseConnection();
		}
	}

	private List<String> getOptionValues(HtmlStreamTokenizer tokenizer) throws IOException, ParseException {
		List<String> values = new ArrayList<String>();
		for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {
			if (token.getType() == Token.TAG) {
				HtmlTag tag = (HtmlTag) token.getValue();
				if (tag.getTagType() == HtmlTag.Type.OPTION && !tag.isEndTag()) {
					String value = getText(tokenizer).trim();
					if (value.length() > 0) {
						values.add(value);
					}
				} else {
					return values;
				}
			}
		}
		return values;
	}

	private String getText(HtmlStreamTokenizer tokenizer) throws IOException, ParseException {
		StringBuffer sb = new StringBuffer();
		for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {
			if (token.getType() == Token.TEXT) {
				sb.append(token.toString());
			} else if (token.getType() == Token.COMMENT) {
				// ignore
			} else {
				break;
			}
		}
		return HtmlStreamTokenizer.unescape(sb).toString();
	}

	/**
	 * Looks for a <code>strong</code> tag and returns the text enclosed by
	 * the tag.
	 */
	private String getStrongText(HtmlStreamTokenizer tokenizer) throws IOException, ParseException {
		for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {
			if (token.getType() == Token.TAG && ((HtmlTag) token.getValue()).getTagType() == HtmlTag.Type.STRONG) {
				return getText(tokenizer);
			} else if (token.getType() == Token.COMMENT) {
				// ignore
			} else if (token.getType() == Token.TEXT) {
				// ignore
			} else {
				break;
			}
		}
		return "";
	}

	public byte[] getAttachmentData(int id, String filename) throws TracException {
		throw new TracException("Unsupported operation");
	}

	public void putAttachmentData(int id, String name, String description, byte[] data) throws TracException {
		throw new TracException("Unsupported operation");
	}

	public void createTicket(TracTicket ticket) throws TracException {
		throw new TracException("Unsupported operation");
	}

	public void updateTicket(TracTicket ticket, String comment) throws TracException {
		throw new TracException("Unsupported operation");
	}

	public Set<Integer> getChangedTickets(Date since) throws TracException {
		return null;
	}

}

Back to the top