*/
@Override
public Resource resolveFile(String host, File file) {
//Catch request user
Request request = new ServletRequest((HttpServletRequest)MiltonServlet.request());
User user = retrieveUserFromRequestAuth(request);
LOGGER.debug("USER NULL??? - " + user);
if (user != null && request.getMethod().equals(Method.PROPFIND)) {
LOGGER.debug("12345");
if(getRoot().equals(new File(ServerConfiguration.getWebDAVrootPath()))){
LOGGER.debug("Ehi! I'm here! - user id: " + user.getId());
setRoot(ServerConfiguration.getWebDAVrootPath() + "/" + user.getId());
LOGGER.debug("file:_" + file.toString());
String newPath = file.toString().replace(ServerConfiguration.getWebDAVrootPath(), "");
file = new File(ServerConfiguration.getWebDAVrootPath() + "/" + user.getId() + "/" + newPath);
LOGGER.debug("new file: " + file.toString());
LOGGER.debug("Nuova root: " + getRoot().toString());
}
}else if(user == null ){
LOGGER.debug("USER == NULL 2");
File fileToConsider = null;
boolean inSharedFolder = false;
String username = null;
if(file.getPath().contains("/Shared")) {
fileToConsider = new File(retrieveRealFilePath(file.getPath()));
inSharedFolder = true;
username = new User(String.valueOf(Utils.retrieveUserID(fileToConsider.getPath()))).getUsername();
}else {
fileToConsider = file;
}
if (lockMap.containsKey(fileToConsider.getParent())) {
LOGGER.debug("user == null and parent: " + fileToConsider.getParent() + " is into lockMap");
String newPath = fileToConsider.toString().replace(ServerConfiguration.getWebDAVrootPath(), "");
LOGGER.debug("new Path: " + newPath + " split path lenght: " + newPath.split("/").length);
if(newPath.split("/").length >= 2)LOGGER.debug("newPath.split(\"/\")[1] : " + newPath.split("/")[1]);
LOGGER.debug("String.valueOf(lockMap.get(file.getParent()): " + String.valueOf(lockMap.get(fileToConsider.getParent())));
boolean founded = false;
if (newPath.split("/").length >= 2 && newPath.split("/")[1].equals(String.valueOf(lockMap.get(fileToConsider.getParent())))) {
LOGGER.debug("FOUND!!!!");
newPath = newPath.substring(String.valueOf(lockMap.get(fileToConsider.getParent())).length()+2);
LOGGER.debug("final new Path: " + newPath);
founded = true;
}
if (inSharedFolder) {
if (founded) {
file = new File(ServerConfiguration.getWebDAVrootPath() + "/" + lockMap.get(fileToConsider.getParent()) + "/Shared/" + username + "/" + newPath);
}else{
file = new File(ServerConfiguration.getWebDAVrootPath() + "/" + lockMap.get(fileToConsider.getParent()) + "/Shared/" + username + "/" + newPath.substring(2+(String.valueOf(User.getId(username)).length())));
}
}else{
file = new File(ServerConfiguration.getWebDAVrootPath() + "/" + lockMap.get(fileToConsider.getParent()) + "/" + newPath);
}
LOGGER.debug("new file: " + file.toString());
}else if(fileToConsider.getPath().equals(ServerConfiguration.getWebDAVrootPath()) && lockMap.containsKey(fileToConsider.getPath()) && !".tokens".equals(fileToConsider.getName())){
LOGGER.debug("user == null and root: " + fileToConsider.getPath() + " is into lockMap");
if (inSharedFolder) {
file = new File(ServerConfiguration.getWebDAVrootPath() + "/" + lockMap.get(fileToConsider.getPath()) + "/Shared/" + username);
}else{
file = new File(ServerConfiguration.getWebDAVrootPath() + "/" + lockMap.get(fileToConsider.getPath()));
}
LOGGER.debug("new file: " + file.toString());
}else if(!fileToConsider.getParent().equals(ServerConfiguration.getWebDAVrootPath()) && !".tokens".equals(fileToConsider.getName())){
LOGGER.debug("a3a");
try{
if (inSharedFolder) {
throw new NumberFormatException("Forced Exception");
}
Utils.retrieveUserID(fileToConsider.getPath());
}catch (NumberFormatException e) {
LOGGER.debug("controllo tra gli utenti loggati");
//ciclo la map che contiene le cartelle locked
//vedo se c'è una corrispondenza con un utente loggato
//IMPEDIRE CHE CE NE SIANO PIU' DI UNA
int userWithSameNameFolderLocked = 0;
Set<String> usersInSessions = SessionManager.getIdInSession();
String rootPath = ServerConfiguration.getWebDAVrootPath();
for (Iterator<String> iterator = usersInSessions.iterator(); iterator
.hasNext();) {
String userId = (String) iterator.next();
String relativePath = fileToConsider.getParent().replace(rootPath, "");
String pathToCompare = "";
if (inSharedFolder) {
if (fileToConsider.isDirectory()) {
pathToCompare = fileToConsider.getPath();
relativePath = fileToConsider.getPath().replace(rootPath, "");
}else{
pathToCompare = rootPath + relativePath;
}
}else{
pathToCompare = rootPath + "/" + userId + relativePath;
}
LOGGER.debug("cerco se lockMap contiene: " + pathToCompare);
if (lockMap.containsKey(pathToCompare)) {
userWithSameNameFolderLocked ++;
if (inSharedFolder) {
if (fileToConsider.isDirectory()) {
file = new File(rootPath + "/" + lockMap.get(rootPath + relativePath) + "/Shared/" + username + relativePath.substring(1 + String.valueOf((User.getId(username))).length()));
}else{
file = new File(rootPath + "/" + lockMap.get(rootPath + relativePath) + "/Shared/" + username + relativePath + "/" + fileToConsider.getName());
}
}else{
file = new File(rootPath + "/" + lockMap.get(rootPath + "/" + userId + relativePath) + relativePath + "/" + fileToConsider.getName());
}
LOGGER.debug("new file: " + fileToConsider.toString());
}
}
if (userWithSameNameFolderLocked > 1) {
LOGGER.error("CI SONO PIU' DI UN UTENTE CONNESSI CHE HANNO LOCKATO UNA CARTELLA CON PATH IDENTICA");
}
if (userWithSameNameFolderLocked == 0) {
//Se nn ho trovato niente provo a vedere se c'è qualcosa nella root del server
//Valido per la richiesta di logout che non può sapere se ci sono utenti connessi o meno
LOGGER.debug("nessuna occorrenza trovata. Provo a vedere se il file è nella root");
File newFile = new File(rootPath + "/" + fileToConsider.getName());
if (newFile.exists()) {
LOGGER.debug("new file esiste! ");
file = newFile;
}
}
}
}else if(fileToConsider.getParent().equals(ServerConfiguration.getWebDAVrootPath()) && !".tokens".equals(fileToConsider.getName())){
LOGGER.debug("b3b");
try{
if (inSharedFolder) {
throw new NumberFormatException("Forced Exception");
}
Utils.retrieveUserID(fileToConsider.getPath());
}catch (NumberFormatException e) {
LOGGER.debug("controllo tra gli utenti loggati");
//ciclo la map che contiene le cartelle locked
//vedo se c'è una corrispondenza con un utente loggato
//IMPEDIRE CHE CE NE SIANO PIU' DI UNA
int userWithSameNameFolderLocked = 0;
Set<String> usersInSessions = SessionManager.getIdInSession();
String rootPath = ServerConfiguration.getWebDAVrootPath();
for (Iterator<String> iterator = usersInSessions.iterator(); iterator
.hasNext();) {
String userId = (String) iterator.next();
String relativePath = "";
if (".response".equals(fileToConsider.getName()) || ".request".equals(fileToConsider.getName())) {
relativePath = fileToConsider.getParent().replace(rootPath, "");
}else{
relativePath = fileToConsider.getPath().replace(rootPath, "");
}
String pathToCompare = "";
if (inSharedFolder) {
pathToCompare = rootPath + relativePath;
}else{
pathToCompare = rootPath + "/" + userId + relativePath;
}
LOGGER.debug("cerco se lockMap contiene: " + pathToCompare);
if (lockMap.containsKey(pathToCompare)) {
userWithSameNameFolderLocked ++;
if (inSharedFolder) {
file = new File(rootPath + "/" + lockMap.get(rootPath + relativePath) + "/Shared/" + username + relativePath);
}else{
file = new File(rootPath + "/" + lockMap.get(rootPath + "/" + userId + relativePath) + relativePath);
}
LOGGER.debug("new file: " + file.toString());
}
}
if (userWithSameNameFolderLocked > 1) {
LOGGER.error("CI SONO PIU' DI UN UTENTE CONNESSI CHE HANNO LOCKATO UNA CARTELLA CON PATH IDENTICA");
}
}
}else if(".tokens".equals(fileToConsider.getName())){
String realTokensPath = TokensResource.getLockedPath(fileToConsider.getPath(), lockMap, username);
file = new File(realTokensPath);
LOGGER.debug("tokens at path: " + file.getPath() + " exists? " + file.exists());
}
}
Map<String,String> headers = request.getHeaders();
try{
Utils.retrieveUserID(file.getPath());
} catch (NumberFormatException e) {
//Lock management
if(!file.getPath().contains("/Shared")){
if(file.getName().contains(".lock")){
return new PlainSimpleResource(lockResource(file, user, false));
}
}else if(file.getPath().contains("/Shared")){
File newFile = new File(retrieveRealFilePath(file.getAbsolutePath()));
if(file.getName().contains(".lock")){
return new PlainSimpleResource(lockResource(newFile, user, true));
}
}
}
//Unlock management
if (file.getName().contains(".unlock")) {
if (request.getMethod().equals(Method.OPTIONS)) {
if(!file.getPath().contains("/Shared")){
return new PlainSimpleResource(unlockResource(file, false));
}else if(file.getPath().contains("/Shared")){
File newFile = new File(retrieveRealFilePath(file.getAbsolutePath()));
return new PlainSimpleResource(unlockResource(newFile, true));
}else return null;
}else{
return null;
}
}
//Bypass only for OVER ENCRYPT REQUEST / RESPONSE CLIENT
LOGGER.debug("FILE '" + file.getPath() +"'EXISTS? " + file.exists());
if(!file.getPath().contains("/Shared") && file.exists()){
if(".request".equals(file.getName())){
LOGGER.debug(".request intercepted");
return new PlainSimpleResource(RequestManager.manageRequest(file, request, false));
}
if(".response".equals(file.getName())){
return new PlainSimpleResource(file);
}
}else if(file.getPath().contains("/Shared")){
File newFile = new File(retrieveRealFilePath(file.getAbsolutePath()));
if(newFile.exists()){
if(".request".equals(file.getName())){
LOGGER.debug(".request intercepted");
return new PlainSimpleResource(RequestManager.manageRequest(newFile, request, true));
}
if(".response".equals(file.getName())){
return new PlainSimpleResource(newFile);
}
}
}
if (".response".equals(file.getName()) && !file.exists() && request.getMethod().equals(Method.DELETE)) {
return null;
}
//TODO CHANGE USER-AGENT NAME IN CLIENT
if(headers.containsKey("user-agent")){
if(headers.get("user-agent").contains("Jakarta Commons-HttpClient")){
LOGGER.debug("user-agent - Jakarta Commons-Http client recognise!");
if(file.getPath().contains("/Shared")){
return resolveFileIfPermitted(host, file, false, false);
}else{
return resolveFileIfPermitted(host, file, true, true);
}
}
}
//Path control
//In this way, it happens only when the user is IN shared folder with some resources
if(file.getPath().contains("/Shared") && file.getPath().indexOf("/Shared/") == -1){
// 1. Special case: "Shared" folder
return resolveFileIfPermitted(host, file, false, false);
}else if(file.getPath().contains("/Shared") && doubleCheckOnShared(file.getPath())){
// 2. Case into shared folders (also 'username' folder, or their subtrees)
LOGGER.debug("INTO Shared folder with any files");
if(previousRequest == request.hashCode()){
LOGGER.debug("same request before");
if(permissionsUpdated){
LOGGER.debug("permissionsUpdated");
return resolveFileIfPermitted(host, file, false, false);
} else {
LOGGER.debug("permissions don't updated");
return null;
}
} else {
permissionsUpdated = false;
previousRequest = request.hashCode();
LOGGER.debug("previousRequest: " + previousRequest);
if(user != null && sharedUpdated.containsKey(user.getId()) && sharedUpdated.get(user.getId())){
LOGGER.debug("sharedUpdated containsKey(userId) and permissions are not deprecated");
LOGGER.debug("PERMISSION ALREADY UPDATE AND NOT DEPRECATED. LIST FILES");
permissionsUpdated = true;
return resolveFileIfPermitted(host, file, false, false);
}else{
//If the user that made request never update permissions or its permissions are out of date
LOGGER.debug("send update permissions request");
if(user == null)
LOGGER.error("Which way has it followed? It's called a webdav request without authorization inside (GET, PUT,...? ");
return new PlainSimpleResource(ServerUpdatePermissionsManager.sendUpdatePermissionsResponseForSharedFiles(file, user));
}
}
} else {
// 3. Not in shared folders, owner folders
LOGGER.debug("Not in /shared/ - user owned file");
if(this.getRoot().equals(file)){
if(this.getRoot().getPath().equals(ServerConfiguration.getWebDAVrootPath())){
LOGGER.debug("resolveFileIfPermitted1");
return resolveFileIfPermitted(host, file, true, true);
} else {
if(user != null) {
LOGGER.debug("OverEncryptedRootFolder");
return new OverEncryptedRootFolder(host, this, user.getId());
}
else {
LOGGER.debug("User null");
return resolveFileIfPermitted(host, new File(file.getParent()), true, true);
}