Java tutorial
/** * openkm, Open Document Management System (http://www.openkm.com) * Copyright (c) 2006-2013 Paco Avila & Josep Llort * * No bytes were intentionally harmed during the development of this application. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package com.ikon.servlet; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ikon.api.OKMDocument; import com.ikon.api.OKMRepository; import com.ikon.bean.Document; import com.ikon.core.PathNotFoundException; import com.ikon.core.RepositoryException; import com.ikon.util.PathUtils; import com.ikon.util.WebUtils; /** * Download Servlet */ public class DownloadServlet extends BasicSecuredServlet { private static Logger log = LoggerFactory.getLogger(DownloadServlet.class); private static final long serialVersionUID = 1L; /** * */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); String userId = request.getRemoteUser(); String path = WebUtils.getString(request, "path"); String uuid = WebUtils.getString(request, "uuid"); boolean inline = WebUtils.getBoolean(request, "inline"); InputStream is = null; try { // Now an document can be located by UUID if (uuid != null && !uuid.equals("")) { path = OKMRepository.getInstance().getNodePath(null, uuid); } if (path != null) { Document doc = OKMDocument.getInstance().getProperties(null, path); String fileName = PathUtils.getName(doc.getPath()); log.info("Download {} by {} ({})", new Object[] { path, userId, (inline ? "inline" : "attachment") }); is = OKMDocument.getInstance().getContent(null, path, false); WebUtils.sendFile(request, response, fileName, doc.getMimeType(), inline, is); } else { response.setContentType("text/plain; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("Missing document reference"); out.close(); } } catch (PathNotFoundException e) { log.warn(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "PathNotFoundException: " + e.getMessage()); } catch (RepositoryException e) { log.warn(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "RepositoryException: " + e.getMessage()); } catch (Exception e) { log.warn(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } finally { IOUtils.closeQuietly(is); } } }