James Moger
2014-07-03 efdb2b3d0c6f03a9aac9e65892cbc8ff755f246f
commit | author | age
f13c4c 1 /*
JM 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  */
5fe7df 16 package com.gitblit;
JM 17
20714a 18 import java.lang.annotation.Documented;
JM 19 import java.lang.annotation.Retention;
20 import java.lang.annotation.RetentionPolicy;
f6b200 21 import java.net.URL;
629806 22 import java.util.Arrays;
JM 23 import java.util.List;
f6b200 24 import java.util.jar.Attributes;
JM 25 import java.util.jar.Manifest;
d896e6 26
892570 27 /**
JM 28  * Constant values used by Gitblit.
699e71 29  *
892570 30  * @author James Moger
699e71 31  *
892570 32  */
5fe7df 33 public class Constants {
JM 34
2a7306 35     public static final String NAME = "Gitblit";
5fe7df 36
2a7306 37     public static final String FULL_NAME = "Gitblit - a pure Java Git solution";
155bf7 38
2a7306 39     public static final String ADMIN_ROLE = "#admin";
699e71 40
1e1b85 41     public static final String FORK_ROLE = "#fork";
699e71 42
6662e3 43     public static final String CREATE_ROLE = "#create";
2a7306 44
831469 45     public static final String NOT_FEDERATED_ROLE = "#notfederated";
699e71 46
ce2a40 47     public static final String NO_ROLE = "#none";
699e71 48
de3474 49     public static final String EXTERNAL_ACCOUNT = "#externalAccount";
831469 50
2a7306 51     public static final String PROPERTIES_FILE = "gitblit.properties";
699e71 52
bb6b14 53     public static final String DEFAULT_USER_REPOSITORY_PREFIX = "~";
2a7306 54
c5069a 55     public static final String R_PATH = "/r/";
JM 56
5450d0 57     public static final String GIT_PATH = "/git/";
2a7306 58
5450d0 59     public static final String ZIP_PATH = "/zip/";
85c2e6 60
5450d0 61     public static final String SYNDICATION_PATH = "/feed/";
85c2e6 62
831469 63     public static final String FEDERATION_PATH = "/federation/";
31abc2 64
93f0b1 65     public static final String RPC_PATH = "/rpc/";
699e71 66
b7502e 67     public static final String PAGES = "/pages/";
699e71 68
b7502e 69     public static final String SPARKLESHARE_INVITE_PATH = "/sparkleshare/";
699e71 70
ff17f7 71     public static final String RAW_PATH = "/raw/";
ca31f5 72
f084f4 73     public static final String BRANCH_GRAPH_PATH = "/graph/";
831469 74
088b6f 75     public static final String BORDER = "*****************************************************************";
JM 76
77     public static final String BORDER2 = "#################################################################";
db653a 78
831469 79     public static final String FEDERATION_USER = "$gitblit";
JM 80
81     public static final String PROPOSAL_EXT = ".json";
699e71 82
6d874a 83     public static final String ENCODING = "UTF-8";
699e71 84
75705a 85     public static final int LEN_SHORTLOG = 78;
699e71 86
a45caa 87     public static final int LEN_SHORTLOG_REFS = 60;
699e71 88
1aabf0 89     public static final String DEFAULT_BRANCH = "default";
699e71 90
380afa 91     public static final String CONFIG_GITBLIT = "gitblit";
699e71 92
380afa 93     public static final String CONFIG_CUSTOM_FIELDS = "customFields";
699e71 94
e92c6d 95     public static final String ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ";
699e71 96
93d506 97     public static final String baseFolder = "baseFolder";
699e71 98
93d506 99     public static final String baseFolder$ = "${" + baseFolder + "}";
699e71 100
93d506 101     public static final String contextFolder$ = "${contextFolder}";
699e71 102
9b26b7 103     public static final String HEAD = "HEAD";
JM 104
c134a0 105     public static final String R_META = "refs/meta/";
699e71 106
9b26b7 107     public static final String R_HEADS = "refs/heads/";
699e71 108
9b26b7 109     public static final String R_NOTES = "refs/notes/";
699e71 110
9b26b7 111     public static final String R_CHANGES = "refs/changes/";
699e71 112
5e3521 113     public static final String R_PULL = "refs/pull/";
9b26b7 114
JM 115     public static final String R_TAGS = "refs/tags/";
699e71 116
9b26b7 117     public static final String R_REMOTES = "refs/remotes/";
f6b200 118
5e3521 119     public static final String R_FOR = "refs/for/";
JM 120
121     public static final String R_TICKET = "refs/heads/ticket/";
122
123     public static final String R_TICKETS_PATCHSETS = "refs/tickets/";
124
0047fb 125     public static final String R_MASTER = "refs/heads/master";
JM 126
127     public static final String MASTER = "master";
128
129     public static final String R_DEVELOP = "refs/heads/develop";
130
131     public static final String DEVELOP = "develop";
132
efdb2b 133     public static final String AUTHENTICATION_TYPE = "authentication-type";
JM 134
f6b200 135     public static String getVersion() {
JM 136         String v = Constants.class.getPackage().getImplementationVersion();
137         if (v == null) {
138             return "0.0.0-SNAPSHOT";
139         }
140         return v;
141     }
142
831469 143     public static String getGitBlitVersion() {
f6b200 144         return NAME + " v" + getVersion();
JM 145     }
699e71 146
f6b200 147     public static String getBuildDate() {
JM 148         return getManifestValue("build-date", "PENDING");
149     }
699e71 150
f6b200 151     private static String getManifestValue(String attrib, String defaultValue) {
JM 152         Class<?> clazz = Constants.class;
153         String className = clazz.getSimpleName() + ".class";
154         String classPath = clazz.getResource(className).toString();
155         if (!classPath.startsWith("jar")) {
156             // Class not from JAR
157             return defaultValue;
158         }
159         try {
160             String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
161             Manifest manifest = new Manifest(new URL(manifestPath).openStream());
162             Attributes attr = manifest.getMainAttributes();
163             String value = attr.getValue(attrib);
164             return value;
165         } catch (Exception e) {
166         }
167         return defaultValue;
831469 168     }
699e71 169
88598b 170     /**
831469 171      * Enumeration representing the four access restriction levels.
88598b 172      */
dfb889 173     public static enum AccessRestrictionType {
JM 174         NONE, PUSH, CLONE, VIEW;
699e71 175
629806 176         private static final AccessRestrictionType [] AUTH_TYPES = { PUSH, CLONE, VIEW };
dfb889 177
d0d438 178         public static AccessRestrictionType fromName(String name) {
dfb889 179             for (AccessRestrictionType type : values()) {
d0d438 180                 if (type.name().equalsIgnoreCase(name)) {
dfb889 181                     return type;
JM 182                 }
183             }
184             return NONE;
185         }
699e71 186
629806 187         public static List<AccessRestrictionType> choices(boolean allowAnonymousPush) {
JM 188             if (allowAnonymousPush) {
189                 return Arrays.asList(values());
190             }
191             return Arrays.asList(AUTH_TYPES);
192         }
f98825 193
JM 194         public boolean exceeds(AccessRestrictionType type) {
195             return this.ordinal() > type.ordinal();
196         }
197
dfb889 198         public boolean atLeast(AccessRestrictionType type) {
JM 199             return this.ordinal() >= type.ordinal();
200         }
201
699e71 202         @Override
dfb889 203         public String toString() {
f98825 204             return name();
dfb889 205         }
699e71 206
9a6a42 207         public boolean isValidPermission(AccessPermission permission) {
JM 208             switch (this) {
209             case VIEW:
210                 // VIEW restriction
211                 // all access permissions are valid
212                 return true;
213             case CLONE:
214                 // CLONE restriction
215                 // only CLONE or greater access permissions are valid
216                 return permission.atLeast(AccessPermission.CLONE);
217             case PUSH:
218                 // PUSH restriction
699e71 219                 // only PUSH or greater access permissions are valid
9a6a42 220                 return permission.atLeast(AccessPermission.PUSH);
JM 221             case NONE:
222                 // NO access restriction
223                 // all access permissions are invalid
224                 return false;
225             }
226             return false;
227         }
dfb889 228     }
699e71 229
6adf56 230     /**
JM 231      * Enumeration representing the types of authorization control for an
232      * access restricted resource.
233      */
234     public static enum AuthorizationControl {
235         AUTHENTICATED, NAMED;
699e71 236
6adf56 237         public static AuthorizationControl fromName(String name) {
JM 238             for (AuthorizationControl type : values()) {
239                 if (type.name().equalsIgnoreCase(name)) {
240                     return type;
241                 }
242             }
243             return NAMED;
244         }
699e71 245
JM 246         @Override
6adf56 247         public String toString() {
JM 248             return name();
249         }
250     }
251
dfb889 252
831469 253     /**
JM 254      * Enumeration representing the types of federation tokens.
255      */
256     public static enum FederationToken {
257         ALL, USERS_AND_REPOSITORIES, REPOSITORIES;
258
259         public static FederationToken fromName(String name) {
260             for (FederationToken type : values()) {
261                 if (type.name().equalsIgnoreCase(name)) {
262                     return type;
263                 }
264             }
265             return REPOSITORIES;
266         }
267
699e71 268         @Override
831469 269         public String toString() {
JM 270             return name();
271         }
5fe7df 272     }
831469 273
JM 274     /**
275      * Enumeration representing the types of federation requests.
276      */
277     public static enum FederationRequest {
df162c 278         POKE, PROPOSAL, PULL_REPOSITORIES, PULL_USERS, PULL_TEAMS, PULL_SETTINGS, PULL_SCRIPTS, STATUS;
831469 279
JM 280         public static FederationRequest fromName(String name) {
281             for (FederationRequest type : values()) {
282                 if (type.name().equalsIgnoreCase(name)) {
283                     return type;
284                 }
285             }
286             return PULL_REPOSITORIES;
287         }
288
699e71 289         @Override
831469 290         public String toString() {
JM 291             return name();
292         }
293     }
294
295     /**
296      * Enumeration representing the statii of federation requests.
297      */
298     public static enum FederationPullStatus {
c729c5 299         PENDING, FAILED, SKIPPED, PULLED, MIRRORED, NOCHANGE, EXCLUDED;
831469 300
JM 301         public static FederationPullStatus fromName(String name) {
302             for (FederationPullStatus type : values()) {
303                 if (type.name().equalsIgnoreCase(name)) {
304                     return type;
305                 }
306             }
307             return PENDING;
308         }
309
310         @Override
311         public String toString() {
312             return name();
313         }
314     }
8f73a7 315
831469 316     /**
JM 317      * Enumeration representing the federation types.
318      */
319     public static enum FederationStrategy {
320         EXCLUDE, FEDERATE_THIS, FEDERATE_ORIGIN;
321
322         public static FederationStrategy fromName(String name) {
323             for (FederationStrategy type : values()) {
324                 if (type.name().equalsIgnoreCase(name)) {
325                     return type;
326                 }
327             }
328             return FEDERATE_THIS;
329         }
330
8f73a7 331         public boolean exceeds(FederationStrategy type) {
JM 332             return this.ordinal() > type.ordinal();
333         }
334
335         public boolean atLeast(FederationStrategy type) {
336             return this.ordinal() >= type.ordinal();
337         }
338
831469 339         @Override
JM 340         public String toString() {
341             return name();
342         }
343     }
344
4aafd4 345     /**
JM 346      * Enumeration representing the possible results of federation proposal
347      * requests.
348      */
349     public static enum FederationProposalResult {
350         ERROR, FEDERATION_DISABLED, MISSING_DATA, NO_PROPOSALS, NO_POKE, ACCEPTED;
351
352         @Override
353         public String toString() {
354             return name();
355         }
356     }
93f0b1 357
JM 358     /**
359      * Enumeration representing the possible remote procedure call requests from
360      * a client.
361      */
362     public static enum RpcRequest {
813a1f 363         // Order is important here.  anything after LIST_SETTINGS requires
ec5a88 364         // administrator privileges and web.allowRpcManagement.
813a1f 365         CLEAR_REPOSITORY_CACHE, REINDEX_TICKETS, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER,
JM 366         FORK_REPOSITORY, LIST_SETTINGS,
367         CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,
699e71 368         LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER,
f08aab 369         LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,
699e71 370         LIST_REPOSITORY_MEMBERS, SET_REPOSITORY_MEMBERS, LIST_REPOSITORY_TEAMS, SET_REPOSITORY_TEAMS,
JM 371         LIST_REPOSITORY_MEMBER_PERMISSIONS, SET_REPOSITORY_MEMBER_PERMISSIONS, LIST_REPOSITORY_TEAM_PERMISSIONS, SET_REPOSITORY_TEAM_PERMISSIONS,
f08aab 372         LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS, LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS,
4ebaf6 373         EDIT_SETTINGS, LIST_STATUS;
31abc2 374
93f0b1 375         public static RpcRequest fromName(String name) {
JM 376             for (RpcRequest type : values()) {
377                 if (type.name().equalsIgnoreCase(name)) {
378                     return type;
379                 }
380             }
b2fde8 381             return null;
699e71 382         }
31abc2 383
ca9d0f 384         public boolean exceeds(RpcRequest type) {
JM 385             return this.ordinal() > type.ordinal();
386         }
31abc2 387
93f0b1 388         @Override
JM 389         public String toString() {
390             return name();
391         }
392     }
33d8d8 393
JM 394     /**
395      * Enumeration of the search types.
396      */
397     public static enum SearchType {
398         AUTHOR, COMMITTER, COMMIT;
699e71 399
33d8d8 400         public static SearchType forName(String name) {
JM 401             for (SearchType type : values()) {
402                 if (type.name().equalsIgnoreCase(name)) {
403                     return type;
404                 }
405             }
406             return COMMIT;
407         }
699e71 408
33d8d8 409         @Override
JM 410         public String toString() {
411             return name().toLowerCase();
412         }
413     }
699e71 414
d896e6 415     /**
f76fee 416      * Enumeration of the feed content object types.
GS 417      */
9ff0c1 418     public static enum FeedObjectType {
f76fee 419         COMMIT, TAG;
GS 420
9ff0c1 421         public static FeedObjectType forName(String name) {
JM 422             for (FeedObjectType type : values()) {
f76fee 423                 if (type.name().equalsIgnoreCase(name)) {
GS 424                     return type;
425                 }
426             }
427             return COMMIT;
428         }
429
430         @Override
431         public String toString() {
432             return name().toLowerCase();
433         }
434     }
435
436     /**
d896e6 437      * The types of objects that can be indexed and queried.
JM 438      */
439     public static enum SearchObjectType {
ed9717 440         commit, blob;
d896e6 441
7bf6e1 442         public static SearchObjectType fromName(String name) {
d896e6 443             for (SearchObjectType value : values()) {
JM 444                 if (value.name().equals(name)) {
445                     return value;
446                 }
447             }
448             return null;
449         }
450     }
699e71 451
20714a 452     /**
699e71 453      * The access permissions available for a repository.
20714a 454      */
JM 455     public static enum AccessPermission {
7ba85b 456         NONE("N"), EXCLUDE("X"), VIEW("V"), CLONE("R"), PUSH("RW"), CREATE("RWC"), DELETE("RWD"), REWIND("RW+"), OWNER("RW+");
699e71 457
2d48e2 458         public static final AccessPermission [] NEWPERMISSIONS = { EXCLUDE, VIEW, CLONE, PUSH, CREATE, DELETE, REWIND };
699e71 459
c78b25 460         public static final AccessPermission [] SSHPERMISSIONS = { VIEW, CLONE, PUSH };
JM 461
20714a 462         public static AccessPermission LEGACY = REWIND;
699e71 463
20714a 464         public final String code;
699e71 465
20714a 466         private AccessPermission(String code) {
JM 467             this.code = code;
468         }
9715e1 469
JM 470         public boolean atMost(AccessPermission perm) {
471             return ordinal() <= perm.ordinal();
472         }
473
20714a 474         public boolean atLeast(AccessPermission perm) {
JM 475             return ordinal() >= perm.ordinal();
476         }
477
478         public boolean exceeds(AccessPermission perm) {
479             return ordinal() > perm.ordinal();
480         }
699e71 481
20714a 482         public String asRole(String repository) {
JM 483             return code + ":" + repository;
484         }
699e71 485
20714a 486         @Override
JM 487         public String toString() {
488             return code;
489         }
699e71 490
20714a 491         public static AccessPermission permissionFromRole(String role) {
JM 492             String [] fields = role.split(":", 2);
493             if (fields.length == 1) {
494                 // legacy/undefined assume full permissions
495                 return AccessPermission.LEGACY;
496             } else {
497                 // code:repository
498                 return AccessPermission.fromCode(fields[0]);
499             }
500         }
699e71 501
20714a 502         public static String repositoryFromRole(String role) {
JM 503             String [] fields = role.split(":", 2);
504             if (fields.length == 1) {
505                 // legacy/undefined assume full permissions
506                 return role;
507             } else {
508                 // code:repository
509                 return fields[1];
510             }
511         }
699e71 512
20714a 513         public static AccessPermission fromCode(String code) {
JM 514             for (AccessPermission perm : values()) {
515                 if (perm.code.equalsIgnoreCase(code)) {
516                     return perm;
517                 }
518             }
519             return AccessPermission.NONE;
520         }
521     }
699e71 522
822dfe 523     public static enum RegistrantType {
JM 524         REPOSITORY, USER, TEAM;
525     }
699e71 526
092f0a 527     public static enum PermissionType {
9a6a42 528         MISSING, ANONYMOUS, EXPLICIT, TEAM, REGEX, OWNER, ADMINISTRATOR;
092f0a 529     }
699e71 530
e92c6d 531     public static enum GCStatus {
JM 532         READY, COLLECTING;
699e71 533
e92c6d 534         public boolean exceeds(GCStatus s) {
JM 535             return ordinal() > s.ordinal();
536         }
537     }
8fef1f 538
JM 539     public static enum AuthenticationType {
44e2ee 540         PUBLIC_KEY, CREDENTIALS, COOKIE, CERTIFICATE, CONTAINER;
699e71 541
8fef1f 542         public boolean isStandard() {
JM 543             return ordinal() <= COOKIE.ordinal();
544         }
545     }
699e71 546
4e3c15 547     public static enum AccountType {
04a985 548         LOCAL, EXTERNAL, CONTAINER, LDAP, REDMINE, SALESFORCE, WINDOWS, PAM, HTPASSWD;
JM 549
550         public static AccountType fromString(String value) {
551             for (AccountType type : AccountType.values()) {
552                 if (type.name().equalsIgnoreCase(value)) {
553                     return type;
554                 }
555             }
556             return AccountType.LOCAL;
557         }
699e71 558
4e3c15 559         public boolean isLocal() {
JM 560             return this == LOCAL;
561         }
562     }
699e71 563
cd9461 564     public static enum CommitMessageRenderer {
BKI 565         PLAIN, MARKDOWN;
699e71 566
cd9461 567         public static CommitMessageRenderer fromName(String name) {
BKI 568             for (CommitMessageRenderer renderer : values()) {
569                 if (renderer.name().equalsIgnoreCase(name)) {
570                     return renderer;
571                 }
572             }
573             return CommitMessageRenderer.PLAIN;
574         }
575     }
8fef1f 576
e9872c 577     public static enum Transport {
JM 578         // ordered for url advertisements, assuming equal access permissions
579         SSH, HTTPS, HTTP, GIT;
580
581         public static Transport fromString(String value) {
582             for (Transport t : values()) {
583                 if (t.name().equalsIgnoreCase(value)) {
584                     return t;
585                 }
586             }
587             return null;
588         }
589
590         public static Transport fromUrl(String url) {
b016de 591             int delim = url.indexOf("://");
JM 592             if (delim == -1) {
593                 // if no protocol is specified, SSH is assumed by git clients
594                 return SSH;
595             }
596             String scheme = url.substring(0, delim);
e9872c 597             return fromString(scheme);
JM 598         }
599     }
600
20714a 601     @Documented
JM 602     @Retention(RetentionPolicy.RUNTIME)
603     public @interface Unused {
604     }
5fe7df 605 }