KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > keystores > UnlockKeystoreHandler


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

17 package org.apache.geronimo.console.keystores;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.geronimo.console.MultiPageModel;
22
23 import javax.portlet.ActionRequest;
24 import javax.portlet.ActionResponse;
25 import javax.portlet.PortletException;
26 import javax.portlet.RenderRequest;
27 import javax.portlet.RenderResponse;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Handler for entering a password to unlock a keystore
32  *
33  * @version $Rev: 477279 $ $Date: 2006-11-20 13:42:26 -0500 (Mon, 20 Nov 2006) $
34  */

35 public class UnlockKeystoreHandler extends BaseKeystoreHandler {
36     private final static Log log = LogFactory.getLog(UnlockKeystoreHandler.class);
37     public UnlockKeystoreHandler() {
38         super(UNLOCK_KEYSTORE_FOR_USAGE, "/WEB-INF/view/keystore/unlockKeystore.jsp");
39     }
40
41     public String JavaDoc actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
42         String JavaDoc keystore = request.getParameter("keystore");
43         if(keystore != null) {
44             response.setRenderParameter("keystore", keystore);
45         } // else we hope this is after a failure and the actionAfterView took care of it below!
46
return getMode();
47     }
48
49     public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
50         String JavaDoc[] params = {ERROR_MSG, INFO_MSG};
51         for(int i = 0; i < params.length; ++i) {
52             String JavaDoc value = request.getParameter(params[i]);
53             if(value != null) request.setAttribute(params[i], value);
54         }
55         String JavaDoc keystore = request.getParameter("keystore");
56         request.setAttribute("keystore", keystore);
57         request.setAttribute("mode", "unlockKeystore");
58         KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
59         request.setAttribute("keys", data.getKeys());
60     }
61
62     public String JavaDoc actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
63         String JavaDoc keystore = request.getParameter("keystore");
64         String JavaDoc password = request.getParameter("password");
65         String JavaDoc alias = request.getParameter("keyAlias");
66         String JavaDoc keyPassword = request.getParameter("keyPassword");
67         if(keystore == null || keystore.equals("")) {
68             return getMode(); // todo: this is bad; if there's no ID, then the form on the page is just not valid!
69
} else if(password == null) {
70             response.setRenderParameter("keystore", keystore);
71             return getMode();
72         }
73         KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
74         char[] storePass = password.toCharArray();
75         try {
76             data.unlockUse(storePass);
77             if(data.getKeys() != null && data.getKeys().length > 0) {
78                 // if it's unlocked for editing and has keys
79
data.unlockPrivateKey(alias, keyPassword.toCharArray());
80             } else if (data.getInstance().listPrivateKeys(storePass) != null && data.getInstance().listPrivateKeys(storePass).length > 0) {
81                 // if it's locked for editing but has keys
82
response.setRenderParameter("keystore", keystore);
83                 response.setRenderParameter("password", password);
84                 return UNLOCK_KEY+BEFORE_ACTION;
85             } // otherwise it has no keys
86
} catch (Exception JavaDoc e) {
87             response.setRenderParameter(ERROR_MSG, "Unable to unlock keystore '"+keystore+"' for availability. "+e.toString());
88             log.error("Unable to unlock keystore '"+keystore+"' for availability.", e);
89             return getMode()+BEFORE_ACTION;
90         }
91         response.setRenderParameter(INFO_MSG, "Successfully unlocked keystore '"+keystore+"' for availability.");
92         return LIST_MODE+BEFORE_ACTION;
93     }
94 }
95
Popular Tags