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