01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.notes;
18:
19: import java.io.BufferedInputStream;
20: import java.io.BufferedOutputStream;
21: import java.io.File;
22: import java.io.FileInputStream;
23: import java.io.IOException;
24: import java.io.OutputStream;
25:
26: import javax.servlet.ServletException;
27: import javax.servlet.http.HttpServlet;
28: import javax.servlet.http.HttpServletRequest;
29: import javax.servlet.http.HttpServletResponse;
30:
31: import edu.iu.uis.eden.KEWServiceLocator;
32:
33: /**
34: * A servlet which can be used to retrieve attachments from Notes.
35: *
36: * @author rkirkend
37: */
38: public class AttachmentServlet extends HttpServlet {
39:
40: private static final long serialVersionUID = -1918858512573502697L;
41: public static final String ATTACHMENT_ID_KEY = "attachmentId";
42:
43: protected void doPost(HttpServletRequest request,
44: HttpServletResponse response) throws ServletException,
45: IOException {
46: Long attachmentId = new Long(request
47: .getParameter(ATTACHMENT_ID_KEY));
48: if (attachmentId == null) {
49: throw new ServletException(
50: "No 'attachmentId' was specified.");
51: }
52: NoteService noteService = KEWServiceLocator.getNoteService();
53: Attachment attachment = noteService
54: .findAttachment(attachmentId);
55: File file = noteService.findAttachmentFile(attachment);
56: response.setContentLength((int) file.length());
57: response.setContentType(attachment.getMimeType());
58: response.setHeader("Content-disposition",
59: "attachment; filename=" + attachment.getFileName());
60: FileInputStream attachmentFile = new FileInputStream(file);
61: BufferedInputStream inputStream = new BufferedInputStream(
62: attachmentFile);
63: OutputStream outputStream = new BufferedOutputStream(response
64: .getOutputStream());
65:
66: int bytesWritten = 0;
67: try {
68: int c;
69: while ((c = inputStream.read()) != -1) {
70: outputStream.write(c);
71: bytesWritten++;
72: }
73: } finally {
74: inputStream.close();
75: }
76: outputStream.close();
77: }
78:
79: protected void doGet(HttpServletRequest request,
80: HttpServletResponse response) throws ServletException,
81: IOException {
82: doPost(request, response);
83: }
84:
85: }
|