1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2004 University of New Mexico and the
|
4
|
* Regents of the University of California
|
5
|
*
|
6
|
* '$Author: daigle $'
|
7
|
* '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
|
8
|
* '$Revision: 4080 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
|
25
|
package edu.ucsb.nceas.metacat.harvesterClient;
|
26
|
|
27
|
import edu.ucsb.nceas.utilities.Options;
|
28
|
import java.io.*;
|
29
|
import java.util.*;
|
30
|
import javax.servlet.ServletConfig;
|
31
|
import javax.servlet.ServletContext;
|
32
|
import javax.servlet.ServletException;
|
33
|
import javax.servlet.ServletInputStream;
|
34
|
import javax.servlet.http.HttpServlet;
|
35
|
import javax.servlet.http.HttpServletRequest;
|
36
|
import javax.servlet.http.HttpServletResponse;
|
37
|
import javax.servlet.http.HttpSession;
|
38
|
import javax.servlet.http.HttpUtils;
|
39
|
import javax.servlet.ServletOutputStream;
|
40
|
import com.oreilly.servlet.multipart.FilePart;
|
41
|
import com.oreilly.servlet.multipart.MultipartParser;
|
42
|
import com.oreilly.servlet.multipart.ParamPart;
|
43
|
import com.oreilly.servlet.multipart.Part;
|
44
|
|
45
|
import edu.ucsb.nceas.metacat.client.*;
|
46
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
47
|
import edu.ucsb.nceas.metacat.service.ServiceException;
|
48
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
49
|
import edu.ucsb.nceas.utilities.GeneralPropertyException;
|
50
|
import edu.ucsb.nceas.utilities.IOUtil;
|
51
|
|
52
|
/**
|
53
|
* MetUpload implements a Harvester servlet to insert, update, or delete
|
54
|
* a single file to Metacat.
|
55
|
*/
|
56
|
public class MetUpload extends HttpServlet {
|
57
|
|
58
|
/*
|
59
|
* Class fields
|
60
|
*/
|
61
|
private static final String CONFIG_DIR = "WEB-INF";
|
62
|
private static final String CONFIG_NAME = "metacat.properties";
|
63
|
|
64
|
/* Object fields */
|
65
|
private ServletConfig config = null;
|
66
|
private ServletContext context = null;
|
67
|
private String metacatURL;
|
68
|
|
69
|
/**
|
70
|
* Handle POST requests made to the Harvester MetUpload servlet.
|
71
|
*
|
72
|
* @param req The request
|
73
|
* @param res The response
|
74
|
* @throws IOException
|
75
|
*/
|
76
|
public void doPost(HttpServletRequest req, HttpServletResponse res)
|
77
|
throws IOException {
|
78
|
Part aPart;
|
79
|
boolean delete = false;
|
80
|
String docid = "";
|
81
|
String fieldName;
|
82
|
String fieldValue;
|
83
|
String fileName = "";
|
84
|
FilePart fPart = null;
|
85
|
PrintWriter out = res.getWriter();
|
86
|
MultipartParser parser = new MultipartParser(req, 1024 * 1024);
|
87
|
ParamPart pPart;
|
88
|
HttpSession sess = req.getSession(true);
|
89
|
String password = (String) sess.getAttribute("password");
|
90
|
StringReader sr = null;
|
91
|
boolean upload = false;
|
92
|
String username = (String) sess.getAttribute("username");
|
93
|
|
94
|
res.setContentType("text/plain");
|
95
|
|
96
|
try {
|
97
|
while ((aPart = parser.readNextPart()) != null) {
|
98
|
|
99
|
if (aPart.isParam() == true) {
|
100
|
pPart = (ParamPart) aPart;
|
101
|
fieldName = pPart.getName();
|
102
|
|
103
|
if (fieldName != null) {
|
104
|
if (fieldName.equals("docid")) {
|
105
|
docid = pPart.getStringValue();
|
106
|
}
|
107
|
else if (fieldName.equals("Upload")) {
|
108
|
upload = true;
|
109
|
}
|
110
|
else if (fieldName.equals("Delete")) {
|
111
|
delete = true;
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
else if (aPart.isFile() == true) {
|
116
|
fPart = (FilePart) aPart;
|
117
|
fieldName = fPart.getName();
|
118
|
fileName = fPart.getFileName();
|
119
|
sr = writeTempFile(fPart, out, docid, fileName);
|
120
|
}
|
121
|
|
122
|
}
|
123
|
}
|
124
|
catch (Exception e) {
|
125
|
System.out.println("Error parsing parameters: " + e.getMessage());
|
126
|
}
|
127
|
|
128
|
if (upload) {
|
129
|
upload(out, docid, sr, username, password);
|
130
|
}
|
131
|
if (delete) {
|
132
|
delete(out, docid, username, password);
|
133
|
}
|
134
|
}
|
135
|
|
136
|
|
137
|
/**
|
138
|
* Deletes a file from Metacat based on its docid.
|
139
|
*
|
140
|
* @param out the PrintWriter output stream
|
141
|
* @param docid the Metacat document id, e.g. document.1.1
|
142
|
* @param username the Metacat username
|
143
|
* @param password the Metacat password
|
144
|
*/
|
145
|
private void delete(PrintWriter out,
|
146
|
String docid,
|
147
|
String username,
|
148
|
String password
|
149
|
) {
|
150
|
Metacat metacat;
|
151
|
String metacatResponse;
|
152
|
|
153
|
if (docid.equals("")) {
|
154
|
out.println("Error deleting document: No DocID specified");
|
155
|
return;
|
156
|
}
|
157
|
|
158
|
try {
|
159
|
metacat = MetacatFactory.createMetacatConnection(metacatURL);
|
160
|
metacat.login(username, password);
|
161
|
metacatResponse = metacat.delete(docid);
|
162
|
out.println(metacatResponse);
|
163
|
}
|
164
|
catch (MetacatAuthException e) {
|
165
|
out.println("Metacat delete failed: MetacatAuthException:" +
|
166
|
e.getMessage());
|
167
|
}
|
168
|
catch (MetacatInaccessibleException e) {
|
169
|
out.println("Metacat delete failed: MetacatInaccessibleException:" +
|
170
|
e.getMessage());
|
171
|
}
|
172
|
catch (InsufficientKarmaException e) {
|
173
|
out.println("Metacat delete failed: InsufficientKarmaException:" +
|
174
|
e.getMessage());
|
175
|
}
|
176
|
catch (MetacatException e) {
|
177
|
out.println("Metacat delete failed: MetacatException:" +
|
178
|
e.getMessage());
|
179
|
}
|
180
|
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Initializes the servlet. Reads properties and initializes object fields.
|
185
|
*
|
186
|
* @throws ServletException
|
187
|
*/
|
188
|
public void init(ServletConfig config) throws ServletException {
|
189
|
String dirPath;
|
190
|
|
191
|
super.init(config);
|
192
|
this.config = config;
|
193
|
this.context = config.getServletContext();
|
194
|
dirPath = context.getRealPath(CONFIG_DIR);
|
195
|
File propertyFile = new File(dirPath, CONFIG_NAME);
|
196
|
|
197
|
try {
|
198
|
PropertyService.getInstance(this.context);
|
199
|
metacatURL = SystemUtil.getServletURL();
|
200
|
} catch (ServiceException se) {
|
201
|
System.out.println("Service problem while initializing MetUpload: " + se.getMessage());
|
202
|
} catch (GeneralPropertyException gpe) {
|
203
|
System.out.println("Error initializing properties utility: " + gpe.getMessage());
|
204
|
}
|
205
|
|
206
|
System.out.println("metacatURL: " + metacatURL);
|
207
|
}
|
208
|
|
209
|
|
210
|
/**
|
211
|
* Uploads a file to Metacat for insertion or updating. This implementation
|
212
|
* is limited by the fact that all ".1" documents are inserted while all
|
213
|
* other revisions are updated. This logic can be improved after the Metacat
|
214
|
* interface is extended with additional methods to query information about
|
215
|
* revisions, for example, hasDocument() and highestRevision().
|
216
|
*
|
217
|
* @param out
|
218
|
* the PrintWriter output stream
|
219
|
* @param docid
|
220
|
* the docid, e.g. "document.1.1"
|
221
|
* @param sr
|
222
|
* the StringReader containing the contents of the document
|
223
|
* @param username
|
224
|
* the Metacat username
|
225
|
* @param password
|
226
|
* the Metacat password
|
227
|
*/
|
228
|
private void upload(PrintWriter out,
|
229
|
String docid,
|
230
|
StringReader sr,
|
231
|
String username,
|
232
|
String password
|
233
|
) {
|
234
|
FileReader fr;
|
235
|
Metacat metacat;
|
236
|
String metacatResponse;
|
237
|
String revision;
|
238
|
int strLen;
|
239
|
|
240
|
if (docid.equals("")) {
|
241
|
out.println("Error uploading: No docid specified");
|
242
|
return;
|
243
|
}
|
244
|
|
245
|
if (sr == null) {
|
246
|
out.println("Error uploading: No EML file specified");
|
247
|
return;
|
248
|
}
|
249
|
|
250
|
try {
|
251
|
metacat = MetacatFactory.createMetacatConnection(metacatURL);
|
252
|
metacat.login(username, password);
|
253
|
strLen = docid.length();
|
254
|
revision = docid.substring((strLen - 2), strLen);
|
255
|
|
256
|
if (revision.equals(".1")) {
|
257
|
metacatResponse = metacat.insert(docid, sr, null);
|
258
|
}
|
259
|
else {
|
260
|
metacatResponse = metacat.update(docid, sr, null);
|
261
|
}
|
262
|
|
263
|
out.println(metacatResponse);
|
264
|
}
|
265
|
catch (MetacatAuthException e) {
|
266
|
out.println("Metacat upload failed: MetacatAuthException:" +
|
267
|
e.getMessage());
|
268
|
}
|
269
|
catch (MetacatInaccessibleException e) {
|
270
|
out.println("Metacat upload failed: MetacatInaccessibleException:" +
|
271
|
e.getMessage());
|
272
|
}
|
273
|
catch (InsufficientKarmaException e) {
|
274
|
out.println("Metacat upload failed: InsufficientKarmaException:" +
|
275
|
e.getMessage());
|
276
|
}
|
277
|
catch (MetacatException e) {
|
278
|
out.println("Metacat upload failed: MetacatException:" +
|
279
|
e.getMessage());
|
280
|
}
|
281
|
catch (IOException e) {
|
282
|
out.println("Metacat upload failed: IOException:" +
|
283
|
e.getMessage());
|
284
|
}
|
285
|
|
286
|
}
|
287
|
|
288
|
|
289
|
/**
|
290
|
* Writes the uploaded file to disk and then reads it into a StringReader for
|
291
|
* subsequent upload to Metacat.
|
292
|
*
|
293
|
* @param fPart the FilePart object containing the file form parameter
|
294
|
* @param out the PrintWriter output stream
|
295
|
* @param docid the document id, e.g. "document.1.1"
|
296
|
* @param fileName the name of the file to be written to disk
|
297
|
* @return sr a StringReader containing the contents of the file
|
298
|
*/
|
299
|
private StringReader writeTempFile(FilePart fPart,
|
300
|
PrintWriter out,
|
301
|
String docid,
|
302
|
String fileName
|
303
|
) {
|
304
|
FileReader fr;
|
305
|
String metacatResponse;
|
306
|
StringReader sr = null;
|
307
|
File tmpDir;
|
308
|
String tmpDirPath;
|
309
|
File tmpFile;
|
310
|
String tmpFilePath = "";
|
311
|
String xmlString = "";
|
312
|
|
313
|
if ((fileName == null) || fileName.equals("")) {
|
314
|
return sr;
|
315
|
}
|
316
|
|
317
|
tmpDirPath = System.getProperties().getProperty("java.io.tmpdir");
|
318
|
tmpDir = new File(tmpDirPath);
|
319
|
|
320
|
// Create the temporary directory if it doesn't exist
|
321
|
try {
|
322
|
if (!tmpDir.exists()) {
|
323
|
tmpDir.mkdirs();
|
324
|
}
|
325
|
}
|
326
|
catch (SecurityException e) {
|
327
|
out.println("Can't create directory: " + tmpDir.getPath());
|
328
|
out.println(e.getMessage());
|
329
|
}
|
330
|
|
331
|
// Write the image to a file
|
332
|
try {
|
333
|
tmpFile = new File(tmpDirPath, fileName);
|
334
|
fPart.writeTo(tmpFile);
|
335
|
tmpFilePath = tmpDirPath + File.separator + fileName;
|
336
|
fr = new FileReader(tmpFilePath);
|
337
|
xmlString = IOUtil.getAsString(fr, true);
|
338
|
sr = new StringReader(xmlString);
|
339
|
tmpFile.delete(); // Clean up the temporary file from disk
|
340
|
}
|
341
|
catch (IOException e) {
|
342
|
out.println("IOException: " + tmpFilePath + e.getMessage());
|
343
|
}
|
344
|
|
345
|
return sr;
|
346
|
}
|
347
|
|
348
|
|
349
|
}
|