/// <summary> /// Try to find the CMIS server associated to any URL. /// Users can provide the URL of the web interface, and we have to return the CMIS URL /// Returns the list of repositories as well. /// </summary> static public Tuple <CmisServer, Exception> GetRepositoriesFuzzy(Credentials.ServerCredentials credentials) { Dictionary <string, string> repositories = null; Exception firstException = null; // Try the given URL, maybe user directly entered the CMIS AtomPub endpoint URL. try { repositories = GetRepositories(credentials); } catch (DotCMIS.Exceptions.CmisRuntimeException e) { if (e.Message == "ConnectFailure") { return(new Tuple <CmisServer, Exception>(new CmisServer(credentials.Address, null), new CmisServerNotFoundException(e.Message, e))); } firstException = e; } catch (Exception e) { // Save first Exception and try other possibilities. firstException = e; } if (repositories != null) { // Found! return(new Tuple <CmisServer, Exception>(new CmisServer(credentials.Address, repositories), null)); } // Extract protocol and server name or IP address string prefix = credentials.Address.GetLeftPart(UriPartial.Authority); // See https://github.com/nicolas-raoul/CmisSync/wiki/What-address for the list of ECM products prefixes // Please send us requests to support more CMIS servers: https://github.com/nicolas-raoul/CmisSync/issues string[] suffixes = { "/cmis/atom11", "/alfresco/cmisatom", "/alfresco/service/cmis", "/cmis/resources/", "/emc-cmis-ea/resources/", "/xcmis/rest/cmisatom", "/files/basic/cmis/my/servicedoc", "/p8cmis/resources/Service", "/_vti_bin/cmis/rest?getRepositories", "/Nemaki/atom/bedroom", "/nuxeo/atom/cmis", "/cmis/atom" }; string bestUrl = null; // Try all suffixes for (int i = 0; i < suffixes.Length; i++) { string fuzzyUrl = prefix + suffixes[i]; Logger.Info("Sync | Trying with " + fuzzyUrl); try { Credentials.ServerCredentials cred = new Credentials.ServerCredentials() { UserName = credentials.UserName, Password = credentials.Password.ToString(), Address = new Uri(fuzzyUrl) }; repositories = GetRepositories(cred); } catch (DotCMIS.Exceptions.CmisPermissionDeniedException e) { firstException = new CmisSync.Lib.Cmis.CmisPermissionDeniedException(e.Message, e); bestUrl = fuzzyUrl; } catch (Exception e) { // Do nothing, try other possibilities. Logger.Debug(e.Message); } if (repositories != null) { // Found! return(new Tuple <CmisServer, Exception>(new CmisServer(new Uri(fuzzyUrl), repositories), null)); } } // Not found. Return also the first exception to inform the user correctly return(new Tuple <CmisServer, Exception>(new CmisServer(bestUrl == null ? credentials.Address : new Uri(bestUrl), null), firstException)); }
/// <summary> /// Try to find the CMIS server associated to any URL. /// Users can provide the URL of the web interface, and we have to return the CMIS URL /// Returns the list of repositories as well. /// </summary> static public Tuple<CmisServer, Exception> GetRepositoriesFuzzy(Credentials.ServerCredentials credentials) { Dictionary<string, string> repositories = null; Exception firstException = null; // Try the given URL, maybe user directly entered the CMIS AtomPub endpoint URL. try { repositories = GetRepositories(credentials); } catch (DotCMIS.Exceptions.CmisRuntimeException e) { if (e.Message == "ConnectFailure") { return new Tuple<CmisServer, Exception>(new CmisServer(credentials.Address, null), new CmisServerNotFoundException(e.Message, e)); } firstException = e; } catch (Exception e) { // Save first Exception and try other possibilities. firstException = e; } if (repositories != null) { // Found! return new Tuple<CmisServer, Exception>(new CmisServer(credentials.Address, repositories), null); } // Extract protocol and server name or IP address string prefix = credentials.Address.GetLeftPart(UriPartial.Authority); // See https://github.com/nicolas-raoul/CmisSync/wiki/What-address for the list of ECM products prefixes // Please send us requests to support more CMIS servers: https://github.com/nicolas-raoul/CmisSync/issues string[] suffixes = { "/cmis/atom11", "/alfresco/cmisatom", "/alfresco/service/cmis", "/cmis/resources/", "/emc-cmis-ea/resources/", "/xcmis/rest/cmisatom", "/files/basic/cmis/my/servicedoc", "/p8cmis/resources/Service", "/_vti_bin/cmis/rest?getRepositories", "/Nemaki/atom/bedroom", "/nuxeo/atom/cmis", "/cmis/atom" }; string bestUrl = null; // Try all suffixes for (int i = 0; i < suffixes.Length; i++) { string fuzzyUrl = prefix + suffixes[i]; Logger.Info("Sync | Trying with " + fuzzyUrl); try { Credentials.ServerCredentials cred = new Credentials.ServerCredentials() { UserName = credentials.UserName, Password = credentials.Password.ToString(), Address = new Uri(fuzzyUrl) }; repositories = GetRepositories(cred); } catch (DotCMIS.Exceptions.CmisPermissionDeniedException e) { firstException = new CmisSync.Lib.Cmis.CmisPermissionDeniedException(e.Message, e); bestUrl = fuzzyUrl; } catch (Exception e) { // Do nothing, try other possibilities. Logger.Debug(e.Message); } if (repositories != null) { // Found! return new Tuple<CmisServer, Exception>(new CmisServer(new Uri(fuzzyUrl), repositories), null); } } // Not found. Return also the first exception to inform the user correctly return new Tuple<CmisServer, Exception>(new CmisServer(bestUrl == null ? credentials.Address : new Uri(bestUrl), null), firstException); }
/// <summary> /// Get the list of repositories of a CMIS server /// Each item contains id + /// </summary> /// <returns>The list of repositories. Each item contains the identifier and the human-readable name of the repository.</returns> static public Dictionary <string, string> GetRepositories(Credentials.ServerCredentials credentials) { Dictionary <string, string> result = new Dictionary <string, string>(); // If no URL was provided, return empty result. if (credentials.Address == null) { return(result); } // Create session factory. SessionFactory factory = SessionFactory.NewInstance(); Dictionary <string, string> cmisParameters = new Dictionary <string, string>(); cmisParameters[SessionParameter.BindingType] = BindingType.AtomPub; cmisParameters[SessionParameter.AtomPubUrl] = credentials.Address.ToString(); cmisParameters[SessionParameter.User] = credentials.UserName; cmisParameters[SessionParameter.Password] = credentials.Password.ToString(); IList <IRepository> repositories; try { repositories = factory.GetRepositories(cmisParameters); } catch (DotCMIS.Exceptions.CmisPermissionDeniedException e) { Logger.Debug("CMIS server found, but permission denied. Please check username/password. " + Utils.ToLogString(e)); throw; } catch (CmisRuntimeException e) { Logger.Debug("No CMIS server at this address, or no connection. " + Utils.ToLogString(e)); throw; } catch (CmisObjectNotFoundException e) { Logger.Debug("No CMIS server at this address, or no connection. " + Utils.ToLogString(e)); throw; } catch (CmisConnectionException e) { Logger.Debug("No CMIS server at this address, or no connection. " + Utils.ToLogString(e)); throw; } catch (CmisInvalidArgumentException e) { Logger.Debug("Invalid URL, maybe Alfresco Cloud? " + Utils.ToLogString(e)); throw; } // Populate the result list with identifier and name of each repository. foreach (IRepository repo in repositories) { if (!Utils.IsRepoNameHidden(repo.Name, ConfigManager.CurrentConfig.HiddenRepoNames)) { result.Add(repo.Id, repo.Name); } } return(result); }