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

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