deletes permissions under drive ID
[Google-Drive-Permission-Set.git] / src / main / java / DriveAPI.java
bloba3336cad9e5fedfd93d992f2e16e4e70659a7d4a
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;
8 import java.util.List;
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;
41 String folder_to_set;
42 String role_level;
43 String parent_id;
44 String type_of_permission;
45 String email_of_permission;
47 int cooldown_time = 5000;
48 int batch_size = 35;
50 public DriveAPI(ConsoleModel cm){
51 this.attatchment = cm;
55 public void setParam(String ft, String rl, String pi, String tp, String ep){
56 folder_to_set = ft;
57 role_level = rl;
58 parent_id = pi;
59 type_of_permission = tp;
60 email_of_permission = ep;
63 @Override
64 public void run() {
65 if(folder_to_set != null)
66 setPermissions(folder_to_set, role_level, parent_id, type_of_permission, email_of_permission);
67 else
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.
75 FileList result;
78 String[] message1 = {"Getting all files of name " + folder_to_set + " to set root folder ID " + parent_id , "c"};
79 attatchment.notifyObservers(message1);
82 try {
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)
89 .setPageSize(1000)
90 .setOrderBy("createdTime")
91 .setFields("files(id, name, teamDriveId, mimeType, parents)")
92 .setQ("name=\"" + folder_to_set + "\"")
93 .execute();
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";
103 } else {
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";
112 else{
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];
121 int counter = 0;
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);
127 int file_no = 0;
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) {
131 if(parent_id != ""){
132 String[] file_count = {total_files + " " + file.getId(), "l"};
133 attatchment.notifyObservers(file_count);
134 if(!checkParent_ids(file, parent_id)){
135 total_files--;
136 continue;
139 else{
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() ){}
149 batch.execute();
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);
156 counter = 0;
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){
167 batch.execute();
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
179 e.printStackTrace();
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)
189 .setPageSize(1000)
190 .setOrderBy("createdTime")
191 .setFields("files(id, name, teamDriveId, mimeType)")
192 //.setQ("\'" + folder_to_set +"\' in parents")
193 .setQ("\'" + parent_id + "\' in parents")
194 .execute();
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);
211 return files_final;
214 public String clearPermissions(String parent_id){
215 String[] message1 = {"Removing permission on all files higher than " + parent_id , "c"};
216 attatchment.notifyObservers(message1);
220 try {
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];
235 int counter = 0;
236 int batch_counter = 0;
237 int file_no = 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() ){}
248 batch.execute();
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);
255 counter = 0;
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){
266 batch.execute();
268 String[] message6 = {total_files + " / " + total_files, "l"};
269 attatchment.notifyObservers(message6);
271 catch (IOException e){
272 e.printStackTrace();
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");
288 return true;
290 else{
291 File results = service.files().get((String) ((ArrayList)file.get("parents")).get(0)).setFields("parents").execute();
292 return checkParent_ids(results, parent_id);
295 else return false;
298 private Object[] buildSetPermissionBatchRequest(){
299 JsonBatchCallback<Permission> callback = new JsonBatchCallback<Permission>() {
300 @Override
301 public void onFailure(GoogleJsonError e,
302 HttpHeaders responseHeaders)
303 throws IOException {
304 // Handle error
305 String[] message = {e.getMessage(), "c"};
306 attatchment.notifyObservers(message);
307 System.err.println(e.getMessage());
310 @Override
311 public void onSuccess(Permission permission,
312 HttpHeaders responseHeaders)
313 throws IOException {
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)};
320 return obj;
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
336 else{
337 userPermission = new Permission()
338 .set("supportsTeamDrives", "true")
339 .setType(type_of_permission)
340 .setRole(role_level)
341 .setEmailAddress(email_of_permission);
344 service.permissions().create(file.getId(), userPermission)
345 .setFields("id")
346 .queue(batch, callback);
347 return "Batch added";
350 private Object[] buildDeletePermissionBatchRequest(){
351 JsonBatchCallback<Permission> callback = new JsonBatchCallback<Permission>() {
352 @Override
353 public void onFailure(GoogleJsonError e,
354 HttpHeaders responseHeaders)
355 throws IOException {
356 // Handle error
357 String[] message = {e.getMessage(), "c"};
358 attatchment.notifyObservers(message);
359 System.err.println(e.getMessage());
362 @Override
363 public void onSuccess(Permission permission,
364 HttpHeaders responseHeaders)
365 throws IOException {
366 System.out.println("Permissions Removed");
367 String[] message = {"Permissions Removed", "c"};
368 attatchment.notifyObservers(message);
371 Object[] obj = {((Object)service.batch()), ((Object)callback)};
372 return obj;
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.
391 try {
392 request_transport = GoogleNetHttpTransport.newTrustedTransport();
393 service = new Drive.Builder(request_transport, JSON_FACTORY, getCredentials(request_transport))
394 .setApplicationName(APPLICATION_NAME)
395 .build();
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")
431 .build();
432 return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");