27 |
27 |
|
28 |
28 |
package edu.ucsb.nceas.metacat;
|
29 |
29 |
|
|
30 |
import com.oreilly.servlet.multipart.FilePart;
|
|
31 |
import com.oreilly.servlet.multipart.MultipartParser;
|
|
32 |
import com.oreilly.servlet.multipart.ParamPart;
|
|
33 |
import com.oreilly.servlet.multipart.Part;
|
|
34 |
|
30 |
35 |
import java.io.File;
|
31 |
36 |
import java.io.PrintWriter;
|
32 |
37 |
import java.io.IOException;
|
... | ... | |
108 |
113 |
private String resultStyleURL = null;
|
109 |
114 |
private String xmlcatalogfile = null;
|
110 |
115 |
private String saxparser = null;
|
111 |
|
private String defaultdatapath = null;
|
|
116 |
private String datafilepath = null;
|
|
117 |
private File dataDirectory = null;
|
112 |
118 |
private String servletpath = null;
|
113 |
119 |
private PropertyResourceBundle options = null;
|
114 |
120 |
private MetaCatUtil util = null;
|
... | ... | |
118 |
124 |
private String htmlpath = null;
|
119 |
125 |
// script to get data file and put it
|
120 |
126 |
// in defaultdocpath dir
|
121 |
|
private String executescript = null;
|
|
127 |
//private String executescript = null;
|
122 |
128 |
|
123 |
129 |
/**
|
124 |
130 |
* Initialize the servlet by creating appropriate database connections
|
... | ... | |
136 |
142 |
resultStyleURL = util.getOption("resultStyleURL");
|
137 |
143 |
xmlcatalogfile = util.getOption("xmlcatalogfile");
|
138 |
144 |
saxparser = util.getOption("saxparser");
|
139 |
|
defaultdatapath = util.getOption("defaultdatapath");
|
140 |
|
executescript = util.getOption("executescript");
|
|
145 |
datafilepath = util.getOption("datafilepath");
|
|
146 |
dataDirectory = new File(datafilepath);
|
|
147 |
//executescript = util.getOption("executescript");
|
141 |
148 |
servletpath = util.getOption("servletpath");
|
142 |
149 |
htmlpath = util.getOption("htmlpath");
|
143 |
150 |
|
... | ... | |
184 |
191 |
* Control servlet response depending on the action parameter specified
|
185 |
192 |
*/
|
186 |
193 |
private void handleGetOrPost(HttpServletRequest request,
|
187 |
|
HttpServletResponse response)
|
188 |
|
throws ServletException, IOException
|
189 |
|
{
|
|
194 |
HttpServletResponse response)
|
|
195 |
throws ServletException, IOException
|
|
196 |
{
|
190 |
197 |
|
191 |
198 |
if ( util == null ) {
|
192 |
199 |
util = new MetaCatUtil();
|
... | ... | |
204 |
211 |
// Get a handle to the output stream back to the client
|
205 |
212 |
//PrintWriter pwout = response.getWriter();
|
206 |
213 |
//response.setContentType("text/html");
|
|
214 |
|
|
215 |
// Deal with forms that are encoded as "multipart/form-data" differently
|
|
216 |
// this is mainly used for uploading non-xml files using that may be binary
|
|
217 |
if (request.getContentType().startsWith("multipart/form-data")) {
|
|
218 |
handleMultipartForm(request, response);
|
|
219 |
} else {
|
|
220 |
// This probably means the data is "application/x-www-url-encoded", we hope :-)
|
207 |
221 |
|
208 |
|
String name = null;
|
209 |
|
String[] value = null;
|
210 |
|
String[] docid = new String[3];
|
211 |
|
Hashtable params = new Hashtable();
|
212 |
|
Enumeration paramlist = request.getParameterNames();
|
213 |
|
while (paramlist.hasMoreElements()) {
|
214 |
|
name = (String)paramlist.nextElement();
|
215 |
|
value = request.getParameterValues(name);
|
216 |
|
|
217 |
|
// Decode the docid and mouse click information
|
218 |
|
if (name.endsWith(".y")) {
|
219 |
|
docid[0] = name.substring(0,name.length()-2);
|
220 |
|
params.put("docid", docid);
|
221 |
|
name = "ypos";
|
222 |
|
}
|
223 |
|
if (name.endsWith(".x")) {
|
224 |
|
name = "xpos";
|
225 |
|
}
|
226 |
|
|
227 |
|
params.put(name,value);
|
228 |
|
}
|
229 |
|
|
230 |
|
//if the user clicked on the input images, decode which image
|
231 |
|
//was clicked then set the action.
|
232 |
|
String action = ((String[])params.get("action"))[0];
|
233 |
|
util.debugMessage("Line 213: Action is: " + action);
|
234 |
|
|
235 |
|
// This block handles session management for the servlet
|
236 |
|
// by looking up the current session information for all actions
|
237 |
|
// other than "login" and "logout"
|
238 |
|
String username = null;
|
239 |
|
String password = null;
|
240 |
|
String groupname = null;
|
241 |
|
String sess_id = null;
|
242 |
|
|
243 |
|
// handle login action
|
244 |
|
if (action.equals("login")) {
|
245 |
|
|
246 |
|
handleLoginAction(response.getWriter(), params, request, response);
|
247 |
|
|
248 |
|
// handle logout action
|
249 |
|
} else if (action.equals("logout")) {
|
250 |
|
|
251 |
|
handleLogoutAction(response.getWriter(), params, request, response);
|
252 |
|
|
253 |
|
// aware of session expiration on every request
|
254 |
|
} else {
|
255 |
|
|
256 |
|
HttpSession sess = request.getSession(true);
|
257 |
|
if (sess.isNew()) {
|
258 |
|
// session expired or has not been stored b/w user requests
|
259 |
|
username = "public";
|
260 |
|
sess.setAttribute("username", username);
|
261 |
|
} else {
|
262 |
|
username = (String)sess.getAttribute("username");
|
263 |
|
password = (String)sess.getAttribute("password");
|
264 |
|
groupname = (String)sess.getAttribute("groupname");
|
265 |
|
try {
|
266 |
|
sess_id = (String)sess.getId();
|
267 |
|
} catch(IllegalStateException ise) {
|
268 |
|
System.out.println("error in handleGetOrPost: this shouldn't " +
|
269 |
|
"happen: the session should be valid: " +
|
270 |
|
ise.getMessage());
|
|
222 |
String name = null;
|
|
223 |
String[] value = null;
|
|
224 |
String[] docid = new String[3];
|
|
225 |
Hashtable params = new Hashtable();
|
|
226 |
Enumeration paramlist = request.getParameterNames();
|
|
227 |
while (paramlist.hasMoreElements()) {
|
|
228 |
name = (String)paramlist.nextElement();
|
|
229 |
value = request.getParameterValues(name);
|
|
230 |
|
|
231 |
// Decode the docid and mouse click information
|
|
232 |
if (name.endsWith(".y")) {
|
|
233 |
docid[0] = name.substring(0,name.length()-2);
|
|
234 |
params.put("docid", docid);
|
|
235 |
name = "ypos";
|
271 |
236 |
}
|
|
237 |
if (name.endsWith(".x")) {
|
|
238 |
name = "xpos";
|
|
239 |
}
|
|
240 |
|
|
241 |
params.put(name,value);
|
272 |
242 |
}
|
273 |
|
}
|
274 |
|
|
275 |
|
// Now that we know the session is valid, we can delegate the request
|
276 |
|
// to a particular action handler
|
277 |
|
if(action.equals("query")) {
|
278 |
|
handleQuery(response.getWriter(), params, response, username, groupname);
|
279 |
|
} else if(action.equals("squery")) {
|
280 |
|
if(params.containsKey("query")) {
|
281 |
|
handleSQuery(response.getWriter(), params, response, username, groupname);
|
282 |
|
} else {
|
|
243 |
|
|
244 |
//if the user clicked on the input images, decode which image
|
|
245 |
//was clicked then set the action.
|
|
246 |
String action = ((String[])params.get("action"))[0];
|
|
247 |
util.debugMessage("Line 213: Action is: " + action);
|
|
248 |
|
|
249 |
// This block handles session management for the servlet
|
|
250 |
// by looking up the current session information for all actions
|
|
251 |
// other than "login" and "logout"
|
|
252 |
String username = null;
|
|
253 |
String password = null;
|
|
254 |
String groupname = null;
|
|
255 |
String sess_id = null;
|
|
256 |
|
|
257 |
// handle login action
|
|
258 |
if (action.equals("login")) {
|
|
259 |
|
|
260 |
handleLoginAction(response.getWriter(), params, request, response);
|
|
261 |
|
|
262 |
// handle logout action
|
|
263 |
} else if (action.equals("logout")) {
|
|
264 |
|
|
265 |
handleLogoutAction(response.getWriter(), params, request, response);
|
|
266 |
|
|
267 |
// aware of session expiration on every request
|
|
268 |
} else {
|
|
269 |
|
|
270 |
HttpSession sess = request.getSession(true);
|
|
271 |
if (sess.isNew()) {
|
|
272 |
// session expired or has not been stored b/w user requests
|
|
273 |
username = "public";
|
|
274 |
sess.setAttribute("username", username);
|
|
275 |
} else {
|
|
276 |
username = (String)sess.getAttribute("username");
|
|
277 |
password = (String)sess.getAttribute("password");
|
|
278 |
groupname = (String)sess.getAttribute("groupname");
|
|
279 |
try {
|
|
280 |
sess_id = (String)sess.getId();
|
|
281 |
} catch(IllegalStateException ise) {
|
|
282 |
System.out.println("error in handleGetOrPost: this shouldn't " +
|
|
283 |
"happen: the session should be valid: " +
|
|
284 |
ise.getMessage());
|
|
285 |
}
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
// Now that we know the session is valid, we can delegate the request
|
|
290 |
// to a particular action handler
|
|
291 |
if(action.equals("query")) {
|
|
292 |
handleQuery(response.getWriter(), params, response, username, groupname);
|
|
293 |
} else if(action.equals("squery")) {
|
|
294 |
if(params.containsKey("query")) {
|
|
295 |
handleSQuery(response.getWriter(), params, response, username, groupname);
|
|
296 |
} else {
|
|
297 |
PrintWriter out = response.getWriter();
|
|
298 |
out.println("Illegal action squery without \"query\" parameter");
|
|
299 |
}
|
|
300 |
} else if (action.equals("read")) {
|
|
301 |
handleReadAction(params, response, username, groupname);
|
|
302 |
} else if (action.equals("insert") || action.equals("update")) {
|
283 |
303 |
PrintWriter out = response.getWriter();
|
284 |
|
out.println("Illegal action squery without \"query\" parameter");
|
285 |
|
}
|
286 |
|
} else if (action.equals("read")) {
|
287 |
|
handleReadAction(params, response, username, groupname);
|
288 |
|
} else if (action.equals("insert") || action.equals("update")) {
|
289 |
|
PrintWriter out = response.getWriter();
|
290 |
|
if ( (username != null) && !username.equals("public") ) {
|
291 |
|
handleInsertOrUpdateAction(out, params, response, username, groupname);
|
292 |
|
} else {
|
293 |
|
out.println("Permission denied for " + action);
|
294 |
|
}
|
295 |
|
} else if (action.equals("delete")) {
|
296 |
|
PrintWriter out = response.getWriter();
|
297 |
|
if ( (username != null) && !username.equals("public") ) {
|
298 |
|
handleDeleteAction(out, params, response, username, groupname);
|
299 |
|
} else {
|
300 |
|
out.println("Permission denied for " + action);
|
301 |
|
}
|
302 |
|
} else if (action.equals("validate")) {
|
303 |
|
PrintWriter out = response.getWriter();
|
304 |
|
handleValidateAction(out, params, response);
|
305 |
|
} else if (action.equals("getdataport")) {
|
306 |
|
PrintWriter out = response.getWriter();
|
307 |
|
if ( (username != null) && !username.equals("public") ) {
|
308 |
|
handleGetDataPortAction(out, params, response, username, groupname,
|
309 |
|
sess_id);
|
310 |
|
} else {
|
311 |
|
out.println("You must be authenticated to perform the getdataport " +
|
312 |
|
"action!");
|
313 |
|
}
|
314 |
|
} else if (action.equals("getaccesscontrol")) {
|
315 |
|
PrintWriter out = response.getWriter();
|
316 |
|
handleGetAccessControlAction(out, params, response, username, groupname);
|
317 |
|
} else if (action.equals("getprincipals")) {
|
318 |
|
PrintWriter out = response.getWriter();
|
319 |
|
handleGetPrincipalsAction(out, username, password);
|
320 |
|
} else if (action.equals("getdoctypes")) {
|
321 |
|
PrintWriter out = response.getWriter();
|
322 |
|
handleGetDoctypesAction(out, params, response);
|
323 |
|
} else if (action.equals("getdtdschema")) {
|
324 |
|
PrintWriter out = response.getWriter();
|
325 |
|
handleGetDTDSchemaAction(out, params, response);
|
326 |
|
} else if (action.equals("getdataguide")) {
|
327 |
|
PrintWriter out = response.getWriter();
|
328 |
|
handleGetDataGuideAction(out, params, response);
|
329 |
|
} else if (action.equals("getlastdocid")) {
|
330 |
|
PrintWriter out = response.getWriter();
|
331 |
|
handleGetLastDocidAction(out, params, response);
|
332 |
|
} else if (action.equals("login") || action.equals("logout")) {
|
333 |
|
} else if (action.equals("protocoltest")) {
|
334 |
|
String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
|
335 |
|
try {
|
336 |
|
testURL = ((String[])params.get("url"))[0];
|
337 |
|
} catch (Throwable t) {
|
338 |
|
}
|
339 |
|
String phandler = System.getProperty("java.protocol.handler.pkgs");
|
340 |
|
response.setContentType("text/html");
|
341 |
|
PrintWriter out = response.getWriter();
|
342 |
|
out.println("<body bgcolor=\"white\">");
|
343 |
|
out.println("<p>Handler property: <code>" + phandler + "</code></p>");
|
344 |
|
out.println("<p>Starting test for:<br>");
|
345 |
|
out.println(" " + testURL + "</p>");
|
346 |
|
try {
|
347 |
|
URL u = new URL(testURL);
|
348 |
|
out.println("<pre>");
|
349 |
|
out.println("Protocol: " + u.getProtocol());
|
350 |
|
out.println(" Host: " + u.getHost());
|
351 |
|
out.println(" Port: " + u.getPort());
|
352 |
|
out.println(" Path: " + u.getPath());
|
353 |
|
out.println(" Ref: " + u.getRef());
|
354 |
|
String pquery = u.getQuery();
|
355 |
|
out.println(" Query: " + pquery);
|
356 |
|
out.println(" Params: ");
|
357 |
|
if (pquery != null) {
|
358 |
|
Hashtable qparams = util.parseQuery(u.getQuery());
|
359 |
|
for (Enumeration en = qparams.keys(); en.hasMoreElements(); ) {
|
360 |
|
String pname = (String)en.nextElement();
|
361 |
|
String pvalue = (String)qparams.get(pname);
|
362 |
|
out.println(" " + pname + ": " + pvalue);
|
|
304 |
if ( (username != null) && !username.equals("public") ) {
|
|
305 |
handleInsertOrUpdateAction(out, params, response, username, groupname);
|
|
306 |
} else {
|
|
307 |
out.println("Permission denied for " + action);
|
|
308 |
}
|
|
309 |
} else if (action.equals("delete")) {
|
|
310 |
PrintWriter out = response.getWriter();
|
|
311 |
if ( (username != null) && !username.equals("public") ) {
|
|
312 |
handleDeleteAction(out, params, response, username, groupname);
|
|
313 |
} else {
|
|
314 |
out.println("Permission denied for " + action);
|
|
315 |
}
|
|
316 |
} else if (action.equals("validate")) {
|
|
317 |
PrintWriter out = response.getWriter();
|
|
318 |
handleValidateAction(out, params, response);
|
|
319 |
} else if (action.equals("getdataport")) {
|
|
320 |
PrintWriter out = response.getWriter();
|
|
321 |
if ( (username != null) && !username.equals("public") ) {
|
|
322 |
handleGetDataPortAction(out, params, response, username, groupname,
|
|
323 |
sess_id);
|
|
324 |
} else {
|
|
325 |
out.println("You must be authenticated to perform the getdataport " +
|
|
326 |
"action!");
|
|
327 |
}
|
|
328 |
} else if (action.equals("getaccesscontrol")) {
|
|
329 |
PrintWriter out = response.getWriter();
|
|
330 |
handleGetAccessControlAction(out, params, response, username, groupname);
|
|
331 |
} else if (action.equals("getprincipals")) {
|
|
332 |
PrintWriter out = response.getWriter();
|
|
333 |
handleGetPrincipalsAction(out, username, password);
|
|
334 |
} else if (action.equals("getdoctypes")) {
|
|
335 |
PrintWriter out = response.getWriter();
|
|
336 |
handleGetDoctypesAction(out, params, response);
|
|
337 |
} else if (action.equals("getdtdschema")) {
|
|
338 |
PrintWriter out = response.getWriter();
|
|
339 |
handleGetDTDSchemaAction(out, params, response);
|
|
340 |
} else if (action.equals("getdataguide")) {
|
|
341 |
PrintWriter out = response.getWriter();
|
|
342 |
handleGetDataGuideAction(out, params, response);
|
|
343 |
} else if (action.equals("getlastdocid")) {
|
|
344 |
PrintWriter out = response.getWriter();
|
|
345 |
handleGetLastDocidAction(out, params, response);
|
|
346 |
} else if (action.equals("login") || action.equals("logout")) {
|
|
347 |
} else if (action.equals("protocoltest")) {
|
|
348 |
String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
|
|
349 |
try {
|
|
350 |
testURL = ((String[])params.get("url"))[0];
|
|
351 |
} catch (Throwable t) {
|
|
352 |
}
|
|
353 |
String phandler = System.getProperty("java.protocol.handler.pkgs");
|
|
354 |
response.setContentType("text/html");
|
|
355 |
PrintWriter out = response.getWriter();
|
|
356 |
out.println("<body bgcolor=\"white\">");
|
|
357 |
out.println("<p>Handler property: <code>" + phandler + "</code></p>");
|
|
358 |
out.println("<p>Starting test for:<br>");
|
|
359 |
out.println(" " + testURL + "</p>");
|
|
360 |
try {
|
|
361 |
URL u = new URL(testURL);
|
|
362 |
out.println("<pre>");
|
|
363 |
out.println("Protocol: " + u.getProtocol());
|
|
364 |
out.println(" Host: " + u.getHost());
|
|
365 |
out.println(" Port: " + u.getPort());
|
|
366 |
out.println(" Path: " + u.getPath());
|
|
367 |
out.println(" Ref: " + u.getRef());
|
|
368 |
String pquery = u.getQuery();
|
|
369 |
out.println(" Query: " + pquery);
|
|
370 |
out.println(" Params: ");
|
|
371 |
if (pquery != null) {
|
|
372 |
Hashtable qparams = util.parseQuery(u.getQuery());
|
|
373 |
for (Enumeration en = qparams.keys(); en.hasMoreElements(); ) {
|
|
374 |
String pname = (String)en.nextElement();
|
|
375 |
String pvalue = (String)qparams.get(pname);
|
|
376 |
out.println(" " + pname + ": " + pvalue);
|
|
377 |
}
|
363 |
378 |
}
|
|
379 |
out.println("</pre>");
|
|
380 |
out.println("</body>");
|
|
381 |
out.close();
|
|
382 |
} catch (MalformedURLException mue) {
|
|
383 |
System.out.println("bad url from MetacatServlet.handleGetOrPost");
|
|
384 |
out.println(mue.getMessage());
|
|
385 |
mue.printStackTrace(out);
|
|
386 |
out.close();
|
364 |
387 |
}
|
365 |
|
out.println("</pre>");
|
366 |
|
out.println("</body>");
|
367 |
|
out.close();
|
368 |
|
} catch (MalformedURLException mue) {
|
369 |
|
System.out.println("bad url from MetacatServlet.handleGetOrPost");
|
370 |
|
out.println(mue.getMessage());
|
371 |
|
mue.printStackTrace(out);
|
372 |
|
out.close();
|
|
388 |
} else {
|
|
389 |
PrintWriter out = response.getWriter();
|
|
390 |
out.println("<?xml version=\"1.0\"?>");
|
|
391 |
out.println("<error>");
|
|
392 |
out.println("Error: action not registered. Please report this error.");
|
|
393 |
out.println("</error>");
|
373 |
394 |
}
|
374 |
|
} else {
|
375 |
|
PrintWriter out = response.getWriter();
|
376 |
|
out.println("Error: action not registered. Please report this error.");
|
|
395 |
|
|
396 |
util.closeConnections();
|
|
397 |
// Close the stream to the client
|
|
398 |
// out.close();
|
377 |
399 |
}
|
378 |
|
|
379 |
|
util.closeConnections();
|
380 |
|
// Close the stream to the client
|
381 |
|
// out.close();
|
382 |
400 |
}
|
383 |
401 |
|
384 |
402 |
// LOGIN & LOGOUT SECTION
|
... | ... | |
1502 |
1520 |
|
1503 |
1521 |
}
|
1504 |
1522 |
|
|
1523 |
/**
|
|
1524 |
* Handle documents passed to metacat that are encoded using the
|
|
1525 |
* "multipart/form-data" mime type. This is typically used for uploading
|
|
1526 |
* data files which may be binary and large.
|
|
1527 |
*/
|
|
1528 |
private void handleMultipartForm(HttpServletRequest request,
|
|
1529 |
HttpServletResponse response)
|
|
1530 |
{
|
|
1531 |
PrintWriter out = null;
|
|
1532 |
String action = null;
|
|
1533 |
|
|
1534 |
// Parse the multipart form, and save the parameters in a Hashtable and
|
|
1535 |
// save the FileParts in a hashtable
|
|
1536 |
|
|
1537 |
Hashtable params = new Hashtable();
|
|
1538 |
Hashtable fileList = new Hashtable();
|
|
1539 |
|
|
1540 |
try {
|
|
1541 |
// MBJ: need to put filesize limit in Metacat config (metacat.properties)
|
|
1542 |
MultipartParser mp = new MultipartParser(request, 200*1024*1024); // 200MB
|
|
1543 |
Part part;
|
|
1544 |
while ((part = mp.readNextPart()) != null) {
|
|
1545 |
String name = part.getName();
|
|
1546 |
|
|
1547 |
if (part.isParam()) {
|
|
1548 |
// it's a parameter part
|
|
1549 |
ParamPart paramPart = (ParamPart) part;
|
|
1550 |
String value = paramPart.getStringValue();
|
|
1551 |
params.put(name, value);
|
|
1552 |
if (name.equals("action")) {
|
|
1553 |
action = value;
|
|
1554 |
}
|
|
1555 |
} else if (part.isFile()) {
|
|
1556 |
// it's a file part
|
|
1557 |
FilePart filePart = (FilePart) part;
|
|
1558 |
fileList.put(name, filePart);
|
|
1559 |
|
|
1560 |
// Stop once the first file part is found, otherwise going onto the
|
|
1561 |
// next part prevents access to the file contents. So...for upload
|
|
1562 |
// to work, the datafile must be the last part
|
|
1563 |
break;
|
|
1564 |
}
|
|
1565 |
}
|
|
1566 |
} catch (IOException ioe) {
|
|
1567 |
try {
|
|
1568 |
out = response.getWriter();
|
|
1569 |
} catch (IOException ioe2) {
|
|
1570 |
System.err.println("Fatal Error: couldn't get response output stream.");
|
|
1571 |
}
|
|
1572 |
out.println("<?xml version=\"1.0\"?>");
|
|
1573 |
out.println("<error>");
|
|
1574 |
out.println("Error: problem reading multipart data.");
|
|
1575 |
out.println("</error>");
|
|
1576 |
}
|
|
1577 |
|
|
1578 |
// Get the session information
|
|
1579 |
String username = null;
|
|
1580 |
String password = null;
|
|
1581 |
String groupname = null;
|
|
1582 |
String sess_id = null;
|
|
1583 |
|
|
1584 |
// be aware of session expiration on every request
|
|
1585 |
HttpSession sess = request.getSession(true);
|
|
1586 |
if (sess.isNew()) {
|
|
1587 |
// session expired or has not been stored b/w user requests
|
|
1588 |
username = "public";
|
|
1589 |
sess.setAttribute("username", username);
|
|
1590 |
} else {
|
|
1591 |
username = (String)sess.getAttribute("username");
|
|
1592 |
password = (String)sess.getAttribute("password");
|
|
1593 |
groupname = (String)sess.getAttribute("groupname");
|
|
1594 |
try {
|
|
1595 |
sess_id = (String)sess.getId();
|
|
1596 |
} catch(IllegalStateException ise) {
|
|
1597 |
System.out.println("error in handleMultipartForm: this shouldn't " +
|
|
1598 |
"happen: the session should be valid: " +
|
|
1599 |
ise.getMessage());
|
|
1600 |
}
|
|
1601 |
}
|
|
1602 |
|
|
1603 |
if ( action.equals("upload")) {
|
|
1604 |
if (username != null && !username.equals("public")) {
|
|
1605 |
handleUploadAction(request, response, params, fileList,
|
|
1606 |
username, groupname);
|
|
1607 |
} else {
|
|
1608 |
try {
|
|
1609 |
out = response.getWriter();
|
|
1610 |
} catch (IOException ioe2) {
|
|
1611 |
System.err.println("Fatal Error: couldn't get response output stream.");
|
|
1612 |
}
|
|
1613 |
out.println("<?xml version=\"1.0\"?>");
|
|
1614 |
out.println("<error>");
|
|
1615 |
out.println("Permission denied for " + action);
|
|
1616 |
out.println("</error>");
|
|
1617 |
}
|
|
1618 |
} else {
|
|
1619 |
try {
|
|
1620 |
out = response.getWriter();
|
|
1621 |
} catch (IOException ioe2) {
|
|
1622 |
System.err.println("Fatal Error: couldn't get response output stream.");
|
|
1623 |
}
|
|
1624 |
out.println("<?xml version=\"1.0\"?>");
|
|
1625 |
out.println("<error>");
|
|
1626 |
out.println("Error: action not registered. Please report this error.");
|
|
1627 |
out.println("</error>");
|
|
1628 |
}
|
|
1629 |
}
|
|
1630 |
|
|
1631 |
/**
|
|
1632 |
* Handle the upload action by saving the attached file to disk and
|
|
1633 |
* registering it in the Metacat db
|
|
1634 |
*/
|
|
1635 |
private void handleUploadAction(HttpServletRequest request,
|
|
1636 |
HttpServletResponse response,
|
|
1637 |
Hashtable params, Hashtable fileList,
|
|
1638 |
String username, String groupname)
|
|
1639 |
{
|
|
1640 |
PrintWriter out = null;
|
|
1641 |
Connection conn = null;
|
|
1642 |
String action = null;
|
|
1643 |
String docid = null;
|
|
1644 |
|
|
1645 |
response.setContentType("text/xml");
|
|
1646 |
try {
|
|
1647 |
out = response.getWriter();
|
|
1648 |
} catch (IOException ioe2) {
|
|
1649 |
System.err.println("Fatal Error: couldn't get response output stream.");
|
|
1650 |
}
|
|
1651 |
|
|
1652 |
if (params.containsKey("docid")) {
|
|
1653 |
docid = (String)params.get("docid");
|
|
1654 |
}
|
|
1655 |
|
|
1656 |
// Make sure we have a docid and datafile
|
|
1657 |
if (docid != null && fileList.containsKey("datafile")) {
|
|
1658 |
|
|
1659 |
// Get a reference to the file part of the form
|
|
1660 |
FilePart filePart = (FilePart)fileList.get("datafile");
|
|
1661 |
String fileName = filePart.getFileName();
|
|
1662 |
MetaCatUtil.debugMessage("Uploading filename: " + fileName);
|
|
1663 |
|
|
1664 |
// Check if the right file existed in the uploaded data
|
|
1665 |
if (fileName != null) {
|
|
1666 |
|
|
1667 |
try {
|
|
1668 |
// register the file in the database (which generates an exception if
|
|
1669 |
// the docid is not acceptable or other untoward things happen
|
|
1670 |
DocumentImpl.registerDocument(fileName, "BIN", docid, username, 1);
|
|
1671 |
|
|
1672 |
// Save the data file to disk using "docid" as the name
|
|
1673 |
dataDirectory.mkdirs();
|
|
1674 |
File newFile = new File(dataDirectory, docid);
|
|
1675 |
long size = filePart.writeTo(newFile);
|
|
1676 |
|
|
1677 |
// set content type and other response header fields first
|
|
1678 |
out.println("<?xml version=\"1.0\"?>");
|
|
1679 |
out.println("<success>");
|
|
1680 |
out.println("<docid>" + docid + "</docid>");
|
|
1681 |
out.println("<size>" + size + "</size>");
|
|
1682 |
out.println("</success>");
|
|
1683 |
|
|
1684 |
} catch (Exception e) {
|
|
1685 |
out.println("<?xml version=\"1.0\"?>");
|
|
1686 |
out.println("<error>");
|
|
1687 |
out.println(e.getMessage());
|
|
1688 |
out.println("</error>");
|
|
1689 |
}
|
|
1690 |
} else {
|
|
1691 |
// the field did not contain a file
|
|
1692 |
out.println("<?xml version=\"1.0\"?>");
|
|
1693 |
out.println("<error>");
|
|
1694 |
out.println("The uploaded data did not contain a valid file.");
|
|
1695 |
out.println("</error>");
|
|
1696 |
}
|
|
1697 |
} else {
|
|
1698 |
// Error bcse docid missing or file missing
|
|
1699 |
out.println("<?xml version=\"1.0\"?>");
|
|
1700 |
out.println("<error>");
|
|
1701 |
out.println("The uploaded data did not contain a valid docid " +
|
|
1702 |
"or valid file.");
|
|
1703 |
out.println("</error>");
|
|
1704 |
}
|
|
1705 |
}
|
1505 |
1706 |
}
|
Modified Metacat to support large data file uploads. This is accomplished
by supporting a new content type for data sent to metacat:
multipart/form-data
which allows multiple files to be sent in a standard MIME format. The
MetacatServlet.handleGetOrPost() method now checks the incoming content
type and sends multipart/form-data encoded submissions to
MetacatServlet.handleMultipartForm(), which in turn delegates to
MetacatServlet.handleUploadAction(). When a client wants to load a data
file to metacat, now create a "multipart/form-data" encoded stream that
contains an "action" parameter set to the value "upload", a "docid"
parameter set to the value of the accession number fro the file, and
a "datafile" part that contains the actual file contents. Because of a
bug / shortcoming in the com.oreilly.servlet library that I am using to parse
this mime encoding, the "action" and "docid" paramters MUST come before
the "datafile" part or metacat won't find the paramters.
The provided docid is inserted into xml_documents (after checking that it
meets expectations [unique, rev 1, etc]). Then the data file
recieved from the "upload" action is stored on the filesystem
using the Accession number provided in the docid. See
DocumentImpl.registerDocument for details.
This functionality completely replaces the socket-based functionality found
in DataFileUploadInterface and DataFileServer. It would still be nice to
abstract away the file storage functions so that they can be either written
to the filesystem or other locations (like the db ina BLOB) when they are
recieved in this new encoding format.
There are concomitant changes in Morpho to produce these types of file uploads.
Morpho now uses the HTTPClient library for http communication, which
allows streaming data between client and server (Sun's http protocol
handler buffers all data before sending, which limits data transfer size
to memory available). No changes in metacat were needed to accomodate this
switch of protocol handlers on the client side; all changes were due to the
need for an alternative data encoding (other than URL encoding) that
efficiently supports binary files).
The cos.jar file is now needed for parsing the new encoding.