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    
038    /**
039     * Handler for Setup CA screen to get CA details from user.
040     *
041     * @version $Rev: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $
042     */
043    public class SetupCAHandler extends BaseCAHandler {
044        private final static Log log = LogFactory.getLog(SetupCAHandler.class);
045        public SetupCAHandler() {
046            super(SETUPCA_MODE, "/WEB-INF/view/ca/setupCA.jsp");
047        }
048    
049        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
050            String[] params = {ERROR_MSG, INFO_MSG, "caCN", "caOU", "caO", "caL", "caST", "caC", "alias", "keyAlgorithm", "keySize", "algorithm", "validFrom", "validTo", "sNo", "password"};
051            for(int i = 0; i < params.length; ++i) {
052                String value = request.getParameter(params[i]);
053                if(value != null) response.setRenderParameter(params[i], value);
054            }
055            return getMode();
056        }
057    
058        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
059            String[] params = {ERROR_MSG, INFO_MSG, "caCN", "caOU", "caO", "caL", "caST", "caC", "alias", "keyAlgorithm", "keySize", "algorithm", "validFrom", "validTo", "sNo", "password"};
060            for(int i = 0; i < params.length; ++i) {
061                Object value = request.getParameter(params[i]);
062                if(value != null) request.setAttribute(params[i], value);
063            }
064        }
065    
066        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
067            String errorMsg = null;
068            try {
069                // Validate the Serial Number
070                String sNo = request.getParameter("sNo");
071                new BigInteger(sNo.trim());
072                
073                // Validate the from and to dates
074                String validFrom = request.getParameter("validFrom");
075                String validTo = request.getParameter("validTo");
076                // Check if the from date format is MM/DD/YYYY
077                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
078                Date validFromDate = df.parse(validFrom);
079                Calendar calendar = new GregorianCalendar();
080                calendar.setTime(validFromDate);
081                String mmddyyyy = (calendar.get(Calendar.MONTH) < 9 ? "0":"") + (calendar.get(Calendar.MONTH)+1);
082                mmddyyyy += "/"+(calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0":"") + (calendar.get(Calendar.DAY_OF_MONTH));
083                mmddyyyy += "/"+calendar.get(Calendar.YEAR);
084                if(!mmddyyyy.equals(validFrom)) {
085                    throw new Exception("validFrom must be a date in MM/DD/YYYY format.");
086                }
087                // Check if the to date format is MM/DD/YYYY
088                Date validToDate = df.parse(validTo);
089                calendar.setTime(validToDate);
090                mmddyyyy = (calendar.get(Calendar.MONTH) < 9 ? "0":"") + (calendar.get(Calendar.MONTH)+1);
091                mmddyyyy += "/"+(calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0":"") + (calendar.get(Calendar.DAY_OF_MONTH));
092                mmddyyyy += "/"+calendar.get(Calendar.YEAR);
093                 if(!mmddyyyy.equals(validTo)) {
094                    throw new Exception("validTo must be a date in MM/DD/YYYY format.");
095                }
096                // Check if the from date is before the to date
097                if(validFromDate.after(validToDate)) {
098                    throw new Exception("Validity: From date '"+validFrom+"' is before the To date '"+validTo+"'.");
099                }
100    
101                // Load page to confirm CA details
102                return CONFIRM_CA_MODE+BEFORE_ACTION;
103            } catch(Exception e) {
104                errorMsg = e.toString();
105                log.error("Error in user input during CA Setup.", e);
106            }
107            if(errorMsg != null) response.setRenderParameter(ERROR_MSG, errorMsg);
108            return getMode()+BEFORE_ACTION;
109        }
110    
111    }