/**
* Process a post request.
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Create the Deposit request
Deposit d = new Deposit();
Date date = new Date();
log.debug("Starting deposit processing at " + date.toString() + " by "
+ request.getRemoteAddr());
// Are there any authentication details?
String usernamePassword = getUsernamePassword(request);
if ((usernamePassword != null) && (!usernamePassword.equals(""))) {
int p = usernamePassword.indexOf(":");
if (p != -1) {
d.setUsername(usernamePassword.substring(0, p));
d.setPassword(usernamePassword.substring(p + 1));
}
} else if (authenticateWithBasic()) {
String s = "Basic realm=\"SWORD\"";
response.setHeader("WWW-Authenticate", s);
response.setStatus(401);
return;
}
// Set up some variables
String filename = null;
File f = null;
FileInputStream fis = null;
// Do the processing
try {
// Write the file to the temp directory
filename = tempDirectory + "SWORD-"
+ request.getRemoteAddr() + "-" + counter.addAndGet(1);
log.debug("Package temporarily stored as: " + filename);
InputStream inputstream = request.getInputStream();
OutputStream outputstream = new FileOutputStream(new File(filename));
try
{
byte[] buf = new byte[1024];
int len;
while ((len = inputstream.read(buf)) > 0)
{
outputstream.write(buf, 0, len);
}
}
finally
{
inputstream.close();
outputstream.close();
}
// Check the size is OK
File file = new File(filename);
long fLength = file.length() / 1024;
if ((maxUploadSize != -1) && (fLength > maxUploadSize)) {
this.makeErrorDocument(ErrorCodes.MAX_UPLOAD_SIZE_EXCEEDED,
HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE,
"The uploaded file exceeded the maximum file size this server will accept (the file is " +
fLength + "kB but the server will only accept files as large as " +
maxUploadSize + "kB)",
request,
response);
return;
}
// Check the MD5 hash
String receivedMD5 = ChecksumUtils.generateMD5(filename);
log.debug("Received filechecksum: " + receivedMD5);
d.setMd5(receivedMD5);
String md5 = request.getHeader("Content-MD5");
log.debug("Received file checksum header: " + md5);
if ((md5 != null) && (!md5.equals(receivedMD5))) {
// Return an error document
this.makeErrorDocument(ErrorCodes.ERROR_CHECKSUM_MISMATCH,
HttpServletResponse.SC_PRECONDITION_FAILED,
"The received MD5 checksum for the deposited file did not match the checksum sent by the deposit client",
request,
response);
log.debug("Bad MD5 for file. Aborting with appropriate error message");
return;
} else {
// Set the file
f = new File(filename);
fis = new FileInputStream(f);
d.setFile(fis);
// Set the X-On-Behalf-Of header
String onBehalfOf = request.getHeader(HttpHeaders.X_ON_BEHALF_OF.toString());
if ((onBehalfOf != null) && (onBehalfOf.equals("reject"))) {
// user name is "reject", so throw a not know error to allow the client to be tested
throw new SWORDErrorException(ErrorCodes.TARGET_OWNER_UKNOWN,"unknown user \"reject\"");
} else {
d.setOnBehalfOf(onBehalfOf);
}
// Set the X-Packaging header
d.setPackaging(request.getHeader(HttpHeaders.X_PACKAGING));
// Set the X-No-Op header
String noop = request.getHeader(HttpHeaders.X_NO_OP);
log.error("X_NO_OP value is " + noop);
if ((noop != null) && (noop.equals("true"))) {
d.setNoOp(true);
} else if ((noop != null) && (noop.equals("false"))) {
d.setNoOp(false);
}else if (noop == null) {
d.setNoOp(false);
} else {
throw new SWORDErrorException(ErrorCodes.ERROR_BAD_REQUEST,"Bad no-op");
}
// Set the X-Verbose header
String verbose = request.getHeader(HttpHeaders.X_VERBOSE);
if ((verbose != null) && (verbose.equals("true"))) {
d.setVerbose(true);
} else if ((verbose != null) && (verbose.equals("false"))) {
d.setVerbose(false);
}else if (verbose == null) {
d.setVerbose(false);
} else {
throw new SWORDErrorException(ErrorCodes.ERROR_BAD_REQUEST,"Bad verbose");
}
// Set the slug
String slug = request.getHeader(HttpHeaders.SLUG);
if (slug != null) {
d.setSlug(slug);
}
// Set the content disposition
d.setContentDisposition(request.getHeader(HttpHeaders.CONTENT_DISPOSITION));
// Set the IP address
d.setIPAddress(request.getRemoteAddr());
// Set the deposit location
d.setLocation(getUrl(request));
// Set the content type
d.setContentType(request.getContentType());
// Set the content length
String cl = request.getHeader(HttpHeaders.CONTENT_LENGTH);
if ((cl != null) && (!cl.equals(""))) {
d.setContentLength(Integer.parseInt(cl));
}
// Get the DepositResponse
DepositResponse dr = myRepository.doDeposit(d);