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.client.common.HTMLFileManagerFields;
027: import org.drools.brms.server.util.FormData;
028:
029: /**
030: * This is for dealing with assets that have an attachment (ie assets that are really an attachment).
031: *
032: * @author Michael Neale
033: * @author Fernando Meyer
034: */
035: public class AssetFileServlet extends RepositoryServlet {
036:
037: private static final long serialVersionUID = 400L;
038:
039: /**
040: * Posting accepts content of various types -
041: * may be an attachement for an asset, or perhaps a repository import to process.
042: */
043: protected void doPost(HttpServletRequest request,
044: HttpServletResponse response) throws ServletException,
045: IOException {
046: response.setContentType("text/plain");
047: FormData uploadItem = new FileManagerUtils()
048: .getFormData(request);
049:
050: if (uploadItem.getFile() != null
051: && uploadItem.getUuid() != null) {
052: //attaching to an asset.
053: response.getWriter().write(
054: processAttachFileToAsset(uploadItem));
055: return;
056: }
057: response.getWriter().write("NO-SCRIPT-DATA");
058: }
059:
060: /**
061: * doGet acting like a dispatcher.
062: */
063: protected void doGet(HttpServletRequest req, HttpServletResponse res)
064: throws ServletException, IOException {
065:
066: String uuid = (String) req
067: .getParameter(HTMLFileManagerFields.FORM_FIELD_UUID);
068:
069: if (uuid != null) {
070: processAttachmentDownload(uuid, res);
071: } else {
072: res.sendError(HttpServletResponse.SC_BAD_REQUEST);
073: return;
074: }
075: }
076:
077: private void processAttachmentDownload(String uuid,
078: HttpServletResponse response) throws IOException {
079: ByteArrayOutputStream output = new ByteArrayOutputStream();
080: String filename = getFileManager().loadFileAttachmentByUUID(
081: uuid, output);
082:
083: response.setContentType("application/x-download");
084: response.setHeader("Content-Disposition",
085: "attachment; filename=" + filename + ";");
086: response.setContentLength(output.size());
087: response.getOutputStream().write(output.toByteArray());
088: response.getOutputStream().flush();
089: }
090:
091: private String processAttachFileToAsset(FormData uploadItem)
092: throws IOException {
093:
094: FileManagerUtils manager = getFileManager();
095: manager.attachFile(uploadItem);
096: uploadItem.getFile().getInputStream().close();
097:
098: return "OK";
099: }
100:
101: }
|