KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > keystore > wizards > types > TrustedServerCertificateImportType


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.wizards.types;
21
22 import java.io.File JavaDoc;
23 import java.security.cert.Certificate JavaDoc;
24 import java.security.cert.X509Certificate JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31
32 import com.sslexplorer.boot.ContextKey;
33 import com.sslexplorer.boot.KeyStoreManager;
34 import com.sslexplorer.core.CoreAttributeConstants;
35 import com.sslexplorer.core.CoreEvent;
36 import com.sslexplorer.core.CoreEventConstants;
37 import com.sslexplorer.core.CoreServlet;
38 import com.sslexplorer.keystore.wizards.AbstractKeyStoreImportType;
39 import com.sslexplorer.properties.Property;
40 import com.sslexplorer.security.SessionInfo;
41 import com.sslexplorer.wizard.AbstractWizardSequence;
42
43
44 /**
45  * Implementation of a {@link com.sslexplorer.keystore.wizards.AbstractKeyStoreImportType}
46  * that imports a trusted server certificate.
47  *
48  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
49  */

50 public class TrustedServerCertificateImportType extends AbstractKeyStoreImportType {
51     
52
53     /**
54      * Constant for importing server certificates that SSL-Explorer should trust
55      */

56     public final static String JavaDoc TRUSTED_SERVER_CERTIFICATE = "trustedServerCertificate";
57
58     /**
59      * Constructor.
60      */

61     public TrustedServerCertificateImportType() {
62         super(TRUSTED_SERVER_CERTIFICATE, "keystore", false, true, 30);
63     }
64
65     /* (non-Javadoc)
66      * @see com.sslexplorer.keystore.wizards.AbstractKeyStoreImportType#validate(org.apache.struts.action.ActionMessages, java.lang.String, java.lang.String, com.sslexplorer.wizard.AbstractWizardSequence, com.sslexplorer.security.SessionInfo)
67      */

68     public void validate(ActionMessages errs, String JavaDoc alias, String JavaDoc passphrase, AbstractWizardSequence seq, SessionInfo sessionInfo) {
69         super.validate(errs, alias, passphrase, seq, sessionInfo);
70         if(alias==null || alias.equals("")) {
71             errs.add(Globals.ERROR_KEY, new ActionMessage("keyStoreImportWizard.keyStoreImportFile.noNameProvided"));
72         }
73         else {
74             KeyStoreManager mgr = KeyStoreManager.getInstance(KeyStoreManager.TRUSTED_SERVER_CERTIFICATES_KEY_STORE);
75             if(mgr.getCertificate(alias.toLowerCase()) != null) {
76                 errs.add(Globals.ERROR_KEY, new ActionMessage("keyStoreImportWizard.keyStoreImportFile.duplicateName", alias.toLowerCase()));
77             }
78         }
79     }
80
81     /* (non-Javadoc)
82      * @see com.sslexplorer.keystore.wizards.AbstractKeyStoreImportType#doInstall(java.io.File, java.lang.String, java.lang.String, com.sslexplorer.wizard.AbstractWizardSequence)
83      */

84     public void doInstall(File JavaDoc file, String JavaDoc alias, String JavaDoc passphrase, AbstractWizardSequence seq, SessionInfo sessionInfo) throws Exception JavaDoc {
85         KeyStoreManager mgr = KeyStoreManager.getInstance(KeyStoreManager.TRUSTED_SERVER_CERTIFICATES_KEY_STORE);
86         mgr.importCert(alias, file, null);
87         mgr.reloadKeystore();
88         Certificate JavaDoc certif = mgr.getCertificate(alias);
89
90         CoreEvent coreEvent = new CoreEvent(this, CoreEventConstants.KEYSTORE_TRUSTED_CERTIFICATE_IMPORTED, Property.getProperty(new ContextKey("webServer.alias")), seq.getSession())
91                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_ALIAS, alias)
92                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_TYPE, certif.getType())
93                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_HOSTNAME, KeyStoreManager.getX509CertificateEntity((X509Certificate JavaDoc)certif, "cn"))
94                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_ORGANISATIONAL_UNIT, KeyStoreManager.getX509CertificateEntity((X509Certificate JavaDoc)certif, "ou"))
95                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_COMPANY, KeyStoreManager.getX509CertificateEntity((X509Certificate JavaDoc)certif, "o"))
96                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_STATE, KeyStoreManager.getX509CertificateEntity((X509Certificate JavaDoc)certif, "st"))
97                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_LOCATION, KeyStoreManager.getX509CertificateEntity((X509Certificate JavaDoc)certif, "l"))
98                         .addAttribute(CoreAttributeConstants.EVENT_ATTR_CERTIFICATE_COUNTRY_CODE, KeyStoreManager.getX509CertificateEntity((X509Certificate JavaDoc)certif, "c"));
99         
100         CoreServlet.getServlet().fireCoreEvent(coreEvent);
101     }
102
103     /* (non-Javadoc)
104      * @see com.sslexplorer.keystore.wizards.AbstractKeyStoreImportType#init(javax.servlet.http.HttpServletRequest)
105      */

106     public void init(HttpServletRequest JavaDoc request) {
107         super.init(request);
108         ActionMessages messages =
109             (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
110         if (messages == null) {
111             messages = new ActionMessages();
112         }
113         messages.add(Globals.MESSAGE_KEY, new ActionMessage("keyStoreImportWizard.keyStoreImportFile.caseWarnimg"));
114         request.setAttribute(Globals.MESSAGE_KEY, messages);
115     }
116
117 }
118
Popular Tags