01: package org.drools.brms.server.files;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.PrintWriter;
22:
23: import javax.jcr.PathNotFoundException;
24: import javax.jcr.RepositoryException;
25: import javax.servlet.ServletException;
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28:
29: import org.drools.brms.server.util.FormData;
30: import org.drools.repository.RulesRepository;
31:
32: /**
33: *
34: * This servlet deals with import and export of the repository to XML/zip files.
35: *
36: * @author Michael Neale
37: * @author Fernando Meyer
38: */
39: public class RepositoryBackupServlet extends RepositoryServlet {
40:
41: private static final long serialVersionUID = 400L;
42: final FileManagerUtils uploadHelper = new FileManagerUtils();
43:
44: /**
45: * This accepts a repository, and will apply it.
46: */
47: protected void doPost(HttpServletRequest request,
48: HttpServletResponse response) throws ServletException,
49: IOException {
50: response.setContentType("text/plain");
51: FormData uploadItem = FileManagerUtils.getFormData(request);
52: response.getWriter().write(
53: processImportRepository(uploadItem.getFile()
54: .getInputStream()));
55: }
56:
57: /**
58: * Explore the repo, provide a download
59: */
60: protected void doGet(HttpServletRequest req, HttpServletResponse res)
61: throws ServletException, IOException {
62: try {
63: processExportRepositoryDownload(res);
64: } catch (Exception e) {
65: e.printStackTrace(new PrintWriter(res.getOutputStream()));
66: }
67: }
68:
69: private void processExportRepositoryDownload(HttpServletResponse res)
70: throws PathNotFoundException, IOException,
71: RepositoryException {
72: res.setContentType("application/zip");
73: res.setHeader("Content-Disposition",
74: "inline; filename=repository_export.zip;");
75:
76: res.getOutputStream().write(
77: getFileManager().exportRulesRepository());
78: res.getOutputStream().flush();
79: }
80:
81: private String processImportRepository(InputStream file)
82: throws IOException {
83: byte[] byteArray = new byte[file.available()];
84: file.read(byteArray);
85: getFileManager().importRulesRepository(byteArray);
86: return "OK";
87: }
88:
89: }
|