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 14KB

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