KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > ca > CertReqDetailsHandler


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.console.ca;
19
20 import java.io.IOException JavaDoc;
21 import java.math.BigInteger JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.GregorianCalendar JavaDoc;
27
28 import javax.portlet.ActionRequest;
29 import javax.portlet.ActionResponse;
30 import javax.portlet.PortletException;
31 import javax.portlet.RenderRequest;
32 import javax.portlet.RenderResponse;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.geronimo.console.MultiPageModel;
37 import org.apache.geronimo.management.geronimo.CertificationAuthority;
38
39 /**
40  * Handler for CSR details screen.
41  *
42  * @version $Rev: 476291 $ $Date: 2006-11-17 15:05:24 -0500 (Fri, 17 Nov 2006) $
43  */

44 public class CertReqDetailsHandler extends BaseCAHandler {
45     private final static Log log = LogFactory.getLog(CertReqDetailsHandler.class);
46     public CertReqDetailsHandler() {
47         super(CERT_REQ_DETAILS_MODE, "/WEB-INF/view/ca/certReqDetails.jsp");
48     }
49
50     public String JavaDoc actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
51         String JavaDoc[] params = {ERROR_MSG, INFO_MSG, "algorithm", "sNo", "validFrom", "validTo", "pkcs10certreq", "subject", "publickey", "requestId"};
52         for(int i = 0; i < params.length; ++i) {
53             String JavaDoc value = request.getParameter(params[i]);
54             if(value != null) response.setRenderParameter(params[i], value);
55         }
56         String JavaDoc sNo = request.getParameter("sNo");
57         if(sNo == null) {
58             // Freshly loading the certificate request details screen
59
CertificationAuthority ca = getCertificationAuthority(request);
60             try {
61                 sNo = ca.getNextSerialNumber().toString();
62                 response.setRenderParameter("sNo", sNo);
63             } catch (Exception JavaDoc e) {
64                 log.error("Unable to get next serial number from CA.", e);
65                 response.setRenderParameter(ERROR_MSG, e.toString());
66             }
67         }
68         return getMode();
69     }
70
71     public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
72         String JavaDoc[] params = {ERROR_MSG, INFO_MSG, "subject", "publickey", "sNo", "validFrom", "validTo", "algorithm", "pkcs10certreq", "requestId"};
73         for(int i = 0; i < params.length; ++i) {
74             Object JavaDoc value = request.getParameter(params[i]);
75             if(value != null) request.setAttribute(params[i], value);
76         }
77     }
78
79     public String JavaDoc actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
80         String JavaDoc errorMsg = null;
81
82         try {
83             // Validate the Serial Number
84
String JavaDoc sNo = request.getParameter("sNo");
85             new BigInteger JavaDoc(sNo.trim());
86             
87             // Validate the from and to dates
88
String JavaDoc validFrom = request.getParameter("validFrom");
89             String JavaDoc validTo = request.getParameter("validTo");
90             DateFormat JavaDoc df = new SimpleDateFormat JavaDoc("MM/dd/yyyy");
91             // Check if the from date format is MM/DD/YYYY
92
Date JavaDoc validFromDate = df.parse(validFrom);
93             Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
94             calendar.setTime(validFromDate);
95             String JavaDoc mmddyyyy = (calendar.get(Calendar.MONTH) < 9 ? "0":"") + (calendar.get(Calendar.MONTH)+1);
96             mmddyyyy += "/"+(calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0":"") + (calendar.get(Calendar.DAY_OF_MONTH));
97             mmddyyyy += "/"+calendar.get(Calendar.YEAR);
98             if(!mmddyyyy.equals(validFrom)) {
99                 throw new Exception JavaDoc("validFrom must be a date in MM/DD/YYYY format.");
100             }
101             // Check if the to date format is MM/DD/YYYY
102
Date JavaDoc 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 JavaDoc("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 JavaDoc("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 JavaDoc 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 }
126
Popular Tags