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