001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.console.keystores;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.apache.geronimo.console.MultiPageModel;
022    import org.apache.geronimo.management.geronimo.KeystoreException;
023    import org.apache.geronimo.util.CertificateUtil;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    import javax.portlet.PortletException;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    
031    import java.io.ByteArrayInputStream;
032    import java.io.IOException;
033    import java.io.InputStream;
034    import java.security.NoSuchAlgorithmException;
035    import java.security.cert.CertificateException;
036    import java.security.cert.CertificateFactory;
037    import java.security.cert.X509Certificate;
038    import java.text.SimpleDateFormat;
039    import java.util.Collection;
040    
041    /**
042     * Handler for entering a password to unlock a keystore
043     *
044     * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
045     */
046    public class ConfirmCertificateHandler extends BaseKeystoreHandler {
047        private final static Log log = LogFactory.getLog(ConfirmCertificateHandler.class);
048    
049        public ConfirmCertificateHandler() {
050            super(CONFIRM_CERTIFICATE, "/WEB-INF/view/keystore/confirmCertificate.jsp");
051        }
052    
053        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
054            return getMode();
055        }
056    
057        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
058            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
059            request.setAttribute("id", request.getParameter("id"));
060            request.setAttribute("alias", request.getParameter("alias"));
061            /*  // Uploading certificate using a disk file fails on Windows.  Certificate text is used instead.
062            String certFile = request.getParameter("certificate");
063            request.setAttribute("certificate", certFile);
064            InputStream is = new FileInputStream(certFile);
065            */
066            String certificate = request.getParameter("certificate");
067            request.setAttribute("certificate", certificate);
068            InputStream is = new ByteArrayInputStream(certificate.getBytes());
069            try {
070                CertificateFactory cf = CertificateFactory.getInstance("X.509");
071                Collection certificates = cf.generateCertificates(is);
072                X509Certificate cert = (X509Certificate) certificates.iterator().next();
073                request.setAttribute("fingerprint", CertificateUtil.generateFingerprint(cert, "MD5"));
074                request.setAttribute("issuer", cert.getIssuerDN().getName());
075                request.setAttribute("subject", cert.getSubjectDN().getName());
076                request.setAttribute("serial", cert.getSerialNumber());
077                request.setAttribute("validStart", sdf.format(cert.getNotBefore()));
078                request.setAttribute("validEnd", sdf.format(cert.getNotAfter()));
079            } catch (CertificateException e) {
080                log.error("Unable to process uploaded certificate", e);
081            } catch (NoSuchAlgorithmException e) {
082                log.error("Unable to process uploaded certificate", e);
083            }
084        }
085    
086        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
087            String id = request.getParameter("id");
088            String alias = request.getParameter("alias");
089            String certificate = request.getParameter("certificate");
090            if(id == null || id.equals("") || alias == null || alias.equals("") || certificate == null || certificate.equals("")) {
091                return LIST_MODE+BEFORE_ACTION; //todo: better handling
092            }
093            KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + id));
094            try {
095                data.importTrustCert(certificate, alias);
096            } catch (KeystoreException e) {
097                throw new PortletException(e);
098            }
099            response.setRenderParameter("id", id);
100            return VIEW_KEYSTORE+BEFORE_ACTION;
101        }
102    }