File file = new File(decodedPath);
MetadataService metadataService = getMetadataService(request);
if (request.getMethod().equals(Method.GET)
|| request.getMethod().equals(Method.HEAD)) {
Representation output = null;
// Get variants for a resource
boolean found = false;
Iterator<Preference<MediaType>> iterator = request.getClientInfo()
.getAcceptedMediaTypes().iterator();
while (iterator.hasNext() && !found) {
Preference<MediaType> pref = iterator.next();
found = pref.getMetadata().equals(MediaType.TEXT_URI_LIST);
}
if (found) {
// Try to list all variants of this resource
// 1- set up base name as the longest part of the name without
// known extensions (beginning from the left)
String baseName = getBaseName(file, metadataService);
// 2- looking for resources with the same base name
if (file.getParentFile() != null) {
File[] files = file.getParentFile().listFiles();
if (files != null) {
ReferenceList rl = new ReferenceList(files.length);
String encodedParentDirectoryURI = path.substring(0,
path.lastIndexOf("/"));
String encodedFileName = path.substring(path
.lastIndexOf("/") + 1);
for (File entry : files) {
if (baseName.equals(getBaseName(entry, metadataService))) {
rl
.add(LocalReference
.createFileReference(encodedParentDirectoryURI
+ "/"
+ getReencodedVariantFileName(
encodedFileName,
entry.getName())));
}
}
output = rl.getTextRepresentation();
}
}
} else {
if ((file != null) && file.exists()) {
if (file.isDirectory()) {
// Return the directory listing
File[] files = file.listFiles();
ReferenceList rl = new ReferenceList(files.length);
rl.setIdentifier(request.getResourceRef());
String directoryUri = request.getResourceRef()
.toString();
// Ensures that the directory URI ends with a slash
if (!directoryUri.endsWith("/")) {
directoryUri += "/";
}
for (File entry : files) {
rl.add(directoryUri + entry.getName());
}
output = rl.getTextRepresentation();
} else {
// Return the file content
output = new FileRepresentation(file, metadataService
.getDefaultMediaType(), getTimeToLive());
updateMetadata(metadataService, file.getName(), output);
}
}
}
if (output == null) {
response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
} else {
output.setIdentifier(request.getResourceRef());
response.setEntity(output);
response.setStatus(Status.SUCCESS_OK);
}
} else if (request.getMethod().equals(Method.PUT)) {
// Several checks : first the consistency of the metadata and the