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 java.io.IOException;
021    import java.math.BigInteger;
022    import java.security.PublicKey;
023    import java.security.cert.Certificate;
024    import java.security.interfaces.RSAPublicKey;
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletException;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    
034    import org.apache.commons.logging.Log;
035    import org.apache.commons.logging.LogFactory;
036    import org.apache.geronimo.console.MultiPageModel;
037    import org.apache.geronimo.management.geronimo.CertificationAuthority;
038    import org.apache.geronimo.util.CertificateUtil;
039    
040    /**
041     * Handler for view certificate screen.
042     *
043     * @version $Rev: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $
044     */
045    public class ViewCertificateHandler extends BaseCAHandler {
046        private final static Log log = LogFactory.getLog(ViewCertificateHandler.class);
047        public ViewCertificateHandler() {
048            super(VIEW_CERT_MODE, "/WEB-INF/view/ca/viewCertificate.jsp");
049        }
050    
051        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
052            String[] params = {ERROR_MSG, INFO_MSG, "sNo"};
053            for(int i = 0; i < params.length; ++i) {
054                String value = request.getParameter(params[i]);
055                if(value != null) response.setRenderParameter(params[i], value);
056            }
057            return getMode();
058        }
059    
060        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
061            String[] params = {ERROR_MSG, INFO_MSG};
062            for(int i = 0; i < params.length; ++i) {
063                String value = request.getParameter(params[i]);
064                if(value != null) request.setAttribute(params[i], value);
065            }
066            String errorMsg = request.getParameter(ERROR_MSG);
067            String sNo = request.getParameter("sNo");
068            try {
069                if(!request.getParameterMap().containsKey("sNo")) {
070                    // Show the page to get serial number of the certificate to be viewed
071                    request.setAttribute("sNo", null);
072                    return;
073                }
074                CertificationAuthority ca = getCertificationAuthority(request);
075                
076                String certText = ca.getCertificateBase64Text(new BigInteger(sNo.trim()));
077                Certificate cert = ca.getCertificate(new BigInteger(sNo.trim()));
078                PublicKey publickey = cert.getPublicKey();
079                String keySize = null;
080                if(publickey instanceof RSAPublicKey) {
081                    keySize = ""+((RSAPublicKey)publickey).getModulus().bitLength();
082                }
083                request.setAttribute("sNo", sNo);
084                request.setAttribute("cert", cert);
085                request.setAttribute("certText", certText);
086                request.setAttribute("keySize", keySize);
087                // Generate Certificate Fingerprints
088                Map fingerPrints = new HashMap();
089                fingerPrints.put("MD5", CertificateUtil.generateFingerprint(cert, "MD5"));
090                fingerPrints.put("SHA1", CertificateUtil.generateFingerprint(cert, "SHA1"));
091                request.setAttribute("fingerPrints", fingerPrints);
092                // Check if the certificate issue process started from "requests to be fulfilled" page.
093                // If so, provide a link to go back to that page
094                if("true".equalsIgnoreCase(request.getParameter("linkToListRequests")))
095                    request.setAttribute("linkToListRequests", Boolean.TRUE);
096            } catch (Exception e) {
097                errorMsg = e.toString();
098                log.error("Errors trying to view certificate with serial number '"+sNo+"'", e);
099            }
100            request.setAttribute(ERROR_MSG, errorMsg);
101        }
102    
103        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
104            return getMode()+BEFORE_ACTION;
105        }
106    }