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