1 |
51
|
jones
|
/**
|
2 |
203
|
jones
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements utility methods for a metadata catalog
|
4 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
309
|
bojilova
|
* Authors: Matt Jones, Jivka Bojilova
|
7 |
1538
|
berkley
|
*
|
8 |
203
|
jones
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
669
|
jones
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
51
|
jones
|
*/
|
26 |
|
|
|
27 |
|
|
package edu.ucsb.nceas.metacat;
|
28 |
|
|
|
29 |
185
|
jones
|
import java.io.File;
|
30 |
3269
|
tao
|
import java.io.FileOutputStream;
|
31 |
|
|
import java.io.FileWriter;
|
32 |
|
|
import java.io.PrintWriter;
|
33 |
|
|
import java.io.StringReader;
|
34 |
2068
|
jones
|
import java.net.MalformedURLException;
|
35 |
185
|
jones
|
import java.net.URL;
|
36 |
3269
|
tao
|
import java.text.SimpleDateFormat;
|
37 |
2893
|
sgarg
|
import java.util.HashMap;
|
38 |
309
|
bojilova
|
import java.util.Hashtable;
|
39 |
1545
|
tao
|
import java.util.Stack;
|
40 |
887
|
berkley
|
import java.util.Vector;
|
41 |
2558
|
sgarg
|
import java.util.regex.PatternSyntaxException;
|
42 |
50
|
jones
|
|
43 |
2591
|
sgarg
|
import org.apache.log4j.Logger;
|
44 |
|
|
|
45 |
777
|
bojilova
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
46 |
1951
|
jones
|
import edu.ucsb.nceas.utilities.Options;
|
47 |
747
|
bojilova
|
|
48 |
50
|
jones
|
/**
|
49 |
|
|
* A suite of utility classes for the metadata catalog server
|
50 |
|
|
*/
|
51 |
2068
|
jones
|
public class MetaCatUtil
|
52 |
|
|
{
|
53 |
50
|
jones
|
|
54 |
2068
|
jones
|
public static AbstractDatabase dbAdapter;
|
55 |
109
|
bojilova
|
|
56 |
2521
|
sgarg
|
public static Vector pathsForIndexing;
|
57 |
2893
|
sgarg
|
|
58 |
|
|
public static HashMap skinconfigs = new HashMap();
|
59 |
2521
|
sgarg
|
|
60 |
3034
|
perry
|
private static edu.ucsb.nceas.utilities.Options options = null;
|
61 |
747
|
bojilova
|
|
62 |
2068
|
jones
|
private static boolean debug = true;
|
63 |
2558
|
sgarg
|
|
64 |
|
|
private static String[] administrators;
|
65 |
|
|
|
66 |
|
|
private static String[] moderators;
|
67 |
2068
|
jones
|
|
68 |
2576
|
sgarg
|
private static String[] allowedSubmitters;
|
69 |
|
|
|
70 |
|
|
private static String[] deniedSubmitters;
|
71 |
2665
|
sgarg
|
|
72 |
|
|
private static Logger logMetacat = Logger.getLogger(MetaCatUtil.class);
|
73 |
2576
|
sgarg
|
|
74 |
2068
|
jones
|
static {
|
75 |
2591
|
sgarg
|
// Determine our db adapter class and create an instance of that class
|
76 |
2068
|
jones
|
try {
|
77 |
|
|
dbAdapter = (AbstractDatabase) createObject(getOption("dbAdapter"));
|
78 |
|
|
} catch (Exception e) {
|
79 |
|
|
System.err.println("Error in MetaCatUtil static block:"
|
80 |
|
|
+ e.getMessage());
|
81 |
3034
|
perry
|
e.printStackTrace();
|
82 |
2068
|
jones
|
}
|
83 |
2558
|
sgarg
|
|
84 |
|
|
// read administrator and moderator lists from metacat.properties
|
85 |
2576
|
sgarg
|
getUserAccessControlLists();
|
86 |
747
|
bojilova
|
}
|
87 |
2665
|
sgarg
|
|
88 |
2068
|
jones
|
/**
|
89 |
|
|
* Instantiate a class using the name of the class at runtime
|
90 |
2165
|
tao
|
*
|
91 |
2068
|
jones
|
* @param className the fully qualified name of the class to instantiate
|
92 |
|
|
*/
|
93 |
|
|
public static Object createObject(String className) throws Exception
|
94 |
|
|
{
|
95 |
747
|
bojilova
|
|
96 |
2068
|
jones
|
Object object = null;
|
97 |
|
|
try {
|
98 |
|
|
Class classDefinition = Class.forName(className);
|
99 |
|
|
object = classDefinition.newInstance();
|
100 |
|
|
} catch (InstantiationException e) {
|
101 |
|
|
throw e;
|
102 |
|
|
} catch (IllegalAccessException e) {
|
103 |
|
|
throw e;
|
104 |
|
|
} catch (ClassNotFoundException e) {
|
105 |
|
|
throw e;
|
106 |
|
|
}
|
107 |
|
|
return object;
|
108 |
|
|
}
|
109 |
1217
|
tao
|
|
110 |
2068
|
jones
|
/**
|
111 |
|
|
* Utility method to get an option value from the properties file
|
112 |
2165
|
tao
|
*
|
113 |
2068
|
jones
|
* @param optionName the name of the option requested
|
114 |
|
|
* @return the String value for the option, or null if not set
|
115 |
|
|
*/
|
116 |
|
|
public static String getOption(String optionName)
|
117 |
|
|
{
|
118 |
|
|
if (options == null) {
|
119 |
3034
|
perry
|
options = edu.ucsb.nceas.utilities.Options.getInstance();
|
120 |
2068
|
jones
|
}
|
121 |
3034
|
perry
|
if (options == null) {
|
122 |
3078
|
jones
|
Logger logMetacat = Logger.getLogger(MetaCatUtil.class);
|
123 |
|
|
logMetacat.info("options is null");
|
124 |
3034
|
perry
|
}
|
125 |
2068
|
jones
|
String value = options.getOption(optionName);
|
126 |
|
|
return value;
|
127 |
|
|
}
|
128 |
2554
|
tao
|
|
129 |
|
|
/**
|
130 |
|
|
* Utility method to set an option value from the properties file
|
131 |
|
|
*
|
132 |
|
|
* @param optionName the name of the option requested
|
133 |
|
|
*/
|
134 |
|
|
public static void setOption(String optionName, String newValue)
|
135 |
|
|
{
|
136 |
|
|
if (options == null) {
|
137 |
3034
|
perry
|
options = edu.ucsb.nceas.utilities.Options.getInstance();
|
138 |
2554
|
tao
|
}
|
139 |
|
|
options.setOption(optionName, newValue);
|
140 |
|
|
|
141 |
|
|
}
|
142 |
1538
|
berkley
|
|
143 |
2068
|
jones
|
/** Utility method to convert a file handle into a URL */
|
144 |
|
|
public static URL fileToURL(File file)
|
145 |
|
|
{
|
146 |
|
|
String path = file.getAbsolutePath();
|
147 |
|
|
String fSep = System.getProperty("file.separator");
|
148 |
|
|
if (fSep != null && fSep.length() == 1)
|
149 |
|
|
path = path.replace(fSep.charAt(0), '/');
|
150 |
|
|
if (path.length() > 0 && path.charAt(0) != '/') path = '/' + path;
|
151 |
|
|
try {
|
152 |
|
|
return new URL("file", null, path);
|
153 |
|
|
} catch (java.net.MalformedURLException e) {
|
154 |
|
|
/*
|
155 |
|
|
* According to the spec this could only happen if the file
|
156 |
|
|
*/
|
157 |
|
|
throw new Error("unexpected MalformedURLException");
|
158 |
|
|
}
|
159 |
747
|
bojilova
|
}
|
160 |
|
|
|
161 |
2068
|
jones
|
/**
|
162 |
|
|
* Utility method to parse the query part of a URL into parameters. This
|
163 |
|
|
* method assumes the format of the query par tof the url is an ampersand
|
164 |
|
|
* separated list of name/value pairs, with equal signs separating the name
|
165 |
|
|
* from the value (e.g., name=tom&zip=99801 ). Returns a has of the name
|
166 |
|
|
* value pairs, hashed on name.
|
167 |
|
|
*/
|
168 |
|
|
public static Hashtable parseQuery(String query)
|
169 |
|
|
throws MalformedURLException
|
170 |
|
|
{
|
171 |
|
|
String[][] params = new String[200][2];
|
172 |
|
|
Hashtable parameters = new Hashtable();
|
173 |
747
|
bojilova
|
|
174 |
2068
|
jones
|
String temp = "";
|
175 |
|
|
boolean ampflag = true;
|
176 |
|
|
boolean poundflag = false;
|
177 |
|
|
int arrcount = 0;
|
178 |
185
|
jones
|
|
179 |
2068
|
jones
|
if (query != null) {
|
180 |
|
|
for (int i = 0; i < query.length(); i++) {
|
181 |
566
|
jones
|
|
182 |
2068
|
jones
|
// go throught the remainder of the query one character at a
|
183 |
|
|
// time.
|
184 |
|
|
if (query.charAt(i) == '=') {
|
185 |
|
|
// if the current char is a # then the preceding should be
|
186 |
|
|
// a name
|
187 |
|
|
if (!poundflag && ampflag) {
|
188 |
|
|
params[arrcount][0] = temp.trim();
|
189 |
|
|
temp = "";
|
190 |
|
|
} else {
|
191 |
|
|
//if there are two #s or &s in a row throw an
|
192 |
|
|
// exception.
|
193 |
|
|
throw new MalformedURLException(
|
194 |
|
|
"metacatURL: Two parameter names "
|
195 |
|
|
+ "not allowed in sequence");
|
196 |
|
|
}
|
197 |
|
|
poundflag = true;
|
198 |
|
|
ampflag = false;
|
199 |
|
|
} else if (query.charAt(i) == '&' || i == query.length() - 1) {
|
200 |
|
|
//the text preceding the & should be the param value.
|
201 |
|
|
if (i == query.length() - 1) {
|
202 |
|
|
//if at the end of the string grab the last value and
|
203 |
|
|
// append it.
|
204 |
|
|
if (query.charAt(i) != '=') {
|
205 |
|
|
//ignore an extra & on the end of the string
|
206 |
|
|
temp += query.charAt(i);
|
207 |
|
|
}
|
208 |
|
|
}
|
209 |
566
|
jones
|
|
210 |
2068
|
jones
|
if (!ampflag && poundflag) {
|
211 |
|
|
params[arrcount][1] = temp.trim();
|
212 |
|
|
parameters
|
213 |
|
|
.put(params[arrcount][0], params[arrcount][1]);
|
214 |
|
|
temp = "";
|
215 |
|
|
arrcount++; //increment the array to the next row.
|
216 |
|
|
} else {
|
217 |
|
|
//if there are two =s or &s in a row through an
|
218 |
|
|
// exception
|
219 |
|
|
throw new MalformedURLException(
|
220 |
|
|
"metacatURL: Two parameter values "
|
221 |
|
|
+ "not allowed in sequence");
|
222 |
|
|
}
|
223 |
|
|
poundflag = false;
|
224 |
|
|
ampflag = true;
|
225 |
|
|
} else {
|
226 |
|
|
//get the next character in the string
|
227 |
|
|
temp += query.charAt(i);
|
228 |
|
|
}
|
229 |
731
|
bojilova
|
}
|
230 |
566
|
jones
|
}
|
231 |
2068
|
jones
|
return parameters;
|
232 |
566
|
jones
|
}
|
233 |
2665
|
sgarg
|
|
234 |
2068
|
jones
|
public static Vector getOptionList(String optiontext)
|
235 |
|
|
{
|
236 |
|
|
Vector optionsVector = new Vector();
|
237 |
|
|
if (optiontext.indexOf(",") == -1) {
|
238 |
|
|
optionsVector.addElement(optiontext);
|
239 |
|
|
return optionsVector;
|
240 |
|
|
}
|
241 |
1538
|
berkley
|
|
242 |
2068
|
jones
|
while (optiontext.indexOf(",") != -1) {
|
243 |
|
|
String s = optiontext.substring(0, optiontext.indexOf(","));
|
244 |
|
|
optionsVector.addElement(s.trim());
|
245 |
|
|
optiontext = optiontext.substring(optiontext.indexOf(",") + 1,
|
246 |
|
|
optiontext.length());
|
247 |
|
|
if (optiontext.indexOf(",") == -1) { //catch the last list entry
|
248 |
|
|
optionsVector.addElement(optiontext.trim());
|
249 |
|
|
}
|
250 |
|
|
}
|
251 |
|
|
return optionsVector;
|
252 |
887
|
berkley
|
}
|
253 |
1538
|
berkley
|
|
254 |
2068
|
jones
|
/** Normalizes the given string. Taken from configXML.java */
|
255 |
|
|
public static String normalize(String s)
|
256 |
887
|
berkley
|
{
|
257 |
2068
|
jones
|
StringBuffer str = new StringBuffer();
|
258 |
|
|
|
259 |
2339
|
sgarg
|
int len = (s != null) ? s.length() : 0;
|
260 |
|
|
for (int i = 0; i < len; i++) {
|
261 |
|
|
char ch = s.charAt(i);
|
262 |
|
|
switch (ch) {
|
263 |
|
|
case '<': {
|
264 |
|
|
str.append("<");
|
265 |
|
|
break;
|
266 |
|
|
}
|
267 |
|
|
case '>': {
|
268 |
|
|
str.append(">");
|
269 |
|
|
break;
|
270 |
|
|
}
|
271 |
|
|
case '&': {
|
272 |
2449
|
sgarg
|
/*
|
273 |
|
|
* patch provided by Johnoel Ancheta from U of Hawaii
|
274 |
|
|
*/
|
275 |
|
|
// check if & is for a character reference &#xnnnn;
|
276 |
|
|
if (i + 1 < len - 1 && s.charAt(i + 1) == '#') {
|
277 |
|
|
str.append("&#");
|
278 |
|
|
i += 2;
|
279 |
|
|
|
280 |
|
|
ch = s.charAt(i);
|
281 |
|
|
while (i < len && ch != ';') {
|
282 |
|
|
str.append(ch);
|
283 |
|
|
i++;
|
284 |
|
|
ch = s.charAt(i);
|
285 |
|
|
}
|
286 |
|
|
str.append(';');
|
287 |
2557
|
sgarg
|
} else
|
288 |
|
|
// check if & is in front of amp;
|
289 |
|
|
// (we dont yet check for other HTML 4.0 Character entities)
|
290 |
|
|
if (i + 4 < len -1 && s.charAt(i + 1) == 'a'
|
291 |
|
|
&& s.charAt(i + 2) == 'm'
|
292 |
|
|
&& s.charAt(i + 3) == 'p'
|
293 |
|
|
&& s.charAt(i + 4) == ';'){
|
294 |
2449
|
sgarg
|
str.append("&");
|
295 |
2997
|
sgarg
|
i += 4;
|
296 |
2557
|
sgarg
|
} else
|
297 |
|
|
str.append("&");
|
298 |
2449
|
sgarg
|
/////////
|
299 |
2339
|
sgarg
|
break;
|
300 |
|
|
}
|
301 |
3150
|
tao
|
case '"':
|
302 |
|
|
str.append(""");
|
303 |
|
|
break;
|
304 |
2449
|
sgarg
|
default: {
|
305 |
|
|
if ( (ch<128) && (ch>31) ) {
|
306 |
2339
|
sgarg
|
str.append(ch);
|
307 |
|
|
}
|
308 |
|
|
else if (ch<32) {
|
309 |
2449
|
sgarg
|
if (ch == 10) { // new line
|
310 |
2339
|
sgarg
|
str.append(ch);
|
311 |
|
|
}
|
312 |
2449
|
sgarg
|
if (ch == 13) { // carriage return
|
313 |
2339
|
sgarg
|
str.append(ch);
|
314 |
|
|
}
|
315 |
2449
|
sgarg
|
if (ch == 9) { // tab
|
316 |
2339
|
sgarg
|
str.append(ch);
|
317 |
|
|
}
|
318 |
|
|
// otherwise skip
|
319 |
|
|
}
|
320 |
|
|
else {
|
321 |
|
|
str.append("&#");
|
322 |
|
|
str.append(Integer.toString(ch));
|
323 |
|
|
str.append(';');
|
324 |
|
|
}
|
325 |
|
|
}
|
326 |
|
|
}
|
327 |
|
|
}
|
328 |
2437
|
sgarg
|
return str.toString();
|
329 |
887
|
berkley
|
}
|
330 |
1538
|
berkley
|
|
331 |
2068
|
jones
|
/**
|
332 |
|
|
* Get docid from online/url string
|
333 |
|
|
*/
|
334 |
|
|
public static String getDocIdWithRevFromOnlineURL(String url)
|
335 |
894
|
berkley
|
{
|
336 |
2068
|
jones
|
String docid = null;
|
337 |
|
|
String DOCID = "docid";
|
338 |
|
|
boolean find = false;
|
339 |
|
|
char limited = '&';
|
340 |
|
|
int count = 0; //keep track how many & was found
|
341 |
|
|
Vector list = new Vector();// keep index number for &
|
342 |
|
|
if (url == null) {
|
343 |
2665
|
sgarg
|
logMetacat.info("url is null and null will be returned");
|
344 |
2068
|
jones
|
return docid;
|
345 |
|
|
}
|
346 |
|
|
// the first element in list is 0
|
347 |
|
|
list.add(new Integer(0));
|
348 |
|
|
for (int i = 0; i < url.length(); i++) {
|
349 |
|
|
if (url.charAt(i) == limited) {
|
350 |
|
|
// count plus 1
|
351 |
|
|
count++;
|
352 |
|
|
list.add(new Integer(i));
|
353 |
|
|
// get substring beween two &
|
354 |
|
|
String str = url.substring(
|
355 |
|
|
((Integer) list.elementAt(count - 1)).intValue(), i);
|
356 |
2665
|
sgarg
|
logMetacat.info("substring between two & is: " + str);
|
357 |
2068
|
jones
|
//if the subString contains docid, we got it
|
358 |
|
|
if (str.indexOf(DOCID) != -1) {
|
359 |
|
|
//get index of '="
|
360 |
|
|
int start = getIndexForGivenChar(str, '=') + 1;
|
361 |
|
|
int end = str.length();
|
362 |
|
|
docid = str.substring(start, end);
|
363 |
|
|
find = true;
|
364 |
|
|
}//if
|
365 |
|
|
}//if
|
366 |
|
|
}//for
|
367 |
|
|
//if not find, we need check the subtring between the index of last &
|
368 |
|
|
// and
|
369 |
|
|
// the end of string
|
370 |
|
|
if (!find) {
|
371 |
2665
|
sgarg
|
logMetacat.info("Checking the last substring");
|
372 |
2068
|
jones
|
String str = url.substring(((Integer) list.elementAt(count))
|
373 |
|
|
.intValue() + 1, url.length());
|
374 |
2665
|
sgarg
|
logMetacat.info("Last substring is: " + str);
|
375 |
2068
|
jones
|
if (str.indexOf(DOCID) != -1) {
|
376 |
|
|
//get index of '="
|
377 |
|
|
int start = getIndexForGivenChar(str, '=') + 1;
|
378 |
|
|
int end = str.length();
|
379 |
|
|
docid = str.substring(start, end);
|
380 |
|
|
find = true;
|
381 |
|
|
}//if
|
382 |
|
|
}//if
|
383 |
2665
|
sgarg
|
logMetacat.info("The docid from online url is:" + docid);
|
384 |
2068
|
jones
|
return docid.trim();
|
385 |
894
|
berkley
|
}
|
386 |
2068
|
jones
|
|
387 |
2165
|
tao
|
|
388 |
|
|
/**
|
389 |
|
|
* Eocgorid identifier will look like: ecogrid://knb/tao.1.1
|
390 |
|
|
* The AccessionNumber tao.1.1 will be returned. If the given doesn't
|
391 |
|
|
* contains ecogrid, null will be returned.
|
392 |
|
|
* @param identifier String
|
393 |
|
|
* @return String
|
394 |
|
|
*/
|
395 |
|
|
public static String getAccessionNumberFromEcogridIdentifier(String identifier)
|
396 |
|
|
{
|
397 |
|
|
String accessionNumber = null;
|
398 |
|
|
if (identifier != null && identifier.startsWith(DBSAXHandler.ECOGRID))
|
399 |
|
|
{
|
400 |
|
|
// find the last "/" in identifier
|
401 |
|
|
int indexOfLastSlash = identifier.lastIndexOf("/");
|
402 |
|
|
int start = indexOfLastSlash+1;
|
403 |
|
|
int end = identifier.length();
|
404 |
|
|
accessionNumber = identifier.substring(start, end);
|
405 |
|
|
}
|
406 |
2665
|
sgarg
|
logMetacat.warn("The accession number from url is " +
|
407 |
|
|
accessionNumber);
|
408 |
2165
|
tao
|
return accessionNumber;
|
409 |
|
|
}
|
410 |
|
|
|
411 |
2068
|
jones
|
private static int getIndexForGivenChar(String str, char character)
|
412 |
1557
|
tao
|
{
|
413 |
2068
|
jones
|
int index = -1;
|
414 |
|
|
// make sure str is not null
|
415 |
|
|
if (str == null) {
|
416 |
2665
|
sgarg
|
logMetacat.info(
|
417 |
|
|
"The given str is null and -1 will be returned");
|
418 |
2068
|
jones
|
return index;
|
419 |
|
|
}
|
420 |
|
|
// got though the string
|
421 |
|
|
for (int i = 0; i < str.length(); i++) {
|
422 |
|
|
// find the first one then break the loop
|
423 |
|
|
if (str.charAt(i) == character) {
|
424 |
|
|
index = i;
|
425 |
|
|
break;
|
426 |
|
|
}//if
|
427 |
|
|
}//for
|
428 |
2665
|
sgarg
|
logMetacat.info("the index for char " + character + " is: "
|
429 |
|
|
+ index);
|
430 |
2068
|
jones
|
return index;
|
431 |
1557
|
tao
|
}
|
432 |
2068
|
jones
|
|
433 |
|
|
/**
|
434 |
|
|
* Utility method to get docid from a given string
|
435 |
2165
|
tao
|
*
|
436 |
2068
|
jones
|
* @param string, the given string should be these two format: 1) str1.str2
|
437 |
|
|
* in this case docid= str1.str2 2) str1.str2.str3, in this case
|
438 |
|
|
* docid =str1.str2
|
439 |
|
|
* @param the sperator char
|
440 |
|
|
*/
|
441 |
|
|
public static String getDocIdFromString(String str)
|
442 |
1557
|
tao
|
{
|
443 |
2068
|
jones
|
String docId = null;
|
444 |
|
|
if (str == null) {
|
445 |
2665
|
sgarg
|
logMetacat.info(
|
446 |
2068
|
jones
|
"The given str is null and null will be returned"
|
447 |
2665
|
sgarg
|
+ " in getDocIdfromString");
|
448 |
2068
|
jones
|
return docId;
|
449 |
|
|
} //make sure docid is not null
|
450 |
|
|
int dotNumber = 0;//count how many dots in given string
|
451 |
|
|
int indexOfLastDot = 0;
|
452 |
1538
|
berkley
|
|
453 |
2068
|
jones
|
//assume that seperator is one charactor string
|
454 |
|
|
char seperator = getOption("accNumSeparator").charAt(0);
|
455 |
1538
|
berkley
|
|
456 |
2068
|
jones
|
for (int i = 0; i < str.length(); i++) {
|
457 |
|
|
if (str.charAt(i) == seperator) {
|
458 |
|
|
dotNumber++;//count how many dots
|
459 |
|
|
indexOfLastDot = i;//keep the last dot postion
|
460 |
|
|
}
|
461 |
|
|
}//for
|
462 |
1538
|
berkley
|
|
463 |
2068
|
jones
|
//The string formatt is wrong, because it has more than two or less
|
464 |
|
|
// than
|
465 |
|
|
//one seperator
|
466 |
|
|
if (dotNumber > 2 || dotNumber < 1) {
|
467 |
|
|
docId = null;
|
468 |
|
|
} else if (dotNumber == 2) //the case for str1.str2.str3
|
469 |
|
|
{
|
470 |
|
|
docId = str.substring(0, indexOfLastDot);
|
471 |
|
|
} else if (dotNumber == 1) //the case for str1.str2
|
472 |
|
|
{
|
473 |
|
|
docId = str;
|
474 |
|
|
}
|
475 |
|
|
|
476 |
|
|
return docId;
|
477 |
|
|
}//getDocIdFromString
|
478 |
|
|
|
479 |
|
|
/**
|
480 |
|
|
* Utility method to get version number from a given string
|
481 |
2165
|
tao
|
*
|
482 |
2068
|
jones
|
* @param string, the given string should be these two format: 1)
|
483 |
|
|
* str1.str2(no version) version =-1; 2) str1.str2.str3, in this
|
484 |
|
|
* case version = str3; 3) other, vresion =-2
|
485 |
|
|
*/
|
486 |
|
|
public static int getVersionFromString(String str)
|
487 |
|
|
throws NumberFormatException
|
488 |
942
|
tao
|
{
|
489 |
2068
|
jones
|
int version = -1;
|
490 |
|
|
String versionString = null;
|
491 |
|
|
int dotNumber = 0;//count how many dots in given string
|
492 |
|
|
int indexOfLastDot = 0;
|
493 |
1538
|
berkley
|
|
494 |
2068
|
jones
|
//assume that seperator is one charactor string
|
495 |
|
|
char seperator = getOption("accNumSeparator").charAt(0);
|
496 |
942
|
tao
|
|
497 |
2068
|
jones
|
for (int i = 0; i < str.length(); i++) {
|
498 |
|
|
if (str.charAt(i) == seperator) {
|
499 |
|
|
dotNumber++;//count how many dots
|
500 |
|
|
indexOfLastDot = i;//keep the last dot postion
|
501 |
|
|
}
|
502 |
|
|
}//for
|
503 |
2045
|
tao
|
|
504 |
2068
|
jones
|
//The string formatt is wrong, because it has more than two or less
|
505 |
|
|
// than
|
506 |
|
|
//one seperator
|
507 |
|
|
if (dotNumber > 2 || dotNumber < 1) {
|
508 |
|
|
version = -2;
|
509 |
|
|
} else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1)))
|
510 |
|
|
//the case for str1.str2.str3
|
511 |
|
|
{
|
512 |
|
|
versionString = str.substring((indexOfLastDot + 1), str.length());
|
513 |
|
|
version = Integer.parseInt(versionString);
|
514 |
|
|
} else if (dotNumber == 1) //the case for str1.str2
|
515 |
|
|
{
|
516 |
|
|
version = -1;
|
517 |
|
|
}
|
518 |
1538
|
berkley
|
|
519 |
2068
|
jones
|
return version;
|
520 |
|
|
}//getVersionFromString
|
521 |
1538
|
berkley
|
|
522 |
2068
|
jones
|
/**
|
523 |
|
|
* Utility method to get version string from a given string
|
524 |
2165
|
tao
|
*
|
525 |
2068
|
jones
|
* @param string, the given string should be these two format: 1)
|
526 |
|
|
* str1.str2(no version) version=null; 2) str1.str2.str3, in
|
527 |
|
|
* this case version = str3; 3) other, vresion =null;
|
528 |
|
|
*/
|
529 |
|
|
public static String getRevisionStringFromString(String str)
|
530 |
|
|
throws NumberFormatException
|
531 |
942
|
tao
|
{
|
532 |
2068
|
jones
|
// String to store the version
|
533 |
|
|
String versionString = null;
|
534 |
|
|
int dotNumber = 0;//count how many dots in given string
|
535 |
|
|
int indexOfLastDot = 0;
|
536 |
1538
|
berkley
|
|
537 |
2068
|
jones
|
//assume that seperator is one charactor string
|
538 |
|
|
char seperator = getOption("accNumSeparator").charAt(0);
|
539 |
1538
|
berkley
|
|
540 |
2068
|
jones
|
for (int i = 0; i < str.length(); i++) {
|
541 |
|
|
if (str.charAt(i) == seperator) {
|
542 |
|
|
dotNumber++;//count how many dots
|
543 |
|
|
indexOfLastDot = i;//keep the last dot postion
|
544 |
|
|
}
|
545 |
|
|
}//for
|
546 |
1538
|
berkley
|
|
547 |
2068
|
jones
|
//The string formatt is wrong, because it has more than two or less
|
548 |
|
|
// than
|
549 |
|
|
//one seperator
|
550 |
|
|
if (dotNumber > 2 || dotNumber < 1) {
|
551 |
|
|
versionString = null;
|
552 |
|
|
} else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1))) {
|
553 |
|
|
//the case for str1.str2.str3
|
554 |
|
|
// indexOfLastDot != (str.length() -1) means get rid of str1.str2.
|
555 |
|
|
versionString = str.substring((indexOfLastDot + 1), str.length());
|
556 |
|
|
} else if (dotNumber == 1) //the case for str1.str2 or str1.str2.
|
557 |
|
|
{
|
558 |
|
|
versionString = null;
|
559 |
|
|
}
|
560 |
1538
|
berkley
|
|
561 |
2068
|
jones
|
return versionString;
|
562 |
|
|
}//getVersionFromString
|
563 |
1538
|
berkley
|
|
564 |
2068
|
jones
|
/**
|
565 |
|
|
* This method will get docid from an AccessionNumber. There is no
|
566 |
|
|
* assumption the accessnumber will be str1.str2.str3. It can be more. So
|
567 |
|
|
* we think the docid will be get rid of last part
|
568 |
|
|
*/
|
569 |
|
|
public static String getDocIdFromAccessionNumber(String accessionNumber)
|
570 |
1292
|
tao
|
{
|
571 |
2068
|
jones
|
String docid = null;
|
572 |
|
|
if (accessionNumber == null) { return docid; }
|
573 |
|
|
String seperator = getOption("accNumSeparator");
|
574 |
|
|
int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
|
575 |
|
|
docid = accessionNumber.substring(0, indexOfLastSeperator);
|
576 |
2665
|
sgarg
|
logMetacat.info("after parsing accessionnumber, docid is "
|
577 |
|
|
+ docid);
|
578 |
2068
|
jones
|
return docid;
|
579 |
|
|
}
|
580 |
1538
|
berkley
|
|
581 |
2068
|
jones
|
/**
|
582 |
2290
|
sgarg
|
* This method will get inline data id without the revision number.
|
583 |
|
|
* So if inlineData.1.2 is passed as input, inlineData.2 is returned.
|
584 |
|
|
*/
|
585 |
|
|
public static String getInlineDataIdWithoutRev(String accessionNumber)
|
586 |
|
|
{
|
587 |
|
|
String docid = null;
|
588 |
|
|
if (accessionNumber == null) { return docid; }
|
589 |
|
|
String seperator = getOption("accNumSeparator");
|
590 |
|
|
int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
|
591 |
|
|
String version = accessionNumber.substring(indexOfLastSeperator,
|
592 |
|
|
accessionNumber.length());
|
593 |
|
|
accessionNumber = accessionNumber.substring(0, indexOfLastSeperator);
|
594 |
|
|
indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
|
595 |
|
|
docid = accessionNumber.substring(0, indexOfLastSeperator) + version;
|
596 |
2665
|
sgarg
|
logMetacat.info("after parsing accessionnumber, docid is "
|
597 |
|
|
+ docid);
|
598 |
2290
|
sgarg
|
|
599 |
|
|
return docid;
|
600 |
|
|
}
|
601 |
|
|
|
602 |
|
|
/**
|
603 |
2068
|
jones
|
* This method will call both getDocIdFromString and
|
604 |
|
|
* getDocIdFromAccessionNumber. So first, if the string looks str1.str2,
|
605 |
|
|
* the docid will be str1.str2. If the string is str1.str2.str3, the docid
|
606 |
|
|
* will be str1.str2. If the string is str1.str2.str3.str4 or more, the
|
607 |
|
|
* docid will be str1.str2.str3. If the string look like str1, null will be
|
608 |
|
|
* returned
|
609 |
2165
|
tao
|
*
|
610 |
2068
|
jones
|
*/
|
611 |
|
|
public static String getSmartDocId(String str)
|
612 |
1292
|
tao
|
{
|
613 |
2068
|
jones
|
String docid = null;
|
614 |
|
|
//call geDocIdFromString first.
|
615 |
|
|
docid = getDocIdFromString(str);
|
616 |
|
|
// If docid is null, try to call getDocIdFromAccessionNumber
|
617 |
|
|
// it will handle the seperator more than2
|
618 |
|
|
if (docid == null) {
|
619 |
|
|
docid = getDocIdFromAccessionNumber(str);
|
620 |
|
|
}
|
621 |
2665
|
sgarg
|
logMetacat.info("The docid get from smart docid getor is "
|
622 |
|
|
+ docid);
|
623 |
2068
|
jones
|
return docid;
|
624 |
1292
|
tao
|
}
|
625 |
2068
|
jones
|
|
626 |
|
|
/**
|
627 |
|
|
* This method will get revision from an AccessionNumber. There is no
|
628 |
|
|
* assumption the accessnumber will be str1.str2.str3. It can be more. So
|
629 |
|
|
* we think the docid will be get rid of last part
|
630 |
|
|
*/
|
631 |
|
|
public static int getRevisionFromAccessionNumber(String accessionNumber)
|
632 |
|
|
throws NumberFormatException
|
633 |
1292
|
tao
|
{
|
634 |
2068
|
jones
|
String rev = null;
|
635 |
|
|
int revNumber = -1;
|
636 |
|
|
if (accessionNumber == null) { return revNumber; }
|
637 |
|
|
String seperator = getOption("accNumSeparator");
|
638 |
|
|
int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
|
639 |
|
|
rev = accessionNumber.substring(indexOfLastSeperator + 1,
|
640 |
|
|
accessionNumber.length());
|
641 |
|
|
revNumber = Integer.parseInt(rev);
|
642 |
2665
|
sgarg
|
logMetacat.info("after parsing accessionnumber, rev is "
|
643 |
|
|
+ revNumber);
|
644 |
2068
|
jones
|
return revNumber;
|
645 |
1292
|
tao
|
}
|
646 |
2068
|
jones
|
|
647 |
|
|
/**
|
648 |
|
|
* Method to get the name of local replication server
|
649 |
|
|
*/
|
650 |
|
|
public static String getLocalReplicationServerName()
|
651 |
1292
|
tao
|
{
|
652 |
2068
|
jones
|
String replicationServerName = null;
|
653 |
|
|
String serverHost = null;
|
654 |
|
|
serverHost = getOption("server");
|
655 |
|
|
// append "context/servelet/replication" to the host name
|
656 |
2574
|
tao
|
replicationServerName = serverHost.trim() + getOption("replicationpath").trim();
|
657 |
2068
|
jones
|
return replicationServerName;
|
658 |
|
|
|
659 |
1292
|
tao
|
}
|
660 |
1538
|
berkley
|
|
661 |
2068
|
jones
|
/**
|
662 |
|
|
* Method to get docidwithrev from eml2 inline data id The eml inline data
|
663 |
|
|
* id would look like eml.200.2.3
|
664 |
|
|
*/
|
665 |
|
|
public static String getDocIdWithoutRevFromInlineDataID(String inlineDataID)
|
666 |
2045
|
tao
|
{
|
667 |
2068
|
jones
|
String docidWithoutRev = null;
|
668 |
|
|
if (inlineDataID == null) { return docidWithoutRev; }
|
669 |
|
|
String seperator = MetaCatUtil.getOption("accNumSeparator");
|
670 |
|
|
char charSeperator = seperator.charAt(0);
|
671 |
|
|
int targetNumberOfSeperator = 2;// we want to know his index
|
672 |
|
|
int numberOfSeperator = 0;
|
673 |
|
|
for (int i = 0; i < inlineDataID.length(); i++) {
|
674 |
|
|
// meet seperator, increase number of seperator
|
675 |
|
|
if (inlineDataID.charAt(i) == charSeperator) {
|
676 |
|
|
numberOfSeperator++;
|
677 |
|
|
}
|
678 |
|
|
// if number of seperator reach the target one, record the index(i)
|
679 |
|
|
// and get substring and terminate the loop
|
680 |
|
|
if (numberOfSeperator == targetNumberOfSeperator) {
|
681 |
|
|
docidWithoutRev = inlineDataID.substring(0, i);
|
682 |
|
|
break;
|
683 |
|
|
}
|
684 |
|
|
}
|
685 |
1538
|
berkley
|
|
686 |
2665
|
sgarg
|
logMetacat.info("Docid without rev from inlinedata id: "
|
687 |
|
|
+ docidWithoutRev);
|
688 |
2068
|
jones
|
return docidWithoutRev;
|
689 |
1538
|
berkley
|
|
690 |
2068
|
jones
|
}
|
691 |
1538
|
berkley
|
|
692 |
2068
|
jones
|
/**
|
693 |
|
|
* Revise stack change a stack to opposite order
|
694 |
|
|
*/
|
695 |
|
|
public static Stack reviseStack(Stack stack)
|
696 |
|
|
{
|
697 |
|
|
Stack result = new Stack();
|
698 |
|
|
// make sure the parameter is correct
|
699 |
|
|
if (stack == null || stack.isEmpty()) {
|
700 |
|
|
result = stack;
|
701 |
|
|
return result;
|
702 |
|
|
}
|
703 |
1538
|
berkley
|
|
704 |
2068
|
jones
|
while (!stack.isEmpty()) {
|
705 |
|
|
Object obj = stack.pop();
|
706 |
|
|
result.push(obj);
|
707 |
|
|
}
|
708 |
|
|
return result;
|
709 |
1598
|
tao
|
}
|
710 |
1538
|
berkley
|
|
711 |
2068
|
jones
|
/** A method to replace whitespace in url */
|
712 |
|
|
public static String replaceWhiteSpaceForURL(String urlHasWhiteSpace)
|
713 |
1598
|
tao
|
{
|
714 |
2068
|
jones
|
StringBuffer newUrl = new StringBuffer();
|
715 |
|
|
String whiteSpaceReplace = "%20";
|
716 |
|
|
if (urlHasWhiteSpace == null || urlHasWhiteSpace.trim().equals("")) { return null; }
|
717 |
1598
|
tao
|
|
718 |
2068
|
jones
|
for (int i = 0; i < urlHasWhiteSpace.length(); i++) {
|
719 |
|
|
char ch = urlHasWhiteSpace.charAt(i);
|
720 |
|
|
if (!Character.isWhitespace(ch)) {
|
721 |
|
|
newUrl.append(ch);
|
722 |
|
|
} else {
|
723 |
|
|
//it is white sapce, replace it by %20
|
724 |
|
|
newUrl = newUrl.append(whiteSpaceReplace);
|
725 |
|
|
}
|
726 |
1598
|
tao
|
|
727 |
2068
|
jones
|
}//for
|
728 |
2665
|
sgarg
|
logMetacat.info("The new string without space is:"
|
729 |
|
|
+ newUrl.toString());
|
730 |
2068
|
jones
|
return newUrl.toString();
|
731 |
1598
|
tao
|
|
732 |
2068
|
jones
|
}// replaceWhiteSpaceForUR
|
733 |
|
|
|
734 |
2558
|
sgarg
|
/**
|
735 |
|
|
* A method to read administrators and moderators list from the metacat.properties
|
736 |
|
|
**/
|
737 |
2576
|
sgarg
|
public static void getUserAccessControlLists(){
|
738 |
|
|
administrators = getListFromOption("administrators");
|
739 |
|
|
moderators = getListFromOption("moderators");
|
740 |
|
|
allowedSubmitters = getListFromOption("allowedSubmitters");
|
741 |
|
|
deniedSubmitters = getListFromOption("deniedSubmitters");
|
742 |
|
|
}
|
743 |
|
|
|
744 |
|
|
/**
|
745 |
|
|
* A method to read value of a given option from the metacat.properties
|
746 |
|
|
* into specified String array
|
747 |
|
|
**/
|
748 |
|
|
private static String[] getListFromOption(String optionName){
|
749 |
|
|
String[] list = null;
|
750 |
|
|
String listString = MetaCatUtil.getOption(optionName);
|
751 |
|
|
|
752 |
2558
|
sgarg
|
try {
|
753 |
2576
|
sgarg
|
if ( listString != null && !listString.trim().equals("")) {
|
754 |
|
|
list = listString.split(":");
|
755 |
|
|
} else {
|
756 |
|
|
list = null;
|
757 |
2566
|
tao
|
}
|
758 |
|
|
|
759 |
2558
|
sgarg
|
} catch (PatternSyntaxException pse) {
|
760 |
2576
|
sgarg
|
list = null;
|
761 |
2665
|
sgarg
|
logMetacat.error("Error in MetacatServlet.init: "
|
762 |
|
|
+ pse.getMessage());
|
763 |
2558
|
sgarg
|
}
|
764 |
2576
|
sgarg
|
return list;
|
765 |
2558
|
sgarg
|
}
|
766 |
2576
|
sgarg
|
|
767 |
2558
|
sgarg
|
/**
|
768 |
2576
|
sgarg
|
* A method to check if the specified user is part of the moderators list
|
769 |
2558
|
sgarg
|
**/
|
770 |
2576
|
sgarg
|
private static boolean onList(String list[], String username, String[] groups){
|
771 |
|
|
|
772 |
|
|
if(list == null){
|
773 |
|
|
return false;
|
774 |
|
|
}
|
775 |
|
|
|
776 |
|
|
// Check that the user is authenticated as an administrator account
|
777 |
|
|
for (int i = 0; i < list.length; i++) {
|
778 |
2558
|
sgarg
|
// check the given admin dn is a group dn...
|
779 |
2585
|
tao
|
if(groups != null && list[i].startsWith("cn=")){
|
780 |
2576
|
sgarg
|
// is a group dn
|
781 |
2558
|
sgarg
|
for (int j = 0; j < groups.length; j++) {
|
782 |
2576
|
sgarg
|
if (groups[j].equals(list[i])) {
|
783 |
2558
|
sgarg
|
return true;
|
784 |
|
|
}
|
785 |
|
|
}
|
786 |
|
|
} else {
|
787 |
|
|
// is a user dn
|
788 |
2585
|
tao
|
if (username != null && username.equals(list[i])) {
|
789 |
2576
|
sgarg
|
return true;
|
790 |
2558
|
sgarg
|
}
|
791 |
|
|
}
|
792 |
|
|
}
|
793 |
|
|
return false;
|
794 |
|
|
}
|
795 |
2576
|
sgarg
|
|
796 |
|
|
/**
|
797 |
|
|
* A method to check if the specified user is part of the administrators list
|
798 |
|
|
**/
|
799 |
|
|
public static boolean isAdministrator(String username, String[] groups){
|
800 |
|
|
return (onList(administrators, username, groups));
|
801 |
|
|
}
|
802 |
2558
|
sgarg
|
|
803 |
|
|
/**
|
804 |
|
|
* A method to check if the specified user is part of the moderators list
|
805 |
|
|
**/
|
806 |
|
|
public static boolean isModerator(String username, String[] groups){
|
807 |
2576
|
sgarg
|
return (onList(moderators, username, groups));
|
808 |
2558
|
sgarg
|
}
|
809 |
2576
|
sgarg
|
|
810 |
|
|
/**
|
811 |
|
|
* A method to check if the specified user is part of the moderators list
|
812 |
|
|
**/
|
813 |
|
|
public static boolean isAllowedSubmitter(String username, String[] groups){
|
814 |
|
|
if(allowedSubmitters != null){
|
815 |
|
|
return (onList(allowedSubmitters, username, groups));
|
816 |
|
|
} else {
|
817 |
|
|
// no allowedSubmitters list specified -
|
818 |
|
|
// hence everyone should be allowed
|
819 |
|
|
return true;
|
820 |
|
|
}
|
821 |
|
|
}
|
822 |
|
|
|
823 |
|
|
/**
|
824 |
|
|
* A method to check if the specified user is part of the moderators list
|
825 |
|
|
**/
|
826 |
|
|
public static boolean isDeniedSubmitter(String username, String[] groups){
|
827 |
|
|
return (onList(deniedSubmitters, username, groups));
|
828 |
|
|
}
|
829 |
|
|
|
830 |
|
|
/**
|
831 |
|
|
* A method to check if the specified user can insert the document
|
832 |
|
|
**/
|
833 |
|
|
public static boolean canInsertOrUpdate(String username, String[] groups){
|
834 |
|
|
return (isAllowedSubmitter(username, groups)
|
835 |
|
|
&& !isDeniedSubmitter(username, groups));
|
836 |
|
|
}
|
837 |
3269
|
tao
|
|
838 |
|
|
/**
|
839 |
|
|
* Writes debug information into a file. In metacat.properties, if property writeDebugToFile is
|
840 |
|
|
* set to true, the debug information will be written to debug file, which value is the property
|
841 |
|
|
* debugOutputFile in metacat.properties.
|
842 |
|
|
*
|
843 |
|
|
*/
|
844 |
|
|
public static void writeDebugToFile(String debugInfo)
|
845 |
|
|
{
|
846 |
|
|
String debug = MetaCatUtil.getOption("writeDebugToFile");
|
847 |
|
|
if (debug != null && debug.equalsIgnoreCase("true"))
|
848 |
|
|
{
|
849 |
|
|
try
|
850 |
|
|
{
|
851 |
|
|
File outputFile = new File(MetaCatUtil.getOption("debugOutputFile"));
|
852 |
|
|
FileOutputStream fos = new FileOutputStream(outputFile, true);
|
853 |
|
|
PrintWriter pw = new PrintWriter(fos);
|
854 |
|
|
pw.println(debugInfo);
|
855 |
|
|
pw.flush();
|
856 |
|
|
pw.close();
|
857 |
|
|
fos.close();
|
858 |
|
|
|
859 |
|
|
}
|
860 |
|
|
catch(Exception io)
|
861 |
|
|
{
|
862 |
|
|
logMetacat.warn("Eorr in MetacatUtil.writeDebugToFile "+io.getMessage()) ;
|
863 |
|
|
}
|
864 |
|
|
}
|
865 |
|
|
|
866 |
|
|
}
|
867 |
3275
|
tao
|
|
868 |
|
|
/**
|
869 |
|
|
* Writes debug information into a file in delimitered format
|
870 |
|
|
* @param debugInfo the debug information
|
871 |
|
|
* @param newLine append the debug info to a line or not
|
872 |
|
|
*/
|
873 |
|
|
public static void writeDebugToDelimiteredFile(String debugInfo, boolean newLine)
|
874 |
|
|
{
|
875 |
|
|
String debug = MetaCatUtil.getOption("writeDebugToFile");
|
876 |
|
|
if (debug != null && debug.equalsIgnoreCase("true"))
|
877 |
|
|
{
|
878 |
|
|
try
|
879 |
|
|
{
|
880 |
|
|
File outputFile = new File(MetaCatUtil.getOption("delimiteredOutputFile"));
|
881 |
|
|
FileOutputStream fos = new FileOutputStream(outputFile, true);
|
882 |
|
|
PrintWriter pw = new PrintWriter(fos);
|
883 |
|
|
if (newLine)
|
884 |
|
|
{
|
885 |
|
|
pw.println(debugInfo);
|
886 |
|
|
}
|
887 |
|
|
else
|
888 |
|
|
{
|
889 |
|
|
pw.print(debugInfo);
|
890 |
|
|
}
|
891 |
|
|
pw.flush();
|
892 |
|
|
pw.close();
|
893 |
|
|
fos.close();
|
894 |
|
|
|
895 |
|
|
}
|
896 |
|
|
catch(Exception io)
|
897 |
|
|
{
|
898 |
|
|
logMetacat.warn("Eorr in writeDebugToDelimiteredFile "+io.getMessage()) ;
|
899 |
|
|
}
|
900 |
|
|
}
|
901 |
|
|
|
902 |
|
|
}
|
903 |
50
|
jones
|
}
|