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