001: package org.drools.brms.server.files;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * 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:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021:
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.drools.brms.server.util.FormData;
027: import org.drools.compiler.DroolsParserException;
028: import org.drools.repository.RulesRepositoryException;
029:
030: /**
031: * This servlet deals with providing packages in binary form.
032: *
033: * @author Michael Neale
034: */
035: public class PackageDeploymentServlet extends RepositoryServlet {
036:
037: private static final long serialVersionUID = 400L;
038:
039: @Override
040: protected void doHead(HttpServletRequest request,
041: HttpServletResponse response) throws ServletException,
042: IOException {
043: if (request.getMethod().equals("HEAD")) {
044: PackageDeploymentURIHelper helper = new PackageDeploymentURIHelper(
045: request.getRequestURI());
046: FileManagerUtils fm = getFileManager();
047: long mod = fm.getLastModified(helper.getPackageName(),
048: helper.getVersion());
049: response.addHeader("lastModified", "" + mod);
050:
051: } else {
052: super .doHead(request, response);
053: }
054: }
055:
056: /**
057: * This is used for importing legacy DRL.
058: */
059: protected void doPost(HttpServletRequest request,
060: HttpServletResponse response) throws ServletException,
061: IOException {
062: FormData data = FileManagerUtils.getFormData(request);
063: //System.err.println("Filename: " + data.getFile().getName());
064:
065: try {
066: getFileManager().importClassicDRL(
067: data.getFile().getInputStream());
068: response.getWriter().write("OK");
069: } catch (DroolsParserException e) {
070: response.getWriter().write(
071: "Unable to process import: " + e.getMessage());
072: } catch (RulesRepositoryException e) {
073: response.getWriter().write(
074: "Unable to process import: " + e.getMessage());
075: }
076:
077: }
078:
079: /**
080: * Get the binary package.
081: * This will get the compiled package stuff from either the latest package,
082: * or a snapshot.
083: *
084: * The end of the URI is of the form:
085: * /<packageName>/(<snapshotVersionName> | LATEST)
086: *
087: * if you pass in "LATEST" it will get the latest (not a snapshot) if it exists.
088: * Normally that will only be used when downloading on demand, otherwise you should ONLY
089: * use a snapshot as they are always "up to date".
090: */
091: protected void doGet(HttpServletRequest req,
092: HttpServletResponse response) throws ServletException,
093: IOException {
094: PackageDeploymentURIHelper helper = new PackageDeploymentURIHelper(
095: req.getRequestURI());
096:
097: System.out.println("PackageName: " + helper.getPackageName());
098: System.out.println("PackageVersion: " + helper.getVersion());
099: System.out.println("PackageIsLatest: " + helper.isLatest());
100:
101: ByteArrayOutputStream out = new ByteArrayOutputStream();
102: String fileName = getFileManager().loadBinaryPackage(
103: helper.getPackageName(), helper.getVersion(),
104: helper.isLatest(), out);
105: response.setContentType("application/x-download");
106: response.setHeader("Content-Disposition",
107: "attachment; filename=" + fileName + ";");
108: response.setContentLength(out.size());
109: response.getOutputStream().write(out.toByteArray());
110: response.getOutputStream().flush();
111:
112: }
113:
114: }
|