forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectHostingClient.java
More file actions
575 lines (510 loc) · 18.7 KB
/
Copy pathProjectHostingClient.java
File metadata and controls
575 lines (510 loc) · 18.7 KB
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.projecthosting;
import com.google.gdata.client.projecthosting.IssuesQuery;
import com.google.gdata.client.projecthosting.ProjectHostingService;
import com.google.gdata.data.HtmlTextConstruct;
import com.google.gdata.data.Person;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.projecthosting.BlockedOn;
import com.google.gdata.data.projecthosting.BlockedOnUpdate;
import com.google.gdata.data.projecthosting.Blocking;
import com.google.gdata.data.projecthosting.Cc;
import com.google.gdata.data.projecthosting.CcUpdate;
import com.google.gdata.data.projecthosting.IssueCommentsEntry;
import com.google.gdata.data.projecthosting.IssueCommentsFeed;
import com.google.gdata.data.projecthosting.IssuesEntry;
import com.google.gdata.data.projecthosting.IssuesFeed;
import com.google.gdata.data.projecthosting.Label;
import com.google.gdata.data.projecthosting.Owner;
import com.google.gdata.data.projecthosting.Updates;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This is a simple client that provides high-level operations on the Google
* Code Project Hosting Issue Tracker Data API. It can also be used as a
* command-line application to test out some of the features of the API.
*
*
*/
public class ProjectHostingClient {
private ProjectHostingService service;
private static final String FEED_URI_BASE =
"https://code.google.com/feeds/issues";
private static final String PROJECTION = "/full";
// User-provided input
private String project;
private String username;
private String password;
/** Issues API base URL constructed from the given project name. */
private String issuesBaseUri;
/** Default issues feed URL constructed from the given project name. */
private URL issuesFeedUrl;
/** Group 1 of the regex will match the ID of the issue. */
private Pattern issueIdPattern;
/** Group 1 of the regex will match the ID of the comment. */
private Pattern commentIdPattern;
private static final String DIVIDER =
"-----------------------------------------------------------------------";
/**
* Constructs a new client.
*
* @throws AuthenticationException if authentication fails
* @throws MalformedURLException if there's a problem with URL
*/
public ProjectHostingClient(
ProjectHostingService service, String project, String username,
String password) throws AuthenticationException, MalformedURLException {
this.service = service;
this.project = project;
this.username = username;
this.password = password;
// Login via ClientLogin
if ((username != null) && (password != null)) {
service.setUserCredentials(username, password);
}
issuesBaseUri = FEED_URI_BASE + "/p/" + project + "/issues";
issuesFeedUrl = makeIssuesFeedUrl(project);
String issuesBaseUriHttp = issuesBaseUri.replaceFirst("https", "http");
issueIdPattern = Pattern.compile(
issuesBaseUriHttp + PROJECTION + "/(\\d+)$");
commentIdPattern = Pattern.compile(
issuesBaseUriHttp + "/\\d+/comments" + PROJECTION + "/(\\d+)$");
}
/**
* Getter method for {@code issuesFeedUrl}.
*/
protected URL getIssuesFeedUrl() {
return issuesFeedUrl;
}
/**
* Setter method for {@code issuesFeedUrl}.
*/
protected void setIssuesFeedUrl(URL url) {
issuesFeedUrl = url;
}
/**
* Constructs issues feed URL.
*
* @param proj name of the project for the issues feed
* @throws MalformedURLException if there's a problem with URL
*/
protected URL makeIssuesFeedUrl(String proj)
throws MalformedURLException {
return new URL(FEED_URI_BASE + "/p/" + proj + "/issues" + PROJECTION);
}
/**
* Constructs issue entry URL.
*
* @param issueId ID number of an issue to construct the issue entry URL
* @throws MalformedURLException if there's a problem with URL
*/
protected URL makeIssueEntryUrl(String issueId)
throws MalformedURLException {
return new URL(issuesBaseUri + PROJECTION + "/" + issueId);
}
/**
* Constructs comments feed URL.
*
* @param issueId ID number of an issue to construct the comments URL
* @throws MalformedURLException if there's a problem with URL
*/
protected URL makeCommentsFeedUrl(String issueId)
throws MalformedURLException {
return new URL(issuesBaseUri + "/" + issueId + "/comments" + PROJECTION);
}
/**
* Constructs comment entry URL.
*
* @param issueId ID number of an issue the comment belongs to
* @param commentId ID number of the comment
* @throws MalformedURLException if there's a problem with URL
*/
protected URL makeCommentEntryUrl(String issueId, String commentId)
throws MalformedURLException {
return new URL(issuesBaseUri + "/" + issueId + "/comments" + PROJECTION
+ "/" + commentId);
}
/**
* Retrieves issues feed.
*
* @param feedUrl feed URL of issues to retrieve
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssuesFeed getIssuesFeed(URL feedUrl)
throws IOException, ServiceException {
return service.getFeed(feedUrl, IssuesFeed.class);
}
/**
* Retrieves a particular issue entry.
*
* @param issueId ID number of an issue to retrieve
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssuesEntry getIssueEntry(String issueId)
throws IOException, ServiceException {
return getIssueEntry(makeIssueEntryUrl(issueId));
}
/**
* Retrieves a particular issue entry.
*
* @param entryUrl URL of an issue entry to retrieve
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssuesEntry getIssueEntry(URL entryUrl)
throws IOException, ServiceException {
return service.getEntry(entryUrl, IssuesEntry.class);
}
/**
* Retrieves comments feed.
*
* @param issueId ID number of an issue to retrieve comments from
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssueCommentsFeed getCommentsFeed(String issueId)
throws IOException, ServiceException {
return getCommentsFeed(makeCommentsFeedUrl(issueId));
}
/**
* Retrieves comments feed.
*
* @param feedUrl comments feed URL to retrieve
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssueCommentsFeed getCommentsFeed(URL feedUrl)
throws IOException, ServiceException {
return service.getFeed(feedUrl, IssueCommentsFeed.class);
}
/**
* Retrieves a particular comment entry.
*
* @param issueId ID number of an issue the comment belongs to
* @param commentId ID number of a comment to retrieve
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssueCommentsEntry getCommentEntry(
String issueId, String commentId)
throws IOException, ServiceException {
return getCommentEntry(makeCommentEntryUrl(issueId, commentId));
}
/**
* Retrieves a particular comment entry.
*
* @param entryUrl comment entry URL to retrieve
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssueCommentsEntry getCommentEntry(URL entryUrl)
throws IOException, ServiceException {
return service.getEntry(entryUrl, IssueCommentsEntry.class);
}
/**
* Inserts an issue entry to the issues feed.
*
* @param entry issue entry to insert
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssuesEntry insertIssue(IssuesEntry entry)
throws IOException, ServiceException {
return service.insert(issuesFeedUrl, entry);
}
/**
* Inserts a comment entry to the comments feed.
*
* @param issueId ID number of an issue to insert the comment entry to
* @param entry comment entry to insert
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssueCommentsEntry insertComment(
String issueId, IssueCommentsEntry entry)
throws IOException, ServiceException {
return insertComment(makeCommentsFeedUrl(issueId), entry);
}
/**
* Inserts a comment entry to the comments feed.
*
* @param commentsFeedUrl comments feed URL to insert the comment entry to
* @param entry comment entry to insert
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssueCommentsEntry insertComment(
URL commentsFeedUrl, IssueCommentsEntry entry)
throws IOException, ServiceException {
return service.insert(commentsFeedUrl, entry);
}
/**
* Queries issues using the given query parameters.
*
* @param query issues query object with query parameters set
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected IssuesFeed queryIssues(IssuesQuery query)
throws IOException, ServiceException {
return service.query(query, IssuesFeed.class);
}
/**
* Returns the numeric issue ID from the given {@code issueUrl}.
*
* @param issueUrl URI to find the issue ID from
*/
protected String getIssueId(String issueUrl) {
Matcher matcher = issueIdPattern.matcher(issueUrl);
return matcher.matches() ? matcher.group(1) : null;
}
/**
* Returns the numeric commentId from the given {@code commentUrl}.
*
* @param commentUrl URI to find the comment ID from
*/
protected String getCommentId(String commentUrl) {
Matcher matcher = commentIdPattern.matcher(commentUrl);
return matcher.matches() ? matcher.group(1) : null;
}
/**
* Prints issues in the given issues feed.
*
* @param issuesFeed issues feed to print
*/
protected void printIssues(IssuesFeed issuesFeed) {
for (IssuesEntry issueEntry : issuesFeed.getEntries()) {
printIssue(issueEntry);
}
}
/**
* Prints an issue entry in human-readable format.
*
* @param entry issue entry to print
*/
protected void printIssue(IssuesEntry entry) {
System.out.println(DIVIDER);
if (entry.getId() != null) {
String issueId = getIssueId(entry.getId());
System.out.printf("Issue #%s:\t%s\n", issueId, entry.getId());
} else {
System.out.println("Issue");
}
if (entry.getTitle() != null) {
System.out.println("\tSummary\n\t\t" + entry.getTitle().getPlainText());
}
Person author = entry.getAuthors().get(0);
printPerson("Reporter", author.getName(), author.getUri());
TextContent textContent = (TextContent) entry.getContent();
if ((textContent != null) && (textContent.getContent() != null)) {
HtmlTextConstruct textConstruct =
(HtmlTextConstruct) textContent.getContent();
System.out.println("\tDescription\n\t\t" + textConstruct.getHtml());
}
if (entry.hasStatus()) {
System.out.println("\tStatus\n\t\t" + entry.getStatus().getValue());
}
if (entry.hasOwner()) {
Owner owner = entry.getOwner();
printPerson(
"Owner", owner.getUsername().getValue(),
(owner.getUri() == null) ? null : owner.getUri().getValue());
}
if (entry.getLabels().size() > 0) {
System.out.println("\tLabel");
for (Label label : entry.getLabels()) {
System.out.println("\t\t" + label.getValue());
}
}
if (entry.getCcs().size() > 0) {
System.out.println("\tCC");
for (Cc cc : entry.getCcs()) {
printPerson(
null, cc.getUsername().getValue(),
(cc.getUri() == null) ? null : cc.getUri().getValue());
}
}
if (entry.getBlockedOns().size() > 0) {
System.out.println("\tBlockedOn");
for (BlockedOn blockedOn : entry.getBlockedOns()) {
System.out.print("\t\t");
if (blockedOn.hasProject()) {
System.out.print(blockedOn.getProject().getValue() + ":");
}
System.out.println(blockedOn.getId().getValue());
}
}
if (entry.getBlockings().size() > 0) {
System.out.println("\tBlocking");
for (Blocking blocking : entry.getBlockings()) {
System.out.print("\t\t");
if (blocking.hasProject()) {
System.out.print(blocking.getProject().getValue() + ":");
}
System.out.println(blocking.getId().getValue());
}
}
if (entry.hasMergedInto()) {
System.out.print("\tMergedInto\n\t\t");
if (entry.getMergedInto().hasProject()) {
System.out.print(entry.getMergedInto().getProject().getValue() + ":");
}
System.out.println(entry.getMergedInto().getId().getValue());
}
}
/**
* Prints Username and URI associated with the labeled person.
*
* @param label label to print
* @param name username of the person to print
* @param uri uri of the person to print, usually the profile url
*/
protected void printPerson(String label, String name, String uri) {
if (label != null) {
System.out.printf("\t%s\n", label);
}
System.out.print("\t\t" + name);
if (uri != null) {
System.out.println("\t" + uri);
} else {
System.out.println();
}
}
/**
* Prints issues and their comments.
*
* @param issuesFeed issues feed to print
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected void printIssuesAndComments(IssuesFeed issuesFeed)
throws IOException, ServiceException {
for (IssuesEntry issueEntry : issuesFeed.getEntries()) {
printIssueAndComments(issueEntry);
}
}
/**
* Prints an issue and its comments.
*
* @param issueId ID number of an issue to print
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected void printIssueAndComments(String issueId)
throws IOException, ServiceException {
printIssueAndComments(getIssueEntry(issueId));
}
/**
* Prints an issue and its comments.
*
* @param issueUrl URL of an issue to print
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected void printIssueAndComments(URL issueUrl)
throws IOException, ServiceException {
printIssueAndComments(getIssueEntry(issueUrl));
}
/**
* Prints an issue and its comments.
*
* @param issue issue entry to print
* @throws IOException if there is a problem communicating with the server
* @throws ServiceException if the service is unable to handle the request
*/
protected void printIssueAndComments(IssuesEntry entry)
throws IOException, ServiceException {
printIssue(entry);
String issueId = getIssueId(entry.getId());
IssueCommentsFeed commentsFeed = getCommentsFeed(issueId);
printComments(commentsFeed);
}
/**
* Prints comments in the given comments feed.
*
* @param commentsFeed comments feed to print
*/
protected void printComments(IssueCommentsFeed commentsFeed) {
for (IssueCommentsEntry commentEntry : commentsFeed.getEntries()) {
printComment(commentEntry);
}
}
/**
* Prints a comment entry in human-readable format.
*
* @param entry comment entry to print
*/
protected void printComment(IssueCommentsEntry entry) {
System.out.println(DIVIDER);
if (entry.getId() != null) {
String commentId = getCommentId(entry.getId());
System.out.printf("Comment #%s:\t%s\n", commentId, entry.getId());
} else {
System.out.println("Comment");
}
Person author = entry.getAuthors().get(0);
printPerson("Author", author.getName(), author.getUri());
TextContent textContent = (TextContent) entry.getContent();
if ((textContent != null) && (textContent.getContent() != null)) {
HtmlTextConstruct textConstruct =
(HtmlTextConstruct) textContent.getContent();
System.out.println("\tComment\n\t\t" + textConstruct.getHtml());
}
if (entry.hasUpdates()) {
Updates updates = entry.getUpdates();
if (updates.hasSummary()) {
System.out.println("\tSummary\n\t\t" + updates.getSummary().getValue());
}
if (updates.hasStatus()) {
System.out.println("\tStatus\n\t\t" + updates.getStatus().getValue());
}
if (updates.hasOwnerUpdate()) {
System.out.println(
"\tOwner\n\t\t" + updates.getOwnerUpdate().getValue());
}
if (updates.getLabels().size() > 0) {
System.out.println("\tLabel");
for (Label label : updates.getLabels()) {
System.out.println("\t\t" + label.getValue());
}
}
if (updates.getCcUpdates().size() > 0) {
System.out.println("\tCC");
for (CcUpdate cc : updates.getCcUpdates()) {
System.out.println("\t\t" + cc.getValue());
}
}
if (updates.getBlockedOnUpdates().size() > 0) {
System.out.println("\tBlockedOnUpdate");
for (BlockedOnUpdate blockedOnUpdate : updates.getBlockedOnUpdates()) {
System.out.println("\t\t" + blockedOnUpdate.getValue());
}
}
if (updates.hasMergedIntoUpdate()) {
System.out.println(
"\tMergedIntoUpdate\n\t\t" +
updates.getMergedIntoUpdate().getValue());
}
}
}
}