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