]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/ServerConstants.java
fix: Somewhat sensibly split the wishlist document
[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     public static void init(Properties conf) {
62         securePort = port = "";
63         if ( !conf.getProperty("https.port").equals("443")) {
64             securePort = ":" + conf.getProperty("https.port");
65         }
66         if ( !conf.getProperty("http.port").equals("80")) {
67             port = ":" + conf.getProperty("http.port");
68         }
69         secureBindPort = conf.getProperty("https.bindPort", conf.getProperty("https.port"));
70         bindPort = conf.getProperty("http.bindPort", conf.getProperty("http.port"));
71
72         suffix = conf.getProperty("name.suffix", conf.getProperty("name.www", "www.wpia.local").substring(4));
73         HashMap<Host, String> hostnames = new HashMap<>();
74         for (Host h : Host.values()) {
75             hostnames.put(h, conf.getProperty("name." + h.getConfigName(), h.getHostDefaultPrefix() + "." + suffix));
76         }
77         ServerConstants.hostnames = Collections.unmodifiableMap(hostnames);
78     }
79
80     public static String getHostName(Host h) {
81         return hostnames.get(h);
82     }
83
84     public static String getHostNamePortSecure(Host h) {
85         return hostnames.get(h) + securePort;
86     }
87
88     public static String getHostNamePort(Host h) {
89         return hostnames.get(h) + port;
90     }
91
92     public static int getSecurePort() {
93         if (secureBindPort != null && !secureBindPort.isEmpty()) {
94             if (secureBindPort.equals("stdin")) {
95                 return -1;
96             } else {
97                 return Integer.parseInt(secureBindPort);
98             }
99         }
100         if (securePort.isEmpty()) {
101             return 443;
102         }
103         return Integer.parseInt(securePort.substring(1, securePort.length()));
104     }
105
106     public static int getPort() {
107         if (bindPort != null && !bindPort.isEmpty()) {
108             if (bindPort.equals("stdin")) {
109                 return -1;
110             } else {
111                 return Integer.parseInt(bindPort);
112             }
113         }
114         if (port.isEmpty()) {
115             return 80;
116         }
117         return Integer.parseInt(port.substring(1, port.length()));
118     }
119
120     public static String getSuffix() {
121         return suffix;
122     }
123
124     public static String getSupportMailAddress() {
125         return "support@" + getSuffix();
126     }
127
128     public static String getBoardMailAddress() {
129         return "board@" + getSuffix();
130     }
131
132     public static String getQuizMailAddress() {
133         return "quiz@" + getSuffix();
134     }
135
136     public static String getQuizAdminMailAddress() {
137         return "quiz-admin@" + getSuffix();
138     }
139
140 }