KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.InetAddress JavaDoc;
23 import java.net.NetworkInterface JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.prefs.Preferences JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31
32 import org.apache.struts.Globals;
33 import org.apache.struts.action.ActionErrors;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.ActionMessage;
36
37 import com.sslexplorer.boot.ContextHolder;
38 import com.sslexplorer.boot.KeyStoreManager;
39 import com.sslexplorer.input.validators.IPAddressValidator;
40 import com.sslexplorer.wizard.AbstractWizardSequence;
41 import com.sslexplorer.wizard.forms.DefaultWizardForm;
42
43 public class CreateNewCertificateForm extends DefaultWizardForm {
44     // Statics for sequence attributes
45
public final static String JavaDoc ATTR_HOSTNAME = "hostname";
46     public final static String JavaDoc ATTR_ORGANISATIONAL_UNIT = "organisationalUnit";
47     public final static String JavaDoc ATTR_COMPANY = "company";
48     public final static String JavaDoc ATTR_COUNTRY_CODE = "countryCode";
49     public final static String JavaDoc ATTR_CITY = "city";
50     public final static String JavaDoc ATTR_STATE = "state";
51     public static final String JavaDoc ATTR_KEY_STORE_TYPE = "keyStoreType";
52
53     // Private instance variables
54

55     private String JavaDoc hostname;
56     private String JavaDoc organisationalUnit;
57     private String JavaDoc company;
58     private String JavaDoc countryCode;
59     private String JavaDoc city;
60     private String JavaDoc state;
61     private String JavaDoc keyStoreType;
62     
63     public final static Preferences JavaDoc PREF_NODE = ContextHolder.getContext().getPreferences().node("installation").node("certificate");
64         
65     public CreateNewCertificateForm() {
66         super(true, true, "/WEB-INF/jsp/content/install/createNewCertificate.jspf",
67             "hostname", true, false, "createNewCertificate",
68             "install", "installation.createNewCertificate", 1);
69     }
70
71     /* (non-Javadoc)
72      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence)
73      */

74     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
75         String JavaDoc localhostAddress = null;
76         hostname = (String JavaDoc)sequence.getAttribute(ATTR_HOSTNAME, PREF_NODE.get("hostname", ""));
77         if (hostname.equals("")) {
78             try {
79                 Enumeration JavaDoc e = NetworkInterface.getNetworkInterfaces();
80
81                 while (e.hasMoreElements() && hostname == null) {
82                     NetworkInterface JavaDoc netface = (NetworkInterface JavaDoc) e.nextElement();
83                     Enumeration JavaDoc e2 = netface.getInetAddresses();
84                     while (e2.hasMoreElements() && hostname == null) {
85                         InetAddress JavaDoc ip = (InetAddress JavaDoc) e2.nextElement();
86                         if (!ip.getCanonicalHostName().equals("localhost")
87                                         && !ip.getCanonicalHostName().equals("localhost.localdomain")) {
88                             hostname = ip.getCanonicalHostName();
89                         } else {
90                             localhostAddress = ip.getCanonicalHostName();
91                         }
92                     }
93                 }
94             } catch (Exception JavaDoc e) {
95             }
96             if (hostname.equals("")) {
97                 hostname = localhostAddress == null ? "localhost" : localhostAddress;
98             }
99         }
100         countryCode =(String JavaDoc) sequence.getAttribute(ATTR_COUNTRY_CODE, PREF_NODE.get("countryCode", ""));
101         if (countryCode.equals("")) {
102             countryCode = Locale.getDefault().getCountry();
103         }
104         organisationalUnit = (String JavaDoc)sequence.getAttribute(ATTR_COUNTRY_CODE, PREF_NODE.get("organisationalUnit", ""));
105         company = (String JavaDoc)sequence.getAttribute(ATTR_COMPANY, PREF_NODE.get("company", ""));
106         city = (String JavaDoc)sequence.getAttribute(ATTR_CITY, PREF_NODE.get("city", ""));
107         state = (String JavaDoc)sequence.getAttribute(ATTR_STATE, PREF_NODE.get("state", ""));
108         keyStoreType = (String JavaDoc)sequence.getAttribute(ATTR_KEY_STORE_TYPE, getAvailableKeyStoreTypes().get(0).toString());
109         
110     }
111
112     public List JavaDoc getAvailableKeyStoreTypes() {
113         return KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getSupportedKeyStoreTypes();
114     }
115     
116     public String JavaDoc getKeyStoreType() {
117         return keyStoreType;
118     }
119     
120     public void setKeyStoreType(String JavaDoc keyStoreType) {
121         this.keyStoreType = keyStoreType;
122     }
123
124     /* (non-Javadoc)
125      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#apply(com.sslexplorer.wizard.AbstractWizardSequence)
126      */

