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

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