Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Constants.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 = "/branch/";
  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 above LIST_SETTINGS requires
  274. // administrator privileges and web.allowRpcManagement.
  275. CLEAR_REPOSITORY_CACHE, REINDEX_TICKETS, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER, LIST_SETTINGS,
  276. CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,
  277. LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER,
  278. LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,
  279. LIST_REPOSITORY_MEMBERS, SET_REPOSITORY_MEMBERS, LIST_REPOSITORY_TEAMS, SET_REPOSITORY_TEAMS,
  280. LIST_REPOSITORY_MEMBER_PERMISSIONS, SET_REPOSITORY_MEMBER_PERMISSIONS, LIST_REPOSITORY_TEAM_PERMISSIONS, SET_REPOSITORY_TEAM_PERMISSIONS,
  281. LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS, LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS,
  282. EDIT_SETTINGS, LIST_STATUS;
  283. public static RpcRequest fromName(String name) {
  284. for (RpcRequest type : values()) {
  285. if (type.name().equalsIgnoreCase(name)) {
  286. return type;
  287. }
  288. }
  289. return null;
  290. }
  291. public boolean exceeds(RpcRequest type) {
  292. return this.ordinal() > type.ordinal();
  293. }
  294. @Override
  295. public String toString() {
  296. return name();
  297. }
  298. }
  299. /**
  300. * Enumeration of the search types.
  301. */
  302. public static enum SearchType {
  303. AUTHOR, COMMITTER, COMMIT;
  304. public static SearchType forName(String name) {
  305. for (SearchType type : values()) {
  306. if (type.name().equalsIgnoreCase(name)) {
  307. return type;
  308. }
  309. }
  310. return COMMIT;
  311. }
  312. @Override
  313. public String toString() {
  314. return name().toLowerCase();
  315. }
  316. }
  317. /**
  318. * The types of objects that can be indexed and queried.
  319. */
  320. public static enum SearchObjectType {
  321. commit, blob;
  322. public static SearchObjectType fromName(String name) {
  323. for (SearchObjectType value : values()) {
  324. if (value.name().equals(name)) {
  325. return value;
  326. }
  327. }
  328. return null;
  329. }
  330. }
  331. /**
  332. * The access permissions available for a repository.
  333. */
  334. public static enum AccessPermission {
  335. NONE("N"), EXCLUDE("X"), VIEW("V"), CLONE("R"), PUSH("RW"), CREATE("RWC"), DELETE("RWD"), REWIND("RW+"), OWNER("RW+");
  336. public static final AccessPermission [] NEWPERMISSIONS = { EXCLUDE, VIEW, CLONE, PUSH, CREATE, DELETE, REWIND };
  337. public static final AccessPermission [] SSHPERMISSIONS = { VIEW, CLONE, PUSH };
  338. public static AccessPermission LEGACY = REWIND;
  339. public final String code;
  340. private AccessPermission(String code) {
  341. this.code = code;
  342. }
  343. public boolean atMost(AccessPermission perm) {
  344. return ordinal() <= perm.ordinal();
  345. }
  346. public boolean atLeast(AccessPermission perm) {
  347. return ordinal() >= perm.ordinal();
  348. }
  349. public boolean exceeds(AccessPermission perm) {
  350. return ordinal() > perm.ordinal();
  351. }
  352. public String asRole(String repository) {
  353. return code + ":" + repository;
  354. }
  355. @Override
  356. public String toString() {
  357. return code;
  358. }
  359. public static AccessPermission permissionFromRole(String role) {
  360. String [] fields = role.split(":", 2);
  361. if (fields.length == 1) {
  362. // legacy/undefined assume full permissions
  363. return AccessPermission.LEGACY;
  364. } else {
  365. // code:repository
  366. return AccessPermission.fromCode(fields[0]);
  367. }
  368. }
  369. public static String repositoryFromRole(String role) {
  370. String [] fields = role.split(":", 2);
  371. if (fields.length == 1) {
  372. // legacy/undefined assume full permissions
  373. return role;
  374. } else {
  375. // code:repository
  376. return fields[1];
  377. }
  378. }
  379. public static AccessPermission fromCode(String code) {
  380. for (AccessPermission perm : values()) {
  381. if (perm.code.equalsIgnoreCase(code)) {
  382. return perm;
  383. }
  384. }
  385. return AccessPermission.NONE;
  386. }
  387. }
  388. public static enum RegistrantType {
  389. REPOSITORY, USER, TEAM;
  390. }
  391. public static enum PermissionType {
  392. MISSING, ANONYMOUS, EXPLICIT, TEAM, REGEX, OWNER, ADMINISTRATOR;
  393. }
  394. public static enum GCStatus {
  395. READY, COLLECTING;
  396. public boolean exceeds(GCStatus s) {
  397. return ordinal() > s.ordinal();
  398. }
  399. }
  400. public static enum AuthenticationType {
  401. PUBLIC_KEY, CREDENTIALS, COOKIE, CERTIFICATE, CONTAINER;
  402. public boolean isStandard() {
  403. return ordinal() <= COOKIE.ordinal();
  404. }
  405. }
  406. public static enum AccountType {
  407. LOCAL, EXTERNAL, CONTAINER, LDAP, REDMINE, SALESFORCE, WINDOWS, PAM, HTPASSWD;
  408. public static AccountType fromString(String value) {
  409. for (AccountType type : AccountType.values()) {
  410. if (type.name().equalsIgnoreCase(value)) {
  411. return type;
  412. }
  413. }
  414. return AccountType.LOCAL;
  415. }
  416. public boolean isLocal() {
  417. return this == LOCAL;
  418. }
  419. }
  420. public static enum CommitMessageRenderer {
  421. PLAIN, MARKDOWN;
  422. public static CommitMessageRenderer fromName(String name) {
  423. for (CommitMessageRenderer renderer : values()) {
  424. if (renderer.name().equalsIgnoreCase(name)) {
  425. return renderer;
  426. }
  427. }
  428. return CommitMessageRenderer.PLAIN;
  429. }
  430. }
  431. public static enum Transport {
  432. // ordered for url advertisements, assuming equal access permissions
  433. SSH, HTTPS, HTTP, GIT;
  434. public static Transport fromString(String value) {
  435. for (Transport t : values()) {
  436. if (t.name().equalsIgnoreCase(value)) {
  437. return t;
  438. }
  439. }
  440. return null;
  441. }
  442. public static Transport fromUrl(String url) {
  443. String scheme = url.substring(0, url.indexOf("://"));
  444. return fromString(scheme);
  445. }
  446. }
  447. @Documented
  448. @Retention(RetentionPolicy.RUNTIME)
  449. public @interface Unused {
  450. }
  451. }