1
|
/*
|
2
|
* ClientViewHelper.java
|
3
|
*
|
4
|
* Created on June 25, 2007, 9:57 AM
|
5
|
*
|
6
|
* To change this template, choose Tools | Template Manager
|
7
|
* and open the template in the editor.
|
8
|
*/
|
9
|
|
10
|
package edu.ucsb.nceas.metacat.clientview;
|
11
|
|
12
|
import com.oreilly.servlet.multipart.FilePart;
|
13
|
import com.oreilly.servlet.multipart.MultipartParser;
|
14
|
import com.oreilly.servlet.multipart.ParamPart;
|
15
|
import com.oreilly.servlet.multipart.Part;
|
16
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
17
|
import edu.ucsb.nceas.metacat.client.MetacatClient;
|
18
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
19
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
20
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
21
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
22
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
23
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
24
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
25
|
import edu.ucsb.nceas.utilities.XMLUtilities;
|
26
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
27
|
import java.io.BufferedReader;
|
28
|
import java.io.ByteArrayOutputStream;
|
29
|
import java.io.IOException;
|
30
|
import java.io.InputStream;
|
31
|
import java.io.InputStreamReader;
|
32
|
import java.io.Reader;
|
33
|
import java.io.StringReader;
|
34
|
import java.util.HashMap;
|
35
|
import java.util.Iterator;
|
36
|
import java.util.Properties;
|
37
|
import java.util.Stack;
|
38
|
import java.util.TreeMap;
|
39
|
import java.util.Vector;
|
40
|
import javax.servlet.http.Cookie;
|
41
|
import javax.servlet.http.HttpServletRequest;
|
42
|
import javax.servlet.http.HttpServletResponse;
|
43
|
import javax.servlet.http.HttpSession;
|
44
|
import javax.xml.xpath.XPath;
|
45
|
import javax.xml.xpath.XPathConstants;
|
46
|
import javax.xml.xpath.XPathExpressionException;
|
47
|
import javax.xml.xpath.XPathFactory;
|
48
|
import org.w3c.dom.DOMException;
|
49
|
import org.w3c.dom.Document;
|
50
|
import org.w3c.dom.Node;
|
51
|
import org.w3c.dom.NodeList;
|
52
|
import org.w3c.dom.Text;
|
53
|
|
54
|
/**
|
55
|
*
|
56
|
* @author barteau
|
57
|
*/
|
58
|
public class ClientViewHelper {
|
59
|
private XPath xpath = XPathFactory.newInstance().newXPath();
|
60
|
private HttpSession clientSession;
|
61
|
private ClientView clientViewBean = null;
|
62
|
private MetacatClient metacatClient = null;
|
63
|
private boolean loggedIn = false;
|
64
|
private Document metadataDoc = null;
|
65
|
private int sizeLimit;
|
66
|
private String contactName = "";
|
67
|
|
68
|
private static final String LDAP_TEMPLATE = "uid=%1s,o=%2s,dc=ecoinformatics,dc=org";
|
69
|
|
70
|
public static final String DOWNLOAD_ACTION = "Download";
|
71
|
|
72
|
public static final String PERMISSION_TYPE_ALLOW = "allow";
|
73
|
|
74
|
public static final String PERMISSION_TYPE_DISALLOW = "deny";
|
75
|
|
76
|
/**
|
77
|
* Creates a new instance of ClientViewHelper, using info in an HttpServletRequest
|
78
|
* for initializing.
|
79
|
* @param request HttpServletRequest, sent from the client browser.
|
80
|
* @throws edu.ucsb.nceas.metacat.client.MetacatInaccessibleException Thrown
|
81
|
*/
|
82
|
public ClientViewHelper(HttpServletRequest request) throws MetacatInaccessibleException {
|
83
|
String host, context;
|
84
|
|
85
|
clientSession = request.getSession(false);
|
86
|
host = request.getHeader("host");
|
87
|
context = request.getContextPath();
|
88
|
init(host, context);
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Creates a new instance of ClientViewHelper, using parameter values
|
93
|
* for initializing. This constructor is plain java code so it's the portal of
|
94
|
* choice for JUnit testing.
|
95
|
* @param host The host with port (if needed), such as "localhost:8084".
|
96
|
* @param context The application root context.
|
97
|
* @param bean ClientView instance, with pre-populated values.
|
98
|
* @throws edu.ucsb.nceas.metacat.client.MetacatInaccessibleException thrown
|
99
|
*/
|
100
|
public ClientViewHelper(String host, String context, ClientView bean) throws MetacatInaccessibleException {
|
101
|
clientViewBean = bean;
|
102
|
init(host, context);
|
103
|
}
|
104
|
|
105
|
private void init(String host, String context) throws MetacatInaccessibleException {
|
106
|
String metacatPath = "http://%1s%2s/metacat";
|
107
|
String tmp;
|
108
|
|
109
|
tmp = metacatPath.replaceFirst("%1s", host);
|
110
|
metacatPath = tmp.replaceFirst("%2s", context);
|
111
|
metacatClient = (MetacatClient) MetacatFactory.createMetacatConnection(metacatPath);
|
112
|
try {
|
113
|
sizeLimit =
|
114
|
(new Integer(PropertyService.getProperty("replication.datafilesizelimit"))).intValue();
|
115
|
} catch (PropertyNotFoundException pnfe) {
|
116
|
throw new MetacatInaccessibleException(pnfe.getMessage());
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Main web API method for handling various actions.
|
122
|
* @param request HttpServletRequest
|
123
|
* @param response HttpServletResponse
|
124
|
* @return String message
|
125
|
*/
|
126
|
public String clientRequest(HttpServletRequest request, HttpServletResponse response) {
|
127
|
String result = null, action, contentType;
|
128
|
MultipartParser multipartParser;
|
129
|
HashMap<String, String> responseMap;
|
130
|
|
131
|
getMetacatClient().setSessionId(request.getSession().getId());
|
132
|
|
133
|
if (clientViewBean == null) {
|
134
|
clientViewBean = (ClientView) clientSession.getAttribute(ClientView.CLIENT_VIEW_BEAN);
|
135
|
|
136
|
if (clientViewBean == null) {
|
137
|
//make a new one and shove it in the session
|
138
|
clientViewBean = new ClientView();
|
139
|
clientSession.setAttribute(ClientView.CLIENT_VIEW_BEAN, clientViewBean);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
if (clientViewBean != null) {
|
144
|
action = clientViewBean.getAction();
|
145
|
contentType = request.getContentType();
|
146
|
|
147
|
//*** BEGIN: manual bind params to bean (if we arrived here via the ClientViewHelper.jspx).
|
148
|
if (action == null || action.equals("")) {
|
149
|
if (contentType != null && contentType.indexOf("multipart/form-data") > -1) {
|
150
|
action = "Upload";
|
151
|
} else {
|
152
|
action = request.getParameter("action");
|
153
|
clientViewBean.setDocId(request.getParameter("docid"));
|
154
|
clientViewBean.setMetaFileDocId(request.getParameter("metadataDocId"));
|
155
|
clientViewBean.setQformat(request.getParameter("qformat"));
|
156
|
clientViewBean.setPublicAccess(request.getParameter("publicAccess") != null);
|
157
|
clientViewBean.setContentStandard(request.getParameter("contentStandard"));
|
158
|
}
|
159
|
clientViewBean.setAction(action);
|
160
|
}
|
161
|
//*** END: manual bind params to bean.
|
162
|
|
163
|
if (action != null) {
|
164
|
if (action.equals("Login")) {
|
165
|
responseMap = handleClientRequest(null);
|
166
|
//*** Now that the action has been processed, clear it.
|
167
|
clientViewBean.setAction("");
|
168
|
if (isLoggedIn()) {
|
169
|
// HttpSession session = request.getSession(false);
|
170
|
// session.setAttribute("ClientViewHelper", this);
|
171
|
Cookie jSessionCookie = new Cookie("JSESSIONID", clientViewBean.getSessionid());
|
172
|
response.addCookie(jSessionCookie);
|
173
|
}
|
174
|
} else if (action.equals("Logout")) {
|
175
|
responseMap = handleClientRequest(null);
|
176
|
clientViewBean.setAction("");
|
177
|
} else if (action.equals("Upload")) {
|
178
|
try {
|
179
|
//*** Init the MultipartParser.
|
180
|
multipartParser = new MultipartParser(request, sizeLimit * 1024 * 1024);
|
181
|
responseMap = handleClientRequest(multipartParser);
|
182
|
} catch (IOException ex) {
|
183
|
responseMap = new HashMap<String, String>();
|
184
|
responseMap.put("message", ex.getMessage());
|
185
|
}
|
186
|
clientViewBean.setAction("");
|
187
|
} else if (action.equals("Download")) {
|
188
|
responseMap = handleClientRequest(null);
|
189
|
try {
|
190
|
handleDownloadResponse(responseMap, response);
|
191
|
} catch (IOException ex) {
|
192
|
responseMap = new HashMap<String, String>();
|
193
|
responseMap.put("message", ex.getMessage());
|
194
|
}
|
195
|
|
196
|
} else if (action.equals("Set Access")) {
|
197
|
responseMap = handleClientRequest(null);
|
198
|
clientViewBean.setAction("");
|
199
|
} else {
|
200
|
responseMap = handleClientRequest(null);
|
201
|
}
|
202
|
result = (String) responseMap.get("message");
|
203
|
}
|
204
|
} else {
|
205
|
result = "ClientViewHelper.clientRequest: ClientView bean is not instantiated.";
|
206
|
}
|
207
|
return(result);
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Main method for handling various actions.
|
212
|
*
|
213
|
* Note: This is mostly plain java code so it is JUnit friendly
|
214
|
* (pass null as the MulipartParser).
|
215
|
*
|
216
|
* @param multipartParser Only needed if the action is "Upload".
|
217
|
* @return HashMap containing "message", and possibly several other values. If
|
218
|
* the action is Download, than this will contain all needed values
|
219
|
* to pass to handleDownloadResponse.
|
220
|
*/
|
221
|
public HashMap<String, String> handleClientRequest(MultipartParser multipartParser) {
|
222
|
String result = "", serverResponse;
|
223
|
String posted_ldapUserName, tmp, action;
|
224
|
HashMap<String, String> responseMap = new HashMap<String, String>();
|
225
|
|
226
|
|
227
|
if (clientViewBean != null) {
|
228
|
action = clientViewBean.getAction();
|
229
|
if (action != null) {
|
230
|
try {
|
231
|
if (action.equals("Login")) {
|
232
|
tmp = LDAP_TEMPLATE.replaceFirst("%1s", clientViewBean.getUsername());
|
233
|
posted_ldapUserName = tmp.replaceFirst("%2s", clientViewBean.getOrganization());
|
234
|
|
235
|
// if (metacatSessionId != null) {
|
236
|
// metacatClient.setSessionId(metacatSessionId);
|
237
|
// }
|
238
|
serverResponse = metacatClient.login(posted_ldapUserName, clientViewBean.getPassword());
|
239
|
setLoggedIn(serverResponse);
|
240
|
result = parseXml("message", serverResponse);
|
241
|
contactName = parseXml("name", serverResponse);
|
242
|
clientViewBean.setMessage(ClientView.LOGIN_MESSAGE, result);
|
243
|
clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, "");
|
244
|
if (isLoggedIn()) {
|
245
|
clientViewBean.setSessionid(metacatClient.getSessionId());
|
246
|
|
247
|
}
|
248
|
} else if (action.equals("Logout")) {
|
249
|
result = metacatClient.logout();
|
250
|
setLoggedIn(result);
|
251
|
result = parseXml("message", result);
|
252
|
clientViewBean.setMessage(ClientView.LOGIN_MESSAGE, result);
|
253
|
clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, "");
|
254
|
if (!isLoggedIn()) {
|
255
|
clientViewBean.setUsername("");
|
256
|
clientViewBean.setPassword("");
|
257
|
clientViewBean.setOrganization("");
|
258
|
clientViewBean.setSessionid(null);
|
259
|
}
|
260
|
} else if (action.equals("Delete")) {
|
261
|
ClientFgdcHelper.clientDeleteRequest(clientViewBean, this);
|
262
|
clientViewBean.setAction("read"); //*** Set for re-query.
|
263
|
//*** Note: the clientViewBean will already have the updated Meta Doc Id.
|
264
|
|
265
|
} else if (action.equals("Upload")) {
|
266
|
//*** Only process request if logged in.
|
267
|
if (isLoggedIn()) {
|
268
|
if (multipartParser == null)
|
269
|
result = "ClientViewHelper.handleClientRequest: MultipartParser is not instantiated.";
|
270
|
else
|
271
|
result = handlePackageUpload(clientViewBean, multipartParser);
|
272
|
} else {
|
273
|
result = "You must be logged in to perform an upload.";
|
274
|
}
|
275
|
clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, result);
|
276
|
} else if (action.equals("Update")) {
|
277
|
result = "This is not implemented here. Call ClientViewHelper.jspx";
|
278
|
} else if (action.equals("Scope")) {
|
279
|
result = handleDocIdSelect();
|
280
|
clientViewBean.setMessage(ClientView.SELECT_MESSAGE, result);
|
281
|
} else if (action.equals("Download")) {
|
282
|
responseMap = download(clientViewBean);
|
283
|
} else if (action.equals("Set Access")) {
|
284
|
result = handleChangeAccess(clientViewBean.getMetaFileDocId(),
|
285
|
(clientViewBean.isPublicAccess()? PERMISSION_TYPE_ALLOW: PERMISSION_TYPE_DISALLOW));
|
286
|
clientViewBean.setMessage(ClientView.UPDATE_MESSAGE, result);
|
287
|
} else {
|
288
|
result = action + " action not recognized.";
|
289
|
}
|
290
|
} catch (Exception ex) {
|
291
|
result = ex.getMessage();
|
292
|
clientViewBean.setMessage(ClientView.ERROR_MESSAGE, result);
|
293
|
ex.printStackTrace();
|
294
|
}
|
295
|
}
|
296
|
} else {
|
297
|
result = "ClientViewHelper.clientRequest: ClientView bean is not instantiated.";
|
298
|
}
|
299
|
responseMap.put("message", result);
|
300
|
return(responseMap);
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* This is a convenience method to reduce the amount of code in a Metacat Client.
|
305
|
* It handles creating/reusing (per session) an instance of a ClientViewHelper.
|
306
|
* @param request Since this is intended to be used by an Http client, it is passed the
|
307
|
* available "request" variable (the HttpServletRequest).
|
308
|
* @throws edu.ucsb.nceas.metacat.client.MetacatInaccessibleException Received by MetacatFactory.
|
309
|
* @return ClientViewHelper instance.
|
310
|
*/
|
311
|
public static ClientViewHelper clientViewHelperInstance(HttpServletRequest request) {
|
312
|
ClientViewHelper result;
|
313
|
|
314
|
String sessionId = request.getSession(false).getId();
|
315
|
|
316
|
result = (ClientViewHelper) request.getSession().getAttribute("ClientViewHelper");
|
317
|
if (result == null) {
|
318
|
try {
|
319
|
result = new ClientViewHelper(request);
|
320
|
request.getSession().setAttribute("ClientViewHelper", result);
|
321
|
} catch (MetacatInaccessibleException ex) {
|
322
|
ex.printStackTrace();
|
323
|
}
|
324
|
}
|
325
|
|
326
|
if (result.clientViewBean == null) {
|
327
|
result.clientViewBean = (ClientView) request.getSession().getAttribute(ClientView.CLIENT_VIEW_BEAN);
|
328
|
|
329
|
if (result.clientViewBean == null) {
|
330
|
//make a new one and shove it in the session
|
331
|
result.clientViewBean = new ClientView();
|
332
|
request.getSession().setAttribute(ClientView.CLIENT_VIEW_BEAN, result.clientViewBean);
|
333
|
}
|
334
|
}
|
335
|
|
336
|
boolean oldLoginValue = result.loggedIn;
|
337
|
result.setLoggedIn(SessionService.getInstance().validateSession(sessionId));
|
338
|
if (result.isLoggedIn()) {
|
339
|
SessionData sessionData = SessionService.getInstance().getRegisteredSession(sessionId);
|
340
|
result.setUserName(sessionData.getName());
|
341
|
}
|
342
|
|
343
|
if (!oldLoginValue || result.loggedIn) {
|
344
|
result.clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, "");
|
345
|
}
|
346
|
|
347
|
return(result);
|
348
|
}
|
349
|
|
350
|
/**
|
351
|
* A convenience method to be used by client code that requires
|
352
|
* the user to be logged in. NOTE: setUser() must have been called first,
|
353
|
* otherwise it will always return false.
|
354
|
* @return boolean true if user has logged in for this session, false otherwise.
|
355
|
*/
|
356
|
public boolean isLoggedIn() {
|
357
|
return(loggedIn);
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* After calling "login(ldapUserName, pwd)", call this with the username
|
362
|
* and servers response message. You can than use isLoggedIn() to determine if
|
363
|
* the user is logged in, getLoginResponseElement(), etc. The user name will also
|
364
|
* used by calls to doMetadataUpload() for Document Id creation (scope).
|
365
|
* @param userName User name
|
366
|
* @param serverResponse XML login response sent from Metacat.
|
367
|
*/
|
368
|
public void setLoggedIn(String serverResponse) {
|
369
|
loggedIn = (serverResponse != null && serverResponse.indexOf("login") > -1);
|
370
|
}
|
371
|
|
372
|
public void setLoggedIn(boolean isLoggedIn) {
|
373
|
this.loggedIn = isLoggedIn;
|
374
|
}
|
375
|
|
376
|
public void setUserName(String userName) {
|
377
|
clientViewBean.setUsername(userName);
|
378
|
}
|
379
|
|
380
|
public String parseXml(String elementName, String xml) {
|
381
|
String result = null;
|
382
|
Document doc;
|
383
|
|
384
|
try {
|
385
|
doc = XMLUtilities.getXMLReaderAsDOMDocument(new StringReader(xml));
|
386
|
result = (String) xpath.evaluate(elementName, doc.getDocumentElement(), XPathConstants.STRING);
|
387
|
if (result != null)
|
388
|
result = result.trim();
|
389
|
} catch (IOException ex) {
|
390
|
ex.printStackTrace();
|
391
|
} catch (XPathExpressionException ex) {
|
392
|
ex.printStackTrace();
|
393
|
}
|
394
|
return(result);
|
395
|
}
|
396
|
|
397
|
public String handleDocIdSelect() {
|
398
|
String result = "";
|
399
|
TreeMap allDocIds;
|
400
|
|
401
|
if (!clientViewBean.getPathValue().equals("")) {
|
402
|
allDocIds = getSelectQueryMap();
|
403
|
result = ClientHtmlHelper.mapToHtmlSelect(allDocIds, "docId", "width: 240", 10);
|
404
|
}
|
405
|
return(result);
|
406
|
}
|
407
|
|
408
|
/**
|
409
|
* Handles metadata file and data file uploads for inserting new
|
410
|
* Metacat data packages. Note: if content type is not "multipart/form-data",
|
411
|
* nothing will happen.
|
412
|
* @param request HTTP request.
|
413
|
* @return A 1-line status message for the user.
|
414
|
*/
|
415
|
public String handlePackageUpload(ClientView clientViewBean, MultipartParser multipartParser) throws Exception {
|
416
|
String result = "", contentType, formatType;
|
417
|
String lastDocId, nextDocId, metaDocId, metaFileName;
|
418
|
String fileInfo[];
|
419
|
Reader reader;
|
420
|
int sizeLimit, idx;
|
421
|
InputStream inputStream;
|
422
|
HashMap paramsMap, dataDocIDs;
|
423
|
StringBuffer fileName;
|
424
|
boolean sendIt;
|
425
|
Iterator iterIt;
|
426
|
Stack docIdStack;
|
427
|
|
428
|
//*** Get the First file, which should be the metadata file.
|
429
|
paramsMap = new HashMap();
|
430
|
fileName = new StringBuffer();
|
431
|
inputStream = getNextInputStream(multipartParser, fileName, paramsMap);
|
432
|
metaFileName = fileName.toString();
|
433
|
if (metaFileName.toLowerCase().endsWith(".xml")) {
|
434
|
//*** Keep it here for updating.
|
435
|
setMetadataDoc(inputStream);
|
436
|
//*** Get the Metadata File's DOC ID.
|
437
|
lastDocId = getMetacatClient().getLastDocid(clientViewBean.getUsername());
|
438
|
metaDocId = lastDocId = nextDocId(lastDocId, clientViewBean.getUsername());
|
439
|
|
440
|
//*** Loop thru all of the data files, get fileName and inputStream.
|
441
|
dataDocIDs = new HashMap();
|
442
|
fileName = new StringBuffer();
|
443
|
while ((inputStream = getNextInputStream(multipartParser, fileName, paramsMap)) != null) {
|
444
|
//*** Get the data file's DOC ID.
|
445
|
nextDocId = nextDocId(lastDocId, clientViewBean.getUsername());
|
446
|
|
447
|
fileInfo = parseFileInfo(fileName.toString());
|
448
|
dataDocIDs.put(nextDocId, fileInfo);
|
449
|
|
450
|
//*** Upload the data file to metacat.
|
451
|
getMetacatClient().upload(nextDocId, fileName.toString(), inputStream, Integer.MAX_VALUE);
|
452
|
|
453
|
lastDocId = nextDocId;
|
454
|
fileName = new StringBuffer();
|
455
|
}
|
456
|
|
457
|
if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
|
458
|
sendIt = ClientFgdcHelper.handlePackageUpload(metaDocId, dataDocIDs, contactName, metaFileName, getMetadataDoc());
|
459
|
} else {
|
460
|
//TODO add other types of metadata grammars here...
|
461
|
System.out.println("ClientViewHelper.handlePackageUpload: not an FGDC file = " + fileName);
|
462
|
result = fileName + " is not an FGDC file. Files not uploaded.";
|
463
|
sendIt = false;
|
464
|
}
|
465
|
|
466
|
if (sendIt) {
|
467
|
//*** Upload the metadata file to metacat.
|
468
|
reader = XMLUtilities.getDOMTreeAsReader(metadataDoc.getDocumentElement(), false);
|
469
|
getMetacatClient().insert(metaDocId, reader, null);
|
470
|
|
471
|
result = "MetaCat Package Inserted: the Document Identifier is " + metaDocId;
|
472
|
reader.close();
|
473
|
//*** Grant the public read access to the meta file.
|
474
|
if (paramsMap.containsKey("publicAccess")) {
|
475
|
docIdStack = new Stack();
|
476
|
docIdStack.addAll(dataDocIDs.keySet());
|
477
|
setPublicAccess(this.PERMISSION_TYPE_ALLOW, metaDocId, docIdStack);
|
478
|
}
|
479
|
}
|
480
|
} else {
|
481
|
result = "The first file must be an XML Metadata file. Files not uploaded.";
|
482
|
}
|
483
|
if (inputStream != null)
|
484
|
inputStream.close();
|
485
|
return(result);
|
486
|
}
|
487
|
|
488
|
private String setPublicAccess(String permissionType, String metaDocId, Stack docIdStack)
|
489
|
throws InsufficientKarmaException, MetacatException, MetacatInaccessibleException {
|
490
|
String result = " for Documents ";
|
491
|
String docId, lst = metaDocId, permOrder;
|
492
|
|
493
|
if (permissionType.equals("allow"))
|
494
|
permOrder = "denyFirst";
|
495
|
else
|
496
|
permOrder = "allowFirst";
|
497
|
|
498
|
getMetacatClient().setAccess(metaDocId, "public", "read", permissionType, permOrder);
|
499
|
//*** Grant the public read access to the data files.
|
500
|
while(!docIdStack.isEmpty()) {
|
501
|
docId = (String) docIdStack.pop();
|
502
|
getMetacatClient().setAccess(docId, "public", "read", permissionType, permOrder);
|
503
|
lst += ", " + docId;
|
504
|
}
|
505
|
result = "Changed public read access to '" + permissionType + "' for " + result + lst;
|
506
|
return(result);
|
507
|
}
|
508
|
|
509
|
private String handleChangeAccess(String metaDocId, String permissionType) throws Exception {
|
510
|
Stack dataDocIDs;
|
511
|
String result = "", xpathExpr = null;
|
512
|
|
513
|
setMetadataDoc(metaDocId);
|
514
|
//*** Build list of sub-documents.
|
515
|
if (clientViewBean.getContentStandard().equals(ClientView.FEDERAL_GEOGRAPHIC_DATA_COMMITTEE)) {
|
516
|
xpathExpr = ClientFgdcHelper.SUB_DOCS_PATH;
|
517
|
} else if (clientViewBean.getContentStandard().equals(ClientView.ECOLOGICAL_METADATA_LANGUAGE)) {
|
518
|
xpathExpr = null; //TODO - EML
|
519
|
}
|
520
|
if (xpathExpr != null) {
|
521
|
dataDocIDs = getNodeTextStack(xpath, xpathExpr, getMetadataDoc().getDocumentElement());
|
522
|
result = setPublicAccess(permissionType, metaDocId, dataDocIDs);
|
523
|
}
|
524
|
return(result);
|
525
|
}
|
526
|
|
527
|
public String handleFileUpdate(MultipartParser multipartParser) throws Exception {
|
528
|
String result = "", fNm, action, lastDocId, newDocId, xPathQuery, qFrmt;
|
529
|
InputStream inputStream;
|
530
|
HashMap paramsMap;
|
531
|
StringBuffer fileName;
|
532
|
Iterator iterIt;
|
533
|
boolean sendIt;
|
534
|
String metadataDocId, fileInfo[];
|
535
|
|
536
|
paramsMap = new HashMap();
|
537
|
fileName = new StringBuffer();
|
538
|
if ((inputStream = getNextInputStream(multipartParser, fileName, paramsMap)) != null) {
|
539
|
action = (String) paramsMap.get("action");
|
540
|
//*** Get the Doc Id.
|
541
|
lastDocId = (String) paramsMap.get("docid");
|
542
|
|
543
|
//*** Get the metadata Doc Id.
|
544
|
metadataDocId = (String) paramsMap.get("metadataDocId");
|
545
|
clientViewBean.setMetaFileDocId(metadataDocId);
|
546
|
|
547
|
//*** Get the qformat.
|
548
|
qFrmt = (String) paramsMap.get("qformat");
|
549
|
clientViewBean.setQformat(qFrmt);
|
550
|
|
551
|
fNm = fileName.toString();
|
552
|
|
553
|
try {
|
554
|
if (lastDocId.equals(metadataDocId)) { //*** This is the metadata file.
|
555
|
//*** Keep it here for updating.
|
556
|
setMetadataDoc(inputStream);
|
557
|
if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
|
558
|
clientViewBean.setContentStandard(ClientView.FEDERAL_GEOGRAPHIC_DATA_COMMITTEE);
|
559
|
if (!ClientFgdcHelper.hasMetacatInfo(lastDocId, getMetadataDoc())) {
|
560
|
|
561
|
//*** Save the Doc Id for re-query.
|
562
|
clientViewBean.setMetaFileDocId(lastDocId);
|
563
|
clientViewBean.setAction("read"); //*** Set for re-query.
|
564
|
result = "Update not performed: the Metadata file has no prior Metacat info in it.";
|
565
|
} else {
|
566
|
xPathQuery = ClientFgdcHelper.XPATH_QUERY_TEMPLATE.replaceFirst("%1s", lastDocId);
|
567
|
newDocId = updateMetadataDoc(lastDocId, xPathQuery, fNm);
|
568
|
|
569
|
//*** Save the Doc Id for re-query.
|
570
|
clientViewBean.setMetaFileDocId(newDocId);
|
571
|
clientViewBean.setAction("read"); //*** Set for re-query.
|
572
|
result = "Updated to new document (from " + lastDocId + " to " + newDocId + ")";
|
573
|
}
|
574
|
} else {
|
575
|
//***TODO This is EML.
|
576
|
clientViewBean.setContentStandard(ClientView.ECOLOGICAL_METADATA_LANGUAGE);
|
577
|
|
578
|
//*** Save the Doc Id for re-query.
|
579
|
clientViewBean.setMetaFileDocId(lastDocId);
|
580
|
clientViewBean.setAction("read"); //*** Set for re-query.
|
581
|
result = "Currently this functionality only supports FGDC metadata.";
|
582
|
}
|
583
|
} else {
|
584
|
//*** This is a data file.
|
585
|
//*** Query for the metadata, we need to update it with the new data file doc id.
|
586
|
setMetadataDoc(metadataDocId);
|
587
|
|
588
|
if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
|
589
|
clientViewBean.setContentStandard(ClientView.FEDERAL_GEOGRAPHIC_DATA_COMMITTEE);
|
590
|
fileInfo = parseFileInfo(fNm);
|
591
|
|
592
|
xPathQuery = ClientFgdcHelper.FGDC_DATA_FILE_QUERY_XPATH.replaceFirst("%1s", lastDocId);
|
593
|
newDocId = nextVersion(lastDocId, xPathQuery);
|
594
|
ClientFgdcHelper.updateFileNameAndType(getMetadataDoc().getDocumentElement(), newDocId, fileInfo);
|
595
|
//*** Upload the data file to metacat.
|
596
|
getMetacatClient().upload(newDocId, fNm, inputStream, Integer.MAX_VALUE);
|
597
|
result = "Updated to new document (from " + lastDocId + " to " + newDocId + ")";
|
598
|
|
599
|
//*** Upload the metadata file to metacat.
|
600
|
xPathQuery = ClientFgdcHelper.XPATH_QUERY_TEMPLATE.replaceFirst("%1s", metadataDocId);
|
601
|
newDocId = updateMetadataDoc(metadataDocId, xPathQuery, null);
|
602
|
|
603
|
//*** Save the new meta Doc Id for re-query.
|
604
|
clientViewBean.setMetaFileDocId(newDocId);
|
605
|
clientViewBean.setAction("read"); //*** Set for re-query.
|
606
|
|
607
|
} else {
|
608
|
//***TODO This is EML.
|
609
|
clientViewBean.setContentStandard(ClientView.ECOLOGICAL_METADATA_LANGUAGE);
|
610
|
|
611
|
//*** Save the old meta Doc Id for re-query.
|
612
|
clientViewBean.setMetaFileDocId(metadataDocId);
|
613
|
clientViewBean.setAction("read"); //*** Set for re-query.
|
614
|
result = "Currently this functionality only supports FGDC metadata.";
|
615
|
}
|
616
|
}
|
617
|
} catch (java.io.IOException ex) {
|
618
|
ex.printStackTrace();
|
619
|
}
|
620
|
} else {
|
621
|
result = "Please enter the updated file path/name.";
|
622
|
}
|
623
|
clientViewBean.setMessage(ClientView.UPDATE_MESSAGE, result);
|
624
|
return(result);
|
625
|
}
|
626
|
|
627
|
private String updateMetadataDoc(String lastDocId, String docIdPath, String origFileName) {
|
628
|
String newDocId = null;
|
629
|
Reader reader;
|
630
|
|
631
|
//*** Update the metadata with the new Doc Id version.
|
632
|
try {
|
633
|
newDocId = nextVersion(lastDocId, docIdPath);
|
634
|
if (origFileName != null) {
|
635
|
if (clientViewBean.getContentStandard().equals(ClientView.FEDERAL_GEOGRAPHIC_DATA_COMMITTEE))
|
636
|
ClientFgdcHelper.updateMetadataFileName(getMetadataDoc().getDocumentElement(), newDocId, origFileName);
|
637
|
else
|
638
|
; //TODO EML, etc.
|
639
|
}
|
640
|
//*** Upload the metadata file to metacat.
|
641
|
reader = XMLUtilities.getDOMTreeAsReader(getMetadataDoc().getDocumentElement(), false);
|
642
|
getMetacatClient().update(newDocId, reader, null);
|
643
|
reader.close();
|
644
|
} catch (Exception ex) {
|
645
|
ex.printStackTrace();
|
646
|
}
|
647
|
return(newDocId);
|
648
|
}
|
649
|
|
650
|
private InputStream getNextInputStream(MultipartParser multipartParser, StringBuffer fileName, HashMap paramsMap)
|
651
|
throws IOException {
|
652
|
InputStream result = null;
|
653
|
Part part;
|
654
|
String parmName = null, value = null, fnam;
|
655
|
|
656
|
while ((part = multipartParser.readNextPart()) != null) {
|
657
|
if (part.isParam()) {
|
658
|
parmName = part.getName();
|
659
|
value = ((ParamPart) part).getStringValue();
|
660
|
paramsMap.put(parmName, value);
|
661
|
|
662
|
} else if (part.isFile()) {
|
663
|
fnam = ((FilePart) part).getFileName();
|
664
|
if (fnam != null && !fnam.equals("")) {
|
665
|
//*** File name is passed back via StringBuffer fileName param.
|
666
|
fileName.append(fnam);
|
667
|
result = ((FilePart) part).getInputStream();
|
668
|
break;
|
669
|
}
|
670
|
}
|
671
|
}
|
672
|
return(result);
|
673
|
}
|
674
|
|
675
|
private void getRemainingParameters(MultipartParser multipartParser, HashMap paramsMap)
|
676
|
throws IOException {
|
677
|
InputStream result = null;
|
678
|
Part part;
|
679
|
String parmName = null, value = null, fnam;
|
680
|
|
681
|
while ((part = multipartParser.readNextPart()) != null) {
|
682
|
if (part.isParam()) {
|
683
|
parmName = part.getName();
|
684
|
value = ((ParamPart) part).getStringValue();
|
685
|
paramsMap.put(parmName, value);
|
686
|
}
|
687
|
}
|
688
|
}
|
689
|
|
690
|
/**
|
691
|
* Queries Metacat for document listings, and returns the results in a TreeMap,
|
692
|
* where the key is the Doc Id, and the value is the Create Date. If the document
|
693
|
* contains the specified 'returnfield', an addtional entry will be created with
|
694
|
* the value being a Vector of sub-DocId's. The key of this entry will be the
|
695
|
* original DocId with some addtional text added.
|
696
|
* Reads bean properties 'pathExpr' (String[]), 'pathValue' (String)
|
697
|
* and 'returnfield' (String).
|
698
|
* @return TreeMap
|
699
|
*/
|
700
|
public TreeMap getSelectQueryMap() {
|
701
|
TreeMap result;
|
702
|
Document doc;
|
703
|
NodeList nodeLst, subNodeLst;
|
704
|
Node node, subNode;
|
705
|
String key, val, paramExpr, paramVal;
|
706
|
String value, returnFld;
|
707
|
String path;
|
708
|
Vector optGroup;
|
709
|
final String DOCID_EXPR = "./docid";
|
710
|
final String DOCNAME_EXPR = "./createdate";
|
711
|
final String PARAM_EXPR = "./param[@name='%1s']";
|
712
|
|
713
|
path = clientViewBean.getPathExpr();
|
714
|
returnFld = clientViewBean.getReturnfield();
|
715
|
value = clientViewBean.getPathValue();
|
716
|
|
717
|
result = new TreeMap();
|
718
|
//paramExpr = String.format(PARAM_EXPR, returnFld);
|
719
|
paramExpr = PARAM_EXPR.replaceFirst("%1s", returnFld);
|
720
|
//*** Query the database ***
|
721
|
doc = query(path, value, returnFld);
|
722
|
//*** Build the TreeMap to return ***
|
723
|
try {
|
724
|
nodeLst = (NodeList) xpath.evaluate("/resultset/document", doc, XPathConstants.NODESET);
|
725
|
for (int i = 0; i < nodeLst.getLength(); i++) {
|
726
|
node = nodeLst.item(i);
|
727
|
key = xpath.evaluate(DOCID_EXPR, node);
|
728
|
val = xpath.evaluate(DOCNAME_EXPR, node);
|
729
|
result.put(key, key + " (" + val + ")");
|
730
|
|
731
|
//*** returnfield values ***
|
732
|
subNodeLst = (NodeList) xpath.evaluate(paramExpr, node, XPathConstants.NODESET);
|
733
|
if (subNodeLst.getLength() > 0) {
|
734
|
optGroup = new Vector();
|
735
|
for (int k = 0; k < subNodeLst.getLength(); k++) {
|
736
|
subNode = subNodeLst.item(k);
|
737
|
paramVal = xpath.evaluate("text()", subNode);
|
738
|
optGroup.add(paramVal);
|
739
|
}
|
740
|
result.put(key + " Data Files", optGroup);
|
741
|
}
|
742
|
|
743
|
}
|
744
|
} catch (XPathExpressionException ex) {
|
745
|
ex.printStackTrace();
|
746
|
}
|
747
|
return(result);
|
748
|
}
|
749
|
|
750
|
/**
|
751
|
* Query metacat for documents that 'CONTAINS' the value at the specified XPath
|
752
|
* expression. Additionally, returns another non-standard field value.
|
753
|
* Standard info contains: DocId, DocName, DocType, CreateDate, and UpdateDate.
|
754
|
* @param pathExpr String contianing an XPath expression.
|
755
|
* @param pathValue String containing a comparison value at the XPath expression.
|
756
|
* @param returnFld String containing an XPath expression to a field which will be returned
|
757
|
* in addition to the standard info.
|
758
|
* @return DOM Document containing the results.
|
759
|
*/
|
760
|
public Document query(String pathExpr, String pathValue, String returnFld) {
|
761
|
Document result = null;
|
762
|
InputStream response;
|
763
|
BufferedReader buffy;
|
764
|
Properties prop;
|
765
|
|
766
|
try {
|
767
|
prop = new Properties();
|
768
|
prop.put("action", "query");
|
769
|
prop.put("qformat", "xml");
|
770
|
prop.put(pathExpr, pathValue);
|
771
|
if (returnFld != null) {
|
772
|
prop.put("returnfield", returnFld);
|
773
|
}
|
774
|
|
775
|
response = metacatClient.sendData(prop, null, null, 0);
|
776
|
if (response != null) {
|
777
|
buffy = new BufferedReader(new InputStreamReader(response));
|
778
|
result = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
|
779
|
}
|
780
|
} catch (IOException ex) {
|
781
|
ex.printStackTrace();
|
782
|
} catch (Exception ex) {
|
783
|
ex.printStackTrace();
|
784
|
}
|
785
|
return(result);
|
786
|
}
|
787
|
|
788
|
public void setMetadataDoc(Document doc) {
|
789
|
metadataDoc = doc;
|
790
|
}
|
791
|
|
792
|
public void setMetadataDoc(String docId) throws Exception {
|
793
|
Document doc = null;
|
794
|
BufferedReader buffy;
|
795
|
Properties prop;
|
796
|
InputStream response;
|
797
|
|
798
|
//*** MetaCatServlet Properties: action, qformat and docid. ***
|
799
|
prop = new Properties();
|
800
|
prop.put("action", "read");
|
801
|
prop.put("qformat", "xml");
|
802
|
prop.put("docid", docId);
|
803
|
response = metacatClient.sendData(prop, null, null, 0);
|
804
|
if (response != null) {
|
805
|
buffy = new BufferedReader(new InputStreamReader(response));
|
806
|
doc = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
|
807
|
response.close();
|
808
|
}
|
809
|
setMetadataDoc(doc);
|
810
|
}
|
811
|
|
812
|
public void setMetadataDoc(InputStream ioStream) throws IOException {
|
813
|
BufferedReader buffy;
|
814
|
|
815
|
if (ioStream != null) {
|
816
|
buffy = new BufferedReader(new InputStreamReader(ioStream));
|
817
|
metadataDoc = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
|
818
|
}
|
819
|
}
|
820
|
|
821
|
public Document getMetadataDoc() {
|
822
|
return(metadataDoc);
|
823
|
}
|
824
|
|
825
|
public String nextVersion(String lastDocId, String xPathQuery) throws XPathExpressionException {
|
826
|
String result = null, tokens[], scope, ready2Split, tmp;
|
827
|
int vers, docNum;
|
828
|
final int LAST_TOKEN = 2;
|
829
|
final String TEMPLATE = "%1s.%2d.%3d";
|
830
|
Node node;
|
831
|
|
832
|
//*** Parse the last Doc Id, and increment the version number.
|
833
|
if(lastDocId != null && lastDocId.indexOf(".") > -1) {
|
834
|
ready2Split = lastDocId.replace('.','~'); //*** This is necessary for the split to work.
|
835
|
tokens = ready2Split.split("~");
|
836
|
if(tokens.length > LAST_TOKEN && !tokens[LAST_TOKEN].equals("")) {
|
837
|
scope = tokens[LAST_TOKEN - 2];
|
838
|
docNum = Integer.parseInt(tokens[LAST_TOKEN - 1]);
|
839
|
try {
|
840
|
vers = Integer.parseInt(tokens[LAST_TOKEN]);
|
841
|
//result = String.format(TEMPLATE, scope, docNum, 1 + vers);
|
842
|
tmp = TEMPLATE.replaceFirst("%1s", scope);
|
843
|
tmp = tmp.replaceFirst("%2d", String.valueOf(docNum));
|
844
|
result = tmp.replaceFirst("%3d", String.valueOf(vers + 1));
|
845
|
|
846
|
} catch (NumberFormatException ex) {
|
847
|
//*** In case the lastDocId has something other than a number.
|
848
|
//result = String.format(TEMPLATE, scope, docNum, 1);
|
849
|
tmp = TEMPLATE.replaceFirst("%1s", scope);
|
850
|
tmp = tmp.replaceFirst("%2d", String.valueOf(docNum));
|
851
|
result = tmp.replaceFirst("%3d", "1");
|
852
|
}
|
853
|
} else {
|
854
|
//*** In case the lastDocId ends with a '.'
|
855
|
result = lastDocId + "1";
|
856
|
}
|
857
|
} else {
|
858
|
//*** In case of missing doc Id.
|
859
|
result = null;
|
860
|
}
|
861
|
//*** Update the Doc Id in the metadata file.
|
862
|
if (getMetadataDoc() != null) {
|
863
|
node = (Node) xpath.evaluate(xPathQuery, getMetadataDoc().getDocumentElement(), XPathConstants.NODE);
|
864
|
setTextContent(xpath, node, result);
|
865
|
}
|
866
|
return(result);
|
867
|
}
|
868
|
|
869
|
private String nextDocId(String lastDocId, String scope) {
|
870
|
String result = null, tokens[], tmp;
|
871
|
int vers;
|
872
|
String template = scope.toLowerCase() + ".%1d.%2d";
|
873
|
|
874
|
if(lastDocId != null && lastDocId.indexOf(".") > -1) {
|
875
|
lastDocId = lastDocId.replace('.','~'); //*** This is necessary for the split to work.
|
876
|
tokens = lastDocId.split("~");
|
877
|
if(tokens.length > 1 && !tokens[1].equals("")) {
|
878
|
try {
|
879
|
vers = Integer.parseInt(tokens[1]);
|
880
|
//result = String.format(template, 1 + vers, 1);
|
881
|
tmp = template.replaceFirst("%1d", String.valueOf(1 + vers));
|
882
|
result = tmp.replaceFirst("%2d", "1");
|
883
|
} catch (NumberFormatException ex) {
|
884
|
//*** In case the lastDocId has something other than a number.
|
885
|
//result = String.format(template, 1, 1);
|
886
|
tmp = template.replaceFirst("%1d", "1");
|
887
|
result = tmp.replaceFirst("%2d", "1");
|
888
|
}
|
889
|
} else {
|
890
|
//*** In case the lastDocId ends with a '.'
|
891
|
//result = String.format(template, 1, 1);
|
892
|
tmp = template.replaceFirst("%1d", "1");
|
893
|
result = tmp.replaceFirst("%2d", "1");
|
894
|
}
|
895
|
} else {
|
896
|
//*** In case there isn't any doc Id's with the user name.
|
897
|
//result = String.format(template, 1, 1);
|
898
|
tmp = template.replaceFirst("%1d", "1");
|
899
|
result = tmp.replaceFirst("%2d", "1");
|
900
|
}
|
901
|
return(result);
|
902
|
}
|
903
|
|
904
|
public MetacatClient getMetacatClient() {
|
905
|
return(metacatClient);
|
906
|
}
|
907
|
|
908
|
//*** BEGIN: Static utility methods ***
|
909
|
|
910
|
public static String[] parseFileInfo(String fileName) {
|
911
|
String[] result = new String[2];
|
912
|
int idx;
|
913
|
String formatType;
|
914
|
|
915
|
//*** Set the file format (just using file extension for now).
|
916
|
idx = fileName.lastIndexOf(".");
|
917
|
if (idx > 1)
|
918
|
formatType = fileName.substring(idx+1).toUpperCase();
|
919
|
else
|
920
|
formatType = "";
|
921
|
|
922
|
result[ClientView.FORMAT_TYPE] = formatType;
|
923
|
result[ClientView.FILE_NAME] = fileName.toString();
|
924
|
return(result);
|
925
|
}
|
926
|
|
927
|
public static void updateNodeText(Node root, XPath xPath, String expression, String text) {
|
928
|
Node targetNode;
|
929
|
|
930
|
if (text != null && !text.equals("")) {
|
931
|
try {
|
932
|
targetNode = (Node) xPath.evaluate(expression, root, XPathConstants.NODE);
|
933
|
setTextContent(xPath, targetNode, text);
|
934
|
//targetNode.setTextContent(text);
|
935
|
} catch (XPathExpressionException ex) {
|
936
|
ex.printStackTrace();
|
937
|
}
|
938
|
}
|
939
|
}
|
940
|
|
941
|
|
942
|
public static Node getNode(XPath xPath, String expression, Node root) {
|
943
|
Node result = null;
|
944
|
|
945
|
try {
|
946
|
result = (Node) xPath.evaluate(expression, root, XPathConstants.NODE);
|
947
|
} catch (XPathExpressionException ex) {
|
948
|
ex.printStackTrace();
|
949
|
}
|
950
|
return(result);
|
951
|
|
952
|
}
|
953
|
|
954
|
public static String getNodeText(XPath xPath, String expression, Node root) {
|
955
|
Node node;
|
956
|
String result = null;
|
957
|
|
958
|
node = getNode(xPath, expression, root);
|
959
|
if (node != null && !node.equals(""))
|
960
|
result = getTextContent(xPath, node);
|
961
|
//result = node.getTextContent(); Not in java 1.4
|
962
|
return(result);
|
963
|
}
|
964
|
|
965
|
public static String[] getNodeTextList(XPath xPath, String expression, Node root) {
|
966
|
NodeList nodes;
|
967
|
String result[] = new String[0];
|
968
|
int size;
|
969
|
|
970
|
try {
|
971
|
nodes = (NodeList) xPath.evaluate(expression, root, XPathConstants.NODESET);
|
972
|
if (nodes != null && (size = nodes.getLength()) > 0) {
|
973
|
result = new String[size];
|
974
|
for(int i = 0; i < size; i++)
|
975
|
result[i] = getTextContent(xPath, nodes.item(i));
|
976
|
//result[i] = nodes.item(i).getTextContent(); Not in java 1.4
|
977
|
}
|
978
|
} catch (XPathExpressionException ex) {
|
979
|
ex.printStackTrace();
|
980
|
}
|
981
|
return(result);
|
982
|
}
|
983
|
|
984
|
public static Stack getNodeTextStack(XPath xpathInstance, String xpathExpr, Node parentNode) {
|
985
|
String nodeLst[];
|
986
|
Stack result = new Stack();
|
987
|
|
988
|
nodeLst = getNodeTextList(xpathInstance, xpathExpr, parentNode);
|
989
|
for(int i = 0; i < nodeLst.length; i++)
|
990
|
result.push(nodeLst[i]);
|
991
|
return(result);
|
992
|
}
|
993
|
|
994
|
public static String getStringFromInputStream(InputStream input) {
|
995
|
StringBuffer result = new StringBuffer();
|
996
|
BufferedReader in = new BufferedReader(new InputStreamReader(input));
|
997
|
String line;
|
998
|
try {
|
999
|
while ((line = in.readLine()) != null) {
|
1000
|
result.append(line);
|
1001
|
}
|
1002
|
} catch (IOException e) {
|
1003
|
System.out.println("ClientViewHelper.getStringFromInputStream: " + e);
|
1004
|
}
|
1005
|
return result.toString();
|
1006
|
}
|
1007
|
|
1008
|
//*** END: Static utility methods ***
|
1009
|
|
1010
|
public String makeRedirectUrl() {
|
1011
|
String result, docId, message;
|
1012
|
|
1013
|
docId = clientViewBean.getMetaFileDocId();
|
1014
|
if (clientViewBean.getAction().equals(DOWNLOAD_ACTION)) {
|
1015
|
result = null;
|
1016
|
} else if (docId != null && !docId.equals("")) {
|
1017
|
message = clientViewBean.getMessage(ClientView.UPDATE_MESSAGE);
|
1018
|
result = "metacat?action=read&qformat=" +clientViewBean.getQformat()
|
1019
|
+ "&docid=" + docId + "&sessionid=" + clientViewBean.getSessionid() + "&message=" + message;
|
1020
|
} else {
|
1021
|
result = "style/skins/" + clientViewBean.getQformat() + "/confirm.jsp";
|
1022
|
}
|
1023
|
//*** Reset bean action property.
|
1024
|
clientViewBean.setAction("");
|
1025
|
return(result);
|
1026
|
}
|
1027
|
|
1028
|
private HashMap<String, String> download(ClientView bean) {
|
1029
|
Properties args;
|
1030
|
InputStream inStream;
|
1031
|
String docId, metaId, fNm = null, pth, txtLst[];
|
1032
|
String msg = "File '~' (~) downloaded";
|
1033
|
Node branchRoot, metaRoot;
|
1034
|
ByteArrayOutputStream outStream;
|
1035
|
int intMe;
|
1036
|
HashMap<String, String> responseMap = new HashMap<String, String>();
|
1037
|
|
1038
|
docId = bean.getDocId();
|
1039
|
metaId = bean.getMetaFileDocId();
|
1040
|
if (docId != null && metaId != null && !docId.equals("") && !metaId.equals("")) {
|
1041
|
//*** Properties args: key=param_value, value=param_name.
|
1042
|
args = new Properties();
|
1043
|
args.put("read", "action");
|
1044
|
try {
|
1045
|
//*** First, retrieve the metadata and get the original filename.
|
1046
|
//*** Also, if this is the metadata, get a list of docId's for the package.
|
1047
|
setMetadataDoc(metaId);
|
1048
|
metaRoot = getMetadataDoc().getDocumentElement();
|
1049
|
if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
|
1050
|
//*** FGDC
|
1051
|
if (docId.equals(metaId)) { //*** This is the metadata file.
|
1052
|
pth = ClientFgdcHelper.FGDC_DOCID_ROOT_XPATH.replaceFirst("%1s", docId);
|
1053
|
branchRoot = getNode(xpath, pth, getMetadataDoc());
|
1054
|
fNm = getNodeText(xpath, ClientFgdcHelper.FGDC_FILE_NAME_XPATH, branchRoot);
|
1055
|
//include the filename for the docid
|
1056
|
args.put(fNm, docId);
|
1057
|
fNm = toZipFileName(fNm);
|
1058
|
responseMap.put("contentType", "application/zip");
|
1059
|
//*** Get the list of docId's for the entire package.
|
1060
|
args.put(metaId, "docid");
|
1061
|
txtLst = getNodeTextList(xpath, ClientFgdcHelper.FGDC_DATA_FILE_NODES_XPATH, branchRoot);
|
1062
|
for (int i = 0; i < txtLst.length; i++) {
|
1063
|
String additionalDocId = txtLst[i];
|
1064
|
if (additionalDocId != null && additionalDocId.length() > 1) {
|
1065
|
//look up the filename from the metadata
|
1066
|
String tempPath = ClientFgdcHelper.PATH4ANCESTOR.replaceFirst("%1s", additionalDocId);
|
1067
|
tempPath = tempPath.replaceFirst("%2s", "digform");
|
1068
|
Node tempBranchRoot = getNode(xpath, tempPath, getMetadataDoc());
|
1069
|
String tempFileName = getNodeText(xpath, ClientFgdcHelper.FGDC_DATA_FILE_NAME_XPATH, tempBranchRoot);
|
1070
|
//include the docid
|
1071
|
args.put(additionalDocId, "docid");
|
1072
|
//include the filename for the docid
|
1073
|
args.put(tempFileName, additionalDocId);
|
1074
|
}
|
1075
|
}
|
1076
|
args.put("zip", "qformat");
|
1077
|
} else { //*** This is a data file.
|
1078
|
pth = ClientFgdcHelper.PATH4ANCESTOR.replaceFirst("%1s", docId);
|
1079
|
pth = pth.replaceFirst("%2s", "digform");
|
1080
|
branchRoot = getNode(xpath, pth, getMetadataDoc());
|
1081
|
fNm = getNodeText(xpath, ClientFgdcHelper.FGDC_DATA_FILE_NAME_XPATH, branchRoot);
|
1082
|
responseMap.put("contentType", "application/octet-stream");
|
1083
|
args.put(docId, "docid");
|
1084
|
args.put("xml", "qformat");
|
1085
|
}
|
1086
|
} else {
|
1087
|
//*** TODO: EML - this is just some basic code to start with.
|
1088
|
if (docId.equals(metaId)) {
|
1089
|
fNm = "emlMetadata.xml";
|
1090
|
txtLst = new String[] {docId};
|
1091
|
args.put(txtLst[0], "docid");
|
1092
|
args.put("zip", "qformat");
|
1093
|
responseMap.put("contentType", "application/zip");
|
1094
|
} else {
|
1095
|
fNm = "emlData.dat";
|
1096
|
args.put("xml", "qformat");
|
1097
|
args.put(docId, "docid");
|
1098
|
responseMap.put("contentType", "application/octet-stream");
|
1099
|
}
|
1100
|
}
|
1101
|
|
1102
|
//*** Set the filename in the response.
|
1103
|
responseMap.put("Content-Disposition", "attachment; filename=" + fNm);
|
1104
|
|
1105
|
//*** Next, read the file from metacat.
|
1106
|
inStream = metacatClient.sendParameters(args);
|
1107
|
|
1108
|
//*** Then, convert the input stream into an output stream.
|
1109
|
outStream = new ByteArrayOutputStream();
|
1110
|
while ((intMe = inStream.read()) != -1) {
|
1111
|
outStream.write(intMe);
|
1112
|
}
|
1113
|
|
1114
|
//*** Now, write the output stream to the response.
|
1115
|
responseMap.put("outputStream", outStream.toString());
|
1116
|
|
1117
|
//*** Finally, set the message for the user interface to display.
|
1118
|
msg = msg.replaceFirst("~", fNm);
|
1119
|
msg = msg.replaceFirst("~", docId);
|
1120
|
bean.setMessage(ClientView.SELECT_MESSAGE, msg);
|
1121
|
} catch (Exception ex) {
|
1122
|
ex.printStackTrace();
|
1123
|
bean.setMessage(ClientView.SELECT_MESSAGE, ex.getMessage());
|
1124
|
}
|
1125
|
}
|
1126
|
responseMap.put("message", bean.getMessage(ClientView.SELECT_MESSAGE));
|
1127
|
return(responseMap);
|
1128
|
}
|
1129
|
|
1130
|
private void handleDownloadResponse(HashMap responseMap, HttpServletResponse response) throws IOException {
|
1131
|
ByteArrayOutputStream outStream;
|
1132
|
String contentDisposition, contentType;
|
1133
|
|
1134
|
contentType = (String) responseMap.get("contentType");
|
1135
|
contentDisposition = (String) responseMap.get("Content-Disposition");
|
1136
|
outStream = (ByteArrayOutputStream) responseMap.get("outputStream");
|
1137
|
|
1138
|
response.setContentType(contentType);
|
1139
|
response.setHeader("Content-Disposition", contentDisposition);
|
1140
|
response.setContentLength(outStream.size());
|
1141
|
outStream.writeTo(response.getOutputStream());
|
1142
|
response.flushBuffer();
|
1143
|
}
|
1144
|
|
1145
|
public static String toZipFileName(String fileName) {
|
1146
|
String result = "metacat";
|
1147
|
int idx;
|
1148
|
|
1149
|
if (fileName != null && !fileName.equals("") && !fileName.equals(".")) {
|
1150
|
idx = fileName.indexOf('.');
|
1151
|
if (idx > -1)
|
1152
|
result = fileName.substring(0, idx);
|
1153
|
else
|
1154
|
result = fileName;
|
1155
|
}
|
1156
|
result += ".zip";
|
1157
|
return(result);
|
1158
|
}
|
1159
|
|
1160
|
public static void setTextContent(XPath xPath, Node elementNode, String content) throws DOMException {
|
1161
|
Text textNode, newTxtNode;
|
1162
|
Document document;
|
1163
|
|
1164
|
textNode = (Text) getNode(xPath, "text()", elementNode);
|
1165
|
if (textNode != null) {
|
1166
|
if (isElementContentWhitespace(textNode)) {
|
1167
|
//*** If there is an existing text node, and it's whitespace,
|
1168
|
//*** create a new text node and insert it before whitespace.
|
1169
|
document = elementNode.getOwnerDocument();
|
1170
|
newTxtNode = document.createTextNode(content);
|
1171
|
elementNode.insertBefore(newTxtNode, textNode);
|
1172
|
} else {
|
1173
|
//*** If there is an existing text node, and it has content,
|
1174
|
//*** overwrite the existing text.
|
1175
|
textNode.setNodeValue(content);
|
1176
|
}
|
1177
|
} else {
|
1178
|
//*** If there isn't an existing text node,
|
1179
|
//*** create a new text node and append it to the elementNode.
|
1180
|
document = elementNode.getOwnerDocument();
|
1181
|
newTxtNode = document.createTextNode(content);
|
1182
|
elementNode.appendChild(newTxtNode);
|
1183
|
}
|
1184
|
}
|
1185
|
|
1186
|
public static String getTextContent(XPath xPath, Node elementNode) throws DOMException {
|
1187
|
String result = "";
|
1188
|
Text textNode;
|
1189
|
|
1190
|
if (elementNode.getNodeType() != Node.TEXT_NODE)
|
1191
|
textNode = (Text) getNode(xPath, "text()", elementNode);
|
1192
|
else
|
1193
|
textNode = (Text) elementNode;
|
1194
|
if (textNode != null)
|
1195
|
result = textNode.getNodeValue();
|
1196
|
return(result);
|
1197
|
}
|
1198
|
|
1199
|
public static boolean isElementContentWhitespace(Text textNode) {
|
1200
|
boolean result = false;
|
1201
|
String val;
|
1202
|
|
1203
|
if ((val = textNode.getNodeValue()) != null) {
|
1204
|
if (val != null) {
|
1205
|
val = val.trim();
|
1206
|
result = (val.length() == 0);
|
1207
|
}
|
1208
|
}
|
1209
|
return(result);
|
1210
|
}
|
1211
|
|
1212
|
}
|