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: */package org.apache.geronimo.console.ca;
018:
019: import java.io.IOException;
020: import java.math.BigInteger;
021: import java.text.DateFormat;
022: import java.text.SimpleDateFormat;
023: import java.util.Calendar;
024: import java.util.Date;
025: import java.util.GregorianCalendar;
026:
027: import javax.portlet.ActionRequest;
028: import javax.portlet.ActionResponse;
029: import javax.portlet.PortletException;
030: import javax.portlet.RenderRequest;
031: import javax.portlet.RenderResponse;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.geronimo.console.MultiPageModel;
036:
037: /**
038: * Handler for Setup CA screen to get CA details from user.
039: *
040: * @version $Rev: 514091 $ $Date: 2007-03-02 22:26:39 -0800 (Fri, 02 Mar 2007) $
041: */
042: public class SetupCAHandler extends BaseCAHandler {
043: private final static Log log = LogFactory
044: .getLog(SetupCAHandler.class);
045:
046: public SetupCAHandler() {
047: super (SETUPCA_MODE, "/WEB-INF/view/ca/setupCA.jsp");
048: }
049:
050: public String actionBeforeView(ActionRequest request,
051: ActionResponse response, MultiPageModel model)
052: throws PortletException, IOException {
053: String[] params = { ERROR_MSG, INFO_MSG, "caCN", "caOU", "caO",
054: "caL", "caST", "caC", "alias", "keyAlgorithm",
055: "keySize", "algorithm", "validFrom", "validTo", "sNo",
056: "password" };
057: for (int i = 0; i < params.length; ++i) {
058: String value = request.getParameter(params[i]);
059: if (value != null)
060: response.setRenderParameter(params[i], value);
061: }
062: return getMode();
063: }
064:
065: public void renderView(RenderRequest request,
066: RenderResponse response, MultiPageModel model)
067: throws PortletException, IOException {
068: String[] params = { ERROR_MSG, INFO_MSG, "caCN", "caOU", "caO",
069: "caL", "caST", "caC", "alias", "keyAlgorithm",
070: "keySize", "algorithm", "validFrom", "validTo", "sNo",
071: "password" };
072: for (int i = 0; i < params.length; ++i) {
073: Object value = request.getParameter(params[i]);
074: if (value != null)
075: request.setAttribute(params[i], value);
076: }
077: }
078:
079: public String actionAfterView(ActionRequest request,
080: ActionResponse response, MultiPageModel model)
081: throws PortletException, IOException {
082: String errorMsg = null;
083: try {
084: // Validate the Serial Number
085: String sNo = request.getParameter("sNo");
086: new BigInteger(sNo.trim());
087:
088: // Validate the from and to dates
089: String validFrom = request.getParameter("validFrom");
090: String validTo = request.getParameter("validTo");
091: // Check if the from date format is MM/DD/YYYY
092: DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
093: Date validFromDate = df.parse(validFrom);
094: Calendar calendar = new GregorianCalendar();
095: calendar.setTime(validFromDate);
096: String mmddyyyy = (calendar.get(Calendar.MONTH) < 9 ? "0"
097: : "")
098: + (calendar.get(Calendar.MONTH) + 1);
099: mmddyyyy += "/"
100: + (calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0"
101: : "")
102: + (calendar.get(Calendar.DAY_OF_MONTH));
103: mmddyyyy += "/" + calendar.get(Calendar.YEAR);
104: if (!mmddyyyy.equals(validFrom)) {
105: throw new Exception(
106: "validFrom must be a date in MM/DD/YYYY format.");
107: }
108: // Check if the to date format is MM/DD/YYYY
109: Date validToDate = df.parse(validTo);
110: calendar.setTime(validToDate);
111: mmddyyyy = (calendar.get(Calendar.MONTH) < 9 ? "0" : "")
112: + (calendar.get(Calendar.MONTH) + 1);
113: mmddyyyy += "/"
114: + (calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0"
115: : "")
116: + (calendar.get(Calendar.DAY_OF_MONTH));
117: mmddyyyy += "/" + calendar.get(Calendar.YEAR);
118: if (!mmddyyyy.equals(validTo)) {
119: throw new Exception(
120: "validTo must be a date in MM/DD/YYYY format.");
121: }
122: // Check if the from date is before the to date
123: if (validFromDate.after(validToDate)) {
124: throw new Exception("Validity: From date '" + validFrom
125: + "' is before the To date '" + validTo + "'.");
126: }
127:
128: // Load page to confirm CA details
129: return CONFIRM_CA_MODE + BEFORE_ACTION;
130: } catch (Exception e) {
131: errorMsg = e.toString();
132: log.error("Error in user input during CA Setup.", e);
133: }
134: if (errorMsg != null)
135: response.setRenderParameter(ERROR_MSG, errorMsg);
136: return getMode() + BEFORE_ACTION;
137: }
138:
139: }
|