Revision 2083
Added by Duane Costa over 20 years ago
src/edu/ucsb/nceas/metacat/harvesterClient/MetUpload.java | ||
---|---|---|
20 | 20 |
import edu.ucsb.nceas.utilities.IOUtil; |
21 | 21 |
|
22 | 22 |
/** |
23 |
* MetUpload implements a Harvester servlet to upload a single file to Metacat |
|
23 |
* MetUpload implements a Harvester servlet to insert, update, or delete |
|
24 |
* a single file to Metacat. |
|
24 | 25 |
*/ |
25 | 26 |
public class MetUpload extends HttpServlet { |
26 |
private Metacat m; |
|
27 |
private String insertXML = ""; |
|
28 |
private String metacatUrl; |
|
29 |
private String userName = System.getProperty("user.name"); |
|
30 |
private String tmpDirPath = "/tmp/" + userName; |
|
31 |
private File tmpDir = new File(tmpDirPath); |
|
32 | 27 |
|
33 |
/** |
|
34 |
* Service requests made to the Harvester MetUpload servlet |
|
35 |
* |
|
36 |
* @param req The request |
|
37 |
* @param res The response |
|
38 |
* @throws IOException |
|
39 |
*/ |
|
40 |
public void service(HttpServletRequest req, |
|
41 |
HttpServletResponse res) throws IOException { |
|
42 |
int strLen; |
|
43 |
File tmpFile; |
|
44 |
FilePart fPart = null; |
|
45 |
FileReader fr; |
|
46 |
Hashtable formElements = new Hashtable(); |
|
47 |
HttpSession sess; |
|
48 |
MultipartParser parser; |
|
49 |
ParamPart pPart; |
|
50 |
Part aPart; |
|
51 |
PrintWriter out = null; |
|
52 |
String docid = ""; |
|
53 |
String tmpFileName = ""; |
|
54 |
String tmpFilePath = ""; |
|
55 |
String fieldName; |
|
56 |
String fieldValue; |
|
57 |
String metacatResponse; |
|
58 |
String password = null; |
|
59 |
String revision; |
|
60 |
String updateResult; |
|
61 |
String username = null; |
|
62 |
StringReader sr; |
|
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", ""); |
|
63 | 57 |
|
64 |
if (userName.equals("harvest")) { |
|
65 |
metacatUrl = "http://knb.lternet.edu:8888/knb/servlet/metacat"; |
|
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 |
} |
|
66 | 76 |
} |
67 |
else { |
|
68 |
metacatUrl = "http://knb.lternet.edu:8088/knb/servlet/metacat"; |
|
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); |
|
69 | 82 |
} |
70 | 83 |
|
71 |
try { |
|
72 |
out = res.getWriter(); |
|
73 |
parser = new MultipartParser(req, 1024 * 1024); |
|
84 |
} |
|
85 |
} |
|
86 |
catch (Exception e) { |
|
87 |
System.out.println("Error parsing parameters: " + e.getMessage()); |
|
88 |
} |
|
74 | 89 |
|
75 |
while ((aPart = parser.readNextPart()) != null) { |
|
76 |
if (aPart.isParam() == true) { |
|
77 |
pPart = (ParamPart) aPart; |
|
78 |
fieldName = pPart.getName(); |
|
79 |
fieldValue = pPart.getStringValue(); |
|
90 |
if (upload) { |
|
91 |
upload(out, docid, sr, metacatUrl, username, password); |
|
92 |
} |
|
93 |
if (delete) { |
|
94 |
delete(out, docid, metacatUrl, username, password); |
|
95 |
} |
|
96 |
} |
|
80 | 97 |
|
81 |
if (fieldName != null) { |
|
82 |
if (fieldValue == null) { |
|
83 |
fieldValue = ""; |
|
84 |
} |
|
85 | 98 |
|
86 |
formElements.put(fieldName, fieldValue); |
|
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; |
|
87 | 116 |
|
88 |
if (fieldName.equals("docid")) { |
|
89 |
docid = fieldValue; |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
else if (aPart.isFile() == true) { |
|
94 |
fPart = (FilePart)aPart; |
|
95 |
fieldName = fPart.getName(); |
|
96 |
tmpFileName = fPart.getFileName(); |
|
117 |
if (docid.equals("")) { |
|
118 |
out.println("Error deleting document: No DocID specified"); |
|
119 |
return; |
|
120 |
} |
|
97 | 121 |
|
98 |
if (tmpFileName != null) { |
|
99 |
// Create the temporary directory if it doesn't exist |
|
100 |
try { |
|
101 |
if (!tmpDir.exists()) { |
|
102 |
tmpDir.mkdirs(); |
|
103 |
} |
|
104 |
} catch (SecurityException se) { |
|
105 |
out.println("Can't create temporary directory: " + |
|
106 |
tmpDir.getPath()); |
|
107 |
out.println(se.getMessage()); |
|
108 |
} |
|
109 |
// Write the image to a file |
|
110 |
tmpFile = new File(tmpDirPath, tmpFileName); |
|
111 |
fPart.writeTo(tmpFile); |
|
112 |
tmpFilePath = tmpDirPath + "/" + tmpFileName; |
|
113 |
formElements.put(fieldName, tmpFileName); |
|
114 |
break; |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
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 |
} |
|
118 | 144 |
|
119 |
sess = req.getSession(true); |
|
120 |
username = (String) sess.getAttribute("Musername"); |
|
121 |
password = (String) sess.getAttribute("Mpassword"); |
|
145 |
} |
|
122 | 146 |
|
123 |
try { |
|
124 |
fr = new FileReader(tmpFilePath); |
|
125 |
insertXML = IOUtil.getAsString(fr, true); |
|
126 |
} catch (IOException ioe) { |
|
127 |
out.println("Error reading file: " + tmpFilePath); |
|
128 |
out.println(ioe.getMessage()); |
|
129 |
} |
|
130 | 147 |
|
131 |
try { |
|
132 |
m = MetacatFactory.createMetacatConnection(metacatUrl); |
|
133 |
} catch (MetacatInaccessibleException mie) { |
|
134 |
out.println("Metacat connection failed."); |
|
135 |
out.println(mie.getMessage()); |
|
136 |
} |
|
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(); |
|
137 | 159 |
|
138 |
try { |
|
139 |
m.login(username, password); |
|
140 |
strLen = docid.length(); |
|
141 |
revision = docid.substring((strLen - 2), strLen); |
|
142 |
sr = new StringReader(insertXML); |
|
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); |
|
143 | 214 |
|
144 |
if (revision.equals(".1")) {
|
|
145 |
metacatResponse = m.insert(docid, sr, null);
|
|
146 |
}
|
|
147 |
else {
|
|
148 |
metacatResponse = m.update(docid, sr, null);
|
|
149 |
}
|
|
215 |
if (revision.equals(".1")) { |
|
216 |
metacatResponse = metacat.insert(docid, sr, null);
|
|
217 |
} |
|
218 |
else { |
|
219 |
metacatResponse = metacat.update(docid, sr, null);
|
|
220 |
} |
|
150 | 221 |
|
151 |
out.println(metacatResponse); |
|
152 |
} catch (Exception e) { |
|
153 |
out.println("General exception:\n" + e.getMessage()); |
|
154 |
} |
|
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 |
} |
|
155 | 244 |
|
156 |
// Clean up the temporary file |
|
157 |
if (!tmpFileName.equals("")) { |
|
158 |
tmpFile = new File(tmpDirPath, tmpFileName); |
|
159 |
tmpFile.delete(); |
|
160 |
} |
|
245 |
} |
|
246 |
|
|
161 | 247 |
|
162 |
} catch (Exception e) { |
|
163 |
out.println("An error occurred while attempting to upload."); |
|
164 |
e.printStackTrace(out); |
|
165 |
} |
|
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 = ""; |
|
166 | 271 |
|
167 |
out.flush(); |
|
272 |
if ((fileName == null) || fileName.equals("")) { |
|
273 |
return sr; |
|
168 | 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 |
|
|
169 | 308 |
} |
Also available in: Unified diff
Add delete capability to single file upload servlet