1 import java
.io
.IOException
;
2 import java
.io
.InputStream
;
3 import java
.io
.InputStreamReader
;
4 import java
.security
.GeneralSecurityException
;
5 import java
.time
.Duration
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collections
;
9 import java
.util
.Timer
;
11 import com
.google
.api
.client
.auth
.oauth2
.Credential
;
12 import com
.google
.api
.client
.extensions
.java6
.auth
.oauth2
.AuthorizationCodeInstalledApp
;
13 import com
.google
.api
.client
.extensions
.jetty
.auth
.oauth2
.LocalServerReceiver
;
14 import com
.google
.api
.client
.googleapis
.auth
.oauth2
.GoogleAuthorizationCodeFlow
;
15 import com
.google
.api
.client
.googleapis
.auth
.oauth2
.GoogleClientSecrets
;
16 import com
.google
.api
.client
.googleapis
.batch
.BatchRequest
;
17 import com
.google
.api
.client
.googleapis
.batch
.json
.JsonBatchCallback
;
18 import com
.google
.api
.client
.googleapis
.javanet
.GoogleNetHttpTransport
;
19 import com
.google
.api
.client
.googleapis
.json
.GoogleJsonError
;
20 import com
.google
.api
.client
.http
.HttpHeaders
;
21 import com
.google
.api
.client
.http
.javanet
.NetHttpTransport
;
22 import com
.google
.api
.client
.json
.JsonFactory
;
23 import com
.google
.api
.client
.json
.jackson2
.JacksonFactory
;
24 import com
.google
.api
.client
.util
.store
.FileDataStoreFactory
;
25 import com
.google
.api
.services
.drive
.Drive
;
26 import com
.google
.api
.services
.drive
.Drive
.Files
;
27 import com
.google
.api
.services
.drive
.Drive
.Files
.Get
;
28 import com
.google
.api
.services
.drive
.DriveScopes
;
29 import com
.google
.api
.services
.drive
.model
.File
;
30 import com
.google
.api
.services
.drive
.model
.FileList
;
31 import com
.google
.api
.services
.drive
.model
.Permission
;
32 import com
.google
.api
.services
.drive
.model
.PermissionList
;
34 import javafx
.animation
.PauseTransition
;
36 //template from Java quickstart code and https://developers.google.com/drive/api/v3/batch
37 public class DriveAPI
implements Runnable
{
38 private NetHttpTransport request_transport
= new NetHttpTransport();
39 private Drive service
;
40 private ConsoleModel attatchment
;
44 String type_of_permission
;
45 String email_of_permission
;
47 int cooldown_time
= 5000;
50 public DriveAPI(ConsoleModel cm
){
51 this.attatchment
= cm
;
55 public void setParam(String ft
, String rl
, String pi
, String tp
, String ep
){
59 type_of_permission
= tp
;
60 email_of_permission
= ep
;
65 if(folder_to_set
!= null)
66 setPermissions(folder_to_set
, role_level
, parent_id
, type_of_permission
, email_of_permission
);
68 clearPermissions(parent_id
);
71 public String
setPermissions(String folder_to_set
, String role_level
, String parent_id
, String type_of_permission
, String email_of_permission
){
72 type_of_permission
= type_of_permission
.toLowerCase();
73 role_level
= role_level
.toLowerCase();
74 // Print the names and IDs for up to 10 files.
78 String
[] message1
= {"Getting all files of name " + folder_to_set
+ " to set root folder ID " + parent_id
, "c"};
79 attatchment
.notifyObservers(message1
);
83 PauseTransition pause
= new PauseTransition();
84 pause
.durationProperty();
85 System
.out
.println(folder_to_set
);
86 result
= service
.files().list()
87 .setSupportsTeamDrives(true)
88 .setIncludeTeamDriveItems(true)
90 .setOrderBy("createdTime")
91 .setFields("files(id, name, teamDriveId, mimeType, parents)")
92 .setQ("name=\"" + folder_to_set
+ "\"")
95 List
<File
> files
= result
.getFiles();
98 String
[] message2
= {"List obtained " + parent_id
, "c"};
99 attatchment
.notifyObservers(message2
);
101 if (files
== null || files
.isEmpty()) {
102 return "No files matched";
104 System
.out
.println("Files: " + files
.size());
105 int total_files
= files
.size();
107 if(total_files
== 1000){
108 String
[] failmsg
= {"1000 files detected(API limit is 1000 files). This program will not run since some data could potentially be missed. Rename your files and try again", "r"};
109 attatchment
.notifyObservers(failmsg
);
110 return "Permissions set finished";
113 String
[] count_msg
= {"Checking " + total_files
+ " files.", "c"};
114 attatchment
.notifyObservers(count_msg
);
117 Object
[] obj
= buildSetPermissionBatchRequest();
118 BatchRequest batch
= (BatchRequest
)obj
[0];
119 @SuppressWarnings("unchecked")
120 JsonBatchCallback
<Permission
> callback
= (JsonBatchCallback
<Permission
>)obj
[1];
122 int batch_counter
= 0;
123 long now
= System
.currentTimeMillis();
124 while(now
+ cooldown_time
> System
.currentTimeMillis() ){}
125 String
[] message
= {"Estimated time: " + (files
.size() / batch_size
* 20) + " Seconds", "c"};
126 attatchment
.notifyObservers(message
);
128 String
[] message3
= {"Determining root hierarchy of Files and adding them to batch requests: " + parent_id
, "c"};
129 attatchment
.notifyObservers(message3
);
130 for (File file
: files
) {
132 String
[] file_count
= {total_files
+ " " + file
.getId(), "l"};
133 attatchment
.notifyObservers(file_count
);
134 if(!checkParent_ids(file
, parent_id
)){
143 addToSetPermissionBatch(batch
, callback
, file
, role_level
, type_of_permission
, email_of_permission
);
144 String
[] message5
= {++file_no
+ " / " + total_files
, "l"};
145 attatchment
.notifyObservers(message5
);
146 if (counter
++ == batch_size
){
147 now
= System
.currentTimeMillis();
148 while(now
+ cooldown_time
> System
.currentTimeMillis() ){}
150 now
= System
.currentTimeMillis();
151 while(now
+ cooldown_time
> System
.currentTimeMillis() ){}
152 System
.out
.println(batch_counter
);
153 String
[] message4
= {"Batch " + ++batch_counter
+ " of " + (int)Math
.ceil(((float)total_files
) / batch_size
) + "(Estimated) Processed", "c"};
154 attatchment
.notifyObservers(message4
);
159 System
.out
.print(total_files
+ " ");
160 System
.out
.println(counter
);
161 if(total_files
== 0){
162 String
[] message7
= {"Nothing was found for " + parent_id
+ " <br/>", "r"};
163 attatchment
.notifyObservers(message7
);
164 return "Permissions set finished";
166 else if(counter
!= batch_size
- 1){
169 String
[] message6
= {total_files
+ " / " + total_files
, "l"};
170 attatchment
.notifyObservers(message6
);
171 String
[] message7
= {"", "r"};
172 attatchment
.notifyObservers(message7
);
173 return "Permissions set finished";
177 } catch (IOException e
) {
178 // TODO Auto-generated catch block
180 return "Error setting permissions: " + e
.getMessage();
184 public List
<File
> getFilesFromParent(String parent_id
, List
<File
> file_obj
) throws IOException
{
185 System
.out
.println("A");
186 FileList result
= service
.files().list()
187 .setSupportsTeamDrives(true)
188 .setIncludeTeamDriveItems(true)
190 .setOrderBy("createdTime")
191 .setFields("files(id, name, teamDriveId, mimeType)")
192 //.setQ("\'" + folder_to_set +"\' in parents")
193 .setQ("\'" + parent_id
+ "\' in parents")
195 List
<File
> files
= result
.getFiles();
197 List
<File
> files_final
= result
.getFiles();
199 for(int file
= 0 ; file
< files
.size(); file
++){
200 System
.out
.println(files
.get(file
).getMimeType() + " " + files
.size());
201 if(files
.get(file
).getMimeType().equals("application/vnd.google-apps.folder")){
202 System
.out
.println(files
.get(file
).getId());
203 List
<File
> files_extra
= getFilesFromParent(files
.get(file
).getId(), null);
204 files_final
.addAll(files_extra
);
207 System
.out
.println(files
.size());
208 System
.out
.println(files_final
.size());
209 String
[] message2
= {"" + files
.size() , "l"};
210 attatchment
.notifyObservers(message2
);
214 public String
clearPermissions(String parent_id
){
215 String
[] message1
= {"Removing permission on all files higher than " + parent_id
, "c"};
216 attatchment
.notifyObservers(message1
);
221 PauseTransition pause
= new PauseTransition();
222 pause
.durationProperty();
224 List
<File
> files
= getFilesFromParent(parent_id
, null);
226 String
[] message2
= {"List obtained " + parent_id
+ " number " + files
.size() , "c"};
227 attatchment
.notifyObservers(message2
);
229 System
.out
.println(files
.size());
231 Object
[] obj
= buildDeletePermissionBatchRequest();
232 BatchRequest batch
= (BatchRequest
)obj
[0];
233 @SuppressWarnings("unchecked")
234 JsonBatchCallback
<Void
> callback
= (JsonBatchCallback
<Void
>)obj
[1];
236 int batch_counter
= 0;
238 int total_files
= files
.size();
239 for (File file
: files
) {
240 long now
= System
.currentTimeMillis();
241 while(now
+ cooldown_time
> System
.currentTimeMillis() ){}
242 addToDeletePermissionBatch(batch
, callback
, file
);
243 String
[] message5
= {++file_no
+ " / " + total_files
, "l"};
244 attatchment
.notifyObservers(message5
);
245 if (counter
++ == batch_size
){
246 now
= System
.currentTimeMillis();
247 while(now
+ cooldown_time
> System
.currentTimeMillis() ){}
249 now
= System
.currentTimeMillis();
250 while(now
+ cooldown_time
> System
.currentTimeMillis() ){}
251 System
.out
.println(batch_counter
);
252 String
[] message4
= {"Batch " + ++batch_counter
+ " of " + (int)Math
.ceil(((float)total_files
) / batch_size
) + "(Estimated) Processed", "c"};
253 attatchment
.notifyObservers(message4
);
258 System
.out
.print(total_files
+ " ");
259 System
.out
.println(counter
);
260 if(total_files
== 0){
261 String
[] message7
= {"Nothing was found for " + parent_id
+ " <br/>", "r"};
262 attatchment
.notifyObservers(message7
);
263 return "Permissions set finished";
265 else if(counter
!= batch_size
- 1){
268 String
[] message6
= {total_files
+ " / " + total_files
, "l"};
269 attatchment
.notifyObservers(message6
);
271 catch (IOException e
){
273 return "Error setting permissions: " + e
.getMessage();
277 String
[] message7
= {"", "r"};
278 attatchment
.notifyObservers(message7
);
279 return "Permissions set finished";
284 private boolean checkParent_ids(File file
, String parent_id
) throws IOException
{
285 if(file
.get("parents") != null){
286 if(((String
) ((ArrayList
)file
.get("parents")).get(0)).equals(parent_id
)){
287 System
.out
.println("TRUE");
291 File results
= service
.files().get((String
) ((ArrayList
)file
.get("parents")).get(0)).setFields("parents").execute();
292 return checkParent_ids(results
, parent_id
);
298 private Object
[] buildSetPermissionBatchRequest(){
299 JsonBatchCallback
<Permission
> callback
= new JsonBatchCallback
<Permission
>() {
301 public void onFailure(GoogleJsonError e
,
302 HttpHeaders responseHeaders
)
305 String
[] message
= {e
.getMessage(), "c"};
306 attatchment
.notifyObservers(message
);
307 System
.err
.println(e
.getMessage());
311 public void onSuccess(Permission permission
,
312 HttpHeaders responseHeaders
)
314 System
.out
.println("Permission ID: " + permission
.getId());
315 String
[] message
= {"Permissions set, ID: " + permission
.getId() , "c"};
316 attatchment
.notifyObservers(message
);
319 Object
[] obj
= {((Object
)service
.batch()), ((Object
)callback
)};
323 private String
addToSetPermissionBatch(BatchRequest batch
, JsonBatchCallback
<Permission
> callback
, File file
, String role_level
,
324 String type_of_permission
, String email_of_permission
) throws IOException
{
325 Permission userPermission
;
326 file
.getTeamDriveId();
327 System
.out
.println(type_of_permission
+ " " + role_level
+ " " + email_of_permission
);
328 if(email_of_permission
== null){
329 userPermission
= new Permission()
330 .set("supportsTeamDrives", "true")
331 .setType(type_of_permission
)
332 .setRole(role_level
);
333 //https://content.googleapis.com/drive/v3/files/140rDrFRHMozR_vScecNn7kZepyhYhIvR/permissions?supportsTeamDrives=true&alt=json&key=AIzaSyD-a9IF8KKYgoC3cpgS-Al7hLQDbugrDcw
334 //https://content.googleapis.com/drive/v3/files/1xdm8dubYeTO_WweyW57qsLun6aZTDYUi/permissions?supportsTeamDrives=true&alt=json&key=AIzaSyD-a9IF8KKYgoC3cpgS-Al7hLQDbugrDcw
337 userPermission
= new Permission()
338 .set("supportsTeamDrives", "true")
339 .setType(type_of_permission
)
341 .setEmailAddress(email_of_permission
);
344 service
.permissions().create(file
.getId(), userPermission
)
346 .queue(batch
, callback
);
347 return "Batch added";
350 private Object
[] buildDeletePermissionBatchRequest(){
351 JsonBatchCallback
<Permission
> callback
= new JsonBatchCallback
<Permission
>() {
353 public void onFailure(GoogleJsonError e
,
354 HttpHeaders responseHeaders
)
357 String
[] message
= {e
.getMessage(), "c"};
358 attatchment
.notifyObservers(message
);
359 System
.err
.println(e
.getMessage());
363 public void onSuccess(Permission permission
,
364 HttpHeaders responseHeaders
)
366 System
.out
.println("Permissions Removed");
367 String
[] message
= {"Permissions Removed", "c"};
368 attatchment
.notifyObservers(message
);
371 Object
[] obj
= {((Object
)service
.batch()), ((Object
)callback
)};
376 private String
addToDeletePermissionBatch(BatchRequest batch
, JsonBatchCallback
<Void
> callback
, File file
) throws IOException
{
377 PermissionList permissions
= service
.permissions().list(file
.getId()).execute();
379 for(Permission perm
: permissions
.getPermissions()){
380 System
.out
.println(perm
.toString());
381 if(!perm
.getRole().equalsIgnoreCase("owner")){
382 service
.permissions().delete(file
.getId(), perm
.getId())
383 .queue(batch
, callback
);
386 return "Batch added";
389 public String
authenticate(){
390 // Build a new authorized API client service.
392 request_transport
= GoogleNetHttpTransport
.newTrustedTransport();
393 service
= new Drive
.Builder(request_transport
, JSON_FACTORY
, getCredentials(request_transport
))
394 .setApplicationName(APPLICATION_NAME
)
396 System
.out
.println(service
.toString());
397 return "Successfully authenticated";
398 } catch (GeneralSecurityException
| IOException e
) {
399 // TODO Auto-generated catch block
400 return "Error: " + e
.getMessage();
404 private final String APPLICATION_NAME
= "Google Drive API Java Quickstart";
405 private final JsonFactory JSON_FACTORY
= JacksonFactory
.getDefaultInstance();
406 private final String CREDENTIALS_FOLDER
= "credentials"; // Directory to store user credentials.
409 * Global instance of the scopes required by this quickstart.
410 * If modifying these scopes, delete your previously saved credentials/ folder.
412 private final List
<String
> SCOPES
= Collections
.singletonList(DriveScopes
.DRIVE
);
413 private final String CLIENT_SECRET_DIR
= "client_secret.json";
416 * Creates an authorized Credential object.
417 * @param HTTP_TRANSPORT The network HTTP Transport.
418 * @return An authorized Credential object.
419 * @throws IOException If there is no client_secret.
421 private Credential
getCredentials(final NetHttpTransport HTTP_TRANSPORT
) throws IOException
{
422 // Load client secrets.
423 InputStream in
= DriveAPI
.class.getResourceAsStream(CLIENT_SECRET_DIR
);
424 GoogleClientSecrets clientSecrets
= GoogleClientSecrets
.load(JSON_FACTORY
, new InputStreamReader(in
));
426 // Build flow and trigger user authorization request.
427 GoogleAuthorizationCodeFlow flow
= new GoogleAuthorizationCodeFlow
.Builder(
428 HTTP_TRANSPORT
, JSON_FACTORY
, clientSecrets
, SCOPES
)
429 .setDataStoreFactory(new FileDataStoreFactory(new java
.io
.File(CREDENTIALS_FOLDER
)))
430 .setAccessType("offline")
432 return new AuthorizationCodeInstalledApp(flow
, new LocalServerReceiver()).authorize("user");