Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7f1f25a4e88a1a6282686606f56ef2e88d5d9f6c (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.ui.tasklist;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaClient;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaClientFactory;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaCorePlugin;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaStatus;
import org.eclipse.mylyn.internal.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylyn.internal.bugzilla.core.RepositoryConfiguration;
import org.eclipse.mylyn.internal.bugzilla.core.IBugzillaConstants.BugzillaServerVersion;
import org.eclipse.mylyn.monitor.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.RepositoryTemplate;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi;
import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * @author Mik Kersten
 * @author Rob Elves
 */
public class BugzillaRepositorySettingsPage extends AbstractRepositorySettingsPage {

	private static final String LABEL_SHORT_LOGINS = "Local users enabled:";

	public static final String LABEL_AUTOMATIC_VERSION = "Automatic (Use Validate Settings)";

	private static final String TITLE = "Bugzilla Repository Settings";

	private static final String DESCRIPTION = "Example: https://bugs.eclipse.org/bugs (do not include index.cgi)";

	protected Combo repositoryVersionCombo;

	private Button cleanQAContact;

	public BugzillaRepositorySettingsPage(AbstractRepositoryConnectorUi repositoryUi) {
		super(TITLE, DESCRIPTION, repositoryUi);
		setNeedsAnonymousLogin(true);
		setNeedsEncoding(true);
		setNeedsTimeZone(false);
		setNeedsHttpAuth(true);
	}

	@Override
	protected void createAdditionalControls(Composite parent) {

		for (RepositoryTemplate template : connector.getTemplates()) {
			serverUrlCombo.add(template.label);
		}
		serverUrlCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String text = serverUrlCombo.getText();
				RepositoryTemplate template = connector.getTemplate(text);
				if (template != null) {
					repositoryLabelEditor.setStringValue(template.label);
					setUrl(template.repositoryUrl);
					// setAnonymous(info.anonymous);
					setBugzillaVersion(template.version);
					if (template.characterEncoding != null) {
						setEncoding(template.characterEncoding);
					}
					getContainer().updateButtons();
					return;
				}
			}
		});

		Label repositoryVersionLabel = new Label(parent, SWT.NONE);
		repositoryVersionLabel.setText("Repository Version: ");
		repositoryVersionCombo = new Combo(parent, SWT.READ_ONLY);

		repositoryVersionCombo.add(LABEL_AUTOMATIC_VERSION);

		for (BugzillaServerVersion version : BugzillaServerVersion.values()) {
			repositoryVersionCombo.add(version.toString());
		}
		if (repository != null && repositoryVersionCombo.indexOf(repository.getVersion()) >= 0) {
			repositoryVersionCombo.select(repositoryVersionCombo.indexOf(repository.getVersion()));
		} else {
			int defaultIndex = repositoryVersionCombo.getItemCount() - 1;
			repositoryVersionCombo.select(defaultIndex);
			setVersion(repositoryVersionCombo.getItem(defaultIndex));
		}

		repositoryVersionCombo.addSelectionListener(new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				if (repositoryVersionCombo.getSelectionIndex() >= 0) {
					setVersion(repositoryVersionCombo.getItem(repositoryVersionCombo.getSelectionIndex()));
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				// ignore
			}
		});

		Label shortLoginLabel = new Label(parent, SWT.NONE);
		shortLoginLabel.setText(LABEL_SHORT_LOGINS);
		cleanQAContact = new Button(parent, SWT.CHECK | SWT.LEFT);
		if (repository != null) {
			boolean shortLogin = Boolean.parseBoolean(repository
					.getProperty(IBugzillaConstants.REPOSITORY_SETTING_SHORT_LOGIN));
			cleanQAContact.setSelection(shortLogin);
		}

	}

	public void setBugzillaVersion(String version) {
		if (version == null) {
			repositoryVersionCombo.select(0);
		} else {
			int i = repositoryVersionCombo.indexOf(version.toString());
			if (i != -1) {
				repositoryVersionCombo.select(i);
				setVersion(version);
			} else {
				for (IBugzillaConstants.BugzillaServerVersion serverVersion : IBugzillaConstants.BugzillaServerVersion
						.values()) {
					if (version.startsWith(serverVersion.toString())) {
						i = repositoryVersionCombo.indexOf(serverVersion.toString());
						if (i != -1) {
							repositoryVersionCombo.select(i);
							setVersion(serverVersion.toString());
							break;
						}
					}
				}
				if (i == -1) {
					StatusHandler.log("Could not resolve repository version: " + version, this);
					setVersion(IBugzillaConstants.BugzillaServerVersion.SERVER_218.toString());
				}
			}
		}
	}

	@Override
	public void updateProperties(TaskRepository repository) {
		repository.setProperty(IBugzillaConstants.REPOSITORY_SETTING_SHORT_LOGIN, String.valueOf(cleanQAContact
				.getSelection()));
	}

	@Override
	public boolean isPageComplete() {
		return super.isPageComplete();
	}

	@Override
	protected boolean isValidUrl(String name) {
		if (name.startsWith(URL_PREFIX_HTTPS) || name.startsWith(URL_PREFIX_HTTP)) {
			try {
				new URL(name);
				return true;
			} catch (MalformedURLException e) {
			}
		}
		return false;
	}

