1 19 20 package com.sslexplorer.install.forms; 21 22 import java.io.IOException ; 23 import java.net.InetAddress ; 24 import java.net.ServerSocket ; 25 import java.net.UnknownHostException ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.Globals; 32 import org.apache.struts.action.ActionErrors; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionMessage; 35 36 import com.sslexplorer.boot.ContextKey; 37 import com.sslexplorer.boot.PropertyList; 38 import com.sslexplorer.core.InterfacesMultiSelectListDataSource; 39 import com.sslexplorer.input.MultiSelectSelectionModel; 40 import com.sslexplorer.properties.Property; 41 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey; 42 import com.sslexplorer.security.LogonControllerFactory; 43 import com.sslexplorer.security.SessionInfo; 44 import com.sslexplorer.wizard.AbstractWizardSequence; 45 import com.sslexplorer.wizard.forms.DefaultWizardForm; 46 47 51 public class WebServerForm extends DefaultWizardForm { 52 53 final static Log log = LogFactory.getLog(WebServerForm.class); 54 55 57 60 public final static String ATTR_WEB_SERVER_PORT = "webServerPort"; 61 62 65 public final static String ATTR_WEB_SERVER_PROTOCOL = "webServerProtocol"; 66 67 70 public final static String ATTR_LISTENING_INTERFACES = "bindAddresses"; 71 72 75 public final static String ATTR_VALID_EXTERNAL_HOSTS = "validExternalHosts"; 76 77 80 public final static String ATTR_INVALID_HOSTNAME_ACTION = "invalidHostnameAction"; 81 82 private String port; 84 private String listeningInterfaces; 85 private String invalidHostnameAction; 86 private final PropertyList validExternalHostnames = new PropertyList(); 87 private MultiSelectSelectionModel model; 88 89 92 public WebServerForm() { 93 super(true, true, "/WEB-INF/jsp/content/install/webServer.jspf", "port", true, false, "webServer", "install", "installation.webServer", 4); 94 } 95 96 100 public void init(AbstractWizardSequence sequence, HttpServletRequest request) throws Exception { 101 try { 102 port = (String ) sequence.getAttribute(ATTR_WEB_SERVER_PORT, Property.getProperty(new ContextKey("webServer.port"))); 103 listeningInterfaces = (String ) sequence.getAttribute(ATTR_LISTENING_INTERFACES, Property.getProperty(new ContextKey("webServer.bindAddress"))); 104 String validExternalHostnamesAsTextFieldText = (String ) sequence.getAttribute(ATTR_VALID_EXTERNAL_HOSTS, Property.getProperty(new SystemConfigKey("webServer.validExternalHostnames"))); 105 validExternalHostnames.setAsPropertyText(validExternalHostnamesAsTextFieldText); 106 invalidHostnameAction = (String ) sequence.getAttribute(ATTR_INVALID_HOSTNAME_ACTION, Property.getProperty(new SystemConfigKey("webServer.invalidHostnameAction"))); 107 PropertyList pl = PropertyList.createFromTextFieldText(listeningInterfaces); 108 SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request); 109 model = new MultiSelectSelectionModel(session, new InterfacesMultiSelectListDataSource(), pl); 110 } catch (Exception e) { 111 log.error("Failed to initialise form."); 112 } 113 } 114 115 119 public void apply(AbstractWizardSequence sequence) throws Exception { 120 sequence.putAttribute(ATTR_WEB_SERVER_PORT, port); 121 sequence.putAttribute(ATTR_WEB_SERVER_PROTOCOL, "https"); sequence.putAttribute(ATTR_LISTENING_INTERFACES, listeningInterfaces); 123 sequence.putAttribute(ATTR_VALID_EXTERNAL_HOSTS, validExternalHostnames.getAsTextFieldText()); 124 sequence.putAttribute(ATTR_INVALID_HOSTNAME_ACTION, invalidHostnameAction); 125 } 126 127 131 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 132 if (isCommiting()) { 133 ActionErrors errors = new ActionErrors(); 134 validateListeningInterfaces(errors); 135 validateExternalHostnames(errors); 136 return errors; 137 } else { 138 return null; 139 } 140 } 141 142 private void validateListeningInterfaces(ActionErrors errs) { 143 try { 144 int port = Integer.parseInt(getPort()); 145 if(!isPortValid(port)) { 146 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.webServer.error.invalidPortNumber", getPort())); 147 return; 148 } 149 150 PropertyList listeningInterfaces = PropertyList.createFromTextFieldText(getListeningInterfaces().equals("") ? "0.0.0.0" : getListeningInterfaces()); 151 boolean containsRootAddress = false; 152 for (String address : listeningInterfaces) { 153 154 if (!isHostAndPortValid(address, port)) { 155 if (port < 1024) { 156 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.webServer.error.portConflictLess1024", getPort(), address)); 157 } else { 158 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.webServer.error.portConflict", getPort(), address)); 159 } 160 } 161 162 if (address.equals("0.0.0.0")) { 163 containsRootAddress = true; 164 } 165 } 166 167 if(containsRootAddress && listeningInterfaces.size() > 1) { 168 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.webServer.invalidSelectedInterfaces")); 169 } 170 } catch (NumberFormatException nfe) { 171 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.webServer.error.invalidPortNumber", getPort())); 172 } 173 } 174 175 private static boolean isPortValid(int port) { 176 return port >= 1 && port <= 65535; 177 } 178 179 private static boolean isHostAndPortValid(String address, int port) { 180 ServerSocket socket = null; 181 try { 182 if (log.isInfoEnabled()) 183 log.info("Testing listener on " + address + ":" + port); 184 socket = new ServerSocket (port, 0, InetAddress.getByName(address)); 185 return true; 186 } catch (IOException e) { 187 log.error("Failed to setup server socket.", e); 188 return false; 189 } finally { 190 if (socket != null) { 191 try { 192 socket.close(); 193 } catch (Exception e) { 194 } 195 } 196 } 197 } 198 199 private void validateExternalHostnames(ActionErrors errs) { 200 PropertyList listeningInterfaces = PropertyList.createFromTextFieldText(getValidExternalHostnames()); 201 for (String address : listeningInterfaces) { 202 if (!isValidIpAddress(address)) { 203 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.webServer.error.invalidExeternalHostname", address)); 204 } 205 } 206 } 207 208 private static boolean isValidIpAddress(String ipAddress) { 209 try { 210 InetAddress.getByName(ipAddress); 211 return true; 212 } catch (UnknownHostException e) { 213 return false; 214 } 215 } 216 217 221 public String getListeningInterfaces() { 222 return listeningInterfaces; 223 } 224 225 229 public void setListeningInterfaces(String listeningInterfaces) { 230 this.listeningInterfaces = listeningInterfaces; 231 } 232 233 237 public String getPort() { 238 return port; 239 } 240 241 245 public void setPort(String port) { 246 this.port = port; 247 } 248 249 253 public String getValidExternalHostnames() { 254 return validExternalHostnames.getAsTextFieldText(); 255 } 256 257 261 public void setValidExternalHostnames(String validExternalHostnames) { 262 this.validExternalHostnames.setAsTextFieldText(validExternalHostnames); 263 } 264 265 269 public MultiSelectSelectionModel getModel() { 270 return model; 271 } 272 273 276 public String getInvalidHostnameAction() { 277 return invalidHostnameAction; 278 } 279 280 283 public void setInvalidHostnameAction(String invalidHostnameAction) { 284 this.invalidHostnameAction = invalidHostnameAction; 285 } 286 } 287 | Popular Tags |