Overview
Ingesting local datastreams to Fedora may be accomplished using the semi-secret upload servlet.
If you POST content to:
http://host:port/fedora/management/upload
the response received will be a temporary URI that may be passed into the other management APIs for ingestion or datastream creation. Fedora will resolve that URI into the datastream that was "POST"ed. Note that the data will only be retained on the server for a short time, so beware of timing problems — the default timeout is five minutes. You can set a higher value by adding the uploadStorageMinutes param to your fedora.fcfg. This parameter goes in the Management module's configuration section, and specifies the number of minutes after which uploaded content will be automatically deleted if not used.
Using FedoraClient to Upload
As of Fedora 2.2, FedoraClient has a convenience method for sending files to the upload endpoint. Example code follows:
import fedora.client.FedoraClient; import fedora.server.management.FedoraAPIM; ... FedoraClient fedora = new FedoraClient("http://localhost:8080/fedora", "fedoraAdmin", "fedoraAdmin"); FedoraAPIM apim = fedora.getAPIM(); String tempURI = fedora.uploadFile(new File("myfile.txt")); apim.addDatastream("demo:1000", // PID "MYDS", // Datastream ID new String\[\] {}, // Alt IDs "My Datastream", // Label true, // Versionable "text/plain", // MIME type null, // Format URI tempURI, // Datastream Location "M", // Control Group "A", // State null, // ChecksumType null, // Checksum "added by me"); // Log message
Note: The "Alt IDs" value should never be null, since that would make FOXMLDOSerializer.java throw a "NullPointerException", leading to a "fedora.server.errors.GeneralException: Unable to add or modify object (commit canceled)" error message in the code where addDatastream is called. new String [0] can be used instead of null.
Relevant Javadocs can be found here:
- http://www.fedora.info/download/2.2/javadocs/fedora/client/FedoraClient.html#uploadFile(java.io.File)
- http://www.fedora.info/download/2.2/javadocs/fedora/server/management/FedoraAPIM.html
| Note The fedora-client.jar file can be found in the FEDORA_HOME/client/ directory. Runtime dependencies (things that need to be in your CLASSPATH when using the jar file) can be found in FEDORA_HOME/client/lib/. |
If can't or don't want to use FedoraClient, you can do the uploading yourself using a standard HTTP upload library. Looking at the FedoraClient.java source code may help in this case:
Technical Details
You must use a multi-part POST, with the part name of "file". As of Fedora 2.2, the server can optionally handle chunked encoding. It is recommended that you use this for very large files (FedoraClient does this automatically).
Java Implementation
| Warning This section was written for Fedora 2.1.1 and is out of date. See the FedoraClient section above for details on doing uploads with Fedora 2.2. |
This implementation in Java is not complete, and has not been compiled or checked for syntax errors. It does not do proper checking of the HTTP status code, or any exception handling or resource cleanup – you'll need to do that yourself. With that said, the general outline was taken from a working component that does Fedora uploads, and it should give you a rough idea how to proceed. It uses the Apache Jakarta Commons HTTPClient package.
For another example, see the source distribution's Uploader.java source code.
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.MultipartPostMethod; import org.apache.commons.httpclient.methods.multipart.PartSource; private String upload(PartSource partSource, long length) { HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(); creds.setUserName("fedoraAdmin"); creds.setPassword("password"); client.getState().setAuthenticationPreemptive(true); client.getState().setCredentials(null, null, creds); MultipartPostMethod post = new MultipartPostMethod("http://host:port/fedora/management/upload"); post.addPart(new FilePart("file", partSource)); client.executeMethod(post); return post.getResponseBodyAsString().trim(); }