/* Copyright (c) 2008, Centre for Educational Technology, Tallinn University Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ package ee.tlu.htk.waramu.servlets; import ee.tlu.htk.waramu.server.Attachment; import ee.tlu.htk.waramu.server.TypesBeanLocal; import java.io.IOException; import java.io.PrintWriter; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author vahur */ public class Downloader extends HttpServlet { @EJB private TypesBeanLocal typesBean; /** * Processes requests for both HTTP GET and POST methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uid = request.getParameter("resourceId"); String aid = request.getParameter("attachmentId"); Attachment at = typesBean.getAttachment(uid, aid); if (at == null) { response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("

" + "

" + "File not found." + "
" + "

"); out.close(); } else { response.setContentType("application/octet-stream"); ServletOutputStream out = response.getOutputStream(); byte[] bytes = at.getAttachmentId().getAttachment(); response.setContentLength((int) bytes.length); response.setHeader("Content-Disposition", "attachment; filename=\"" + at.getAttachmentId().getFilename() + "\""); out.write(bytes); out.close(); } } // /** * Handles the HTTP GET method. * @param request servlet request * @param response servlet response */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method. * @param request servlet request * @param response servlet response */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ @Override public String getServletInfo() { return "Short description"; }// }