1 19 20 package com.sslexplorer.install.forms; 21 22 import java.net.InetAddress ; 23 import java.net.NetworkInterface ; 24 import java.net.UnknownHostException ; 25 import java.util.Enumeration ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.prefs.Preferences ; 29 30 import javax.servlet.http.HttpServletRequest ; 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 public final static String ATTR_HOSTNAME = "hostname"; 46 public final static String ATTR_ORGANISATIONAL_UNIT = "organisationalUnit"; 47 public final static String ATTR_COMPANY = "company"; 48 public final static String ATTR_COUNTRY_CODE = "countryCode"; 49 public final static String ATTR_CITY = "city"; 50 public final static String ATTR_STATE = "state"; 51 public static final String ATTR_KEY_STORE_TYPE = "keyStoreType"; 52 53 55 private String hostname; 56 private String organisationalUnit; 57 private String company; 58 private String countryCode; 59 private String city; 60 private String state; 61 private String keyStoreType; 62 63 public final static Preferences 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 74 public void init(AbstractWizardSequence sequence, HttpServletRequest request) throws Exception { 75 String localhostAddress = null; 76 hostname = (String )sequence.getAttribute(ATTR_HOSTNAME, PREF_NODE.get("hostname", "")); 77 if (hostname.equals("")) { 78 try { 79 Enumeration e = NetworkInterface.getNetworkInterfaces(); 80 81 while (e.hasMoreElements() && hostname == null) { 82 NetworkInterface netface = (NetworkInterface ) e.nextElement(); 83 Enumeration e2 = netface.getInetAddresses(); 84 while (e2.hasMoreElements() && hostname == null) { 85 InetAddress ip = (InetAddress ) 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 e) { 95 } 96 if (hostname.equals("")) { 97 hostname = localhostAddress == null ? "localhost" : localhostAddress; 98 } 99 } 100 countryCode =(String ) sequence.getAttribute(ATTR_COUNTRY_CODE, PREF_NODE.get("countryCode", "")); 101 if (countryCode.equals("")) { 102 countryCode = Locale.getDefault().getCountry(); 103 } 104 organisationalUnit = (String )sequence.getAttribute(ATTR_COUNTRY_CODE, PREF_NODE.get("organisationalUnit", "")); 105 company = (String )sequence.getAttribute(ATTR_COMPANY, PREF_NODE.get("company", "")); 106 city = (String )sequence.getAttribute(ATTR_CITY, PREF_NODE.get("city", "")); 107 state = (String )sequence.getAttribute(ATTR_STATE, PREF_NODE.get("state", "")); 108 keyStoreType = (String )sequence.getAttribute(ATTR_KEY_STORE_TYPE, getAvailableKeyStoreTypes().get(0).toString()); 109 110 } 111 112 public List getAvailableKeyStoreTypes() { 113 return KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getSupportedKeyStoreTypes(); 114 } 115 116 public String getKeyStoreType() { 117 return keyStoreType; 118 } 119 120 public void setKeyStoreType(String keyStoreType) { 121 this.keyStoreType = keyStoreType; 122 } 123 124 127 public void apply(AbstractWizardSequence sequence) throws Exception { 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 city) { 138 this.city = city; 139 } 140 141 public void setState(String state) { 142 this.state = state; 143 } 144 145 public String getCompany() { 146 return company; 147 } 148 149 public void setCompany(String company) { 150 this.company = company; 151 } 152 153 public String getCountryCode() { 154 return countryCode; 155 } 156 157 public String getCity() { 158 return city; 159 } 160 161 public String getState() { 162 return state; 163 } 164 165 public void setCountryCode(String countryCode) { 166 this.countryCode = countryCode; 167 } 168 169 public String getHostname() { 170 return hostname; 171 } 172 173 public void setHostname(String hostname) { 174 this.hostname = hostname; 175 } 176 177 public String getOrganisationalUnit() { 178 return organisationalUnit; 179 } 180 181 public void setOrganisationalUnit(String organisationalUnit) { 182 this.organisationalUnit = organisationalUnit; 183 } 184 185 public ActionErrors validate(ActionMapping mapping, HttpServletRequest 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 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 228 } | Popular Tags |