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