1
|
/**
|
2
|
* '$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
|
* Authors: Matt Jones, Jivka Bojilova
|
7
|
*
|
8
|
* '$Author: tao $'
|
9
|
* '$Date: 2007-05-11 12:04:48 -0700 (Fri, 11 May 2007) $'
|
10
|
* '$Revision: 3275 $'
|
11
|
*
|
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
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacat;
|
28
|
|
29
|
import java.io.File;
|
30
|
import java.io.FileOutputStream;
|
31
|
import java.io.FileWriter;
|
32
|
import java.io.PrintWriter;
|
33
|
import java.io.StringReader;
|
34
|
import java.net.MalformedURLException;
|
35
|
import java.net.URL;
|
36
|
import java.text.SimpleDateFormat;
|
37
|
import java.util.HashMap;
|
38
|
import java.util.Hashtable;
|
39
|
import java.util.Stack;
|
40
|
import java.util.Vector;
|
41
|
import java.util.regex.PatternSyntaxException;
|
42
|
|
43
|
import org.apache.log4j.Logger;
|
44
|
|
45
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
46
|
import edu.ucsb.nceas.utilities.Options;
|
47
|
|
48
|
/**
|
49
|
* A suite of utility classes for the metadata catalog server
|
50
|
*/
|
51
|
public class MetaCatUtil
|
52
|
{
|
53
|
|
54
|
public static AbstractDatabase dbAdapter;
|
55
|
|
56
|
public static Vector pathsForIndexing;
|
57
|
|
58
|
public static HashMap skinconfigs = new HashMap();
|
59
|
|
60
|
private static edu.ucsb.nceas.utilities.Options options = null;
|
61
|
|
62
|
private static boolean debug = true;
|
63
|
|
64
|
private static String[] administrators;
|
65
|
|
66
|
private static String[] moderators;
|
67
|
|
68
|
private static String[] allowedSubmitters;
|
69
|
|
70
|
private static String[] deniedSubmitters;
|
71
|
|
72
|
private static Logger logMetacat = Logger.getLogger(MetaCatUtil.class);
|
73
|
|
74
|
static {
|
75
|
// Determine our db adapter class and create an instance of that class
|
76
|
try {
|
77
|
dbAdapter = (AbstractDatabase) createObject(getOption("dbAdapter"));
|
78
|
} catch (Exception e) {
|
79
|
System.err.println("Error in MetaCatUtil static block:"
|
80
|
+ e.getMessage());
|
81
|
e.printStackTrace();
|
82
|
}
|
83
|
|
84
|
// read administrator and moderator lists from metacat.properties
|
85
|
getUserAccessControlLists();
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Instantiate a class using the name of the class at runtime
|
90
|
*
|
91
|
* @param className the fully qualified name of the class to instantiate
|
92
|
*/
|
93
|
public static Object createObject(String className) throws Exception
|
94
|
{
|
95
|
|
96
|
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
|
|
110
|
/**
|
111
|
* Utility method to get an option value from the properties file
|
112
|
*
|
113
|
* @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
|
options = edu.ucsb.nceas.utilities.Options.getInstance();
|
120
|
}
|
121
|
if (options == null) {
|
122
|
Logger logMetacat = Logger.getLogger(MetaCatUtil.class);
|
123
|
logMetacat.info("options is null");
|
124
|
}
|
125
|
String value = options.getOption(optionName);
|
126
|
return value;
|
127
|
}
|
128
|
|
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
|
options = edu.ucsb.nceas.utilities.Options.getInstance();
|
138
|
}
|
139
|
options.setOption(optionName, newValue);
|
140
|
|
141
|
}
|
142
|
|
143
|
/** 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
|
}
|
160
|
|
161
|
/**
|
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
|
|
174
|
String temp = "";
|
175
|
boolean ampflag = true;
|
176
|
boolean poundflag = false;
|
177
|
int arrcount = 0;
|
178
|
|
179
|
if (query != null) {
|
180
|
for (int i = 0; i < query.length(); i++) {
|
181
|
|
182
|
// 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
|
|
210
|
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
|
}
|
230
|
}
|
231
|
return parameters;
|
232
|
}
|
233
|
|
234
|
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
|
|
242
|
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
|
}
|
253
|
|
254
|
/** Normalizes the given string. Taken from configXML.java */
|
255
|
public static String normalize(String s)
|
256
|
{
|
257
|
StringBuffer str = new StringBuffer();
|
258
|
|
259
|
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
|
/*
|
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
|
} 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
|
str.append("&");
|
295
|
i += 4;
|
296
|
} else
|
297
|
str.append("&");
|
298
|
/////////
|
299
|
break;
|
300
|
}
|
301
|
case '"':
|
302
|
str.append(""");
|
303
|
break;
|
304
|
default: {
|
305
|
if ( (ch<128) && (ch>31) ) {
|
306
|
str.append(ch);
|
307
|
}
|
308
|
else if (ch<32) {
|
309
|
if (ch == 10) { // new line
|
310
|
str.append(ch);
|
311
|
}
|
312
|
if (ch == 13) { // carriage return
|
313
|
str.append(ch);
|
314
|
}
|
315
|
if (ch == 9) { // tab
|
316
|
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
|
return str.toString();
|
329
|
}
|
330
|
|
331
|
/**
|
332
|
* Get docid from online/url string
|
333
|
*/
|
334
|
public static String getDocIdWithRevFromOnlineURL(String url)
|
335
|
{
|
336
|
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
|
logMetacat.info("url is null and null will be returned");
|
344
|
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
|
logMetacat.info("substring between two & is: " + str);
|
357
|
//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
|
logMetacat.info("Checking the last substring");
|
372
|
String str = url.substring(((Integer) list.elementAt(count))
|
373
|
.intValue() + 1, url.length());
|
374
|
logMetacat.info("Last substring is: " + str);
|
375
|
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
|
logMetacat.info("The docid from online url is:" + docid);
|
384
|
return docid.trim();
|
385
|
}
|
386
|
|
387
|
|
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
|
logMetacat.warn("The accession number from url is " +
|
407
|
accessionNumber);
|
408
|
return accessionNumber;
|
409
|
}
|
410
|
|
411
|
private static int getIndexForGivenChar(String str, char character)
|
412
|
{
|
413
|
int index = -1;
|
414
|
// make sure str is not null
|
415
|
if (str == null) {
|
416
|
logMetacat.info(
|
417
|
"The given str is null and -1 will be returned");
|
418
|
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
|
logMetacat.info("the index for char " + character + " is: "
|
429
|
+ index);
|
430
|
return index;
|
431
|
}
|
432
|
|
433
|
/**
|
434
|
* Utility method to get docid from a given string
|
435
|
*
|
436
|
* @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
|
{
|
443
|
String docId = null;
|
444
|
if (str == null) {
|
445
|
logMetacat.info(
|
446
|
"The given str is null and null will be returned"
|
447
|
+ " in getDocIdfromString");
|
448
|
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
|
|
453
|
//assume that seperator is one charactor string
|
454
|
char seperator = getOption("accNumSeparator").charAt(0);
|
455
|
|
456
|
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
|
|
463
|
//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
|
*
|
482
|
* @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
|
{
|
489
|
int version = -1;
|
490
|
String versionString = null;
|
491
|
int dotNumber = 0;//count how many dots in given string
|
492
|
int indexOfLastDot = 0;
|
493
|
|
494
|
//assume that seperator is one charactor string
|
495
|
char seperator = getOption("accNumSeparator").charAt(0);
|
496
|
|
497
|
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
|
|
504
|
//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
|
|
519
|
return version;
|
520
|
}//getVersionFromString
|
521
|
|
522
|
/**
|
523
|
* Utility method to get version string from a given string
|
524
|
*
|
525
|
* @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
|
{
|
532
|
// 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
|
|
537
|
//assume that seperator is one charactor string
|
538
|
char seperator = getOption("accNumSeparator").charAt(0);
|
539
|
|
540
|
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
|
|
547
|
//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
|
|
561
|
return versionString;
|
562
|
}//getVersionFromString
|
563
|
|
564
|
/**
|
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
|
{
|
571
|
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
|
logMetacat.info("after parsing accessionnumber, docid is "
|
577
|
+ docid);
|
578
|
return docid;
|
579
|
}
|
580
|
|
581
|
/**
|
582
|
* 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
|
logMetacat.info("after parsing accessionnumber, docid is "
|
597
|
+ docid);
|
598
|
|
599
|
return docid;
|
600
|
}
|
601
|
|
602
|
/**
|
603
|
* 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
|
*
|
610
|
*/
|
611
|
public static String getSmartDocId(String str)
|
612
|
{
|
613
|
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
|
logMetacat.info("The docid get from smart docid getor is "
|
622
|
+ docid);
|
623
|
return docid;
|
624
|
}
|
625
|
|
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
|
{
|
634
|
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
|
logMetacat.info("after parsing accessionnumber, rev is "
|
643
|
+ revNumber);
|
644
|
return revNumber;
|
645
|
}
|
646
|
|
647
|
/**
|
648
|
* Method to get the name of local replication server
|
649
|
*/
|
650
|
public static String getLocalReplicationServerName()
|
651
|
{
|
652
|
String replicationServerName = null;
|
653
|
String serverHost = null;
|
654
|
serverHost = getOption("server");
|
655
|
// append "context/servelet/replication" to the host name
|
656
|
replicationServerName = serverHost.trim() + getOption("replicationpath").trim();
|
657
|
return replicationServerName;
|
658
|
|
659
|
}
|
660
|
|
661
|
/**
|
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
|
{
|
667
|
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
|
|
686
|
logMetacat.info("Docid without rev from inlinedata id: "
|
687
|
+ docidWithoutRev);
|
688
|
return docidWithoutRev;
|
689
|
|
690
|
}
|
691
|
|
692
|
/**
|
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
|
|
704
|
while (!stack.isEmpty()) {
|
705
|
Object obj = stack.pop();
|
706
|
result.push(obj);
|
707
|
}
|
708
|
return result;
|
709
|
}
|
710
|
|
711
|
/** A method to replace whitespace in url */
|
712
|
public static String replaceWhiteSpaceForURL(String urlHasWhiteSpace)
|
713
|
{
|
714
|
StringBuffer newUrl = new StringBuffer();
|
715
|
String whiteSpaceReplace = "%20";
|
716
|
if (urlHasWhiteSpace == null || urlHasWhiteSpace.trim().equals("")) { return null; }
|
717
|
|
718
|
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
|
|
727
|
}//for
|
728
|
logMetacat.info("The new string without space is:"
|
729
|
+ newUrl.toString());
|
730
|
return newUrl.toString();
|
731
|
|
732
|
}// replaceWhiteSpaceForUR
|
733
|
|
734
|
/**
|
735
|
* A method to read administrators and moderators list from the metacat.properties
|
736
|
**/
|
737
|
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
|
try {
|
753
|
if ( listString != null && !listString.trim().equals("")) {
|
754
|
list = listString.split(":");
|
755
|
} else {
|
756
|
list = null;
|
757
|
}
|
758
|
|
759
|
} catch (PatternSyntaxException pse) {
|
760
|
list = null;
|
761
|
logMetacat.error("Error in MetacatServlet.init: "
|
762
|
+ pse.getMessage());
|
763
|
}
|
764
|
return list;
|
765
|
}
|
766
|
|
767
|
/**
|
768
|
* A method to check if the specified user is part of the moderators list
|
769
|
**/
|
770
|
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
|
// check the given admin dn is a group dn...
|
779
|
if(groups != null && list[i].startsWith("cn=")){
|
780
|
// is a group dn
|
781
|
for (int j = 0; j < groups.length; j++) {
|
782
|
if (groups[j].equals(list[i])) {
|
783
|
return true;
|
784
|
}
|
785
|
}
|
786
|
} else {
|
787
|
// is a user dn
|
788
|
if (username != null && username.equals(list[i])) {
|
789
|
return true;
|
790
|
}
|
791
|
}
|
792
|
}
|
793
|
return false;
|
794
|
}
|
795
|
|
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
|
|
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
|
return (onList(moderators, username, groups));
|
808
|
}
|
809
|
|
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
|
|
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
|
|
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
|
}
|