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 allow editing of a keystore
033     *
034     * @version $Rev: 477134 $ $Date: 2006-11-20 05:19:42 -0500 (Mon, 20 Nov 2006) $
035     */
036    public class EditKeystoreHandler extends BaseKeystoreHandler {
037        private final static Log log = LogFactory.getLog(EditKeystoreHandler.class);
038        public EditKeystoreHandler() {
039            super(UNLOCK_KEYSTORE_FOR_EDITING, "/WEB-INF/view/keystore/unlockKeystore.jsp");
040        }
041    
042        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
043            String keystore = request.getParameter("keystore");
044            if(keystore != null) {
045                response.setRenderParameter("keystore", keystore);
046            } // else we hope this is after a failure and the actionAfterView took care of it below!
047            return getMode();
048        }
049    
050        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
051            String[] params = {ERROR_MSG, INFO_MSG};
052            for(int i = 0; i < params.length; ++i) {
053                String value = request.getParameter(params[i]);
054                if(value != null) request.setAttribute(params[i], value);
055            }
056            request.setAttribute("keystore", request.getParameter("keystore"));
057            request.setAttribute("mode", "unlockEdit");
058        }
059    
060        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
061            String keystore = request.getParameter("keystore");
062            String password = request.getParameter("password");
063            if(keystore == null || keystore.equals("")) {
064                return getMode(); // todo: this is bad; if there's no ID, then the form on the page is just not valid!
065            } else if(password == null) {
066                response.setRenderParameter("keystore", keystore);
067                return getMode();
068            }
069            KeystoreData data = ((KeystoreData) request.getPortletSession(true).getAttribute(KEYSTORE_DATA_PREFIX + keystore));
070            char[] storePass = password.toCharArray();
071            try {
072                data.unlockEdit(storePass);
073            } catch (KeystoreException e) {
074                response.setRenderParameter(ERROR_MSG, "Unable to unlock keystore "+keystore+" for editing. "+e.toString());
075                log.error("Unable to unlock keystore "+keystore+" for editing.", e);
076                return getMode()+BEFORE_ACTION;
077            }
078            response.setRenderParameter(INFO_MSG, "Keystore "+keystore+" successfully unlocked for editing.");
079            return LIST_MODE+BEFORE_ACTION;
080        }
081    }