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:
022: import javax.portlet.ActionRequest;
023: import javax.portlet.ActionResponse;
024: import javax.portlet.PortletException;
025: import javax.portlet.RenderRequest;
026: import javax.portlet.RenderResponse;
027: import java.io.IOException;
028:
029: /**
030: * Handler for entering a password to unlock a keystore
031: *
032: * @version $Rev: 477279 $ $Date: 2006-11-20 10:42:26 -0800 (Mon, 20 Nov 2006) $
033: */
034: public class UnlockKeystoreHandler extends BaseKeystoreHandler {
035: private final static Log log = LogFactory
036: .getLog(UnlockKeystoreHandler.class);
037:
038: public UnlockKeystoreHandler() {
039: super (UNLOCK_KEYSTORE_FOR_USAGE,
040: "/WEB-INF/view/keystore/unlockKeystore.jsp");
041: }
042:
043: public String actionBeforeView(ActionRequest request,
044: ActionResponse response, MultiPageModel model)
045: throws PortletException, IOException {
046: String keystore = request.getParameter("keystore");
047: if (keystore != null) {
048: response.setRenderParameter("keystore", keystore);
049: } // else we hope this is after a failure and the actionAfterView took care of it below!
050: return getMode();
051: }
052:
053: public void renderView(RenderRequest request,
054: RenderResponse response, MultiPageModel model)
055: throws PortletException, IOException {
056: String[] params = { ERROR_MSG, INFO_MSG };
057: for (int i = 0; i < params.length; ++i) {
058: String value = request.getParameter(params[i]);
059: if (value != null)
060: request.setAttribute(params[i], value);
061: }
062: String keystore = request.getParameter("keystore");
063: request.setAttribute("keystore", keystore);
064: request.setAttribute("mode", "unlockKeystore");
065: KeystoreData data = ((KeystoreData) request.getPortletSession(
066: true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
067: request.setAttribute("keys", data.getKeys());
068: }
069:
070: public String actionAfterView(ActionRequest request,
071: ActionResponse response, MultiPageModel model)
072: throws PortletException, IOException {
073: String keystore = request.getParameter("keystore");
074: String password = request.getParameter("password");
075: String alias = request.getParameter("keyAlias");
076: String keyPassword = request.getParameter("keyPassword");
077: if (keystore == null || keystore.equals("")) {
078: return getMode(); // todo: this is bad; if there's no ID, then the form on the page is just not valid!
079: } else if (password == null) {
080: response.setRenderParameter("keystore", keystore);
081: return getMode();
082: }
083: KeystoreData data = ((KeystoreData) request.getPortletSession(
084: true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
085: char[] storePass = password.toCharArray();
086: try {
087: data.unlockUse(storePass);
088: if (data.getKeys() != null && data.getKeys().length > 0) {
089: // if it's unlocked for editing and has keys
090: data.unlockPrivateKey(alias, keyPassword.toCharArray());
091: } else if (data.getInstance().listPrivateKeys(storePass) != null
092: && data.getInstance().listPrivateKeys(storePass).length > 0) {
093: // if it's locked for editing but has keys
094: response.setRenderParameter("keystore", keystore);
095: response.setRenderParameter("password", password);
096: return UNLOCK_KEY + BEFORE_ACTION;
097: } // otherwise it has no keys
098: } catch (Exception e) {
099: response.setRenderParameter(ERROR_MSG,
100: "Unable to unlock keystore '" + keystore
101: + "' for availability. " + e.toString());
102: log.error("Unable to unlock keystore '" + keystore
103: + "' for availability.", e);
104: return getMode() + BEFORE_ACTION;
105: }
106: response.setRenderParameter(INFO_MSG,
107: "Successfully unlocked keystore '" + keystore
108: + "' for availability.");
109: return LIST_MODE + BEFORE_ACTION;
110: }
111: }
|