James Moger
2013-03-29 ffe9bd0ea959cf768983ff1a3d2de897390016d7
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;
JM 22 import java.util.jar.Attributes;
23 import java.util.jar.Manifest;
d896e6 24
892570 25 /**
JM 26  * Constant values used by Gitblit.
27  * 
28  * @author James Moger
29  * 
30  */
5fe7df 31 public class Constants {
JM 32
2a7306 33     public static final String NAME = "Gitblit";
5fe7df 34
2a7306 35     public static final String FULL_NAME = "Gitblit - a pure Java Git solution";
155bf7 36
2a7306 37     public static final String ADMIN_ROLE = "#admin";
1e1b85 38     
JM 39     public static final String FORK_ROLE = "#fork";
6662e3 40     
JM 41     public static final String CREATE_ROLE = "#create";
2a7306 42
831469 43     public static final String NOT_FEDERATED_ROLE = "#notfederated";
ce2a40 44     
JM 45     public static final String NO_ROLE = "#none";
831469 46
2a7306 47     public static final String PROPERTIES_FILE = "gitblit.properties";
JM 48
5450d0 49     public static final String GIT_PATH = "/git/";
2a7306 50
5450d0 51     public static final String ZIP_PATH = "/zip/";
85c2e6 52
5450d0 53     public static final String SYNDICATION_PATH = "/feed/";
85c2e6 54
831469 55     public static final String FEDERATION_PATH = "/federation/";
31abc2 56
93f0b1 57     public static final String RPC_PATH = "/rpc/";
6d874a 58     
JM 59     public static final String PAGES= "/pages/";
831469 60
1f9dae 61     public static final String BORDER = "***********************************************************";
db653a 62
831469 63     public static final String FEDERATION_USER = "$gitblit";
JM 64
65     public static final String PROPOSAL_EXT = ".json";
6d874a 66     
JM 67     public static final String ENCODING = "UTF-8";
68     
75705a 69     public static final int LEN_SHORTLOG = 78;
a45caa 70     
JM 71     public static final int LEN_SHORTLOG_REFS = 60;
72     
1aabf0 73     public static final String DEFAULT_BRANCH = "default";
JM 74     
380afa 75     public static final String CONFIG_GITBLIT = "gitblit";
4a5a55 76     
380afa 77     public static final String CONFIG_CUSTOM_FIELDS = "customFields";
4a5a55 78     
e92c6d 79     public static final String ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ";
JM 80     
f8bb95 81     public static final String R_GITBLIT = "refs/gitblit/";
JM 82     
93d506 83     public static final String baseFolder = "baseFolder";
JM 84     
85     public static final String baseFolder$ = "${" + baseFolder + "}";
86     
87     public static final String contextFolder$ = "${contextFolder}";
f6b200 88
JM 89     public static String getVersion() {
90         String v = Constants.class.getPackage().getImplementationVersion();
91         if (v == null) {
92             return "0.0.0-SNAPSHOT";
93         }
94         return v;
95     }
96
831469 97     public static String getGitBlitVersion() {
f6b200 98         return NAME + " v" + getVersion();
JM 99     }
100     
101     public static String getBuildDate() {
102         return getManifestValue("build-date", "PENDING");
103     }
104     
105     private static String getManifestValue(String attrib, String defaultValue) {
106         Class<?> clazz = Constants.class;
107         String className = clazz.getSimpleName() + ".class";
108         String classPath = clazz.getResource(className).toString();
109         if (!classPath.startsWith("jar")) {
110             // Class not from JAR
111             return defaultValue;
112         }
113         try {
114             String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
115             Manifest manifest = new Manifest(new URL(manifestPath).openStream());
116             Attributes attr = manifest.getMainAttributes();
117             String value = attr.getValue(attrib);
118             return value;
119         } catch (Exception e) {
120         }
121         return defaultValue;
831469 122     }
93d506 123     
88598b 124     /**
831469 125      * Enumeration representing the four access restriction levels.
88598b 126      */
dfb889 127     public static enum AccessRestrictionType {
JM 128         NONE, PUSH, CLONE, VIEW;
129
d0d438 130         public static AccessRestrictionType fromName(String name) {
dfb889 131             for (AccessRestrictionType type : values()) {
d0d438 132                 if (type.name().equalsIgnoreCase(name)) {
dfb889 133                     return type;
JM 134                 }
135             }
136             return NONE;
137         }
f98825 138
JM 139         public boolean exceeds(AccessRestrictionType type) {
140             return this.ordinal() > type.ordinal();
141         }
142
dfb889 143         public boolean atLeast(AccessRestrictionType type) {
JM 144             return this.ordinal() >= type.ordinal();
145         }
146
147         public String toString() {
f98825 148             return name();
dfb889 149         }
JM 150     }
6adf56 151     
JM 152     /**
153      * Enumeration representing the types of authorization control for an
154      * access restricted resource.
155      */
156     public static enum AuthorizationControl {
157         AUTHENTICATED, NAMED;
158         
159         public static AuthorizationControl fromName(String name) {
160             for (AuthorizationControl type : values()) {
161                 if (type.name().equalsIgnoreCase(name)) {
162                     return type;
163                 }
164             }
165             return NAMED;
166         }
167         
168         public String toString() {
169             return name();
170         }
171     }
172
dfb889 173
831469 174     /**
JM 175      * Enumeration representing the types of federation tokens.
176      */
177     public static enum FederationToken {
178         ALL, USERS_AND_REPOSITORIES, REPOSITORIES;
179
180         public static FederationToken fromName(String name) {
181             for (FederationToken type : values()) {
182                 if (type.name().equalsIgnoreCase(name)) {
183                     return type;
184                 }
185             }
186             return REPOSITORIES;
187         }
188
189         public String toString() {
190             return name();
191         }
5fe7df 192     }
831469 193
JM 194     /**
195      * Enumeration representing the types of federation requests.
196      */
197     public static enum FederationRequest {
df162c 198         POKE, PROPOSAL, PULL_REPOSITORIES, PULL_USERS, PULL_TEAMS, PULL_SETTINGS, PULL_SCRIPTS, STATUS;
831469 199
JM 200         public static FederationRequest fromName(String name) {
201             for (FederationRequest type : values()) {
202                 if (type.name().equalsIgnoreCase(name)) {
203                     return type;
204                 }
205             }
206             return PULL_REPOSITORIES;
207         }
208
209         public String toString() {
210             return name();
211         }
212     }
213
214     /**
215      * Enumeration representing the statii of federation requests.
216      */
217     public static enum FederationPullStatus {
c729c5 218         PENDING, FAILED, SKIPPED, PULLED, MIRRORED, NOCHANGE, EXCLUDED;
831469 219
JM 220         public static FederationPullStatus fromName(String name) {
221             for (FederationPullStatus type : values()) {
222                 if (type.name().equalsIgnoreCase(name)) {
223                     return type;
224                 }
225             }
226             return PENDING;
227         }
228
229         @Override
230         public String toString() {
231             return name();
232         }
233     }
8f73a7 234
831469 235     /**
JM 236      * Enumeration representing the federation types.
237      */
238     public static enum FederationStrategy {
239         EXCLUDE, FEDERATE_THIS, FEDERATE_ORIGIN;
240
241         public static FederationStrategy fromName(String name) {
242             for (FederationStrategy type : values()) {
243                 if (type.name().equalsIgnoreCase(name)) {
244                     return type;
245                 }
246             }
247             return FEDERATE_THIS;
248         }
249
8f73a7 250         public boolean exceeds(FederationStrategy type) {
JM 251             return this.ordinal() > type.ordinal();
252         }
253
254         public boolean atLeast(FederationStrategy type) {
255             return this.ordinal() >= type.ordinal();
256         }
257
831469 258         @Override
JM 259         public String toString() {
260             return name();
261         }
262     }
263
4aafd4 264     /**
JM 265      * Enumeration representing the possible results of federation proposal
266      * requests.
267      */
268     public static enum FederationProposalResult {
269         ERROR, FEDERATION_DISABLED, MISSING_DATA, NO_PROPOSALS, NO_POKE, ACCEPTED;
270
271         @Override
272         public String toString() {
273             return name();
274         }
275     }
93f0b1 276
JM 277     /**
278      * Enumeration representing the possible remote procedure call requests from
279      * a client.
280      */
281     public static enum RpcRequest {
ec5a88 282         // Order is important here.  anything above LIST_SETTINGS requires
JM 283         // administrator privileges and web.allowRpcManagement.
4ebaf6 284         CLEAR_REPOSITORY_CACHE, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, LIST_SETTINGS,
f08aab 285         CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY, 
JM 286         LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER, 
287         LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,
288         LIST_REPOSITORY_MEMBERS, SET_REPOSITORY_MEMBERS, LIST_REPOSITORY_TEAMS, SET_REPOSITORY_TEAMS, 
822dfe 289         LIST_REPOSITORY_MEMBER_PERMISSIONS, SET_REPOSITORY_MEMBER_PERMISSIONS, LIST_REPOSITORY_TEAM_PERMISSIONS, SET_REPOSITORY_TEAM_PERMISSIONS, 
f08aab 290         LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS, LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS,
4ebaf6 291         EDIT_SETTINGS, LIST_STATUS;
31abc2 292
93f0b1 293         public static RpcRequest fromName(String name) {
JM 294             for (RpcRequest type : values()) {
295                 if (type.name().equalsIgnoreCase(name)) {
296                     return type;
297                 }
298             }
b2fde8 299             return null;
ec5a88 300         }        
31abc2 301
ca9d0f 302         public boolean exceeds(RpcRequest type) {
JM 303             return this.ordinal() > type.ordinal();
304         }
31abc2 305
93f0b1 306         @Override
JM 307         public String toString() {
308             return name();
309         }
310     }
33d8d8 311
JM 312     /**
313      * Enumeration of the search types.
314      */
315     public static enum SearchType {
316         AUTHOR, COMMITTER, COMMIT;
317     
318         public static SearchType forName(String name) {
319             for (SearchType type : values()) {
320                 if (type.name().equalsIgnoreCase(name)) {
321                     return type;
322                 }
323             }
324             return COMMIT;
325         }
326     
327         @Override
328         public String toString() {
329             return name().toLowerCase();
330         }
331     }
d896e6 332     
JM 333     /**
334      * The types of objects that can be indexed and queried.
335      */
336     public static enum SearchObjectType {
337         commit, blob, issue;
338
339         static SearchObjectType fromName(String name) {
340             for (SearchObjectType value : values()) {
341                 if (value.name().equals(name)) {
342                     return value;
343                 }
344             }
345             return null;
346         }
347     }
20714a 348     
JM 349     /**
350      * The access permissions available for a repository. 
351      */
352     public static enum AccessPermission {
7ba85b 353         NONE("N"), EXCLUDE("X"), VIEW("V"), CLONE("R"), PUSH("RW"), CREATE("RWC"), DELETE("RWD"), REWIND("RW+"), OWNER("RW+");
20714a 354         
2d48e2 355         public static final AccessPermission [] NEWPERMISSIONS = { EXCLUDE, VIEW, CLONE, PUSH, CREATE, DELETE, REWIND };
b0e164 356         
20714a 357         public static AccessPermission LEGACY = REWIND;
JM 358         
359         public final String code;
360         
361         private AccessPermission(String code) {
362             this.code = code;
363         }
364         
365         public boolean atLeast(AccessPermission perm) {
366             return ordinal() >= perm.ordinal();
367         }
368
369         public boolean exceeds(AccessPermission perm) {
370             return ordinal() > perm.ordinal();
371         }
372         
373         public String asRole(String repository) {
374             return code + ":" + repository;
375         }
376         
377         @Override
378         public String toString() {
379             return code;
380         }
381         
382         public static AccessPermission permissionFromRole(String role) {
383             String [] fields = role.split(":", 2);
384             if (fields.length == 1) {
385                 // legacy/undefined assume full permissions
386                 return AccessPermission.LEGACY;
387             } else {
388                 // code:repository
389                 return AccessPermission.fromCode(fields[0]);
390             }
391         }
392         
393         public static String repositoryFromRole(String role) {
394             String [] fields = role.split(":", 2);
395             if (fields.length == 1) {
396                 // legacy/undefined assume full permissions
397                 return role;
398             } else {
399                 // code:repository
400                 return fields[1];
401             }
402         }
403         
404         public static AccessPermission fromCode(String code) {
405             for (AccessPermission perm : values()) {
406                 if (perm.code.equalsIgnoreCase(code)) {
407                     return perm;
408                 }
409             }
410             return AccessPermission.NONE;
411         }
412     }
413     
822dfe 414     public static enum RegistrantType {
JM 415         REPOSITORY, USER, TEAM;
416     }
417     
092f0a 418     public static enum PermissionType {
7ba85b 419         MISSING, EXPLICIT, TEAM, REGEX, OWNER, ADMINISTRATOR;
092f0a 420     }
JM 421     
e92c6d 422     public static enum GCStatus {
JM 423         READY, COLLECTING;
424         
425         public boolean exceeds(GCStatus s) {
426             return ordinal() > s.ordinal();
427         }
428     }
8fef1f 429
JM 430     public static enum AuthenticationType {
37fa66 431         CREDENTIALS, COOKIE, CERTIFICATE, CONTAINER;
8fef1f 432         
JM 433         public boolean isStandard() {
434             return ordinal() <= COOKIE.ordinal();
435         }
436     }
4e3c15 437     
JM 438     public static enum AccountType {
439         LOCAL, LDAP, REDMINE;
440         
441         public boolean isLocal() {
442             return this == LOCAL;
443         }
444     }
8fef1f 445
20714a 446     @Documented
JM 447     @Retention(RetentionPolicy.RUNTIME)
448     public @interface Unused {
449     }
5fe7df 450 }