KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.geronimo.management.geronimo.KeystoreException;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.PortletException;
27 import javax.portlet.RenderRequest;
28 import javax.portlet.RenderResponse;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * Handler for entering a password to unlock a key
33  *
34  * @version $Rev: 477279 $ $Date: 2006-11-20 13:42:26 -0500 (Mon, 20 Nov 2006) $
35  */

36 public class UnlockKeyHandler extends BaseKeystoreHandler {
37     private final static Log log = LogFactory.getLog(UnlockKeyHandler.class);
38     public UnlockKeyHandler() {
39         super(UNLOCK_KEY, "/WEB-INF/view/keystore/unlockKey.jsp");
40     }
41
42     public String JavaDoc actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
43         return getMode();
44     }
45
46     public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
47         String JavaDoc[] params = {ERROR_MSG, INFO_MSG};
48         for(int i = 0; i < params.length; ++i) {
49             String JavaDoc value = request.getParameter(params[i]);
50             if(value != null) request.setAttribute(params[i], value);
51         }
52         String JavaDoc keystore = request.getParameter("keystore");
53         String JavaDoc password = request.getParameter("password");
54         request.setAttribute("keystore", keystore);
55         request.setAttribute("password", password);
56         KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
57         try {
58             request.setAttribute("keys", data.getInstance().listPrivateKeys(password.toCharArray()));
59         } catch (KeystoreException e) {
60             throw new PortletException(e);
61         }
62     }
63
64     public String JavaDoc actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
65         String JavaDoc keystore = request.getParameter("keystore");
66         String JavaDoc password = request.getParameter("password");
67         String JavaDoc alias = request.getParameter("keyAlias");
68         String JavaDoc keyPassword = request.getParameter("keyPassword");
69         if(keystore == null || keystore.equals("")) {
70             return getMode(); // todo: this is bad; if there's no ID, then the form on the page is just not valid!
71
}
72         KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
73         try {
74             data.unlockPrivateKey(alias, keyPassword.toCharArray());
75         } catch (KeystoreException e) {
76             response.setRenderParameter("keystore", keystore);
77             response.setRenderParameter("password", password);
78             response.setRenderParameter(ERROR_MSG, "Unable to unlock key '"+alias+"'." + e);
79             log.error("Unable to unlock key '"+alias+"'.", e);
80             return getMode()+BEFORE_ACTION;
81         }
82         response.setRenderParameter(INFO_MSG, "Successfully unlocked key '"+alias+"' in keystore '"+keystore+"'.");
83         return LIST_MODE+BEFORE_ACTION;
84     }
85 }
86
Popular Tags