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