]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/ServerConstants.java
add: configuration of CommunityCA
[gigi.git] / src / club / wpia / gigi / util / ServerConstants.java
1 package club.wpia.gigi.util;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Properties;
7
8 import club.wpia.gigi.ocsp.OCSPResponder;
9
10 public class ServerConstants {
11
12     public enum Host {
13         /**
14          * Serves the main application. Both via HTTP and HTTPS.
15          */
16         WWW("www"),
17         /**
18          * Serves static resource like css, js, for modal dialogs on
19          * delete-operations and similar things.
20          */
21         STATIC("static"),
22         /**
23          * Serves the same content as {@link #WWW}, but requires
24          * authentification via client certificate.
25          */
26         SECURE("secure"),
27         /**
28          * Serves the API for issuing certificates, receiving Quiz results.
29          */
30         API("api"),
31         /**
32          * Hosts a link-redirector (not served by Gigi) for external links from
33          * Gigi.
34          */
35         LINK("link"),
36         /**
37          * Hosts the certificate repository for the certificates generated
38          * during NRE. Also not served by Gigi.
39          */
40         CRT_REPO("g2.crt"),
41         /**
42          * Hosts the {@link OCSPResponder}.
43          */
44         OCSP_RESPONDER("g2.ocsp");
45
46         private final String value;
47
48         private Host(String value) {
49             this.value = value;
50         }
51
52         public String getConfigName() {
53             return value;
54         }
55
56         public String getHostDefaultPrefix() {
57             return value;
58         }
59     }
60
61     private static Map<Host, String> hostnames;
62
63     private static String securePort, port, secureBindPort, bindPort;
64
65     private static String suffix = "wpia.local";
66
67     private static String appName = null;
68
69     private static String appIdentifier = null;
70
71     private static String mailSupport;
72
73     private static String mailBoard;
74
75     private static String mailQuiz;
76
77     private static String mailQuizAdmin;
78
79     private static boolean communityCA;
80
81     public static void init(Properties conf) {
82         securePort = port = "";
83         if ( !conf.getProperty("https.port").equals("443")) {
84             securePort = ":" + conf.getProperty("https.port");
85         }
86         if ( !conf.getProperty("http.port").equals("80")) {
87             port = ":" + conf.getProperty("http.port");
88         }
89         secureBindPort = conf.getProperty("https.bindPort", conf.getProperty("https.port"));
90         bindPort = conf.getProperty("http.bindPort", conf.getProperty("http.port"));
91
92         suffix = conf.getProperty("name.suffix", "wpia.local");
93         HashMap<Host, String> hostnames = new HashMap<>();
94         for (Host h : Host.values()) {
95             hostnames.put(h, conf.getProperty("name." + h.getConfigName(), h.getHostDefaultPrefix() + "." + suffix));
96         }
97         ServerConstants.hostnames = Collections.unmodifiableMap(hostnames);
98         appName = conf.getProperty("appName");
99         if (appName == null) {
100             throw new Error("App name missing");
101         }
102         appIdentifier = conf.getProperty("appIdentifier");
103         if (appIdentifier == null) {
104             throw new Error("App identifier missing");
105         }
106         mailSupport = conf.getProperty("mail.support", "support@" + suffix);
107         mailBoard = conf.getProperty("mail.board", "board@" + suffix);
108         mailQuiz = conf.getProperty("mail.quiz", "quiz@" + suffix);
109         mailQuizAdmin = conf.getProperty("mail.quizAdmin", "quiz-admin@" + suffix);
110         communityCA = conf.getProperty("communityCA", "false").equalsIgnoreCase("true");
111     }
112
113     public static String getHostName(Host h) {
114         return hostnames.get(h);
115     }
116
117     public static String getHostNamePortSecure(Host h) {
118         return hostnames.get(h) + securePort;
119     }
120
121     public static String getHostNamePort(Host h) {
122         return hostnames.get(h) + port;
123     }
124
125     public static int getSecurePort() {
126         if (secureBindPort != null && !secureBindPort.isEmpty()) {
127             if (secureBindPort.equals("stdin")) {
128                 return -1;
129             } else {
130                 return Integer.parseInt(secureBindPort);
131             }
132         }
133         if (securePort.isEmpty()) {
134             return 443;
135         }
136         return Integer.parseInt(securePort.substring(1, securePort.length()));
137     }
138
139     public static int getPort() {
140         if (bindPort != null && !bindPort.isEmpty()) {
141             if (bindPort.equals("stdin")) {
142                 return -1;
143             } else {
144                 return Integer.parseInt(bindPort);
145             }
146         }
147         if (port.isEmpty()) {
148             return 80;
149         }
150         return Integer.parseInt(port.substring(1, port.length()));
151     }
152
153     public static String getSuffix() {
154         return suffix;
155     }
156
157     public static String getSupportMailAddress() {
158         return mailSupport;
159     }
160
161     public static String getBoardMailAddress() {
162         return mailBoard;
163     }
164
165     public static String getQuizMailAddress() {
166         return mailQuiz;
167     }
168
169     public static String getQuizAdminMailAddress() {
170         return mailQuizAdmin;
171     }
172
173     public static String getAppName() {
174         if (appName == null) {
175             throw new Error("AppName not initialized.");
176         }
177         return appName;
178     }
179
180     public static String getAppIdentifier() {
181         if (appIdentifier == null) {
182             throw new Error("AppIdentifier not initialized.");
183         }
184         return appIdentifier;
185     }
186
187     public static boolean isCommunityCA() {
188         return communityCA;
189     }
190
191 }