KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > keystore > forms > ShowKeyStoreForm


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.keystore.forms;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34
35 import com.sslexplorer.boot.KeyStoreManager;
36 import com.sslexplorer.keystore.CertificateItem;
37 import com.sslexplorer.setup.CertificatesTableItemModel;
38 import com.sslexplorer.table.forms.AbstractPagerForm;
39
40 /**
41  * Form for showing keystore content.
42  *
43  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
44  *
45  */

46 public class ShowKeyStoreForm extends AbstractPagerForm {
47
48     private static final long serialVersionUID = 2153872643060037840L;
49
50     static Log log = LogFactory.getLog(ShowKeyStoreForm.class);
51
52     // Private instance varaibles
53
private String JavaDoc password;
54     private String JavaDoc confirmPassword;
55     private String JavaDoc selectedKeyStoreName;
56
57     /**
58      * Constructor
59      */

60     public ShowKeyStoreForm() {
61         super(new CertificatesTableItemModel());
62         selectedKeyStoreName = KeyStoreManager.DEFAULT_KEY_STORE;
63     }
64     
65     /**
66      * Get the password.
67      *
68      * @return password
69      */

70     public String JavaDoc getPassword() {
71         return password;
72     }
73
74     /**
75      * Set the password.
76      *
77      * @param password old password
78      */

79     public void setPassword(String JavaDoc password) {
80         this.password = password.trim();
81     }
82     
83     /* (non-Javadoc)
84      * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
85      */

86     public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest JavaDoc request) {
87         super.reset(mapping, request);
88         this.password = null;
89         this.confirmPassword = null;
90     }
91
92     /**
93      * @param selectedKeyStoreName
94      */

95     public void setSelectedKeyStoreName(String JavaDoc selectedKeyStoreName) {
96         this.selectedKeyStoreName = selectedKeyStoreName;
97     }
98
99     /**
100      * @return String
101      */

102     public String JavaDoc getSelectedKeyStoreName() {
103         return selectedKeyStoreName;
104     }
105
106     /**
107      * @return KeyStoreManager
108      */

109     public KeyStoreManager getSelectedKeyStore() {
110         return KeyStoreManager.getInstance(getSelectedKeyStoreName());
111     }
112
113     /**
114      * @return List
115      */

116     public List JavaDoc getKeyStores() {
117         return KeyStoreManager.getKeyStores();
118     }
119
120     /**
121      * @param session
122      */

123     public void initialize(HttpSession JavaDoc session) {
124         super.initialize(session, "alias");
125         CertificateItem[] c = getCertificateItems();
126         if(c != null) {
127             for(int i = 0 ; i < c.length; i++) {
128                 getModel().addItem(c[i]);
129             }
130         }
131         getPager().rebuild(getFilterText());
132     }
133
134     /**
135      * Return an array of {@link CertificateItem} objects contained within
136      * the keystore.
137      *
138      * @return array of {@link CertificateItem}s.
139      */

140     public CertificateItem[] getCertificateItems() {
141         KeyStoreManager sel = getSelectedKeyStore();
142         if (!sel.isKeyStoreEmpty()){
143             Enumeration JavaDoc e = sel.getCertificateAliases();
144             if(e != null) {
145                 CertificateItem[] cert = new CertificateItem[sel.getSize()];
146                 int i = 0;
147                 while(e.hasMoreElements()) {
148                     String JavaDoc alias = (String JavaDoc) e.nextElement();
149                     cert[i++] = new CertificateItem(alias, sel.getCertificate(alias), sel);
150                 }
151                 return cert;
152             }
153         }
154         return null;
155     }
156
157     /**
158      * @return String
159      */

160     public String JavaDoc getConfirmPassword() {
161         return confirmPassword;
162     }
163
164     /**
165      * @param confirmPassword
166      */

167     public void setConfirmPassword(String JavaDoc confirmPassword) {
168         this.confirmPassword = confirmPassword;
169     }
170
171     @Override JavaDoc
172     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
173         if ("exportPrivate".equals(request.getParameter("actionTarget"))) {
174             ActionErrors errors = new ActionErrors();
175             try {
176                 if (getPassword().length() == 0) {
177                     errors.add(Globals.ERROR_KEY, new ActionMessage("setPassword.error.noNewPassword"));
178                 } else if (!getPassword().equals(getConfirmPassword())) {
179                     errors.add(Globals.ERROR_KEY, new ActionMessage("setPassword.error.newAndConfirmPasswordsDontMatch"));
180                 }
181             } catch (Exception JavaDoc e) {
182                 errors.add(Globals.ERROR_KEY, new ActionMessage("setPassword.error.validateFailed", e.getMessage()));
183             }
184             return errors;
185         }
186         return null;
187     }
188
189 }
Popular Tags