-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSitesHelper.java
More file actions
726 lines (655 loc) · 27.9 KB
/
SitesHelper.java
File metadata and controls
726 lines (655 loc) · 27.9 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/* 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.sites;
import com.google.gdata.client.http.HttpGDataRequest;
import com.google.gdata.client.sites.SitesService;
import com.google.gdata.data.Link;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.OutOfLineContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.XhtmlTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.data.sites.ActivityFeed;
import com.google.gdata.data.sites.AnnouncementEntry;
import com.google.gdata.data.sites.AnnouncementsPageEntry;
import com.google.gdata.data.sites.AttachmentEntry;
import com.google.gdata.data.sites.BaseActivityEntry;
import com.google.gdata.data.sites.BaseContentEntry;
import com.google.gdata.data.sites.BasePageEntry;
import com.google.gdata.data.sites.CommentEntry;
import com.google.gdata.data.sites.ContentFeed;
import com.google.gdata.data.sites.FileCabinetPageEntry;
import com.google.gdata.data.sites.ListItemEntry;
import com.google.gdata.data.sites.ListPageEntry;
import com.google.gdata.data.sites.RevisionFeed;
import com.google.gdata.data.sites.SitesLink;
import com.google.gdata.data.sites.WebAttachmentEntry;
import com.google.gdata.data.sites.WebPageEntry;
import com.google.gdata.data.spreadsheet.Column;
import com.google.gdata.data.spreadsheet.Field;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.gdata.util.XmlBlob;
import com.google.gdata.util.XmlParser;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.MimetypesFileTypeMap;
/**
* Wrapper class for lower level Sites API calls.
*
*
*/
public class SitesHelper {
private String domain;
private String siteName;
private String scheme = "http://";
public SitesService service;
public MimetypesFileTypeMap mediaTypes;
public static final String[] KINDS = {
"announcement", "announcementspage", "attachment", "comment",
"filecabinet", "listitem", "listpage", "webpage", "webattachment"
};
/**
* Constructor
*
* @param applicationName The name of the application.
* @param domain The Site's Google Apps domain or "site".
* @param siteName The webspace name of the Site.
*/
public SitesHelper(String applicationName, String domain, String siteName) {
this(applicationName, domain, siteName, false, false);
}
/**
* Constructor
*
* @param applicationName The name of the application.
* @param domain The Site's Google Apps domain or "site".
* @param siteName The webspace name of the Site.
* @param logging Whether to enable HTTP requests to stdout
* @param useSsl Whether to enable all API requests over https
*/
public SitesHelper(String applicationName, String domain, String siteName,
boolean logging, boolean useSsl) {
if (logging) {
turnOnLogging(false);
}
this.domain = domain;
this.siteName = siteName;
this.service = new SitesService(applicationName);
if (useSsl) {
this.service.useSsl();
this.scheme = "https://";
}
registerMediaTypes();
}
private void registerMediaTypes() {
// Common MIME types used for uploading attachments.
mediaTypes = new MimetypesFileTypeMap();
mediaTypes.addMimeTypes("application/msword doc");
mediaTypes.addMimeTypes("application/vnd.ms-excel xls");
mediaTypes.addMimeTypes("application/pdf pdf");
mediaTypes.addMimeTypes("text/richtext rtx");
mediaTypes.addMimeTypes("text/csv csv");
mediaTypes.addMimeTypes("text/tab-separated-values tsv tab");
mediaTypes.addMimeTypes("application/x-vnd.oasis.opendocument.spreadsheet ods");
mediaTypes.addMimeTypes("application/vnd.oasis.opendocument.text odt");
mediaTypes.addMimeTypes("application/vnd.ms-powerpoint ppt pps pot");
mediaTypes.addMimeTypes("application/vnd.openxmlformats-officedocument."
+ "wordprocessingml.document docx");
mediaTypes.addMimeTypes("application/vnd.openxmlformats-officedocument."
+ "spreadsheetml.sheet xlsx");
mediaTypes.addMimeTypes("audio/mpeg mp3 mpeg3");
mediaTypes.addMimeTypes("image/png png");
mediaTypes.addMimeTypes("application/zip zip");
mediaTypes.addMimeTypes("application/x-tar tar");
mediaTypes.addMimeTypes("video/quicktime qt mov moov");
mediaTypes.addMimeTypes("video/mpeg mpeg mpg mpe mpv vbs mpegv");
mediaTypes.addMimeTypes("video/msvideo avi");
}
/**
* Authenticates the user using ClientLogin
*
* @param username User's email address.
* @param password User's password.
*/
public void login(String username, String password) throws AuthenticationException {
service.setUserCredentials(username, password);
}
/**
* Authenticates the user using AuthSub
*
* @param authSubToken A valid AuthSub session token.
*/
public void login(String authSubToken) {
service.setAuthSubToken(authSubToken);
}
/**
* Logs HTTP requests and XML to stdout.
*/
private void turnOnLogging(boolean logXML) {
// Create a log handler which prints all log events to the console
ConsoleHandler logHandler = new ConsoleHandler();
logHandler.setLevel(Level.ALL);
// Configure the logging mechanisms
Logger httpLogger =
Logger.getLogger(HttpGDataRequest.class.getName());
httpLogger.setLevel(Level.ALL);
httpLogger.addHandler(logHandler);
if (logXML) {
Logger xmlLogger = Logger.getLogger(XmlParser.class.getName());
xmlLogger.setLevel(Level.ALL);
xmlLogger.addHandler(logHandler);
}
}
/**
* Returns an entry's numeric ID.
*/
private String getEntryId(BaseContentEntry<?> entry) {
String selfLink = entry.getSelfLink().getHref();
return selfLink.substring(selfLink.lastIndexOf("/") + 1);
}
/**
* Returns an entry's numeric ID.
*/
private String getEntryId(String selfLink) {
return selfLink.substring(selfLink.lastIndexOf("/") + 1);
}
public String getContentFeedUrl() {
return this.scheme + "sites.google.com/feeds/content/" + domain + "/" + siteName + "/";
}
public String getRevisionFeedUrl() {
return this.scheme + "sites.google.com/feeds/revision/" + domain + "/" + siteName + "/";
}
public String getActivityFeedUrl() {
return this.scheme + "sites.google.com/feeds/activity/" + domain + "/" + siteName + "/";
}
/**
* Fetches and displays the Site's activity feed.
*/
public void getActivityFeed() throws IOException, ServiceException {
ActivityFeed activityFeed = service.getFeed(
new URL(getActivityFeedUrl()), ActivityFeed.class);
for (BaseActivityEntry<?> entry : activityFeed.getEntries()){
System.out.println(entry.getSummary().getPlainText());
}
}
/**
* Fetches and displays the revisions feed for an entry.
*/
public void getRevisionFeed(String contentEntryId) throws IOException, ServiceException {
URL url = new URL(getRevisionFeedUrl() + contentEntryId);
RevisionFeed revisionFeed = service.getFeed(url, RevisionFeed.class);
for (BaseContentEntry<?> entry : revisionFeed.getEntries()){
System.out.println(entry.getTitle().getPlainText());
System.out.println(" updated: " + entry.getUpdated().toUiString() + " by " +
entry.getAuthors().get(0).getEmail());
System.out.println(" revision #: " + entry.getRevision().getValue());
}
}
/**
* Fetches and displays entries from the content feed.
*
* @param kind An entry kind to fetch. For example, "webpage". If null, the
* entire content feed is returned.
*/
public void listSiteContents(String kind) throws IOException, ServiceException {
String url = kind.equals("all") ? getContentFeedUrl() : getContentFeedUrl() + "?kind=" + kind;
ContentFeed contentFeed = service.getFeed(new URL(url), ContentFeed.class);
for (WebPageEntry entry : contentFeed.getEntries(WebPageEntry.class)) {
System.out.println("WebPageEntry:");
System.out.println(" title: " + entry.getTitle().getPlainText());
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
System.out.println(" authors: " + entry.getAuthors().get(0).getEmail());
System.out.println(" content: " + getContentBlob(entry));
System.out.println("");
}
for (ListPageEntry entry : contentFeed.getEntries(ListPageEntry.class)) {
System.out.println("ListPageEntry:");
System.out.println(" title: " + entry.getTitle().getPlainText());
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
for (Column col : entry.getData().getColumns()) {
System.out.print(" [" + col.getIndex() + "] " + col.getName() + "\t");
}
System.out.println("");
}
for (ListItemEntry entry : contentFeed.getEntries(ListItemEntry.class)) {
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
for (Field field : entry.getFields()) {
System.out.print(" [" + field.getIndex() + "] " + field.getValue() + "\t");
}
System.out.println("\n");
}
for (FileCabinetPageEntry entry : contentFeed.getEntries(FileCabinetPageEntry.class)) {
System.out.println("FileCabinetPageEntry:");
System.out.println(" title: " + entry.getTitle().getPlainText());
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
System.out.println(" content: " + getContentBlob(entry));
System.out.println("");
}
for (CommentEntry entry : contentFeed.getEntries(CommentEntry.class)) {
System.out.println("CommentEntry:");
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
System.out.println(" in-reply-to: " + entry.getInReplyTo().toString());
System.out.println(" content: " + getContentBlob(entry));
System.out.println("");
}
for (AnnouncementsPageEntry entry : contentFeed.getEntries(AnnouncementsPageEntry.class)) {
System.out.println("AnnouncementsPageEntry:");
System.out.println(" title: " + entry.getTitle().getPlainText());
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
System.out.println(" content: " + getContentBlob(entry));
System.out.println("");
}
for (AnnouncementEntry entry : contentFeed.getEntries(AnnouncementEntry.class)) {
System.out.println("AnnouncementEntry:");
System.out.println(" title: " + entry.getTitle().getPlainText());
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
System.out.println(" draft?: " + entry.isDraft());
System.out.println(" content: " + getContentBlob(entry));
System.out.println("");
}
for (AttachmentEntry entry : contentFeed.getEntries(AttachmentEntry.class)) {
System.out.println("AttachmentEntry:");
System.out.println(" title: " + entry.getTitle().getPlainText());
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
if (entry.getSummary() != null) {
System.out.println(" description: " + entry.getSummary().getPlainText());
}
System.out.println(" revision: " + entry.getRevision().getValue());
MediaContent content = (MediaContent) entry.getContent();
System.out.println(" src: " + content.getUri());
System.out.println(" content type: " + content.getMimeType().getMediaType());
System.out.println("");
}
for (WebAttachmentEntry entry : contentFeed.getEntries(WebAttachmentEntry.class)) {
System.out.println("WebAttachmentEntry:");
System.out.println(" title: " + entry.getTitle().getPlainText());
System.out.println(" id: " + getEntryId(entry));
if (entry.getParentLink() != null) {
System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
}
if (entry.getSummary() != null) {
System.out.println(" description: " + entry.getSummary().getPlainText());
}
System.out.println(" src: " + ((MediaContent) entry.getContent()).getUri());
System.out.println("");
}
}
public String getContentBlob(BaseContentEntry<?> entry) {
return ((XhtmlTextConstruct) entry.getTextContent().getContent()).getXhtml().getBlob();
}
private void setContentBlob(BaseContentEntry<?> entry) {
XmlBlob xml = new XmlBlob();
xml.setBlob(String.format(
"content for %s", entry.getCategories().iterator().next().getLabel()));
entry.setContent(new XhtmlTextConstruct(xml));
}
public BaseContentEntry<?> createPage(String kind, String title)
throws MalformedURLException, SitesException, IOException, ServiceException {
return createPage(kind, title, null);
}
/**
* Creates a new (sub)page.
*
* @param kind The type of page to created (e.g. webpage, filecabinet, listpage, etc).
* @param title A title heading for the page
* @param parent The full URL of the parent to create the subpage under
* or the parent entry's ID (e.g. 1234567890).
* @return The created entry.
* @throws SitesException
* @throws ServiceException
* @throws IOException
* @throws MalformedURLException
*/
public BaseContentEntry<?> createPage(String kind, String title, String parent)
throws SitesException, MalformedURLException, IOException, ServiceException {
BaseContentEntry<?> entry = null;
if (kind.equals("announcement")) {
entry = new AnnouncementEntry();
} else if (kind.equals("announcementspage")) {
entry = new AnnouncementsPageEntry();
} else if (kind.equals("comment")) {
entry = new CommentEntry();
} else if (kind.equals("filecabinet")) {
entry = new FileCabinetPageEntry();
} else if (kind.equals("listitem")) {
entry = new ListItemEntry();
} else if (kind.equals("listpage")) {
entry = new ListPageEntry();
} else if (kind.equals("webpage")) {
entry = new WebPageEntry();
} else {
if (kind.equals("attachment") || kind.equals("webattachment")) {
throw new SitesException("Trying to create " + kind
+ ". Please use upload command instead.");
} else {
throw new SitesException("Unknown kind '" + kind + "'");
}
}
entry.setTitle(new PlainTextConstruct(title));
setContentBlob(entry);
// Upload to a parent page?
if (parent != null) {
if (parent.lastIndexOf("/") == -1) {
parent = getContentFeedUrl() + parent;
}
entry.addLink(SitesLink.Rel.PARENT, Link.Type.ATOM, parent);
}
return service.insert(new URL(getContentFeedUrl()), entry);
}
/**
* Downloads a file from the specified URL to disk.
*
* @param downloadUrl The full URL to download the file from.
* @param fullFilePath The local path to save the file to on disk.
* @throws ServiceException
* @throws IOException
*/
private void downloadFile(String downloadUrl, String fullFilePath) throws IOException,
ServiceException {
System.out.println("Downloading file from: " + downloadUrl);
MediaContent mc = new MediaContent();
mc.setUri(downloadUrl);
MediaSource ms = service.getMedia(mc);
InputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = ms.getInputStream();
outStream = new FileOutputStream(fullFilePath);
int c;
while ((c = inStream.read()) != -1) {
outStream.write(c);
}
} finally {
if (inStream != null) {
inStream.close();
}
if (outStream != null) {
outStream.flush();
outStream.close();
}
}
}
/**
* Downloads an attachment.
*
* @param entry The attachment (entry) to download.
* @param directory Path to a local directory to download the attach to.
* @throws ServiceException
* @throws IOException
*/
public void downloadAttachment(AttachmentEntry entry, String directory)
throws IOException, ServiceException {
String url = ((OutOfLineContent) entry.getContent()).getUri();
downloadFile(url, directory + entry.getTitle().getPlainText());
}
/**
* Downloads an attachment.
*
* @param entry The attachment (entry) to download.
* @param entryId The content entry id of the attachment (e.g. 1234567890)
* @throws ServiceException
* @throws IOException
*/
public void downloadAttachment(String entryId, String directory)
throws IOException, ServiceException {
AttachmentEntry attachment = service.getEntry(
new URL(getContentFeedUrl() + entryId), AttachmentEntry.class);
String url = ((OutOfLineContent) attachment.getContent()).getUri();
///*MediaSource mediaSource = service.getMedia((MediaContent) entry.getContent());*/
downloadFile(url, directory + attachment.getTitle().getPlainText());
}
/**
* Downloads all attachments from a Site and assumes they all have different names.
*
* @param entry The attachment (entry) to download.
* @param directory Path to a local directory to download the attach to.
* @throws ServiceException
* @throws IOException
*/
public void downloadAllAttachments(String directory) throws IOException, ServiceException {
URL contentFeedUrl = new URL(getContentFeedUrl() + "?kind=attachment");
ContentFeed contentFeed = service.getFeed(contentFeedUrl, ContentFeed.class);
for (AttachmentEntry entry : contentFeed.getEntries(AttachmentEntry.class)) {
downloadAttachment(entry, directory);
}
}
/**
* Uploads an attachment to a selected parent page.
*
* @param file The file contents to upload.
* @param parentPage Entry of the page to upload the attachment to.
* @return Created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(File file, BasePageEntry<?> parentPage)
throws IOException, ServiceException {
return uploadAttachment(file, parentPage.getSelfLink().getHref(), file.getName(), "");
}
/**
* Uploads an attachment to a selected parent page.
*
* @param file The file contents to upload,
* @param parentPage Entry of the page to upload the attachment to.
* @param title A title to name the attachment.
* @return Created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(File file, BasePageEntry<?> parentPage, String title)
throws IOException, ServiceException {
return uploadAttachment(file, parentPage.getSelfLink().getHref(), title, "");
}
/**
* Uploads an attachment to a selected parent page.
*
* @param file The file contents to upload.
* @param parentPage Entry of the page to upload the attachment to.
* @param title A title to name the attachment.
* @param description A description for the file.
* @return Created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(File file, BasePageEntry<?> parentPage,
String title, String description) throws IOException, ServiceException {
return uploadAttachment(file, parentPage.getSelfLink().getHref(), title, description);
}
/**
* Uploads an attachment to a selected parent page.
*
* @param filename Path of the file to upload.
* @param parentPage Entry of the page to upload the attachment to.
* @return Created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(String filename, BasePageEntry<?> parentPage)
throws IOException, ServiceException {
File file = new File(filename);
return uploadAttachment(file, parentPage.getSelfLink().getHref(), file.getName(), "");
}
/**
* Uploads an attachment to a selected parent page.
*
* @param filename Path of the file to upload.
* @param parentUrl Full self id of the parent entry to upload the attachmen to.
* @param description A file description.
* @return Created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(String filename, String parentUrl, String description)
throws IOException, ServiceException {
File file = new File(filename);
return uploadAttachment(file, parentUrl, file.getName(), description);
}
/**
* Uploads an attachment to a parent page using the file name for a title.
*
* @param file The file contents to upload.
* @param parentUrl Feed URL of the page to upload the attachment to.
* @return Created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(File file, String parentUrl)
throws IOException, ServiceException {
return uploadAttachment(file, parentUrl, file.getName(), "");
}
/**
* Uploads an attachment to a parent page using the file name for a title.
*
* @param file The file contents to upload
* @param parentLink Full self id of the parent entry to upload the attach to.
* @param description A file description.
* @return The created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(File file, String parentLink, String description)
throws IOException, ServiceException {
return uploadAttachment(file, parentLink, file.getName(), description);
}
/**
* Uploads an attachment to a parent page.
*
* @param file The file contents to upload
* @param parentLink Full self id of the parent entry to upload the attach to.
* @param title A title for the attachment.
* @param description A description for the attachment.
* @return The created attachment entry.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry uploadAttachment(File file, String parentLink, String title,
String description) throws IOException, ServiceException {
String fileMimeType = mediaTypes.getContentType(file);
AttachmentEntry newAttachment = new AttachmentEntry();
newAttachment.setMediaSource(new MediaFileSource(file, fileMimeType));
newAttachment.setTitle(new PlainTextConstruct(title));
newAttachment.setSummary(new PlainTextConstruct(description));
newAttachment.addLink(SitesLink.Rel.PARENT, Link.Type.ATOM, parentLink);
return service.insert(new URL(getContentFeedUrl()), newAttachment);
}
/**
* Creates a web attachment under the selected file cabinet.
*
* @param contentUrl The full URL of the hosted file.
* @param filecabinet File cabinet to create the web attachment on.
* @param title A title for the attachment.
* @param description A description for the attachment.
* @return The created web attachment.
* @throws ServiceException
* @throws IOException
* @throws MalformedURLException
*/
public WebAttachmentEntry uploadWebAttachment(String contentUrl,
FileCabinetPageEntry filecabinet, String title, String description)
throws MalformedURLException, IOException, ServiceException {
MediaContent content = new MediaContent();
content.setUri(contentUrl);
WebAttachmentEntry webAttachment = new WebAttachmentEntry();
webAttachment.setTitle(new PlainTextConstruct(title));
webAttachment.setSummary(new PlainTextConstruct(description));
webAttachment.setContent(content);
webAttachment.addLink(SitesLink.Rel.PARENT, Link.Type.ATOM,
filecabinet.getSelfLink().getHref());
return service.insert(new URL(getContentFeedUrl()), webAttachment);
}
/**
* Updates an existing attachment with new content.
*
* @param entry Attachment entry to update.
* @param newFile The replacement file content.
* @return The created attachment.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry updateAttachment(AttachmentEntry entry, File newFile)
throws IOException, ServiceException {
return updateAttachment(entry, newFile, null, null);
}
/**
* Updates an existing attachment's metadata.
*
* @param entry Attachment entry to update.
* @param newTitle A new title for the attachment.
* @param newDescription A new description for the attachment.
* @return The created attachment.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry updateAttachment(AttachmentEntry entry, String newTitle,
String newDescription) throws IOException, ServiceException {
entry.setTitle(new PlainTextConstruct(newTitle));
entry.setSummary(new PlainTextConstruct(newDescription));
return entry.update();
}
/**
* Updates an existing attachment's metadata and content.
*
* @param entry Attachment entry to update.
* @param newFile The replacement file content.
* @param newTitle A new title for the attachment.
* @param newDescription A new description for the attachment.
* @return The created attachment.
* @throws ServiceException
* @throws IOException
*/
public AttachmentEntry updateAttachment(AttachmentEntry entry, File newFile,
String newTitle, String newDescription) throws IOException, ServiceException {
entry.setMediaSource(new MediaFileSource(newFile, mediaTypes.getContentType(newFile)));
if (newTitle != null) {
entry.setTitle(new PlainTextConstruct(newTitle));
}
if (newDescription != null) {
entry.setSummary(new PlainTextConstruct(newDescription));
}
return entry.updateMedia(true);
}
}