source: main/trunk/greenstone3/src/java/org/greenstone/util/ProtocolPortProperties.java@ 32358

Last change on this file since 32358 was 32358, checked in by ak19, 6 years ago

Forgot to commit with previous rev 32357. Newly added class. Commit message same as before: GS3 Java code has moved away from using tomcat.port to using tomcat.port.<protocol>.

File size: 8.3 KB
Line 
1/*
2 * ProtocolPortProperties.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.util;
20
21import java.util.Properties;
22
23/*
24 * Utility class to set the protocol and port values from the Properties provided,
25 * which can be a Properties object loaded from either build.properties or global.properties
26 * Currently used by ServletRealmCheck and FedoraServiceProxy.
27 * (Could also be used by Server3.java and GlobalProperties in future, if applicable.)
28 * Can adjust fallback behaviour in this one location to make them all work consistently.
29 */
30public class ProtocolPortProperties {
31
32 public final boolean legacyMode; // true if before https protocol supported
33
34 public static final int ALL_CORRECT = 0;
35 public static final int SECONDARY_PORT_NON_NUMERIC = 1;
36 public static final int INVALID_PRIMARY_PORT = 2;
37 public static final int OLD_TOMCATPORT_BUT_NO_VALUE = 3;
38 public static final int NO_PROTOCOL_OR_PORT = 4;
39 public static final int INVALID_PROTOCOL = 5;
40 public static final int HTTPS_INCONSISTENCY = 6;
41
42 private static final String defProtocol = "http";
43 private static final String defHttpPort = "8383";
44 private static final String defHttpsPort = "8443";
45
46 private String protocol; //= defProtocol;
47 private String port; //defHttpPort; //defProtocol.equals("https") ? defHttpsPort : defHttpPort; // port that matches protocol as String
48 private String httpPort; //defHttpPort;
49 private String httpsPort; //defHttpsPort;
50 //private int portNum = -1; // port that matches protocol as int
51
52 private String errorMsg;
53 private int errorCode;
54
55 public String getProtocol() { return protocol; }
56 public String getPort() { return port; }
57 public String getHttpPort() { return httpPort; }
58 public String getHttpsPort() { return httpsPort; }
59 public int getPortNum() { return Integer.parseInt(port); }
60
61 public String getErrorMsg() { return errorMsg; }
62 public int getErrorCode() { return errorCode; }
63
64 public boolean hadError() { return errorCode != ALL_CORRECT; }
65
66 // Won't attempt to recover on error.
67 // This means on error, port etc values will be invalid
68 public ProtocolPortProperties(Properties props) {
69 this(props, false);
70 }
71
72 public ProtocolPortProperties(Properties props, boolean recover)
73 {
74 StringBuffer msg = new StringBuffer();
75
76 port = props.getProperty("tomcat.port");
77
78 // We could either be dealing with properties files from before https support was introduced, in which case we want to be backwards compatible
79 // Or we're dealing with properties files after https support was introduced.
80 // To determine which, can ignore server.protocol: server.protocol was introduced at a time when tomcat.port was still in effect,
81 // server.protocol's presence does not indicate whether our GS3 installation supports https or not. Only the tomcat.port property
82 // indicates that: if tomcat.port exists it's an older GS3 that has no https support, so default to http. If there is no tomcat.port,
83 // but there is a server.protocol check for the newer tomcat.port.<protocol> properties.
84
85 if(port != null) { // tomcat.port exists, so this is a GS3 before https support.
86 legacyMode = true;
87
88 // Back when tomcat.port was used, server.protocol if specified
89 // would always be treated as http regardless of what it was set to
90 protocol = defProtocol;
91
92 if(port.equals("")) {
93 errorCode = OLD_TOMCATPORT_BUT_NO_VALUE;
94 msg.append("tomcat.port enabled but did not have a value.");
95 if(recover) {
96 port = httpPort = defHttpPort;
97 }
98 } else { // No issus: using tomcat.port is the pre-https way.
99 errorCode = ALL_CORRECT;
100 httpPort = port;
101 }
102 }
103 else {
104 legacyMode = false;
105
106 protocol = props.getProperty("server.protocol");
107 if(protocol == null || (!protocol.equals("http") && !protocol.equals("https"))) {
108 // tomcat.port was null AND now server.protocol is null or wrong. Something very wrong with the properties file
109 if(protocol == null) {
110 errorCode = NO_PROTOCOL_OR_PORT;
111 msg.append("server.protocol not set. And can't determine port.");
112 } else {
113 errorCode = INVALID_PROTOCOL;
114 msg.append("server.protocol property must be http or https, but is set to invalid value. And can't determine port.");
115 }
116
117 if(recover) {
118 protocol = defProtocol;
119 port = httpPort = defHttpPort;
120 }
121
122 } else { // server.protocol was explicitly set and set to an acceptable value, try to find matching tomcat.port
123
124 port = props.getProperty("tomcat.port."+protocol); // tomcat.port.http or tomcat.port.https, depending on server.protocol.
125
126 // Handle cases where there's some inconsistency in the properties file between protocol and port.
127 // i.e. if server.protocol=http, then tomcat.port.http must be set too.
128 if(port == null || port.equals("")) {
129 errorCode = INVALID_PROTOCOL;
130 msg.append("server.protocol="+protocol+", but matching tomcat.port."+protocol+"not enabled or set.");
131
132 if(recover) {
133 protocol = defProtocol;
134 if(protocol.startsWith("https")) { // server.protocol=https yet tomcat.port.https not provided
135 port = httpsPort = defHttpsPort; // use default https port to set
136 } else {
137 port = httpPort = defHttpPort;
138 }
139 }
140
141 } else { //tomcat.port.<protocol> property matching server.protocol is set, not checking if it's an int and a valid port, though
142 errorCode = ALL_CORRECT;
143 // port is set
144
145 if(protocol.startsWith("https")) {
146 httpsPort = port;
147 httpPort = props.getProperty("tomcat.port.http"); // httpPort will remain null if tomcat.port.http not set
148 } else { // protocol=http
149 httpPort = port;
150 httpsPort = props.getProperty("tomcat.port.https"); // httpsPort will remain null if tomcat.port.https not set
151 }
152 }
153 }
154 }
155
156 if(errorCode == ALL_CORRECT) { // then check any assigned ports are valid
157 if(httpPort != null) {
158 try {
159 Integer.parseInt(httpPort);
160 } catch(NumberFormatException nfe) {
161 msg.append("\nInvalid port specified for over http: not numeric.");
162 if(port == httpPort) {
163 errorCode = INVALID_PRIMARY_PORT;
164 if(recover) {
165 port = httpPort = defHttpPort;
166 } else { // no recovery requested, so if port for protocol is non-numeric, consider it a 'fatal' error
167 port = httpPort = httpsPort = null;
168 }
169 } else { // secondary protocol's port is non-numeric, not fatal?
170 errorCode = SECONDARY_PORT_NON_NUMERIC;
171 if(recover) {
172 msg.append("\nNot using this port");
173 }
174 httpPort = null;
175 }
176 }
177 }
178
179 if(httpsPort != null) {
180 try {
181 Integer.parseInt(httpsPort);
182 } catch(NumberFormatException nfe) {
183 msg.append("\nInvalid port specified for over https: not numeric.");
184 if(port == httpsPort) {
185 errorCode = INVALID_PRIMARY_PORT;
186 if(recover) {
187 port = httpsPort = defHttpsPort;
188 } else { // primary port affected and not asked to recover, treat as fatal
189 port = httpsPort = httpPort = null;
190 }
191 } else { // non primary port is invalid/non-numeric, not fatal
192 errorCode = SECONDARY_PORT_NON_NUMERIC;
193 if(recover) {
194 msg.append("\nNot using this port");
195 }
196 httpsPort = null;
197 }
198 }
199 }
200 }
201
202
203 if(recover) {
204 msg.append("\nFalling back to port ").append(port).append(" and protocol ").append(protocol).append(".");
205 }
206 // else if(errorCode == ALL_CORRECT) {
207 // msg.append("\nUsing port ").append(port).append(" and protocol ").append(protocol).append(".");
208 //} // else invalid property value(s) and we've not been asked to recover. msg alreay set.
209
210 errorMsg = msg.toString();
211 }
212
213}
Note: See TracBrowser for help on using the repository browser.