//	/* public for testing */
//	@Override
//	public void validateSettings() {
//
//		final String serverUrl = getServerUrl();
//		final String newUserId = getUserName();
//		final String newPassword = getPassword();
//		final boolean isAnonymous = isAnonymousAccess();
//		final String newEncoding = getCharacterEncoding();
//		final String httpAuthUser = getHttpAuthUserId();
//		final String httpAuthPass = getHttpAuthPassword();
//		final Proxy tempProxy;
//		try {
//			setMessage("Validating server settings...");
//			setErrorMessage(null);
//			if (getUseDefaultProxy()) {
//				tempProxy = TaskRepository.getSystemProxy();
//			} else {
//				tempProxy = WebClientUtil.getProxy(getProxyHostname(), getProxyPort(), getProxyUsername(),
//						getProxyPassword());
//			}
//			final boolean checkVersion = repositoryVersionCombo.getSelectionIndex() == 0;
//			final String[] version = new String[1];
//			getWizard().getContainer().run(true, false, new IRunnableWithProgress() {
//				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
//					if (monitor == null) {
//						monitor = new NullProgressMonitor();
//					}
//					try {
//						monitor.beginTask("Validating server settings", IProgressMonitor.UNKNOWN);
//						BugzillaClient client = null;
//						if (isAnonymous) {
//							client = BugzillaClientFactory.createClient(serverUrl, newUserId, newPassword,
//									httpAuthUser, httpAuthPass, tempProxy, newEncoding);
//							client.logout();
//						} else if (version != null) {
//							client = BugzillaClientFactory.createClient(serverUrl, newUserId, newPassword,
//									httpAuthUser, httpAuthPass, tempProxy, newEncoding);
//							client.validate();
//						}
//						if (checkVersion && client != null) {
//							RepositoryConfiguration config = client.getRepositoryConfiguration();
//							if (config != null) {
//								version[0] = config.getInstallVersion();
//							}
//						}
//
//					} catch (Exception ex) {
//						throw new InvocationTargetException(ex);
//
//					} finally {
//						monitor.done();
//					}
//				}
//			});
//
//			if (version[0] != null) {
//				setBugzillaVersion(version[0]);
//			}
//
//			if (!isAnonymous) {
//				setMessage("Valid Bugzilla server found and your login was accepted");
//			} else {
//				setMessage("Valid Bugzilla server found");
//			}
//		} catch (InvocationTargetException e) {
//			setMessage(null);
//			displayError(serverUrl, e.getTargetException());
//
//		} catch (InterruptedException e) {
//			setErrorMessage("Could not connect to Bugzilla server or authentication failed");
//		}
//	}


	@Override
	protected Validator getValidator(TaskRepository repository) {
		
		if(repositoryVersionCombo.getSelectionIndex() != 0) {
			return new BugzillaValidator(repository, repositoryVersionCombo.getItem(repositoryVersionCombo.getSelectionIndex()));
		} else {
			return new BugzillaValidator(repository, null);
		}
	}
	
