try { tmp = File.createTempFile("restlet-upload", "bin");  if (request.isEntityAvailable()) { FileOutputStream fos = new FileOutputStream(tmp); ByteUtils.write(request.getEntity().getStream(), fos); fos.close(); } } 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")); }   if (file.delete()) {  if ((tmp != null) && tmp.renameTo(file)) { if (request.getEntity() == null) { response.setStatus(Status.SUCCESS_NO_CONTENT);
if (path.endsWith("/")) { isDirectory = true;  if (file.mkdirs()) { response.setStatus(Status.SUCCESS_NO_CONTENT); } else { getLogger().log(Level.WARNING, "Unable to create the new directory"); response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL, "Unable to create the new directory")); } } }  if (!isDirectory) {   String baseName = getBaseName(file, metadataService); Set<String> extensions = getExtensions(file, metadataService);  File[] files = file.getParentFile().listFiles(); File uniqueVariant = null;  List<File> variantsList = new ArrayList<File>(); for (File entry : files) { if (entry.getName().startsWith(baseName)) { Set<String> entryExtensions = getExtensions(entry, metadataService); if (entryExtensions.containsAll(extensions)) { variantsList.add(entry); if (extensions.containsAll(entryExtensions)) {  uniqueVariant = entry; } } } } if (uniqueVariant != null) { file = uniqueVariant; } else { if (!variantsList.isEmpty()) {    response .setStatus(new Status( Status.CLIENT_ERROR_UNAUTHORIZED, "Unable to process properly the request. Several variants exist but none of them suits precisely.")); } else {   updateMetadata(metadataService, file.getName(), request.getEntity()); if (request.getEntity().getLanguage() == null) { request.getEntity().setLanguage( metadataService.getDefaultLanguage()); } if (request.getEntity().getMediaType() == null) { request.getEntity().setMediaType( metadataService.getDefaultMediaType()); } if (request.getEntity().getEncoding() == null) { request.getEntity().setEncoding( metadataService.getDefaultEncoding()); }  StringBuilder fileName = new StringBuilder(baseName); if (metadataService.getExtension(request.getEntity().getMediaType()) != null) { fileName.append("." + metadataService.getExtension(request.getEntity() .getMediaType())); } if (metadataService.getExtension(request.getEntity().getLanguage()) != null) { fileName.append("." + metadataService.getExtension(request.getEntity() .getLanguage())); } if (metadataService.getExtension(request.getEntity().getEncoding()) != null) { fileName.append("." + metadataService.getExtension(request.getEntity() .getEncoding())); }  file = new File(file.getParentFile(), fileName.toString()); } }  if (!checkExtensionsConsistency(file, metadataService)) { response .setStatus(new Status(Status.SERVER_ERROR_INTERNAL, "Unable to process properly the URI. At least one extension is not known by the server.")); } else { File tmp = null;  if (file.exists()) {   try { tmp = File.createTempFile("restlet-upload", "bin");  if (request.isEntityAvailable()) { FileOutputStream fos = new FileOutputStream(tmp); ByteUtils.write(request.getEntity().getStream(), fos); fos.close(); } } 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")); }   if (file.delete()) {  if ((tmp != null) && tmp.renameTo(file)) { if (request.getEntity() == null) { response.setStatus(Status.SUCCESS_NO_CONTENT); } else { response.setStatus(Status.SUCCESS_OK); } } else { 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")); }
	protected void handleFile(Request request, Response response, String path)	{		File file = new File(LocalReference.localizePath(path));		MetadataService metadataService = getMetadataService(request);		if (request.getMethod().equals(Method.GET)				|| request.getMethod().equals(Method.HEAD))		{			Representation output = null;			// TBoi : 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)			{				//1- set up base name as the longest part of the name without known extensions (beginning from the left)				String[] result = file.getName().split("\\.");				StringBuilder baseName = new StringBuilder().append(result[0]);				boolean extensionFound = false;				for (int i = 1; (i < result.length) && !extensionFound; i++)				{					extensionFound = metadataService.getMetadata(result[i]) != null;					if (!extensionFound)					{						baseName.append(".").append(result[i]);					}				}				//2- loooking for resources with the same base name				File[] files = file.getParentFile().listFiles();				ReferenceList rl = new ReferenceList(files.length);				rl.setListRef(request.getResourceRef());				for (File entry : files)				{					try					{						if (entry.getName().startsWith(baseName.toString()))						{							rl.add(LocalReference.createFileReference(entry));						}					}					catch (IOException ioe)					{						getLogger().log(Level.WARNING, "Unable to create file reference", ioe);					}				}				output = rl.getRepresentation();			}			else			{				if ((file != null) && file.exists())				{					if (file.isDirectory())					{						// Return the directory listing						File[] files = file.listFiles();						ReferenceList rl = new ReferenceList(files.length);						rl.setListRef(request.getResourceRef());						for (File entry : files)						{							try							{								rl.add(LocalReference.createFileReference(entry));							}							catch (IOException ioe)							{								getLogger().log(Level.WARNING, "Unable to create file reference",										ioe);							}						}						output = rl.getRepresentation();					}					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			{				response.setEntity(output);				response.setStatus(Status.SUCCESS_OK);			}		}		else if (request.getMethod().equals(Method.PUT))		{			File tmp = null;			if (file.exists())			{				if (file.isDirectory())				{					response.setStatus(new Status(Status.CLIENT_ERROR_FORBIDDEN,							"Can't put a new representation of a directory"));				}				else				{					// Replace the content of the file					// First, create a temporary file					try					{						tmp = File.createTempFile("restlet-upload", "bin");						if (request.isEntityAvailable())						{							FileOutputStream fos = new FileOutputStream(tmp);							ByteUtils.write(request.getEntity().getStream(), fos);							fos.close();						}					}					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"));					}					// Then delete the existing file					if (file.delete())					{						// Finally move the temporary file to the existing file location						if ((tmp != null) && tmp.renameTo(file))						{							if (request.getEntity() == null)							{								response.setStatus(Status.SUCCESS_NO_CONTENT);							}							else							{								response.setStatus(Status.SUCCESS_OK);							}						}						else						{							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"));					}				}			}			else			{				// No existing file or directory found				if (path.endsWith("/"))				{					// Create a new directory and its necessary parents					if (file.mkdirs())					{						response.setStatus(Status.SUCCESS_NO_CONTENT);					}					else					{						getLogger().log(Level.WARNING, "Unable to create the new directory");						response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,								"Unable to create the new directory"));					}				}				else				{					File parent = file.getParentFile();					if ((parent != null) && parent.isDirectory())					{						if (!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					try					{						if (file.createNewFile())						{							if (request.getEntity() == null)							{								response.setStatus(Status.SUCCESS_NO_CONTENT);							}							else							{								FileOutputStream fos = new FileOutputStream(file);								ByteUtils.write(request.getEntity().getStream(), fos);								fos.close();								response.setStatus(Status.SUCCESS_OK);							}						}						else						{							getLogger().log(Level.WARNING, "Unable to create the new file");							response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,									"Unable to create the new file"));						}					}					catch (FileNotFoundException fnfe)					{						getLogger().log(Level.WARNING, "Unable to create the new file", fnfe);						response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,								"Unable to create the new file"));					}					catch (IOException ioe)					{						getLogger().log(Level.WARNING, "Unable to create the new file", ioe);						response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,								"Unable to create the new file"));					}				}			}		}		else if (request.getMethod().equals(Method.DELETE))		{			if (file.delete())			{				response.setStatus(Status.SUCCESS_NO_CONTENT);			}			else			{				if (file.isDirectory())				{					if (file.listFiles().length == 0)					{						response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,								"Couldn't delete the empty directory"));					}					else					{						response.setStatus(new Status(Status.CLIENT_ERROR_FORBIDDEN,								"Couldn't delete the non-empty directory"));					}				}				else				{					response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,							"Couldn't delete the file"));				}			}		}		else		{			response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);		}	}