1 | /*
|
---|
2 | * GSParams.java
|
---|
3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.ArrayList;
|
---|
23 |
|
---|
24 | import org.apache.log4j.Logger;
|
---|
25 |
|
---|
26 | /** keeps track of the servlet parameters, and their defaults */
|
---|
27 | public class GSParams
|
---|
28 | {
|
---|
29 |
|
---|
30 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSParams.class.getName());
|
---|
31 |
|
---|
32 | // Only used internally in the session
|
---|
33 | public static final String USER_SESSION_CACHE = "user_session_cache";
|
---|
34 |
|
---|
35 | // cgi parameter names
|
---|
36 | public static final String ACTION = "a"; // the major type of action- eg query or browse or process
|
---|
37 | public static final String SUBACTION = "sa"; // subtype of action if we want different processing than the default
|
---|
38 | public static final String REQUEST_TYPE = "rt"; // whether the request is just to display the service form, or to actually do a request to the service
|
---|
39 | public static final String RESPONSE_ONLY = "ro"; // if == 1 do the request and pass back the response xml - no page formatting
|
---|
40 | public static final String OUTPUT = "o"; // if processing is to be done, what type of output - html/xml/other??
|
---|
41 | public static final String SERVICE = "s"; // the name of the service
|
---|
42 |
|
---|
43 | public static final String CACHE_KEY = "ck"; // if we want to use another cache key apart from the collection name
|
---|
44 | public static final String UN = "un"; // username for authenticated-ping
|
---|
45 | public static final String PW = "pw"; // pwd for authenticated-ping
|
---|
46 |
|
---|
47 | public static final String CLUSTER = "c"; // these two are the same
|
---|
48 | public static final String COLLECTION = "c";
|
---|
49 | public static final String COLLECTION_TYPE = "ct"; // collection type - mg, mgpp, lucene etc
|
---|
50 | public static final String GROUP = "group";
|
---|
51 | public static final String LANGUAGE = "l";
|
---|
52 | public static final String DOCUMENT = "d";
|
---|
53 | public static final String DOCUMENT_TYPE = "dt";
|
---|
54 | public static final String HREF = "href"; // url. might be an external url, or a relative one that needs translating
|
---|
55 | public static final String RELATIVE_LINK = "rl"; // whether the href url is relative to the collection or not.
|
---|
56 | public static final String EXTERNAL_LINK_TYPE = "el"; // for an external link, go direct to the page or frame it in the collection
|
---|
57 | public static final String PROCESS_ID = "pid"; // if a request wasn't completed, this identifies the request - used when asking for a status update
|
---|
58 |
|
---|
59 | public static final String HTTP_HEADER_FIELDS = "hhf";
|
---|
60 |
|
---|
61 | public static final String BERRYBASKET = "berrybasket";
|
---|
62 | public static final String FAVOURITEBASKET = "favouritebasket";
|
---|
63 | // internal configure args
|
---|
64 | public static final String SYSTEM_SUBSET = "ss";
|
---|
65 | public static final String SYSTEM_CLUSTER = "sc";
|
---|
66 | public static final String SYSTEM_MODULE_NAME = "sn";
|
---|
67 | public static final String SYSTEM_MODULE_TYPE = "st";
|
---|
68 |
|
---|
69 | // used for filtering out a piece of the final page
|
---|
70 | public static final String EXCERPT_ID = "excerptid";
|
---|
71 | public static final String EXCERPT_TAG = "excerpttag";
|
---|
72 |
|
---|
73 | public static final String INLINE_TEMPLATE = "ilt";
|
---|
74 | public static final String FILE_LOCATION = "fl";
|
---|
75 |
|
---|
76 | //Administration
|
---|
77 | public static final String PASSWORD = "password";
|
---|
78 | public static final String USERNAME = "username";
|
---|
79 | public static final String LOGOUT = "logout";
|
---|
80 | public static final String VERIFIED = "hmvf";
|
---|
81 |
|
---|
82 | // some standard arg values
|
---|
83 | public static final String SYSTEM_ACTION = "s";
|
---|
84 |
|
---|
85 | // rss feeds
|
---|
86 | public static final String RSS_ACTION = "rss";
|
---|
87 |
|
---|
88 | public static final String EXTERNAL_LINK_TYPE_DIRECT = "direct";
|
---|
89 | public static final String EXTERNAL_LINK_TYPE_FRAMED = "frame";
|
---|
90 |
|
---|
91 | public static final String DEBUG = "debug";
|
---|
92 |
|
---|
93 | public static final String SERVICE_PREFIX = "s1";
|
---|
94 | public static final String PREVIOUS_PREFIX = "p";
|
---|
95 | public static final String MD_PREFIX = "md___";
|
---|
96 |
|
---|
97 | public static final String SERVICE_PARAM_REGEX = "^s\\d\\..*";
|
---|
98 |
|
---|
99 | protected HashMap<String, Param> param_map = null;
|
---|
100 | protected HashMap<String, Param> service_param_map = null;
|
---|
101 |
|
---|
102 | protected ArrayList<String> params_with_default_list = null;
|
---|
103 |
|
---|
104 | public GSParams()
|
---|
105 | {
|
---|
106 | this.param_map = new HashMap<String, Param>(30);
|
---|
107 | this.service_param_map = new HashMap<String, Param>(30);
|
---|
108 | this.params_with_default_list = new ArrayList<String>(10);
|
---|
109 |
|
---|
110 | // we now only need to add in the ones that need saving, as we will default to "not save"
|
---|
111 | // these ones are global params - apply across all collections
|
---|
112 | addParameter(LANGUAGE, true, false, true);
|
---|
113 | addParameter(BERRYBASKET, true, false, true);
|
---|
114 | addParameter(FAVOURITEBASKET, true, false, true);
|
---|
115 | addParameter(DEBUG, true, false, true);
|
---|
116 |
|
---|
117 | // these will only be saved per collection/service
|
---|
118 | addParameter(DOCUMENT, true);
|
---|
119 | addParameter(PROCESS_ID, true);
|
---|
120 | addParameter(COLLECTION_TYPE, true);
|
---|
121 |
|
---|
122 | // password is sensitive. don't save, but also don't return it in the page response
|
---|
123 | addParameter(PASSWORD, false, true);
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | public boolean addParameter(String name, boolean save)
|
---|
128 | {
|
---|
129 | return addParameter(name, "", save);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public boolean addParameter(String name, boolean save, boolean sensitive) {
|
---|
133 | if (this.param_map.containsKey(name))
|
---|
134 | {
|
---|
135 | // already there so could not add
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 | this.param_map.put(name, new Param("", save, sensitive));
|
---|
139 | return true;
|
---|
140 | }
|
---|
141 | public boolean addParameter(String name, String default_value, boolean save)
|
---|
142 | {
|
---|
143 | if (this.param_map.containsKey(name))
|
---|
144 | {
|
---|
145 | // already there so could not add
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 |
|
---|
149 | this.param_map.put(name, new Param(default_value, save));
|
---|
150 | if (default_value != null && !default_value.equals("")) {
|
---|
151 | this.params_with_default_list.add(name);
|
---|
152 | }
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 | public boolean addParameter(String name, boolean save, boolean sensitive, boolean global)
|
---|
156 | {
|
---|
157 | if (this.param_map.containsKey(name))
|
---|
158 | {
|
---|
159 | // already there so could not add
|
---|
160 | return false;
|
---|
161 | }
|
---|
162 | this.param_map.put(name, new Param("", save, sensitive, global));
|
---|
163 | return true;
|
---|
164 |
|
---|
165 | }
|
---|
166 |
|
---|
167 | public boolean addServiceParameter(String name, String default_value, boolean save, boolean sensitive) {
|
---|
168 | if (this.service_param_map.containsKey(name)) {
|
---|
169 | // already there, could not add
|
---|
170 | return false;
|
---|
171 | }
|
---|
172 | this.service_param_map.put(name, new Param(default_value, save, sensitive));
|
---|
173 | return true;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | public boolean setParamDefault(String name, String default_value)
|
---|
178 | {
|
---|
179 | Param p = this.param_map.get(name);
|
---|
180 | if (p == null) {
|
---|
181 | addParameter(name, default_value, true);
|
---|
182 | }else {
|
---|
183 | p.default_value = default_value;
|
---|
184 | }
|
---|
185 | if (!this.params_with_default_list.contains(name)) {
|
---|
186 | this.params_with_default_list.add(name);
|
---|
187 | }
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|
191 | public String getParamDefault(String name) {
|
---|
192 | Param p = this.param_map.get(name);
|
---|
193 | if (p==null) {
|
---|
194 | return null;
|
---|
195 | }
|
---|
196 | return p.default_value;
|
---|
197 |
|
---|
198 | }
|
---|
199 | public boolean shouldSave(String name)
|
---|
200 | {
|
---|
201 | // p. is used to store previous settings
|
---|
202 | if (name.startsWith(PREVIOUS_PREFIX+".") || name.startsWith(MD_PREFIX))
|
---|
203 | return false;
|
---|
204 | Param p;
|
---|
205 | if (name.matches(SERVICE_PARAM_REGEX)) {
|
---|
206 | // its a service param
|
---|
207 | p = this.service_param_map.get(name.substring(3));
|
---|
208 | } else {
|
---|
209 | // an ordinary param
|
---|
210 | p = this.param_map.get(name);
|
---|
211 | }
|
---|
212 | if (p==null) {
|
---|
213 | return false; // never save unknown params
|
---|
214 | }
|
---|
215 | return p.save;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | public boolean isSensitive(String name) {
|
---|
220 | Param p;
|
---|
221 | if (name.matches(SERVICE_PARAM_REGEX)) {
|
---|
222 | p = this.service_param_map.get(name.substring(3));
|
---|
223 | } else {
|
---|
224 | p = this.param_map.get(name);
|
---|
225 | }
|
---|
226 | if (p==null) {
|
---|
227 | return false;
|
---|
228 | }
|
---|
229 | return p.sensitive;
|
---|
230 | }
|
---|
231 |
|
---|
232 | public boolean isGlobal(String name) {
|
---|
233 | Param p = this.param_map.get(name);
|
---|
234 | if (p==null) {
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 | return p.is_global;
|
---|
238 | }
|
---|
239 |
|
---|
240 | public ArrayList<String> getParamsWithDefaults() {
|
---|
241 | return (ArrayList<String> )this.params_with_default_list.clone();
|
---|
242 | }
|
---|
243 |
|
---|
244 | private class Param
|
---|
245 | {
|
---|
246 |
|
---|
247 | public String default_value = null;
|
---|
248 | public boolean save = true;
|
---|
249 | public boolean sensitive = false;
|
---|
250 | public boolean is_global = false;
|
---|
251 |
|
---|
252 | public Param(String default_value, boolean save)
|
---|
253 | {
|
---|
254 | this.default_value = default_value;
|
---|
255 | this.save = save;
|
---|
256 | }
|
---|
257 |
|
---|
258 | public Param(String default_value, boolean save, boolean sensitive) {
|
---|
259 | this.default_value = default_value;
|
---|
260 | this.save = save;
|
---|
261 | this.sensitive = sensitive;
|
---|
262 | }
|
---|
263 |
|
---|
264 | public Param(String default_value, boolean save, boolean sensitive, boolean global) {
|
---|
265 | this.default_value = default_value;
|
---|
266 | this.save = save;
|
---|
267 | this.sensitive = sensitive;
|
---|
268 | this.is_global = global;
|
---|
269 | }
|
---|
270 |
|
---|
271 | }
|
---|
272 | }
|
---|