source: gli/trunk/src/org/greenstone/gatherer/remote/ZipCollectionConfigurations.java@ 14306

Last change on this file since 14306 was 14306, checked in by qq6, 17 years ago

zip collectionConfig.xml for a remote GS3

  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 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 // Add the etc and metadata directories from each of the collections the user has access to
34 File[] collection_directories = collect_directory.listFiles();
35 for (int i = 0; i < collection_directories.length; i++) {
36 if (collection_directories[i].isDirectory()) {
37 String collection_name = collection_directories[i].getName();
38 if (checkAccess(collection_name, user_name, user_groups)) {
39 String etc_relative_path = collection_directories[i].getName() + File.separator + "etc";
40 ZipTools.addFileToZip(zos, collect_directory_path, etc_relative_path, collection_etc_zip_filter);
41
42 // The "metadata" directory may not exist (non-GLI collections)
43 String metadata_relative_path = collection_directories[i].getName() + File.separator + "metadata";
44 File metadata_directory = new File(collect_directory_path + File.separator + metadata_relative_path);
45 if (metadata_directory.exists()) {
46 ZipTools.addFileToZip(zos, collect_directory_path, metadata_relative_path, collection_metadata_zip_filter);
47 }
48 }
49 }
50 }
51
52 zos.putNextEntry(new ZipEntry(".gli"));
53 zos.close();
54 }
55 catch (Exception exception) {
56 exception.printStackTrace();
57 }
58 }
59
60
61 static private boolean checkAccess(String collection_name, String user_name, String user_groups)
62 {
63 String[] user_groups_array = user_groups.split(",");
64 for (int i = 0; i < user_groups_array.length; i++) {
65 if (user_groups_array[i].equals("all-collections-editor")) {
66 return true;
67 }
68 if (user_groups_array[i].equals("personal-collections-editor") && collection_name.startsWith(user_name + "-")) {
69 return true;
70 }
71 if (user_groups_array[i].equals(collection_name + "-collection-editor")) {
72 return true;
73 }
74 }
75
76 return false;
77 }
78
79
80 static private class CollectionEtcZipFilter
81 extends ZipTools.NullZipFilter
82 {
83 public boolean shouldIncludeFile(String relative_file_path)
84 {
85 // Only collect.cfg files are included
86 if (relative_file_path.endsWith("collect.cfg")){
87 return (relative_file_path.equals("collect.cfg") || relative_file_path.endsWith(File.separator + "collect.cfg"));
88 }
89
90 // Only collectConfig.xml files are included
91 if (relative_file_path.endsWith("collectionConfig.xml")){
92 return (relative_file_path.equals("collectionConfig.xml") || relative_file_path.endsWith(File.separator + "collectionConfig.xml"));
93 }
94 return false;
95 }
96 }
97
98
99 static private class CollectionMetadataZipFilter
100 extends ZipTools.NullZipFilter
101 {
102 public boolean shouldIncludeFileContent(String relative_file_path)
103 {
104 // We don't include content for any of the metadata set files
105 return false;
106 }
107 }
108}
Note: See TracBrowser for help on using the repository browser.