raw.jsp revision 422
422N/A<%--
422N/ACDDL HEADER START
422N/A
422N/AThe contents of this file are subject to the terms of the
422N/ACommon Development and Distribution License (the "License").
422N/AYou may not use this file except in compliance with the License.
422N/A
422N/ASee LICENSE.txt included in this distribution for the specific
422N/Alanguage governing permissions and limitations under the License.
422N/A
422N/AWhen distributing Covered Code, include this CDDL HEADER in each
422N/Afile and include the License file at LICENSE.txt.
422N/AIf applicable, add the following below this CDDL HEADER, with the
422N/Afields enclosed by brackets "[]" replaced with your own identifying
422N/Ainformation: Portions Copyright [yyyy] [name of copyright owner]
422N/A
422N/ACDDL HEADER END
422N/A
422N/ACopyright 2008 Sun Microsystems, Inc. All rights reserved.
422N/AUse is subject to license terms.
422N/A--%><%@page import="java.io.File,
422N/Ajava.io.InputStream,
422N/Ajava.io.OutputStream,
422N/Ajava.io.FileInputStream,
422N/Aorg.opensolaris.opengrok.configuration.RuntimeEnvironment,
422N/Aorg.opensolaris.opengrok.history.HistoryGuru"%><%
422N/AString path = request.getPathInfo();
422N/AString root = RuntimeEnvironment.getInstance().getSourceRootPath();
422N/Aif (path == null) {
422N/A path = "";
422N/A}
422N/AFile file = new File(root, path);
422N/Apath = file.getCanonicalPath();
422N/A
422N/Aif (!path.startsWith(root) || !file.exists() || !file.canRead() || RuntimeEnvironment.getInstance().getIgnoredNames().ignore(file)) {
422N/A response.sendError(response.SC_NOT_FOUND);
422N/A return;
422N/A} else if (file.isDirectory()) {
422N/A response.sendError(response.SC_NOT_FOUND, "Can't download a directory");
422N/A return;
422N/A}
422N/A
422N/AString mimeType = getServletContext().getMimeType(file.getAbsolutePath());
422N/Aresponse.setContentType(mimeType);
422N/A
422N/AString revision = request.getParameter("r");
422N/Aif (revision != null && revision.length() == 0) {
422N/A revision = null;
422N/A}
422N/A
422N/AInputStream in = null;
422N/A
422N/Aif (revision != null) {
422N/A try{
422N/A in = HistoryGuru.getInstance().getRevision(file.getParent(), file.getName(), revision);
422N/A } catch (Exception e) {
422N/A response.sendError(404, "Revision not found");
422N/A return ;
422N/A }
422N/A} else {
422N/A in = new FileInputStream(file);
422N/A response.setContentLength((int)file.length());
422N/A response.setDateHeader("Last-Modified", file.lastModified());
422N/A}
422N/Aresponse.setHeader("content-disposition", "attachment; filename=" + file.getName());
422N/A
422N/AOutputStream o = response.getOutputStream();
422N/Abyte[] buffer = new byte[8192];
422N/Aint nr;
422N/Awhile ((nr = in.read(buffer)) > 0) {
422N/A o.write(buffer, 0, nr);
422N/A}
422N/Ao.flush();
422N/Ao.close();
422N/A%>