source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/ServiceUtil.java@ 24565

Last change on this file since 24565 was 24565, checked in by sjm84, 13 years ago

Adding in a place for random services called ServiceUtil

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