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