001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.console.ca;
019    
020    import javax.portlet.ActionResponse;
021    import javax.portlet.PortletRequest;
022    import javax.portlet.PortletSession;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.geronimo.console.MultiPageAbstractHandler;
027    import org.apache.geronimo.console.MultiPageModel;
028    import org.apache.geronimo.console.util.PortletManager;
029    import org.apache.geronimo.management.geronimo.CertificateRequestStore;
030    import org.apache.geronimo.management.geronimo.CertificateStore;
031    import org.apache.geronimo.management.geronimo.CertificationAuthority;
032    import org.apache.geronimo.management.geronimo.KeystoreException;
033    import org.apache.geronimo.management.geronimo.KeystoreInstance;
034    
035    /**
036     * The base class for all handlers for CA portlet
037     *
038     * @version $Rev: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $
039     */
040    public abstract class BaseCAHandler extends MultiPageAbstractHandler {
041        private final static Log log = LogFactory.getLog(BaseCAHandler.class);
042    
043        protected static final String INDEX_MODE = "index";
044        protected static final String SETUPCA_MODE = "setupCA";
045        protected static final String CONFIRM_CA_MODE = "confirmCA";
046        protected static final String CADETAILS_MODE = "caDetails";
047        protected static final String UNLOCKCA_MODE = "unlockCA";
048        protected static final String PROCESS_CSR_MODE = "processCSR";
049        protected static final String CERT_REQ_DETAILS_MODE = "certReqDetails";
050        protected static final String CONFIRM_CLIENT_CERT_MODE = "confirmClientCert";
051        protected static final String VIEW_CERT_MODE = "viewCert";
052        protected static final String LIST_REQUESTS_ISSUE_MODE = "listRequestsIssue";
053        protected static final String LIST_REQUESTS_VERIFY_MODE = "listRequestsVerify";
054        protected static final String CONFIRM_CERT_REQ_MODE = "confirmCertReq";
055        
056        // Key algorithm for CA's keypair
057        protected static final String defaultKeyAlgorithm = "RSA";
058        // CA's private key and self-signed certificate is stored under this keystore created using KeystoreManager
059        // Using FileKeystoreManager, the file willbe <server-base-dir>/var/security/keystores/<defaultCAKeystore>
060        protected static final String defaultCAKeystore = "ca-keystore";
061        // CA's certificate store directory
062        protected static final String defaultCAStoreDir = "var/security/ca/certs";
063        // Certificate request store directory
064        protected static final String defaultCSRStoreDir = "var/security/ca/requests";
065    
066        // Name of the attribute for error message to be displayed in a page
067        protected static final String ERROR_MSG = "errorMsg";
068        // Name of the attribute for information message to be displayed in a page
069        protected static final String INFO_MSG = "infoMsg";
070    
071        /**
072         * Constructor
073         */
074        protected BaseCAHandler(String mode, String viewName) {
075            super(mode, viewName);
076        }
077    
078        public final static class CAModel implements MultiPageModel {
079            public CAModel(PortletRequest request) {
080            }
081    
082            public void save(ActionResponse response, PortletSession session) {
083            }
084        }
085        
086        /**
087         * This method returns CertificationAuthority GBbean.
088         * @param request PortletRequest to execute retrieve GBean
089         * @return  null if a CA GBean is not running.
090         */
091        protected CertificationAuthority getCertificationAuthority(PortletRequest request) {
092            Object[] cas = PortletManager.getManagementHelper(request).getGBeansImplementing(CertificationAuthority.class);
093            return (CertificationAuthority)(cas != null && cas.length > 0 ? cas[0] : null);
094        }
095    
096        /**
097         * This methods creates CA's keystore using KeystoreManager.
098         * @param request PortletRequest to get KeystoreManager
099         * @param password Password for newly created Keystore
100         * @throws KeystoreException 
101         */
102        protected KeystoreInstance createCAKeystoreInstance(PortletRequest request, String password) throws KeystoreException {
103            return PortletManager.getCurrentServer(request).getKeystoreManager().createKeystore(defaultCAKeystore, password.toCharArray());
104        }
105        
106        /**
107         * This method returns CertificateRequestStore GBean.
108         * @param request PortletRequest to execute retrieve GBean
109         * @return  null if a CertificateRequestStore GBean is not running.
110         */
111        protected CertificateRequestStore getCertificateRequestStore(PortletRequest request) {
112            Object[] crs = PortletManager.getManagementHelper(request).getGBeansImplementing(CertificateRequestStore.class);
113            return (CertificateRequestStore)(crs != null && crs.length > 0 ? crs[0] : null);
114        }
115    
116        /**
117         * This method returns CertificateStore GBean.
118         * @param request PortletRequest to execute retrieve GBean
119         * @return  null if a CertificateStore GBean is not running.
120         */
121        protected CertificateStore getCertificateStore(PortletRequest request) {
122            Object[] cs = PortletManager.getManagementHelper(request).getGBeansImplementing(CertificateStore.class);
123            return (CertificateStore)(cs != null && cs.length > 0 ? cs[0] : null);
124        }
125    }