001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.keystores;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.console.MultiPageModel;
021: import org.apache.geronimo.management.geronimo.KeystoreException;
022: import org.apache.geronimo.crypto.CertificateUtil;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletException;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029:
030: import java.io.ByteArrayInputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.security.NoSuchAlgorithmException;
034: import java.security.cert.CertificateException;
035: import java.security.cert.CertificateFactory;
036: import java.security.cert.X509Certificate;
037: import java.text.SimpleDateFormat;
038: import java.util.Collection;
039:
040: /**
041: * Handler for entering a password to unlock a keystore
042: *
043: * @version $Rev: 617588 $ $Date: 2008-02-01 10:20:07 -0800 (Fri, 01 Feb 2008) $
044: */
045: public class ConfirmCertificateHandler extends BaseKeystoreHandler {
046: private final static Log log = LogFactory
047: .getLog(ConfirmCertificateHandler.class);
048:
049: public ConfirmCertificateHandler() {
050: super (CONFIRM_CERTIFICATE,
051: "/WEB-INF/view/keystore/confirmCertificate.jsp");
052: }
053:
054: public String actionBeforeView(ActionRequest request,
055: ActionResponse response, MultiPageModel model)
056: throws PortletException, IOException {
057: return getMode();
058: }
059:
060: public void renderView(RenderRequest request,
061: RenderResponse response, MultiPageModel model)
062: throws PortletException, IOException {
063: SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
064: request.setAttribute("id", request.getParameter("id"));
065: request.setAttribute("alias", request.getParameter("alias"));
066: /* // Uploading certificate using a disk file fails on Windows. Certificate text is used instead.
067: String certFile = request.getParameter("certificate");
068: request.setAttribute("certificate", certFile);
069: InputStream is = new FileInputStream(certFile);
070: */
071: String certificate = request.getParameter("certificate");
072: request.setAttribute("certificate", certificate);
073: InputStream is = new ByteArrayInputStream(certificate
074: .getBytes());
075: try {
076: CertificateFactory cf = CertificateFactory
077: .getInstance("X.509");
078: Collection certificates = cf.generateCertificates(is);
079: X509Certificate cert = (X509Certificate) certificates
080: .iterator().next();
081: request.setAttribute("fingerprint", CertificateUtil
082: .generateFingerprint(cert, "MD5"));
083: request
084: .setAttribute("issuer", cert.getIssuerDN()
085: .getName());
086: request.setAttribute("subject", cert.getSubjectDN()
087: .getName());
088: request.setAttribute("serial", cert.getSerialNumber());
089: request.setAttribute("validStart", sdf.format(cert
090: .getNotBefore()));
091: request.setAttribute("validEnd", sdf.format(cert
092: .getNotAfter()));
093: } catch (CertificateException e) {
094: log.error("Unable to process uploaded certificate", e);
095: } catch (NoSuchAlgorithmException e) {
096: log.error("Unable to process uploaded certificate", e);
097: }
098: }
099:
100: public String actionAfterView(ActionRequest request,
101: ActionResponse response, MultiPageModel model)
102: throws PortletException, IOException {
103: String id = request.getParameter("id");
104: String alias = request.getParameter("alias");
105: String certificate = request.getParameter("certificate");
106: if (id == null || id.equals("") || alias == null
107: || alias.equals("") || certificate == null
108: || certificate.equals("")) {
109: return LIST_MODE + BEFORE_ACTION; //todo: better handling
110: }
111: KeystoreData data = ((KeystoreData) request.getPortletSession(
112: true).getAttribute(KEYSTORE_DATA_PREFIX + id));
113: try {
114: data.importTrustCert(certificate, alias);
115: } catch (KeystoreException e) {
116: throw new PortletException(e);
117: }
118: response.setRenderParameter("id", id);
119: return VIEW_KEYSTORE + BEFORE_ACTION;
120: }
121: }
|