1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.Serializable;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import org.apache.log4j.Logger;
|
---|
9 | import org.greenstone.gsdl3.util.GSXML;
|
---|
10 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
11 |
|
---|
12 | import org.w3c.dom.Document;
|
---|
13 | import org.w3c.dom.Element;
|
---|
14 |
|
---|
15 | public class ServiceUtil extends ServiceRack
|
---|
16 | {
|
---|
17 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ServiceUtil.class.getName());
|
---|
18 |
|
---|
19 | /**********************************************************
|
---|
20 | * The list of services the utility service rack supports *
|
---|
21 | *********************************************************/
|
---|
22 | protected static final String GET_ALL_IMAGES_IN_COLLECTION = "GetAllImagesInCollection";
|
---|
23 | /*********************************************************/
|
---|
24 |
|
---|
25 | String[] services = { GET_ALL_IMAGES_IN_COLLECTION };
|
---|
26 |
|
---|
27 | public boolean configure(Element info, Element extra_info)
|
---|
28 | {
|
---|
29 | if (!super.configure(info, extra_info))
|
---|
30 | {
|
---|
31 | return false;
|
---|
32 | }
|
---|
33 |
|
---|
34 | logger.info("Configuring ServiceUtil...");
|
---|
35 | this.config_info = info;
|
---|
36 |
|
---|
37 | for (int i = 0; i < services.length; i++)
|
---|
38 | {
|
---|
39 | Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
40 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
41 | service.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
42 | this.short_service_info.appendChild(service);
|
---|
43 | }
|
---|
44 |
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
|
---|
49 | {
|
---|
50 | for (int i = 0; i < services.length; i++)
|
---|
51 | {
|
---|
52 | if (service_id.equals(services[i]))
|
---|
53 | {
|
---|
54 | Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
55 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
56 | service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
57 | return service_elem;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | return null;
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected Element processGetAllImagesInCollection(Element request)
|
---|
65 | {
|
---|
66 | Document result_doc = XMLConverter.newDOM();
|
---|
67 | Element result = GSXML.createBasicResponse(result_doc, GET_ALL_IMAGES_IN_COLLECTION);
|
---|
68 |
|
---|
69 | if (request == null)
|
---|
70 | {
|
---|
71 | GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
72 | return result;
|
---|
73 | }
|
---|
74 |
|
---|
75 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
76 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
77 |
|
---|
78 | // Get the parameters of the request
|
---|
79 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
80 |
|
---|
81 | if (param_list == null) {
|
---|
82 | GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
83 | return result; // Return the empty result
|
---|
84 | }
|
---|
85 |
|
---|
86 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
87 |
|
---|
88 | String regex = (String)params.get("extregex");
|
---|
89 | if(regex == null)
|
---|
90 | {
|
---|
91 | GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": No file name extensions specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
92 | return result;
|
---|
93 | }
|
---|
94 | regex = ".*" + regex + ".*";
|
---|
95 |
|
---|
96 | String collection = (String)params.get("c");
|
---|
97 | if(collection == null)
|
---|
98 | {
|
---|
99 | GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": No collection specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
100 | return result;
|
---|
101 | }
|
---|
102 |
|
---|
103 | File indexDir = new File(this.site_home + File.separator + "collect" + File.separator + collection + File.separator + "index" + File.separator + "assoc");
|
---|
104 | ArrayList<String> images = new ArrayList<String>();
|
---|
105 | getImagesRecursive(indexDir, regex, images);
|
---|
106 |
|
---|
107 | Element imageListElem = result_doc.createElement("imageList");
|
---|
108 | result.appendChild(imageListElem);
|
---|
109 | for(String i : images)
|
---|
110 | {
|
---|
111 | Element imageElem = result_doc.createElement("image");
|
---|
112 | imageElem.appendChild(result_doc.createTextNode(i));
|
---|
113 | imageListElem.appendChild(imageElem);
|
---|
114 | }
|
---|
115 | return result;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /********************
|
---|
119 | * Helper functions *
|
---|
120 | *******************/
|
---|
121 |
|
---|
122 | protected void getImagesRecursive(File current, String regex, ArrayList<String> images)
|
---|
123 | {
|
---|
124 | if(current.isDirectory())
|
---|
125 | {
|
---|
126 | File[] files = current.listFiles();
|
---|
127 | for(File f : files)
|
---|
128 | {
|
---|
129 | getImagesRecursive(f, regex, images);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | else
|
---|
133 | {
|
---|
134 | String path = current.getAbsolutePath();
|
---|
135 | String filename = path.substring(path.lastIndexOf(File.separator) + File.separator.length());
|
---|
136 |
|
---|
137 | if(filename.matches(regex))
|
---|
138 | {
|
---|
139 | images.add(path);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 |
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | } |
---|