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.text.DateFormat;
023    import java.text.SimpleDateFormat;
024    import java.util.Calendar;
025    import java.util.Date;
026    import java.util.GregorianCalendar;
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    
039    /**
040     * Handler for CSR details screen.
041     *
042     * @version $Rev: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $
043     */
044    public class CertReqDetailsHandler extends BaseCAHandler {
045        private final static Log log = LogFactory.getLog(CertReqDetailsHandler.class);
046        public CertReqDetailsHandler() {
047            super(CERT_REQ_DETAILS_MODE, "/WEB-INF/view/ca/certReqDetails.jsp");
048        }
049    
050        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
051            String[] params = {ERROR_MSG, INFO_MSG, "algorithm", "sNo", "validFrom", "validTo", "pkcs10certreq", "subject", "publickey", "requestId"};
052            for(int i = 0; i < params.length; ++i) {
053                String value = request.getParameter(params[i]);
054                if(value != null) response.setRenderParameter(params[i], value);
055            }
056            String sNo = request.getParameter("sNo");
057            if(sNo == null) {
058                // Freshly loading the certificate request details screen
059                CertificationAuthority ca = getCertificationAuthority(request);
060                try {
061                    sNo = ca.getNextSerialNumber().toString();
062                    response.setRenderParameter("sNo", sNo);
063                } catch (Exception e) {
064                    log.error("Unable to get next serial number from CA.", e);
065                    response.setRenderParameter(ERROR_MSG, e.toString());
066                }
067            }
068            return getMode();
069        }
070    
071        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
072            String[] params = {ERROR_MSG, INFO_MSG, "subject", "publickey", "sNo", "validFrom", "validTo", "algorithm", "pkcs10certreq", "requestId"};
073            for(int i = 0; i < params.length; ++i) {
074                Object value = request.getParameter(params[i]);
075                if(value != null) request.setAttribute(params[i], value);
076            }
077        }
078    
079        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
080            String errorMsg = null;
081    
082            try {
083                // Validate the Serial Number
084                String sNo = request.getParameter("sNo");
085                new BigInteger(sNo.trim());
086                
087                // Validate the from and to dates
088                String validFrom = request.getParameter("validFrom");
089                String validTo = request.getParameter("validTo");
090                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
091                // Check if the from date format is MM/DD/YYYY
092                Date validFromDate = df.parse(validFrom);
093                Calendar calendar = new GregorianCalendar();
094                calendar.setTime(validFromDate);
095                String mmddyyyy = (calendar.get(Calendar.MONTH) < 9 ? "0":"") + (calendar.get(Calendar.MONTH)+1);
096                mmddyyyy += "/"+(calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0":"") + (calendar.get(Calendar.DAY_OF_MONTH));
097                mmddyyyy += "/"+calendar.get(Calendar.YEAR);
098                if(!mmddyyyy.equals(validFrom)) {
099                    throw new Exception("validFrom must be a date in MM/DD/YYYY format.");
100                }
101                // Check if the to date format is MM/DD/YYYY
102                Date validToDate = df.parse(validTo);
103                calendar.setTime(validToDate);
104                mmddyyyy = (calendar.get(Calendar.MONTH) < 9 ? "0":"") + (calendar.get(Calendar.MONTH)+1);
105                mmddyyyy += "/"+(calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0":"") + (calendar.get(Calendar.DAY_OF_MONTH));
106                mmddyyyy += "/"+calendar.get(Calendar.YEAR);
107                if(!mmddyyyy.equals(validTo)) {
108                    throw new Exception("validTo must be a date in MM/DD/YYYY format.");
109                }
110                // Check if the from date is before the to date
111                if(validFromDate.after(validToDate)) {
112                    throw new Exception("Validity: From date '"+validFrom+"' is before the To date '"+validTo+"'.");
113                }
114                
115                // Go to client certificate confirmation page
116                return CONFIRM_CLIENT_CERT_MODE+BEFORE_ACTION;
117            } catch(Exception e) {
118                errorMsg = e.toString();
119                log.error("Errors in user input while processing a CSR.", e);
120            }
121            
122            if(errorMsg != null) response.setRenderParameter(ERROR_MSG, errorMsg);
123            return getMode()+BEFORE_ACTION;
124        }
125    }