//	public String getBugzillaVersion() {
//		if (repositoryVersionCombo.getSelectionIndex() == 0) {
//			return null;
//		} else {
//			return repositoryVersionCombo.getItem(repositoryVersionCombo.getSelectionIndex());
//		}
//	}

	@Override
	protected void applyValidatorResult(Validator validator) {
		super.applyValidatorResult(validator);

		if (((BugzillaValidator) validator).getResult() != null && ((BugzillaValidator) validator).getResult() != null) {
			setBugzillaVersion(((BugzillaValidator) validator).getResult());			
		}
	}

	public class BugzillaValidator extends Validator {

		final String serverUrl;

		final String newUserId;

		final String newPassword;

		final boolean isAnonymous;

		final String newEncoding;

		final String httpAuthUser;

		final String httpAuthPass;
		
		final Proxy proxy;

		private String[] versions = new String[1];;

		public BugzillaValidator(TaskRepository repository, String version) {
			serverUrl = getServerUrl();
			newUserId = getUserName();
			newPassword = getPassword();
			isAnonymous = isAnonymousAccess();
			newEncoding = getCharacterEncoding();
			httpAuthUser = getHttpAuthUserId();
			httpAuthPass = getHttpAuthPassword();
			proxy = repository.getProxy();
			versions[0] = version;
		}

		@Override
		public void run(IProgressMonitor monitor) throws CoreException {
			try {
				validate(monitor);
			} catch (Exception e) {
				displayError(serverUrl, e);
//				IStatus status;
//				if (e instanceof MalformedURLException) {
//					status = new MylarStatus(Status.WARNING, BugzillaCorePlugin.PLUGIN_ID,
//							IMylarStatusConstants.NETWORK_ERROR, "Server URL is invalid.");
//				} else if (e instanceof CoreException) {
//					status = ((CoreException) e).getStatus();
//				} else if (e instanceof IOException) {
//					status = new MylarStatus(Status.WARNING, BugzillaCorePlugin.PLUGIN_ID,
//							IMylarStatusConstants.IO_ERROR, serverUrl, e.getMessage());
//				} else {
//					status = new MylarStatus(Status.WARNING, BugzillaCorePlugin.PLUGIN_ID,
//							IMylarStatusConstants.NETWORK_ERROR, serverUrl, e.getMessage());
//				}
//				MylarStatusHandler.displayStatus("Validation failed", status);
			}
		}
		

		private void displayError(final String serverUrl, Throwable e) {
			IStatus status;
			if (e instanceof MalformedURLException) {
				status = new BugzillaStatus(Status.WARNING, BugzillaCorePlugin.PLUGIN_ID, RepositoryStatus.ERROR_NETWORK,
						"Server URL is invalid.");
			} else if (e instanceof CoreException) {
				status = ((CoreException) e).getStatus();
			} else if (e instanceof IOException) {
				status = new BugzillaStatus(Status.WARNING, BugzillaCorePlugin.PLUGIN_ID, RepositoryStatus.ERROR_IO,
						serverUrl, e.getMessage());
			} else {
				status = new BugzillaStatus(Status.WARNING, BugzillaCorePlugin.PLUGIN_ID, RepositoryStatus.ERROR_NETWORK,
						serverUrl, e.getMessage());
			}
			StatusHandler.displayStatus("Validation failed", status);
			setStatus(status);
		}

		public void validate(IProgressMonitor monitor) throws IOException, CoreException {

			if (monitor == null) {
				monitor = new NullProgressMonitor();
			}
			try {
				monitor.beginTask("Validating server settings", IProgressMonitor.UNKNOWN);
				BugzillaClient client = null;

//				Proxy tempProxy = Proxy.NO_PROXY;
//
//				if (getUseDefaultProxy()) {
//					tempProxy = TaskRepository.getSystemProxy();
//				} else {
//					tempProxy = WebClientUtil.getProxy(getProxyHostname(), getProxyPort(), getProxyUsername(),
//							getProxyPassword());
//				}
				boolean checkVersion = versions[0] == null;

				if (isAnonymous) {
					client = BugzillaClientFactory.createClient(serverUrl, newUserId, newPassword, httpAuthUser,
							httpAuthPass, proxy, newEncoding);
					client.logout();
				} else if (versions != null) {
					client = BugzillaClientFactory.createClient(serverUrl, newUserId, newPassword, httpAuthUser,
							httpAuthPass, proxy, newEncoding);
					client.validate();
				}
				if (checkVersion && client != null) {
					RepositoryConfiguration config = client.getRepositoryConfiguration();
					if (config != null) {
						versions[0] = config.getInstallVersion();
					}
				}

			} finally {
				monitor.done();
			}
		}

		public String getResult() {
			return versions[0];
		}

	}

}

Back to the top