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

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

Fix. If tomcat.port exists in a properties file, it does not automatically mean legacy mode, since global.properties still has a tomcat.port property.

File size: 8.8 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 properties
82 // indicates that: if tomcat.port exists BUT tomcat.port.http(s) don't, then it's an older GS3 that has no https support, so default to http.
83 // If there is no tomcat.port at all, but there is a server.protocol check for the newer tomcat.port.<protocol> properties.
84 // NOTE: global.properties still has a property called tomcat.port. We're only dealing with the old tomcat.port-only way if tomcat.port.http(s) don't exist
85
86 if(props.getProperty("tomcat.port.http") == null && props.getProperty("tomcat.port.https") == null && port != null) {
87 // tomcat.port exists but tomcat.port.http(s) don't, so this is a GS3 before https support.
88 legacyMode = true;
89
90 // Back when tomcat.port was used AND tomcat.port.http(s) didn't exist, server.protocol if specified
91 // would always be treated as http regardless of what it was set to
92
93 protocol = defProtocol; // tomcat.port.http(s) doesn't exist, just use http
94
95
96 if(port.equals("")) {
97 errorCode = OLD_TOMCATPORT_BUT_NO_VALUE;
98 msg.append("tomcat.port enabled but did not have a value.");
99 if(recover) {
100 port = httpPort = defHttpPort;
101 }
102 } else { // No issues: using tomcat.port is the pre-https way.
103 errorCode = ALL_CORRECT;
104 httpPort = port;
105 }
106 }
107 else {
108 legacyMode = false;
109
110 protocol = props.getProperty("server.protocol");
111 if(protocol == null || (!protocol.equals("http") && !protocol.equals("https"))) {
112 if(port == null) { // if tomcat.port is null AND server.protocol is null or wrong. Something very wrong with the properties file
113 errorCode = NO_PROTOCOL_OR_PORT;
114 msg.append("server.protocol not set. And can't determine port.");
115 } else if(protocol == null) {
116 errorCode = NO_PROTOCOL_OR_PORT;
117 msg.append("server.protocol not set.");
118 } else {
119 errorCode = INVALID_PROTOCOL;
120 msg.append("server.protocol property must be http or https, but is set to invalid value. And can't determine port.");
121 }
122
123 if(recover) {
124 protocol = defProtocol;
125 port = httpPort = defHttpPort;
126 }
127
128 } else { // server.protocol was explicitly set and set to an acceptable value, try to find matching tomcat.port
129
130 port = props.getProperty("tomcat.port."+protocol); // tomcat.port.http or tomcat.port.https, depending on server.protocol.
131
132 // Handle cases where there's some inconsistency in the properties file between protocol and port.
133 // i.e. if server.protocol=http, then tomcat.port.http must be set too.
134 if(port == null || port.equals("")) {
135 errorCode = INVALID_PROTOCOL;
136 msg.append("server.protocol="+protocol+", but matching tomcat.port."+protocol+"not enabled or set.");
137
138 if(recover) {
139 protocol = defProtocol;
140 if(protocol.startsWith("https")) { // server.protocol=https yet tomcat.port.https not provided
141 port = httpsPort = defHttpsPort; // use default https port to set
142 } else {
143 port = httpPort = defHttpPort;
144 }
145 }
146
147 } else { //tomcat.port.<protocol> property matching server.protocol is set, not checking if it's an int and a valid port, though
148 errorCode = ALL_CORRECT;
149 // port is set
150
151 if(protocol.startsWith("https")) {
152 httpsPort = port;
153 httpPort = props.getProperty("tomcat.port.http"); // httpPort will remain null if tomcat.port.http not set
154 } else { // protocol=http
155 httpPort = port;
156 httpsPort = props.getProperty("tomcat.port.https"); // httpsPort will remain null if tomcat.port.https not set
157 }
158 }
159 }
160 }
161
162 if(errorCode == ALL_CORRECT) { // then check any assigned ports are valid
163 if(httpPort != null) {
164 try {
165 Integer.parseInt(httpPort);
166 } catch(NumberFormatException nfe) {
167 msg.append("\nInvalid port specified for over http: not numeric.");
168 if(port == httpPort) {
169 errorCode = INVALID_PRIMARY_PORT;
170 if(recover) {
171 port = httpPort = defHttpPort;
172 } else { // no recovery requested, so if port for protocol is non-numeric, consider it a 'fatal' error
173 port = httpPort = httpsPort = null;
174 }
175 } else { // secondary protocol's port is non-numeric, not fatal?
176 errorCode = SECONDARY_PORT_NON_NUMERIC;
177 if(recover) {
178 msg.append("\nNot using this port");
179 }
180 httpPort = null;
181 }
182 }
183 }
184
185 if(httpsPort != null) {
186 try {
187 Integer.parseInt(httpsPort);
188 } catch(NumberFormatException nfe) {
189 msg.append("\nInvalid port specified for over https: not numeric.");
190 if(port == httpsPort) {
191 errorCode = INVALID_PRIMARY_PORT;
192 if(recover) {
193 port = httpsPort = defHttpsPort;
194 } else { // primary port affected and not asked to recover, treat as fatal
195 port = httpsPort = httpPort = null;
196 }
197 } else { // non primary port is invalid/non-numeric, not fatal
198 errorCode = SECONDARY_PORT_NON_NUMERIC;
199 if(recover) {
200 msg.append("\nNot using this port");
201 }
202 httpsPort = null;
203 }
204 }
205 }
206 }
207
208
209 if(recover) {
210 msg.append("\nFalling back to port ").append(port).append(" and protocol ").append(protocol).append(".");
211 }
212 // else if(errorCode == ALL_CORRECT) {
213 // msg.append("\nUsing port ").append(port).append(" and protocol ").append(protocol).append(".");
214 //} // else invalid property value(s) and we've not been asked to recover. msg alreay set.
215
216 errorMsg = msg.toString();
217 }
218
219}
Note: See TracBrowser for help on using the repository browser.