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.car;
018    
019    import java.net.URLEncoder;
020    
021    import org.apache.geronimo.kernel.Kernel;
022    import org.apache.geronimo.kernel.KernelRegistry;
023    import org.apache.geronimo.kernel.config.ConfigurationManager;
024    import org.apache.geronimo.kernel.config.ConfigurationStore;
025    import org.apache.geronimo.kernel.config.ConfigurationUtil;
026    import org.apache.geronimo.kernel.config.NoSuchConfigException;
027    import org.apache.geronimo.kernel.repository.Artifact;
028    
029    import javax.servlet.ServletException;
030    import javax.servlet.http.HttpServlet;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    import java.io.IOException;
034    
035    /**
036     * Servlet that lets you download a CAR from the repository
037     *
038     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039     */
040    public class CARExportServlet extends HttpServlet {
041        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
042            String configId = request.getParameter("configId");
043            if(configId == null) {
044                throw new ServletException("No configId specified for CAR download");
045            }
046            Artifact artifact = Artifact.create(configId);
047            Kernel kernel = KernelRegistry.getSingleKernel();
048            ConfigurationManager mgr = ConfigurationUtil.getConfigurationManager(kernel);
049            ConfigurationStore store = mgr.getStoreForConfiguration(artifact);
050            try {
051                response.setContentType("application/zip");
052                String filename = artifact.getArtifactId() + "-" + artifact.getVersion() + "." + artifact.getType();
053                response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
054                store.exportConfiguration(artifact, response.getOutputStream());
055            } catch (NoSuchConfigException e) {
056                throw new ServletException("No such configuration '"+configId+"'");
057            }
058        }
059    }