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     */
017    package org.apache.geronimo.console.keystores;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.apache.geronimo.console.MultiPageModel;
022    import org.apache.geronimo.management.geronimo.KeystoreException;
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    import java.io.IOException;
030    
031    /**
032     * Handler for entering a password to unlock a key
033     *
034     * @version $Rev: 477279 $ $Date: 2006-11-20 13:42:26 -0500 (Mon, 20 Nov 2006) $
035     */
036    public class UnlockKeyHandler extends BaseKeystoreHandler {
037        private final static Log log = LogFactory.getLog(UnlockKeyHandler.class);
038        public UnlockKeyHandler() {
039            super(UNLOCK_KEY, "/WEB-INF/view/keystore/unlockKey.jsp");
040        }
041    
042        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
043            return getMode();
044        }
045    
046        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
047            String[] params = {ERROR_MSG, INFO_MSG};
048            for(int i = 0; i < params.length; ++i) {
049                String value = request.getParameter(params[i]);
050                if(value != null) request.setAttribute(params[i], value);
051            }
052            String keystore = request.getParameter("keystore");
053            String password = request.getParameter("password");
054            request.setAttribute("keystore", keystore);
055            request.setAttribute("password", password);
056            KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
057            try {
058                request.setAttribute("keys", data.getInstance().listPrivateKeys(password.toCharArray()));
059            } catch (KeystoreException e) {
060                throw new PortletException(e);
061            }
062        }
063    
064        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
065            String keystore = request.getParameter("keystore");
066            String password = request.getParameter("password");
067            String alias = request.getParameter("keyAlias");
068            String keyPassword = request.getParameter("keyPassword");
069            if(keystore == null || keystore.equals("")) {
070                return getMode(); // todo: this is bad; if there's no ID, then the form on the page is just not valid!
071            }
072            KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
073            try {
074                data.unlockPrivateKey(alias, keyPassword.toCharArray());
075            } catch (KeystoreException e) {
076                response.setRenderParameter("keystore", keystore);
077                response.setRenderParameter("password", password);
078                response.setRenderParameter(ERROR_MSG, "Unable to unlock key '"+alias+"'." + e);
079                log.error("Unable to unlock key '"+alias+"'.", e);
080                return getMode()+BEFORE_ACTION;
081            }
082            response.setRenderParameter(INFO_MSG, "Successfully unlocked key '"+alias+"' in keystore '"+keystore+"'.");
083            return LIST_MODE+BEFORE_ACTION;
084        }
085    }