Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3e68d653b5e7c93b43ac6e8bfa466dc88d4c0899 (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
/*******************************************************************************
 * Copyright (c) 2003 - 2005 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.mylar.bugzilla.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.security.auth.login.LoginException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.bugzilla.core.internal.BugParser;
import org.eclipse.mylar.bugzilla.core.internal.NewBugParser;
import org.eclipse.mylar.bugzilla.core.internal.ProductParser;
import org.eclipse.mylar.bugzilla.core.offline.OfflineReportsFile;


/**
 * Singleton class that creates <code>BugReport</code> objects by fetching
 * bug's state and contents from the Bugzilla server.
 * 
 * @author Mik Kersten (hardening of initial prototype)
 */
public class BugzillaRepository 
{
	
	/**
	 * Test method.
	 */
	public static void main(String[] args) throws Exception {
		instance =
			new BugzillaRepository(BugzillaPlugin.getDefault().getServerName() + "/long_list.cgi?buglist=");
		BugReport bug = instance.getBug(16161);
		System.out.println("Bug " + bug.getId() + ": " + bug.getSummary());
		for (Iterator<Attribute> it = bug.getAttributes().iterator(); it.hasNext();) {
			Attribute attribute = it.next();
			System.out.println(attribute.getName() + ": " + attribute.getValue());
		}
		System.out.println(bug.getDescription());
		for (Iterator<Comment> it = bug.getComments().iterator(); it.hasNext();) {
			Comment comment = it.next();
			System.out.println(comment.getAuthorName() + "<" + comment.getAuthor() + "> (" + comment.getCreated() + ")");
			System.out.print(comment.getText());
			System.out.println();
		}
	}

	/** URL of the Bugzilla server */
	private static String bugzillaUrl;

	/** singleton instance */
	private static BugzillaRepository instance;
		
	/**
	 * Constructor
	 * @param bugzillaUrl - the url of the bugzilla repository
	 */
	private BugzillaRepository(String bugzillaUrl) 
	{
		BugzillaRepository.bugzillaUrl = bugzillaUrl;
	}

	/**
	 * Get the singleton instance of the <code>BugzillaRepository</code>
	 * @return The instance of the repository
	 */
	public synchronized static BugzillaRepository getInstance() 
	{
		if (instance == null) 
		{
			// if the instance hasn't been created yet, create one
			instance = new BugzillaRepository(
							BugzillaPlugin.getDefault().getServerName());
		}
		
		// fix bug 58 by updating url if it changes
		if(! BugzillaRepository.bugzillaUrl.equals(BugzillaPlugin.getDefault().getServerName()))
		{
			BugzillaRepository.bugzillaUrl = BugzillaPlugin.getDefault().getServerName();
		}
		
		return instance;
	}
	
	/**
	 * Get a bug from the server
	 * @param id - the id of the bug to get
	 * @return - a <code>BugReport</code> for the selected bug or null if it doesn't exist
	 * @throws IOException
	 */
	public BugReport getBug(int id) throws IOException, MalformedURLException, LoginException
	{

		BufferedReader in = null;
		try {
			
			// create a new input stream for getting the bug
			
			String url = bugzillaUrl + "/show_bug.cgi?id=" + id;
	
			// allow the use to only see the operations that they can do to a bug if they have
			// their user name and password in the preferences
			if(BugzillaPreferences.getUserName() != null && !BugzillaPreferences.getUserName().equals("") && BugzillaPreferences.getPassword() != null && !BugzillaPreferences.getPassword().equals(""))
			{
				/*
				 * The UnsupportedEncodingException exception for
				 * URLEncoder.encode() should not be thrown, since every
				 * implementation of the Java platform is required to support
				 * the standard charset "UTF-8"
				 */
				url += "&GoAheadAndLogIn=1&Bugzilla_login=" + URLEncoder.encode(BugzillaPreferences.getUserName(), "UTF-8") + "&Bugzilla_password=" + URLEncoder.encode(BugzillaPreferences.getPassword(), "UTF-8");
			}
			
			URL bugUrl = new URL(url);
			URLConnection cntx = BugzillaPlugin.getDefault().getUrlConnection(bugUrl);
			if(cntx != null){
				InputStream input = cntx.getInputStream();
				if(input != null) {
					in = new BufferedReader(new InputStreamReader(input));
		
					// get the actual bug fron the server and return it
					BugReport bug = BugParser.parseBug(in, id, BugzillaPlugin.getDefault().getServerName(), BugzillaPreferences.is218(), BugzillaPreferences.getUserName(), BugzillaPreferences.getPassword());
					return bug; 
				}
			}
			// TODO handle the error
			return null;
		} 
		catch (MalformedURLException e) {
			throw e;
		}
		catch (IOException e) {
			throw e;
		}
		catch(LoginException e)
		{
			throw e;
		}
		catch(Exception e) {
			// throw an exception if there is a problem reading the bug from the server
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
		finally
		{
			try{
				if(in != null)
					in.close();
			}catch(IOException e)
			{
				BugzillaPlugin.log(new Status(IStatus.ERROR, IBugzillaConstants.PLUGIN_ID,IStatus.ERROR,"Problem closing the stream", e));
			}
		}
	}
	
	/**
	 * Get a bug from the server. 
	 * If a bug with the given id is saved offline, the offline version is returned instead.
	 * @param id - the id of the bug to get
	 * @return - a <code>BugReport</code> for the selected bug or null if it doesn't exist
	 * @throws IOException, MalformedURLException, LoginException
	 */
	public BugReport getCurrentBug(int id) throws MalformedURLException, LoginException, IOException {
		// Look among the offline reports for a bug with the given id.
		OfflineReportsFile reportsFile = BugzillaPlugin.getDefault().getOfflineReports();
		int offlineId = reportsFile.find(id);
		
		// If an offline bug was found, return it if possible.
		if (offlineId != -1) {
			IBugzillaBug bug = reportsFile.elements().get(offlineId);
			if (bug instanceof BugReport) {
				return (BugReport)bug;
			}
		}
		
		// If a suitable offline report was not found, try to get one from the server.
		return getBug(id);
	}
	
	/**
	 * Get the list of products when creating a new bug
	 * @return The list of valid products a bug can be logged against
	 * @throws IOException
	 */
	public List<String> getProductList() throws IOException, LoginException, Exception
	{
		BufferedReader in = null;
		try 
		{
			// connect to the bugzilla server
			String urlText = "";

			// use the usename and password to get into bugzilla if we have it
			if(BugzillaPreferences.getUserName() != null && !BugzillaPreferences.getUserName().equals("") && BugzillaPreferences.getPassword() != null && !BugzillaPreferences.getPassword().equals(""))
			{
				/*
				 * The UnsupportedEncodingException exception for
				 * URLEncoder.encode() should not be thrown, since every
				 * implementation of the Java platform is required to support
				 * the standard charset "UTF-8"
				 */
				urlText += "?GoAheadAndLogIn=1&Bugzilla_login=" + URLEncoder.encode(BugzillaPreferences.getUserName(), "UTF-8") + "&Bugzilla_password=" + URLEncoder.encode(BugzillaPreferences.getPassword(), "UTF-8");
			}

			URL url = new URL(bugzillaUrl + "/enter_bug.cgi"+urlText);

			URLConnection cntx = BugzillaPlugin.getDefault().getUrlConnection(url);
			if(cntx != null){
				InputStream input = cntx.getInputStream();
				if(input != null) {
			
					// create a new input stream for getting the bug
					in = new BufferedReader(new InputStreamReader(input));
				
					return new ProductParser(in).getProducts();
				}
			}
			return null;
		}
		finally
		{
			try{
				if(in != null)
					in.close();
			}catch(IOException e)
			{
				BugzillaPlugin.log(new Status(IStatus.ERROR, IBugzillaConstants.PLUGIN_ID,IStatus.ERROR,"Problem closing the stream", e));
			}
		}
	}
	
	/**
	 * Get the attribute values for a new bug
	 * @param nbm A reference to a NewBugModel to store all of the data
	 * @throws Exception
	 */
	public void getnewBugAttributes(NewBugModel nbm, boolean getProd) throws Exception
	{
		BufferedReader in = null;
		try 
		{
			// create a new input stream for getting the bug
			String prodname = URLEncoder.encode(nbm.getProduct(), "UTF-8");
			
			String url = bugzillaUrl + "/enter_bug.cgi";
			
			// use the proper url if we dont know the product yet
			if(!getProd)
				url += "?product=" + prodname + "&";
			else
				url += "?"; 
				
			// add the password and username to the url so that bugzilla logs us in
			/*
			 * The UnsupportedEncodingException exception for
			 * URLEncoder.encode() should not be thrown, since every
			 * implementation of the Java platform is required to support
			 * the standard charset "UTF-8"
			 */
			url += "&GoAheadAndLogIn=1&Bugzilla_login=" + URLEncoder.encode(BugzillaPreferences.getUserName(), "UTF-8") + "&Bugzilla_password=" + URLEncoder.encode(BugzillaPreferences.getPassword(), "UTF-8");
			
			URL bugUrl = new URL(url);
			URLConnection cntx = BugzillaPlugin.getDefault().getUrlConnection(bugUrl);
			if(cntx != null){
				InputStream input = cntx.getInputStream();
				if(input != null) {	
					in = new BufferedReader(new InputStreamReader(input));

					new NewBugParser(in).parseBugAttributes(nbm, getProd);
				}
			}
			
		} catch(Exception e) {
			
			if ( e instanceof KeyManagementException || e instanceof NoSuchAlgorithmException || e instanceof IOException ){
			 	if(MessageDialog.openQuestion(null, "Bugzilla Connect Error", "Unable to connect to Bugzilla server.\n" +
				   "Bug report will be created offline and saved for submission later.")){
			 		nbm.setConnected(false);
			 		getProdConfigAttributes(nbm);
			 	}
			 	else
			 		throw new Exception("Bug report will not be created.");
			 }
			 else
			 	throw e;
		}
		finally
		{
			try{
				if(in != null)
					in.close();
			}catch(IOException e)
			{
				BugzillaPlugin.log(new Status(IStatus.ERROR, IBugzillaConstants.PLUGIN_ID,IStatus.ERROR,"Problem closing the stream", e));
			}
		}
	}
	
	/**
	 * Get the bugzilla url that the repository is using
	 * @return A <code>String</code> containing the url of the bugzilla server
	 */
	public static String getURL()
	{
		return bugzillaUrl;
	}
	
	
	/** Method to get attributes from ProductConfiguration if unable to connect
	 *  to Bugzilla server
	 * @param model - the NewBugModel to store the attributes
	 */
	public void getProdConfigAttributes(NewBugModel model){
		
		HashMap<String, Attribute> attributes = new HashMap<String, Attribute>();
		
		// ATTRIBUTE: Severity
		Attribute a = new Attribute("Severity");
		a.setParameterName("bug_severity");
		// get optionValues from ProductConfiguration
		String[] optionValues = BugzillaPlugin.getDefault().getProductConfiguration().getSeverities();			
		// add option values from ProductConfiguration to Attribute optionValues
		for( int i=0; i<optionValues.length; i++ ){
			a.addOptionValue(optionValues[i], optionValues[i]);
		}								
		// add Attribute to model
		attributes.put("severites", a);
		
		// ATTRIBUTE: OS
		a = new Attribute("OS");
		a.setParameterName("op_sys");
		optionValues = BugzillaPlugin.getDefault().getProductConfiguration().getOSs();
		for( int i=0; i<optionValues.length; i++ ){
			a.addOptionValue(optionValues[i], optionValues[i]);
		}
		attributes.put("OSs", a);
	
		// ATTRIBUTE: Platform
		a = new Attribute("Platform");
		a.setParameterName("rep_platform");
		optionValues = BugzillaPlugin.getDefault().getProductConfiguration().getPlatforms();
		for( int i=0; i<optionValues.length; i++ ){
			a.addOptionValue(optionValues[i], optionValues[i]);
		}
		attributes.put("platforms",a);
		
		// ATTRIBUTE: Version
		a = new Attribute("Version");
		a.setParameterName("version");
		optionValues = BugzillaPlugin.getDefault().getProductConfiguration().getVersions(model.getProduct());
		for( int i=0; i<optionValues.length; i++ ){
			a.addOptionValue(optionValues[i], optionValues[i]);						
		}
		attributes.put("versions", a);

		// ATTRIBUTE: Component
		a = new Attribute("Component");
		a.setParameterName("component");
		optionValues = BugzillaPlugin.getDefault().getProductConfiguration().getComponents(model.getProduct());			
		for( int i=0; i<optionValues.length; i++ ){
			a.addOptionValue(optionValues[i], optionValues[i]);
		}
		attributes.put("components", a);

		// ATTRIBUTE: Priority
		a = new Attribute("Priority");
		a.setParameterName("bug_severity");
		optionValues = BugzillaPlugin.getDefault().getProductConfiguration().getPriorities();
		for( int i=0; i<optionValues.length; i++ ){
			a.addOptionValue(optionValues[i], optionValues[i]);
		}
							
		// set NBM Attributes (after all Attributes have been created, and added to attributes map)
		model.attributes = attributes;
	}
	
	public static String getBugUrl(int id) {		
		String url = BugzillaPlugin.getDefault().getServerName() + "/show_bug.cgi?id=" + id;
		try {
			if (BugzillaPreferences.getUserName() != null
					&& !BugzillaPreferences.getUserName().equals("")
					&& BugzillaPreferences.getPassword() != null
					&& !BugzillaPreferences.getPassword().equals("")) {

				url += "&GoAheadAndLogIn=1&Bugzilla_login="
						+ URLEncoder.encode(BugzillaPreferences.getUserName(), "UTF-8")
						+ "&Bugzilla_password="
						+ URLEncoder.encode(BugzillaPreferences.getPassword(),"UTF-8");
			}
		} catch (UnsupportedEncodingException e) {		
			return "";
		}
		return url;
	}
	
	public static String getBugUrlWithoutLogin(int id) {		
		String url = BugzillaPlugin.getDefault().getServerName() + "/show_bug.cgi?id=" + id;
		return url;
	}
}

Back to the top