source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/remote/ZipCollectionConfigurations.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 5.4 KB
Line 
1package org.greenstone.gatherer.remote;
2
3import java.io.*;
4import java.util.zip.*;
5
6
7/**
8 */
9public class ZipCollectionConfigurations
10{
11 static public void main(String[] args)
12 {
13 if (args.length != 4) {
14 System.err.println("Usage: ZipCollectionConfigurations <zip-file> <collect-directory-path> <user-name> <user-groups>");
15 return;
16 }
17
18 String zip_file_path = args[0];
19 String collect_directory_path = args[1];
20 String user_name = args[2];
21 String user_groups = args[3];
22
23 if (!collect_directory_path.endsWith(File.separator)) {
24 collect_directory_path += File.separator;
25 }
26 File collect_directory = new File(collect_directory_path);
27
28 try {
29 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip_file_path));
30 ZipTools.ZipFilter collection_etc_zip_filter = new CollectionEtcZipFilter();
31 ZipTools.ZipFilter collection_metadata_zip_filter = new CollectionMetadataZipFilter();
32
33 addUserAccessibleCollections(collect_directory_path, "", collect_directory, zos,
34 collection_etc_zip_filter, collection_metadata_zip_filter,
35 user_name, user_groups);
36
37
38
39 zos.putNextEntry(new ZipEntry(".gli"));
40 zos.close();
41 }
42 catch (Exception exception) {
43 exception.printStackTrace();
44 }
45 }
46
47 static private void addUserAccessibleCollections(String toplevel_path, // remains unchanged
48 String relative_path, // grows with each level of recursion
49 File collect_directory, // current collection (group) directory being considered
50 ZipOutputStream zos,
51 ZipTools.ZipFilter collection_etc_zip_filter,
52 ZipTools.ZipFilter collection_metadata_zip_filter,
53 String user_name,
54 String user_groups)
55 {
56
57 if(!relative_path.equals("")) {
58 relative_path += File.separator;
59 }
60
61 // Add the etc and metadata directories from each of the collections the user has access to
62 File[] collection_directories = collect_directory.listFiles();
63 for (int i = 0; i < collection_directories.length; i++) {
64 if (collection_directories[i].isDirectory()) {
65 String collection_name = collection_directories[i].getName();
66 if (checkAccess(collection_name, user_name, user_groups)) {
67
68 String etc_relative_path = relative_path + collection_directories[i].getName() + File.separator + "etc";
69 ZipTools.addFileToZip(zos, toplevel_path, etc_relative_path, collection_etc_zip_filter);
70
71 // The "metadata" directory may not exist (non-GLI collections)
72 String metadata_relative_path = relative_path + collection_directories[i].getName() + File.separator + "metadata";
73 File metadata_directory = new File(toplevel_path + File.separator + metadata_relative_path);
74 if (metadata_directory.exists()) {
75 ZipTools.addFileToZip(zos, toplevel_path, metadata_relative_path, collection_metadata_zip_filter);
76 }
77 else { // if a collection contains no metadata directory, then check if it is a collectgroup
78
79 // read from collection config file in etc folder
80 File config_file = new File(collection_directories[i], "etc"+File.separator+"collect.cfg");
81 if(config_file.exists()) {
82 try {
83 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(config_file)));
84 String line = null;
85 while((line = reader.readLine()) != null) { // read lines and look for: collectgroup true
86 line = line.trim(); // need to ignore surrounding whitespace
87 if(line.startsWith("collectgroup") && line.endsWith("true")) {
88 // if collectgroup, then recurse on current folder
89 addUserAccessibleCollections(toplevel_path, relative_path+collection_directories[i].getName(), collection_directories[i],
90 zos, collection_etc_zip_filter, collection_metadata_zip_filter,
91 user_name, user_groups);
92 }
93 }
94
95 reader.close();
96 } catch(Exception e) {
97 System.err.println("Exception reading from file " + config_file + ": " + e);
98 e.printStackTrace();
99 }
100 }
101 }
102 }
103 }
104 }
105 }
106
107
108 static private boolean checkAccess(String collection_name, String user_name, String user_groups)
109 {
110 String[] user_groups_array = user_groups.split(",");
111 for (int i = 0; i < user_groups_array.length; i++) {
112 if (user_groups_array[i].equals("all-collections-editor")) {
113 return true;
114 }
115 if (user_groups_array[i].equals("personal-collections-editor") && collection_name.startsWith(user_name + "-")) {
116 return true;
117 }
118 if (user_groups_array[i].equals(collection_name + "-collection-editor")) {
119 return true;
120 }
121 }
122
123 return false;
124 }
125
126
127 static private class CollectionEtcZipFilter
128 extends ZipTools.NullZipFilter
129 {
130 public boolean shouldIncludeFile(String relative_file_path)
131 {
132 // Only collect.cfg files are included
133 if (relative_file_path.endsWith("collect.cfg")){
134 return (relative_file_path.equals("collect.cfg") || relative_file_path.endsWith(File.separator + "collect.cfg"));
135 }
136
137 // Only collectConfig.xml files are included
138 if (relative_file_path.endsWith("collectionConfig.xml")){
139 return (relative_file_path.equals("collectionConfig.xml") || relative_file_path.endsWith(File.separator + "collectionConfig.xml"));
140 }
141 return false;
142 }
143 }
144
145
146 static private class CollectionMetadataZipFilter
147 extends ZipTools.NullZipFilter
148 {
149 public boolean shouldIncludeFileContent(String relative_file_path)
150 {
151 // We don't include content for any of the metadata set files
152 return false;
153 }
154 }
155}
Note: See TracBrowser for help on using the repository browser.