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.

Constants.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.lang.annotation.Documented;
  18. import java.lang.annotation.Retention;
  19. import java.lang.annotation.RetentionPolicy;
  20. import java.net.URL;
  21. import java.util.jar.Attributes;
  22. import java.util.jar.Manifest;
  23. /**
  24. * Constant values used by Gitblit.
  25. *
  26. * @author James Moger
  27. *
  28. */
  29. public class Constants {
  30. public static final String NAME = "Gitblit";
  31. public static final String FULL_NAME = "Gitblit - a pure Java Git solution";
  32. public static final String ADMIN_ROLE = "#admin";
  33. public static final String FORK_ROLE = "#fork";
  34. public static final String CREATE_ROLE = "#create";
  35. public static final String NOT_FEDERATED_ROLE = "#notfederated";
  36. public static final String NO_ROLE = "#none";
  37. public static final String EXTERNAL_ACCOUNT = "#externalAccount";
  38. public static final String PROPERTIES_FILE = "gitblit.properties";
  39. public static final String GIT_PATH = "/git/";
  40. public static final String ZIP_PATH = "/zip/";
  41. public static final String SYNDICATION_PATH = "/feed/";
  42. public static final String FEDERATION_PATH = "/federation/";
  43. public static final String RPC_PATH = "/rpc/";
  44. public static final String PAGES = "/pages/";
  45. public static final String SPARKLESHARE_INVITE_PATH = "/sparkleshare/";
  46. public static final String BORDER = "***********************************************************";
  47. public static final String FEDERATION_USER = "$gitblit";
  48. public static final String PROPOSAL_EXT = ".json";
  49. public static final String ENCODING = "UTF-8";
  50. public static final int LEN_SHORTLOG = 78;
  51. public static final int LEN_SHORTLOG_REFS = 60;
  52. public static final String DEFAULT_BRANCH = "default";
  53. public static final String CONFIG_GITBLIT = "gitblit";
  54. public static final String CONFIG_CUSTOM_FIELDS = "customFields";
  55. public static final String ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ";
  56. public static final String baseFolder = "baseFolder";
  57. public static final String baseFolder$ = "${" + baseFolder + "}";
  58. public static final String contextFolder$ = "${contextFolder}";
  59. public static final String HEAD = "HEAD";
  60. public static final String R_GITBLIT = "refs/gitblit/";
  61. public static final String R_HEADS = "refs/heads/";
  62. public static final String R_NOTES = "refs/notes/";
  63. public static final String R_CHANGES = "refs/changes/";
  64. public static final String R_PULL= "refs/pull/";
  65. public static final String R_TAGS = "refs/tags/";
  66. public static final String R_REMOTES = "refs/remotes/";
  67. public static String getVersion() {
  68. String v = Constants.class.getPackage().getImplementationVersion();
  69. if (v == null) {
  70. return "0.0.0-SNAPSHOT";
  71. }
  72. return v;
  73. }
  74. public static String getGitBlitVersion() {
  75. return NAME + " v" + getVersion();
  76. }
  77. public static String getBuildDate() {
  78. return getManifestValue("build-date", "PENDING");
  79. }
  80. private static String getManifestValue(String attrib, String defaultValue) {
  81. Class<?> clazz = Constants.class;
  82. String className = clazz.getSimpleName() + ".class";
  83. String classPath = clazz.getResource(className).toString();
  84. if (!classPath.startsWith("jar")) {
  85. // Class not from JAR
  86. return defaultValue;
  87. }
  88. try {
  89. String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
  90. Manifest manifest = new Manifest(new URL(manifestPath).openStream());
  91. Attributes attr = manifest.getMainAttributes();
  92. String value = attr.getValue(attrib);
  93. return value;
  94. } catch (Exception e) {
  95. }
  96. return defaultValue;
  97. }
  98. /**
  99. * Enumeration representing the four access restriction levels.
  100. */
  101. public static enum AccessRestrictionType {
  102. NONE, PUSH, CLONE, VIEW;
  103. public static AccessRestrictionType fromName(String name) {
  104. for (AccessRestrictionType type : values()) {
  105. if (type.name().equalsIgnoreCase(name)) {
  106. return type;
  107. }
  108. }
  109. return NONE;
  110. }
  111. public boolean exceeds(AccessRestrictionType type) {
  112. return this.ordinal() > type.ordinal();
  113. }
  114. public boolean atLeast(AccessRestrictionType type) {
  115. return this.ordinal() >= type.ordinal();
  116. }
  117. public String toString() {
  118. return name();
  119. }
  120. public boolean isValidPermission(AccessPermission permission) {
  121. switch (this) {
  122. case VIEW:
  123. // VIEW restriction
  124. // all access permissions are valid
  125. return true;
  126. case CLONE:
  127. // CLONE restriction
  128. // only CLONE or greater access permissions are valid
  129. return permission.atLeast(AccessPermission.CLONE);
  130. case PUSH:
  131. // PUSH restriction
  132. // only PUSH or greater access permissions are valid
  133. return permission.atLeast(AccessPermission.PUSH);
  134. case NONE:
  135. // NO access restriction
  136. // all access permissions are invalid
  137. return false;
  138. }
  139. return false;
  140. }
  141. }
  142. /**
  143. * Enumeration representing the types of authorization control for an
  144. * access restricted resource.
  145. */
  146. public static enum AuthorizationControl {
  147. AUTHENTICATED, NAMED;
  148. public static AuthorizationControl fromName(String name) {
  149. for (AuthorizationControl type : values()) {
  150. if (type.name().equalsIgnoreCase(name)) {
  151. return type;
  152. }
  153. }
  154. return NAMED;
  155. }
  156. public String toString() {
  157. return name();
  158. }
  159. }
  160. /**
  161. * Enumeration representing the types of federation tokens.
  162. */
  163. public static enum FederationToken {
  164. ALL, USERS_AND_REPOSITORIES, REPOSITORIES;
  165. public static FederationToken fromName(String name) {
  166. for (FederationToken type : values()) {
  167. if (type.name().equalsIgnoreCase(name)) {
  168. return type;
  169. }
  170. }
  171. return REPOSITORIES;
  172. }
  173. public String toString() {
  174. return name();
  175. }
  176. }
  177. /**
  178. * Enumeration representing the types of federation requests.
  179. */
  180. public static enum FederationRequest {
  181. POKE, PROPOSAL, PULL_REPOSITORIES, PULL_USERS, PULL_TEAMS, PULL_SETTINGS, PULL_SCRIPTS, STATUS;
  182. public static FederationRequest fromName(String name) {
  183. for (FederationRequest type : values()) {
  184. if (type.name().equalsIgnoreCase(name)) {
  185. return type;
  186. }
  187. }
  188. return PULL_REPOSITORIES;
  189. }
  190. public String toString() {
  191. return name();
  192. }
  193. }
  194. /**
  195. * Enumeration representing the statii of federation requests.
  196. */
  197. public static enum FederationPullStatus {
  198. PENDING, FAILED, SKIPPED, PULLED, MIRRORED, NOCHANGE, EXCLUDED;
  199. public static FederationPullStatus fromName(String name) {
  200. for (FederationPullStatus type : values()) {
  201. if (type.name().equalsIgnoreCase(name)) {
  202. return type;
  203. }
  204. }
  205. return PENDING;
  206. }
  207. @Override
  208. public String toString() {
  209. return name();
  210. }
  211. }
  212. /**
  213. * Enumeration representing the federation types.
  214. */
  215. public static enum FederationStrategy {
  216. EXCLUDE, FEDERATE_THIS, FEDERATE_ORIGIN;
  217. public static FederationStrategy fromName(String name) {
  218. for (FederationStrategy type : values()) {
  219. if (type.name().equalsIgnoreCase(name)) {
  220. return type;
  221. }
  222. }
  223. return FEDERATE_THIS;
  224. }
  225. public boolean exceeds(FederationStrategy type) {
  226. return this.ordinal() > type.ordinal();
  227. }
  228. public boolean atLeast(FederationStrategy type) {
  229. return this.ordinal() >= type.ordinal();
  230. }
  231. @Override
  232. public String toString() {
  233. return name();
  234. }
  235. }
  236. /**
  237. * Enumeration representing the possible results of federation proposal
  238. * requests.
  239. */
  240. public static enum FederationProposalResult {
  241. ERROR, FEDERATION_DISABLED, MISSING_DATA, NO_PROPOSALS, NO_POKE, ACCEPTED;
  242. @Override
  243. public String toString() {
  244. return name();
  245. }
  246. }
  247. /**
  248. * Enumeration representing the possible remote procedure call requests from
  249. * a client.
  250. */
  251. public static enum RpcRequest {
  252. // Order is important here. anything above LIST_SETTINGS requires
  253. // administrator privileges and web.allowRpcManagement.
  254. CLEAR_REPOSITORY_CACHE, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, LIST_SETTINGS,
  255. CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,
  256. LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER,
  257. LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,
  258. LIST_REPOSITORY_MEMBERS, SET_REPOSITORY_MEMBERS, LIST_REPOSITORY_TEAMS, SET_REPOSITORY_TEAMS,
  259. LIST_REPOSITORY_MEMBER_PERMISSIONS, SET_REPOSITORY_MEMBER_PERMISSIONS, LIST_REPOSITORY_TEAM_PERMISSIONS, SET_REPOSITORY_TEAM_PERMISSIONS,
  260. LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS, LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS,
  261. EDIT_SETTINGS, LIST_STATUS;
  262. public static RpcRequest fromName(String name) {
  263. for (RpcRequest type : values()) {
  264. if (type.name().equalsIgnoreCase(name)) {
  265. return type;
  266. }
  267. }
  268. return null;
  269. }
  270. public boolean exceeds(RpcRequest type) {
  271. return this.ordinal() > type.ordinal();
  272. }
  273. @Override
  274. public String toString() {
  275. return name();
  276. }
  277. }
  278. /**
  279. * Enumeration of the search types.
  280. */
  281. public static enum SearchType {
  282. AUTHOR, COMMITTER, COMMIT;
  283. public static SearchType forName(String name) {
  284. for (SearchType type : values()) {
  285. if (type.name().equalsIgnoreCase(name)) {
  286. return type;
  287. }
  288. }
  289. return COMMIT;
  290. }
  291. @Override
  292. public String toString() {
  293. return name().toLowerCase();
  294. }
  295. }
  296. /**
  297. * The types of objects that can be indexed and queried.
  298. */
  299. public static enum SearchObjectType {
  300. commit, blob, issue;
  301. static SearchObjectType fromName(String name) {
  302. for (SearchObjectType value : values()) {
  303. if (value.name().equals(name)) {
  304. return value;
  305. }
  306. }
  307. return null;
  308. }
  309. }
  310. /**
  311. * The access permissions available for a repository.
  312. */
  313. public static enum AccessPermission {
  314. NONE("N"), EXCLUDE("X"), VIEW("V"), CLONE("R"), PUSH("RW"), CREATE("RWC"), DELETE("RWD"), REWIND("RW+"), OWNER("RW+");
  315. public static final AccessPermission [] NEWPERMISSIONS = { EXCLUDE, VIEW, CLONE, PUSH, CREATE, DELETE, REWIND };
  316. public static AccessPermission LEGACY = REWIND;
  317. public final String code;
  318. private AccessPermission(String code) {
  319. this.code = code;
  320. }
  321. public boolean atMost(AccessPermission perm) {
  322. return ordinal() <= perm.ordinal();
  323. }
  324. public boolean atLeast(AccessPermission perm) {
  325. return ordinal() >= perm.ordinal();
  326. }
  327. public boolean exceeds(AccessPermission perm) {
  328. return ordinal() > perm.ordinal();
  329. }
  330. public String asRole(String repository) {
  331. return code + ":" + repository;
  332. }
  333. @Override
  334. public String toString() {
  335. return code;
  336. }
  337. public static AccessPermission permissionFromRole(String role) {
  338. String [] fields = role.split(":", 2);
  339. if (fields.length == 1) {
  340. // legacy/undefined assume full permissions
  341. return AccessPermission.LEGACY;
  342. } else {
  343. // code:repository
  344. return AccessPermission.fromCode(fields[0]);
  345. }
  346. }
  347. public static String repositoryFromRole(String role) {
  348. String [] fields = role.split(":", 2);
  349. if (fields.length == 1) {
  350. // legacy/undefined assume full permissions
  351. return role;
  352. } else {
  353. // code:repository
  354. return fields[1];
  355. }
  356. }
  357. public static AccessPermission fromCode(String code) {
  358. for (AccessPermission perm : values()) {
  359. if (perm.code.equalsIgnoreCase(code)) {
  360. return perm;
  361. }
  362. }
  363. return AccessPermission.NONE;
  364. }
  365. }
  366. public static enum RegistrantType {
  367. REPOSITORY, USER, TEAM;
  368. }
  369. public static enum PermissionType {
  370. MISSING, ANONYMOUS, EXPLICIT, TEAM, REGEX, OWNER, ADMINISTRATOR;
  371. }
  372. public static enum GCStatus {
  373. READY, COLLECTING;
  374. public boolean exceeds(GCStatus s) {
  375. return ordinal() > s.ordinal();
  376. }
  377. }
  378. public static enum AuthenticationType {
  379. CREDENTIALS, COOKIE, CERTIFICATE, CONTAINER;
  380. public boolean isStandard() {
  381. return ordinal() <= COOKIE.ordinal();
  382. }
  383. }
  384. public static enum AccountType {
  385. LOCAL, EXTERNAL, LDAP, REDMINE, SALESFORCE, WINDOWS;
  386. public boolean isLocal() {
  387. return this == LOCAL;
  388. }
  389. }
  390. @Documented
  391. @Retention(RetentionPolicy.RUNTIME)
  392. public @interface Unused {
  393. }
  394. }