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