Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.guvnor.server.files;
 
 
 import java.util.List;
This servlet deals with import and export of the repository to XML/zip files.
 
 public class RepositoryBackupServlet extends RepositoryServlet {
 
     private static final LoggingHelper log = LoggingHelper.getLogger(RepositoryBackupServlet.class);
     private static final long serialVersionUID = 510l;
 
     private static final List<StringzipMimeTypes = new ArrayList<String>();
 
     @Inject
 
     static {
         .add("application/zip");
         .add("application/x-compress");
         .add("application/x-compressed");
         .add("application/x-zip");
         .add("application/x-zip-compressed");
         .add("application/zip-compressed");
         .add("application/x-7zip-compressed");
 
         //Firefox maps .zip file extensions to this in /mimeTypes.rdf
         .add("application/x-sdlc");
     }

    
This accepts a repository, and will apply it.
 
     protected void doPost(final HttpServletRequest request,
                           final HttpServletResponse responsethrows ServletException,
             IOException {
 
         doAuthorizedAction(request,
                 response,
                 new Command() {
                     public void execute() throws Exception {
 
                         String repoConfig = request.getParameter("repoConfig");
 
                         if (repoConfig != null) {
                             processExportRepoConfig(response,
                                     repoConfig);
                         } else {
                             response.setContentType("text/html");
                             FormData uploadItem = FileManagerService.getFormData(request);
 
                             String packageImport = request.getParameter("packageImport");
 
                             InputStream is = uploadItem.getFile().getInputStream();
                             if (isFileZipped(uploadItem.getFile())) {
                                 ZipInputStream zipInputStream = new ZipInputStream(is);
                                 ZipEntry zipEntry = zipInputStream.getNextEntry();
                                 if (zipEntry != null) {
                                     is = zipInputStream;
                                 } else {
                                     throw new RuntimeDroolsException("Invalid compressed repository");
                                 }
                             }
 
                             if ("true".equals(packageImport)) {
                                boolean importAsNew = "true".equals(request.getParameter("importAsNew"));
                                response.getWriter().write(processImportPackage(is,
                                        importAsNew));
                            } else {
                                response.getWriter().write(processImportRepository(is));
                            }
                            is.close();
                        }
                    }
                });
    }
    private boolean isFileZipped(FileItem filethrows IOException {
        String mimeType = file.getContentType().toLowerCase();
        return .contains(mimeType);
    }

    
Explore the repo, provide a download
    protected void doGet(final HttpServletRequest req,
                         final HttpServletResponse res)
            throws ServletException,
            IOException {
        doAuthorizedAction(req,
                res,
                new Command() {
                    public void execute() throws Exception {
                        try {
                            String packageName = req.getParameter("packageName");
                            if (packageName == null) {
                                processExportRepositoryDownload(res);
                            } else {
                                if(.isPackageExist(packageName)) {
                                    processExportPackageFromRepositoryDownload(res,
                                        packageName);
                                } else {
                                    res.setContentType("text/plain");
                                    res.setStatus(500);
                                    res.getWriter().write("Package [" + packageName + "] does not exist");
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace(new PrintWriter(res.getOutputStream()));
                        }
                    }
                });
    }
    private void processExportRepoConfig(HttpServletResponse res,
                                         String repoConfig)
            throws IOException {
        .debug("Exporting Repository Config...");
        res.setContentType("application/x-download");
        res.setHeader("Content-Disposition",
                "attachment; filename=repository.xml;");
        .debug("Starting to process repository configuration");
        res.getOutputStream().write(repoConfig.getBytes());
        res.getOutputStream().flush();
        .debug("Done exporting repository config!");
    }
            throws PathNotFoundException,
            IOException,
            RepositoryException {
        .debug("Exporting...");
        res.setContentType("application/zip");
        res.setHeader("Content-Disposition",
                "attachment; filename=repository_export.zip;");
        .debug("Starting to process export");
        ZipOutputStream zout = new ZipOutputStream(res.getOutputStream());
        zout.putNextEntry(new ZipEntry("repository_export.xml"));
        zout.closeEntry();
        zout.finish();
        res.getOutputStream().flush();
        .debug("Done exporting!");
    }
            HttpServletResponse res,
            String packageName)
            throws PathNotFoundException,
            IOException,
            RepositoryException {
        res.setContentType("application/zip");
        res.setHeader("Content-Disposition",
                "inline; filename=" + packageName
                        + ".zip;");
        res.getOutputStream().write(
                .exportPackageFromRepository(packageName));
        res.getOutputStream().flush();
    }
    private String processImportRepository(InputStream filethrows IOException {
        return "OK";
    }
    private String processImportPackage(InputStream file,
                                        boolean importAsNew)
            throws IOException {
        byte[] byteArray = new byte[file.available()];
        file.read(byteArray);
                importAsNew);
        return "OK";
    }
New to GrepCode? Check out our FAQ X