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