]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/ServerConstants.java
fix: create config variables for email addresses
[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     public static void init(Properties conf) {
80         securePort = port = "";
81         if ( !conf.getProperty("https.port").equals("443")) {
82             securePort = ":" + conf.getProperty("https.port");
83         }
84         if ( !conf.getProperty("http.port").equals("80")) {
85             port = ":" + conf.getProperty("http.port");
86         }
87         secureBindPort = conf.getProperty("https.bindPort", conf.getProperty("https.port"));
88         bindPort = conf.getProperty("http.bindPort", conf.getProperty("http.port"));
89
90         suffix = conf.getProperty("name.suffix", "wpia.local");
91         HashMap<Host, String> hostnames = new HashMap<>();
92         for (Host h : Host.values()) {
93             hostnames.put(h, conf.getProperty("name." + h.getConfigName(), h.getHostDefaultPrefix() + "." + suffix));
94         }
95         ServerConstants.hostnames = Collections.unmodifiableMap(hostnames);
96         appName = conf.getProperty("appName");
97         if (appName == null) {
98             throw new Error("App name missing");
99         }
100         appIdentifier = conf.getProperty("appIdentifier");
101         if (appIdentifier == null) {
102             throw new Error("App identifier missing");
103         }
104         mailSupport = conf.getProperty("mail.support", "support@" + suffix);
105         mailBoard = conf.getProperty("mail.board", "board@" + suffix);
106         mailQuiz = conf.getProperty("mail.quiz", "quiz@" + suffix);
107         mailQuizAdmin = conf.getProperty("mail.quizAdmin", "quiz-admin@" + suffix);
108     }
109
110     public static String getHostName(Host h) {
111         return hostnames.get(h);
112     }
113
114     public static String getHostNamePortSecure(Host h) {
115         return hostnames.get(h) + securePort;
116     }
117
118     public static String getHostNamePort(Host h) {
119         return hostnames.get(h) + port;
120     }
121
122     public static int getSecurePort() {
123         if (secureBindPort != null && !secureBindPort.isEmpty()) {
124             if (secureBindPort.equals("stdin")) {
125                 return -1;
126             } else {
127                 return Integer.parseInt(secureBindPort);
128             }
129         }
130         if (securePort.isEmpty()) {
131             return 443;
132         }
133         return Integer.parseInt(securePort.substring(1, securePort.length()));
134     }
135
136     public static int getPort() {
137         if (bindPort != null && !bindPort.isEmpty()) {
138             if (bindPort.equals("stdin")) {
139                 return -1;
140             } else {
141                 return Integer.parseInt(bindPort);
142             }
143         }
144         if (port.isEmpty()) {
145             return 80;
146         }
147         return Integer.parseInt(port.substring(1, port.length()));
148     }
149
150     public static String getSuffix() {
151         return suffix;
152     }
153
154     public static String getSupportMailAddress() {
155         return mailSupport;
156     }
157
158     public static String getBoardMailAddress() {
159         return mailBoard;
160     }
161
162     public static String getQuizMailAddress() {
163         return mailQuiz;
164     }
165
166     public static String getQuizAdminMailAddress() {
167         return mailQuizAdmin;
168     }
169
170     public static String getAppName() {
171         if (appName == null) {
172             throw new Error("AppName not initialized.");
173         }
174         return appName;
175     }
176
177     public static String getAppIdentifier() {
178         if (appIdentifier == null) {
179             throw new Error("AppIdentifier not initialized.");
180         }
181         return appIdentifier;
182     }
183
184 }