Revision 8748
Added by ben leinfelder over 10 years ago
test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java | ||
---|---|---|
155 | 155 |
suite.addTest(new MetacatClientTest("getNewestDocRevision")); // (also tries to insert) |
156 | 156 |
suite.addTest(new MetacatClientTest("upload")); |
157 | 157 |
suite.addTest(new MetacatClientTest("upload_stream")); |
158 |
suite.addTest(new MetacatClientTest("upload_stream_chunked")); |
|
158 | 159 |
suite.addTest(new MetacatClientTest("invalidRead")); |
159 | 160 |
suite.addTest(new MetacatClientTest("read")); |
160 | 161 |
suite.addTest(new MetacatClientTest("query")); |
... | ... | |
549 | 550 |
fail("General exception:\n" + e.getMessage()); |
550 | 551 |
} |
551 | 552 |
} |
553 |
|
|
554 |
/** |
|
555 |
* Test the upload() function by passing an InputStream |
|
556 |
*/ |
|
557 |
public void upload_stream_chunked() |
|
558 |
{ |
|
559 |
debug("\nStarting upload_stream_chunked test..."); |
|
560 |
try { |
|
561 |
newdocid = generateDocumentId(); |
|
562 |
String identifier = newdocid + ".1"; |
|
563 |
m.login(username, password); |
|
564 |
File testFile = new File(onlinetestdatafile); |
|
565 |
String response = m.upload(identifier, "onlineDataFile1", |
|
566 |
new FileInputStream(testFile), -1); |
|
552 | 567 |
|
568 |
assertTrue(response.indexOf("<success>") != -1); |
|
569 |
assertTrue(response.indexOf(identifier) != -1); |
|
570 |
identifier = newdocid + ".2"; |
|
571 |
response = m.upload(identifier, "onlineDataFile1", |
|
572 |
new FileInputStream(testFile), -1); |
|
573 |
|
|
574 |
assertTrue(response.indexOf("<success>") != -1); |
|
575 |
assertTrue(response.indexOf(identifier) != -1); |
|
576 |
debug("upload_stream_chunked(): response=" + response); |
|
577 |
|
|
578 |
} catch (MetacatAuthException mae) { |
|
579 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
580 |
} catch (MetacatInaccessibleException mie) { |
|
581 |
mie.printStackTrace(); |
|
582 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
583 |
} catch (InsufficientKarmaException ike) { |
|
584 |
assertTrue(1 == 1); |
|
585 |
fail("Insufficient karma:\n" + ike.getMessage()); |
|
586 |
} catch (MetacatException me) { |
|
587 |
fail("Metacat Error:\n" + me.getMessage()); |
|
588 |
} catch (Exception e) { |
|
589 |
fail("General exception:\n" + e.getMessage()); |
|
590 |
} |
|
591 |
} |
|
592 |
|
|
553 | 593 |
/** |
554 | 594 |
* Test the invalidUpdate() function. A user try to update a document |
555 | 595 |
* which it doesn't have permission |
src/edu/ucsb/nceas/metacat/MetacatHandler.java | ||
---|---|---|
67 | 67 |
import javax.servlet.http.HttpServletResponse; |
68 | 68 |
import javax.servlet.http.HttpSession; |
69 | 69 |
|
70 |
import org.apache.commons.fileupload.FileItem; |
|
71 |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
72 |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
|
70 | 73 |
import org.apache.commons.io.IOUtils; |
71 | 74 |
import org.apache.commons.io.input.XmlStreamReader; |
72 | 75 |
import org.apache.log4j.Logger; |
... | ... | |
79 | 82 |
|
80 | 83 |
import au.com.bytecode.opencsv.CSVWriter; |
81 | 84 |
|
82 |
import com.oreilly.servlet.multipart.FilePart; |
|
83 |
import com.oreilly.servlet.multipart.MultipartParser; |
|
84 |
import com.oreilly.servlet.multipart.ParamPart; |
|
85 |
import com.oreilly.servlet.multipart.Part; |
|
86 |
|
|
87 | 85 |
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException; |
88 | 86 |
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile; |
89 | 87 |
import edu.ucsb.nceas.utilities.access.AccessControlInterface; |
... | ... | |
94 | 92 |
import edu.ucsb.nceas.metacat.database.DBConnection; |
95 | 93 |
import edu.ucsb.nceas.metacat.database.DBConnectionPool; |
96 | 94 |
import edu.ucsb.nceas.metacat.dataone.D1NodeService; |
97 |
import edu.ucsb.nceas.metacat.dataone.MNodeService; |
|
98 | 95 |
import edu.ucsb.nceas.metacat.dataone.SyncAccessPolicy; |
99 | 96 |
import edu.ucsb.nceas.metacat.dataone.SystemMetadataFactory; |
100 | 97 |
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService; |
... | ... | |
2867 | 2864 |
Hashtable<String,String[]> params = new Hashtable<String,String[]>(); |
2868 | 2865 |
Hashtable<String,String> fileList = new Hashtable<String,String>(); |
2869 | 2866 |
int sizeLimit = 1000; |
2867 |
String tmpDir = "/tmp"; |
|
2870 | 2868 |
try { |
2871 | 2869 |
sizeLimit = |
2872 | 2870 |
(new Integer(PropertyService.getProperty("replication.datafilesizelimit"))).intValue(); |
... | ... | |
2876 | 2874 |
pnfe.getMessage()); |
2877 | 2875 |
pnfe.printStackTrace(System.out); |
2878 | 2876 |
} |
2877 |
try { |
|
2878 |
tmpDir = PropertyService.getProperty("application.tempDir"); |
|
2879 |
} catch (PropertyNotFoundException pnfe) { |
|
2880 |
logMetacat.error("MetacatHandler.handleMultipartForm - " + |
|
2881 |
"Could not determine temp dir, using default. " + |
|
2882 |
pnfe.getMessage()); |
|
2883 |
pnfe.printStackTrace(System.out); |
|
2884 |
} |
|
2879 | 2885 |
logMetacat.debug("MetacatHandler.handleMultipartForm - " + |
2880 | 2886 |
"The size limit of uploaded data files is: " + |
2881 | 2887 |
sizeLimit); |
2882 | 2888 |
|
2883 | 2889 |
try { |
2884 |
MultipartParser mp = new MultipartParser(request, |
|
2885 |
sizeLimit * 1024 * 1024); |
|
2886 |
Part part; |
|
2887 |
|
|
2888 |
while ((part = mp.readNextPart()) != null) { |
|
2889 |
String name = part.getName(); |
|
2890 |
|
|
2891 |
if (part.isParam()) { |
|
2890 |
boolean isMultipart = ServletFileUpload.isMultipartContent(request); |
|
2891 |
// Create a factory for disk-based file items |
|
2892 |
DiskFileItemFactory factory = new DiskFileItemFactory(); |
|
2893 |
|
|
2894 |
// Configure a repository (to ensure a secure temp location is used) |
|
2895 |
File repository = new File(tmpDir); |
|
2896 |
factory.setRepository(repository); |
|
2897 |
|
|
2898 |
// Create a new file upload handler |
|
2899 |
ServletFileUpload upload = new ServletFileUpload(factory); |
|
2900 |
|
|
2901 |
// Parse the request |
|
2902 |
List<FileItem> items = upload.parseRequest(request); |
|
2903 |
|
|
2904 |
Iterator<FileItem> iter = items.iterator(); |
|
2905 |
while (iter.hasNext()) { |
|
2906 |
FileItem item = iter.next(); |
|
2907 |
String name = item.getFieldName(); |
|
2908 |
|
|
2909 |
if (item.isFormField()) { |
|
2910 |
|
|
2892 | 2911 |
// it's a parameter part |
2893 |
ParamPart paramPart = (ParamPart) part; |
|
2894 | 2912 |
String[] values = new String[1]; |
2895 |
values[0] = paramPart.getStringValue();
|
|
2913 |
values[0] = item.getString();
|
|
2896 | 2914 |
params.put(name, values); |
2897 | 2915 |
if (name.equals("action")) { |
2898 | 2916 |
action = values[0]; |
2899 | 2917 |
} |
2900 |
} else if (part.isFile()) {
|
|
2918 |
} else { |
|
2901 | 2919 |
// it's a file part |
2902 |
FilePart filePart = (FilePart) part; |
|
2903 |
String fileName = filePart.getFileName(); |
|
2904 |
|
|
2905 |
// the filePart will be clobbered on the next loop, save to disk |
|
2906 |
tempFile = MetacatUtil.writeTempUploadFile(filePart, fileName); |
|
2920 |
String fileName = item.getName(); |
|
2921 |
|
|
2922 |
// write to disk |
|
2923 |
tempFile = MetacatUtil.writeTempUploadFile(item, fileName); |
|
2907 | 2924 |
fileList.put(name, tempFile.getAbsolutePath()); |
2908 | 2925 |
fileList.put("filename", fileName); |
2909 | 2926 |
fileList.put("name", tempFile.getAbsolutePath()); |
2910 |
} else { |
|
2911 |
logMetacat.info("MetacatHandler.handleMultipartForm - " + |
|
2912 |
"Upload name '" + name + "' was empty."); |
|
2913 | 2927 |
} |
2914 | 2928 |
} |
2915 |
} catch (IOException ioe) {
|
|
2929 |
} catch (Exception ioe) { |
|
2916 | 2930 |
try { |
2917 | 2931 |
out = response.getWriter(); |
2918 | 2932 |
} catch (IOException ioe2) { |
src/edu/ucsb/nceas/metacat/util/MetacatUtil.java | ||
---|---|---|
30 | 30 |
import java.io.FileInputStream; |
31 | 31 |
import java.io.FileOutputStream; |
32 | 32 |
import java.io.IOException; |
33 |
import java.io.InputStream; |
|
33 | 34 |
import java.io.PrintWriter; |
34 | 35 |
import java.net.MalformedURLException; |
35 | 36 |
import java.util.Hashtable; |
36 | 37 |
import java.util.Vector; |
37 | 38 |
|
39 |
import org.apache.commons.fileupload.FileItem; |
|
40 |
import org.apache.commons.io.IOUtils; |
|
38 | 41 |
import org.apache.log4j.Logger; |
39 | 42 |
|
40 | 43 |
import com.oreilly.servlet.multipart.FilePart; |
... | ... | |
400 | 403 |
* the name of the file to be written to disk |
401 | 404 |
* @return tempFilePath a String containing location of temporary file |
402 | 405 |
*/ |
403 |
public static File writeTempUploadFile (FilePart filePart, String fileName) throws IOException {
|
|
406 |
public static File writeTempUploadFile (FileItem fi, String fileName) throws Exception {
|
|
404 | 407 |
File tempFile = null; |
405 | 408 |
String tempDirPath = null; |
406 | 409 |
try { |
... | ... | |
441 | 444 |
|
442 | 445 |
//tempFile = new File(tempDirPath, fileName); |
443 | 446 |
tempFile = File.createTempFile("upload", ".tmp", tempDir); |
444 |
fileSize = filePart.writeTo(tempFile); |
|
447 |
fi.write(tempFile); |
|
448 |
fileSize = fi.getSize(); |
|
445 | 449 |
|
446 | 450 |
if (fileSize == 0) { |
447 | 451 |
logMetacat.warn("Uploaded file '" + fileName + "'is empty!"); |
src/edu/ucsb/nceas/metacat/client/MetacatClient.java | ||
---|---|---|
50 | 50 |
import org.apache.http.client.methods.HttpPost; |
51 | 51 |
import org.apache.http.entity.mime.HttpMultipartMode; |
52 | 52 |
import org.apache.http.entity.mime.MultipartEntity; |
53 |
import org.apache.http.entity.mime.content.AbstractContentBody; |
|
54 |
import org.apache.http.entity.mime.content.ByteArrayBody; |
|
53 | 55 |
import org.apache.http.entity.mime.content.FileBody; |
54 | 56 |
import org.apache.http.entity.mime.content.InputStreamBody; |
55 | 57 |
import org.apache.http.entity.mime.content.StringBody; |
... | ... | |
676 | 678 |
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); |
677 | 679 |
|
678 | 680 |
// For File parameters |
679 |
InputStreamBody content = null;
|
|
681 |
AbstractContentBody content = null;
|
|
680 | 682 |
if (size < 0) { |
681 | 683 |
content = new InputStreamBody(fileData, filename); |
684 |
//content = new ByteArrayBody(IOUtils.toByteArray(fileData), filename); |
|
682 | 685 |
} else { |
683 | 686 |
content = new InputStreamKnownSizeBody(fileData, filename, size); |
684 | 687 |
} |
Also available in: Unified diff
switch to use FIleUpload instead of O'Reilly COS library for handling chunked file uploads. https://projects.ecoinformatics.org/ecoinfo/issues/6517