001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.console.ca;
019    
020    import java.io.IOException;
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    
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    import org.apache.geronimo.console.MultiPageModel;
031    import org.apache.geronimo.management.geronimo.CertificationAuthority;
032    
033    /**
034     * Handler for unlock CA screen.
035     *
036     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
037     */
038    public class UnlockCAHandler extends BaseCAHandler {
039        private final static Log log = LogFactory.getLog(UnlockCAHandler.class);
040        public UnlockCAHandler() {
041            super(UNLOCKCA_MODE, "/WEB-INF/view/ca/unlockCA.jsp");
042        }
043    
044        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
045            String[] params = {ERROR_MSG, INFO_MSG};
046            for(int i = 0; i < params.length; ++i) {
047                String value = request.getParameter(params[i]);
048                if(value != null) response.setRenderParameter(params[i], value);
049            }
050            return getMode();
051        }
052    
053        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
054            String[] params = {ERROR_MSG, INFO_MSG};
055            for(int i = 0; i < params.length; ++i) {
056                Object value = request.getParameter(params[i]);
057                if(value != null) request.setAttribute(params[i], value);
058            }
059        }
060    
061        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
062            String errorMsg = null;
063            try {
064                String password = request.getParameter("password");
065                if(password == null) {
066                    throw new Exception("Password is null.");
067                }
068                CertificationAuthority ca = getCertificationAuthority(request);
069                if(ca == null) {
070                    throw new Exception("CA is not running.  CA may not have been initialized.");
071                }
072                ca.unlock(password.toCharArray());
073    
074                // Return to CA's index page
075                response.setRenderParameter(INFO_MSG, "CA has been unlocked successfully!");
076                log.info("CA has been unlocked successfully!");
077                return INDEX_MODE+BEFORE_ACTION;
078            } catch(Exception e) {
079                errorMsg = e.toString();
080                log.error("Errors in unlocking CA.", e);
081            }
082            // An error occurred.  Set the error message and load the page again.
083            response.setRenderParameter(ERROR_MSG, errorMsg);
084            return getMode()+BEFORE_ACTION;
085        }
086    }