1 |
2094
|
jones
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2004 University of New Mexico and the
|
4 |
|
|
* Regents of the University of California
|
5 |
|
|
*
|
6 |
|
|
* '$Author$'
|
7 |
|
|
* '$Date$'
|
8 |
|
|
* '$Revision$'
|
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 |
1949
|
costa
|
package edu.ucsb.nceas.metacat.harvesterClient;
|
26 |
|
|
|
27 |
|
|
import java.io.*;
|
28 |
|
|
import java.util.*;
|
29 |
1737
|
skrish
|
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 |
1741
|
skrish
|
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 |
1830
|
skrish
|
import edu.ucsb.nceas.metacat.client.*;
|
44 |
|
|
import edu.ucsb.nceas.utilities.IOUtil;
|
45 |
1741
|
skrish
|
|
46 |
1949
|
costa
|
/**
|
47 |
2083
|
costa
|
* MetUpload implements a Harvester servlet to insert, update, or delete
|
48 |
|
|
* a single file to Metacat.
|
49 |
1949
|
costa
|
*/
|
50 |
|
|
public class MetUpload extends HttpServlet {
|
51 |
1737
|
skrish
|
|
52 |
2083
|
costa
|
/**
|
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 |
1737
|
skrish
|
|
82 |
2083
|
costa
|
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 |
1949
|
costa
|
}
|
101 |
2083
|
costa
|
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 |
1949
|
costa
|
}
|
107 |
1737
|
skrish
|
|
108 |
2083
|
costa
|
}
|
109 |
|
|
}
|
110 |
|
|
catch (Exception e) {
|
111 |
|
|
System.out.println("Error parsing parameters: " + e.getMessage());
|
112 |
|
|
}
|
113 |
1737
|
skrish
|
|
114 |
2083
|
costa
|
if (upload) {
|
115 |
|
|
upload(out, docid, sr, metacatUrl, username, password);
|
116 |
|
|
}
|
117 |
|
|
if (delete) {
|
118 |
|
|
delete(out, docid, metacatUrl, username, password);
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
1737
|
skrish
|
|
122 |
|
|
|
123 |
2083
|
costa
|
/**
|
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 |
1737
|
skrish
|
|
141 |
2083
|
costa
|
if (docid.equals("")) {
|
142 |
|
|
out.println("Error deleting document: No DocID specified");
|
143 |
|
|
return;
|
144 |
|
|
}
|
145 |
1741
|
skrish
|
|
146 |
2083
|
costa
|
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 |
1741
|
skrish
|
|
169 |
2083
|
costa
|
}
|
170 |
1737
|
skrish
|
|
171 |
|
|
|
172 |
2083
|
costa
|
/**
|
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 |
1737
|
skrish
|
|
184 |
2083
|
costa
|
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 |
1949
|
costa
|
|
239 |
2083
|
costa
|
if (revision.equals(".1")) {
|
240 |
|
|
metacatResponse = metacat.insert(docid, sr, null);
|
241 |
|
|
}
|
242 |
|
|
else {
|
243 |
|
|
metacatResponse = metacat.update(docid, sr, null);
|
244 |
|
|
}
|
245 |
1737
|
skrish
|
|
246 |
2083
|
costa
|
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 |
1949
|
costa
|
|
269 |
2083
|
costa
|
}
|
270 |
|
|
|
271 |
1737
|
skrish
|
|
272 |
2083
|
costa
|
/**
|
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 |
1949
|
costa
|
|
296 |
2083
|
costa
|
if ((fileName == null) || fileName.equals("")) {
|
297 |
|
|
return sr;
|
298 |
1949
|
costa
|
}
|
299 |
2083
|
costa
|
|
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 |
1737
|
skrish
|
}
|