You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModelUtils.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2013 gitblit.com
  3. * Copyright 2013 Florian Zschocke
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.gitblit.utils;
  18. import com.gitblit.Constants;
  19. /**
  20. * Utility functions for model classes that do not fit in any other category.
  21. *
  22. * @author Florian Zschocke
  23. */
  24. public class ModelUtils
  25. {
  26. private static String userRepoPrefix = Constants.DEFAULT_USER_REPOSITORY_PREFIX;
  27. /**
  28. * Set the user repository prefix.
  29. * @param prefix
  30. */
  31. public static void setUserRepoPrefix(String prefix)
  32. {
  33. if (StringUtils.isEmpty(prefix)) {
  34. userRepoPrefix = Constants.DEFAULT_USER_REPOSITORY_PREFIX;
  35. return;
  36. }
  37. String newPrefix = prefix.replace('\\', '/');
  38. if (prefix.charAt(0) == '/') {
  39. newPrefix = prefix.substring(1);
  40. }
  41. userRepoPrefix = newPrefix;
  42. }
  43. /**
  44. * Get the active user repository project prefix.
  45. */
  46. public static String getUserRepoPrefix()
  47. {
  48. return userRepoPrefix;
  49. }
  50. /**
  51. * Get the user project name for a user.
  52. *
  53. * @param username name of user
  54. * @return the active user repository project prefix concatenated with the user name
  55. */
  56. public static String getPersonalPath(String username)
  57. {
  58. return userRepoPrefix + username.toLowerCase();
  59. }
  60. /**
  61. * Test if a repository path is for a personal repository.
  62. *
  63. * @param name
  64. * A project name, a relative path to a repository.
  65. * @return true, if the name starts with the active user repository project prefix. False, otherwise.
  66. */
  67. public static boolean isPersonalRepository(String name)
  68. {
  69. if ( name.startsWith(userRepoPrefix) ) return true;
  70. return false;
  71. }
  72. /**
  73. * Test if a repository path is for a personal repository of a specific user.
  74. *
  75. * @param username
  76. * Name of a user
  77. * @param name
  78. * A project name, a relative path to a repository.
  79. * @return true, if the name starts with the active user repository project prefix. False, otherwise.
  80. */
  81. public static boolean isUsersPersonalRepository(String username, String name)
  82. {
  83. if ( name.equalsIgnoreCase(getPersonalPath(username)) ) return true;
  84. return false;
  85. }
  86. /**
  87. * Exrtract a user's name from a personal repository path.
  88. *
  89. * @param path
  90. * A project name, a relative path to a repository.
  91. * @return If the path does not point to a personal repository, an empty string is returned.
  92. * Otherwise the name of the user the personal repository belongs to is returned.
  93. */
  94. public static String getUserNameFromRepoPath(String path)
  95. {
  96. if ( !isPersonalRepository(path) ) return "";
  97. return path.substring(userRepoPrefix.length());
  98. }
  99. }