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