try {
// The temporary file used for partial PUT.
tmp = new File(file.getCanonicalPath() + "."
+ getTemporaryExtension());
// Support only one range.
Range range = request.getRanges().get(0);
if (tmp.exists() && !isResumeUpload()) {
BioUtils.delete(tmp);
}
if (!tmp.exists()) {
// Copy the target file.
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(tmp);
BioUtils.copy(in, out);
out.flush();
out.close();
}
raf = new RandomAccessFile(tmp, "rwd");
// Go to the desired offset.
if (range.getIndex() == Range.INDEX_LAST) {
if (raf.length() <= range.getSize()) {
raf.seek(range.getSize());
} else {
raf.seek(raf.length() - range.getSize());
}
} else {
raf.seek(range.getIndex());
}
// Write the entity to the temporary file.
if (request.isEntityAvailable()) {
BioUtils.copy(request.getEntity().getStream(), raf);
}
} catch (IOException ioe) {
getLogger().log(Level.WARNING,
"Unable to create the temporary file", ioe);
response.setStatus(new Status(
Status.SERVER_ERROR_INTERNAL,
"Unable to create a temporary file"));
error = true;
} finally {
try {
if (raf != null) {
raf.close();
}
} catch (IOException ioe) {
getLogger().log(Level.WARNING,
"Unable to close the temporary file", ioe);
response.setStatus(Status.SERVER_ERROR_INTERNAL,
ioe);
error = true;
}
}
} else {
FileOutputStream fos = null;
try {
tmp = File.createTempFile("restlet-upload", "bin");
if (request.isEntityAvailable()) {
fos = new FileOutputStream(tmp);
BioUtils.copy(request.getEntity().getStream(), fos);
}
} catch (IOException ioe) {
getLogger().log(Level.WARNING,
"Unable to create the temporary file", ioe);
response.setStatus(new Status(
Status.SERVER_ERROR_INTERNAL,
"Unable to create a temporary file"));
error = true;
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException ioe) {
getLogger().log(Level.WARNING,
"Unable to close the temporary file", ioe);
response.setStatus(Status.SERVER_ERROR_INTERNAL,
ioe);
error = true;
}
}
}
if (error) {
if (tmp.exists() && !isResumeUpload()) {
BioUtils.delete(tmp);
}
return;
}
// Then delete the existing file
if (tmp.exists() && BioUtils.delete(file)) {
// Finally move the temporary file to the existing file
// location
boolean renameSuccessful = false;
if (tmp.renameTo(file)) {
if (request.getEntity() == null) {
response.setStatus(Status.SUCCESS_NO_CONTENT);
} else {
response.setStatus(Status.SUCCESS_OK);
}
renameSuccessful = true;
} else {
// Many aspects of the behavior of the method "renameTo"
// are inherently platform-dependent: the rename
// operation might not be able to move a file from one
// file system to another.
if (tmp.exists()) {
try {
InputStream in = new FileInputStream(tmp);
OutputStream out = new FileOutputStream(file);
BioUtils.copy(in, out);
out.close();
renameSuccessful = true;
BioUtils.delete(tmp);
} catch (Exception e) {
renameSuccessful = false;
}
}
if (!renameSuccessful) {
getLogger()
.log(Level.WARNING,
"Unable to move the temporary file to replace the existing file");
response.setStatus(new Status(
Status.SERVER_ERROR_INTERNAL,
"Unable to move the temporary file to replace the existing file"));
}
}
} else {
getLogger().log(Level.WARNING,
"Unable to delete the existing file");
response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,
"Unable to delete the existing file"));
if (tmp.exists() && !isResumeUpload()) {
BioUtils.delete(tmp);
}
}
} else {
// The file does not exist yet.
File parent = file.getParentFile();
if ((parent != null) && !parent.exists()) {
// Create the parent directories then the new file
if (!parent.mkdirs()) {
getLogger().log(Level.WARNING,
"Unable to create the parent directory");
response.setStatus(new Status(
Status.SERVER_ERROR_INTERNAL,
"Unable to create the parent directory"));
}
}
// Create the new file
if (partialPut) {
// This is a partial PUT
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(file, "rwd");
// Support only one range.
Range range = request.getRanges().get(0);
// Go to the desired offset.
if (range.getIndex() == Range.INDEX_LAST) {
if (raf.length() <= range.getSize()) {
raf.seek(range.getSize());
} else {
raf.seek(raf.length() - range.getSize());
}
} else {
raf.seek(range.getIndex());
}
// Write the entity to the file.
if (request.isEntityAvailable()) {
BioUtils.copy(request.getEntity().getStream(), raf);
}