KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > install > forms > WebServerForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
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
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.install.forms;
21
22 import java.io.IOException JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.ServerSocket JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
48  * Form used during installation to enter the web server details.
49  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
50  */

51 public class WebServerForm extends DefaultWizardForm {
52
53     final static Log log = LogFactory.getLog(WebServerForm.class);
54
55     // Private statics for sequence attributes
56

57     /**
58      * Web server port
59      */

60     public final static String JavaDoc ATTR_WEB_SERVER_PORT = "webServerPort";
61
62     /**
63      * Web server protocol
64      */

65     public final static String JavaDoc ATTR_WEB_SERVER_PROTOCOL = "webServerProtocol";
66
67     /**
68      * Listening interfaces
69      */

70     public final static String JavaDoc ATTR_LISTENING_INTERFACES = "bindAddresses";
71
72     /**
73      * Valid external hosts
74      */

75     public final static String JavaDoc ATTR_VALID_EXTERNAL_HOSTS = "validExternalHosts";
76
77     /**
78      * Invalid hostname action
79      */

80     public final static String JavaDoc ATTR_INVALID_HOSTNAME_ACTION = "invalidHostnameAction";
81
82     // Private instance variables
83
private String JavaDoc port;
84     private String JavaDoc listeningInterfaces;
85     private String JavaDoc invalidHostnameAction;
86     private final PropertyList validExternalHostnames = new PropertyList();
87     private MultiSelectSelectionModel model;
88
89     /**
90      * Constructor
91      */

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     /*
97      * (non-Javadoc)
98      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence, javax.servlet.http.HttpServletRequest)
99      */

100     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
101         try {
102             port = (String JavaDoc) sequence.getAttribute(ATTR_WEB_SERVER_PORT, Property.getProperty(new ContextKey("webServer.port")));
103             listeningInterfaces = (String JavaDoc) sequence.getAttribute(ATTR_LISTENING_INTERFACES, Property.getProperty(new ContextKey("webServer.bindAddress")));
104             String JavaDoc validExternalHostnamesAsTextFieldText = (String JavaDoc) sequence.getAttribute(ATTR_VALID_EXTERNAL_HOSTS, Property.getProperty(new SystemConfigKey("webServer.validExternalHostnames")));
105             validExternalHostnames.setAsPropertyText(validExternalHostnamesAsTextFieldText);
106             invalidHostnameAction = (String JavaDoc) 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 JavaDoc e) {
111             log.error("Failed to initialise form.");
112         }
113     }
114
115     /*
116      * (non-Javadoc)
117      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#apply(com.sslexplorer.wizard.AbstractWizardSequence)
118      */

119     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
120         sequence.putAttribute(ATTR_WEB_SERVER_PORT, port);
121         sequence.putAttribute(ATTR_WEB_SERVER_PROTOCOL, "https"); // this is now hard coded to always be HTTPS
122
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     /*
128      * (non-Javadoc)
129      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
130      */

131     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc address, int port) {
180         ServerSocket JavaDoc socket = null;
181         try {
182             if (log.isInfoEnabled())
183                 log.info("Testing listener on " + address + ":" + port);
184             socket = new ServerSocket JavaDoc(port, 0, InetAddress.getByName(address));
185             return true;
186         } catch (IOException JavaDoc 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 JavaDoc e) {
194                 }
195             }
196         }
197     }
198     
199     private void validateExternalHostnames(ActionErrors errs) {
200         PropertyList listeningInterfaces = PropertyList.createFromTextFieldText(getValidExternalHostnames());
201         for (String JavaDoc 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 JavaDoc ipAddress) {
209         try {
210             InetAddress.getByName(ipAddress);
211             return true;
212         } catch (UnknownHostException JavaDoc e) {
213             return false;
214         }
215     }
216
217     /**
218      * Get list list of listening interfaces as newline separated string
219      * @return listening interfaces as newline separated string
220      */

221     public String JavaDoc getListeningInterfaces() {
222         return listeningInterfaces;
223     }
224
225     /**
226      * Set the list of listening interfaces as newline separated string
227      * @param listeningInterfaces listening interfaces as newline separated string
228      */

229     public void setListeningInterfaces(String JavaDoc listeningInterfaces) {
230         this.listeningInterfaces = listeningInterfaces;
231     }
232
233     /**
234      * Get the port on which the server should run
235      * @return port
236      */

237     public String JavaDoc getPort() {
238         return port;
239     }
240
241     /**
242      * Set the port on which the server should run
243      * @param port port on which server should run
244      */

245     public void setPort(String JavaDoc port) {
246         this.port = port;
247     }
248
249     /**
250      * Get the newline separated list of valid external hostnames
251      * @return valid external hostnames
252      */

253     public String JavaDoc getValidExternalHostnames() {
254         return validExternalHostnames.getAsTextFieldText();
255     }
256
257     /**
258      * Set the newline separated list of valid external hostnames
259      * @param validExternalHostnames valid external hostnames
260      */

261     public void setValidExternalHostnames(String JavaDoc validExternalHostnames) {
262         this.validExternalHostnames.setAsTextFieldText(validExternalHostnames);
263     }
264
265     /**
266      * Get the model of items that may be selected for valid listening interfaces
267      * @return model for valid listening interfaces
268      */

269     public MultiSelectSelectionModel getModel() {
270         return model;
271     }
272
273     /**
274      * @return String
275      */

276     public String JavaDoc getInvalidHostnameAction() {
277         return invalidHostnameAction;
278     }
279     
280     /**
281      * @param invalidHostnameAction
282      */

283     public void setInvalidHostnameAction(String JavaDoc invalidHostnameAction) {
284         this.invalidHostnameAction = invalidHostnameAction;
285     }
286 }
287
Popular Tags