127     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
128         sequence.putAttribute(ATTR_CITY, city);
129         sequence.putAttribute(ATTR_COMPANY, company);
130         sequence.putAttribute(ATTR_COUNTRY_CODE, countryCode);
131         sequence.putAttribute(ATTR_HOSTNAME, hostname);
132         sequence.putAttribute(ATTR_ORGANISATIONAL_UNIT, organisationalUnit);
133         sequence.putAttribute(ATTR_STATE, state);
134         sequence.putAttribute(ATTR_KEY_STORE_TYPE, keyStoreType);
135     }
136
137     public void setCity(String JavaDoc city) {
138         this.city = city;
139     }
140
141     public void setState(String JavaDoc state) {
142         this.state = state;
143     }
144
145     public String JavaDoc getCompany() {
146         return company;
147     }
148
149     public void setCompany(String JavaDoc company) {
150         this.company = company;
151     }
152
153     public String JavaDoc getCountryCode() {
154         return countryCode;
155     }
156
157     public String JavaDoc getCity() {
158         return city;
159     }
160
161     public String JavaDoc getState() {
162         return state;
163     }
164
165     public void setCountryCode(String JavaDoc countryCode) {
166         this.countryCode = countryCode;
167     }
168
169     public String JavaDoc getHostname() {
170         return hostname;
171     }
172
173     public void setHostname(String JavaDoc hostname) {
174         this.hostname = hostname;
175     }
176
177     public String JavaDoc getOrganisationalUnit() {
178         return organisationalUnit;
179     }
180
181     public void setOrganisationalUnit(String JavaDoc organisationalUnit) {
182         this.organisationalUnit = organisationalUnit;
183     }
184     
185     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
186         ActionErrors errs = new ActionErrors();
187         if(isCommiting()) {
188             if ("".equals(hostname)) {
189                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.createNewCertificate.error.noHost"));
190             }
191             // LDP - This is a problem because it stops wildcard certificates from being generated with
192
// hostnames such as *.3sp.co.uk
193
/*else if (!isValidIpAddress(hostname)) {
194                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.createNewCertificate.error.invalidHost"));
195             }*/

196
197             if ("".equals(organisationalUnit)) {
198                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.createNewCertificate.error.noOrganisationalUnit"));
199             }
200
201             if ("".equals(company)) {
202                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.createNewCertificate.error.noCompany"));
203             }
204             
205             if ("".equals(city)) {
206                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.createNewCertificate.error.noCity"));
207             }
208             
209             if ("".equals(state)) {
210                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.createNewCertificate.error.noState"));
211             }
212
213             if ("".equals(countryCode)) {
214                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.createNewCertificate.error.noCountryCode"));
215             }
216         }
217         return errs;
218     }
219     
220     /*private static boolean isValidIpAddress(String ipAddress) {
221         try {
222             InetAddress.getByName(ipAddress);
223             return true;
224         } catch (UnknownHostException e) {
225             return false;
226         }
227     }*/

228 }
Popular Tags