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