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