raw.jsp revision 1156
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch<%--
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenCDDL HEADER START
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenThe contents of this file are subject to the terms of the
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenCommon Development and Distribution License (the "License").
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenYou may not use this file except in compliance with the License.
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenSee LICENSE.txt included in this distribution for the specific
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenlanguage governing permissions and limitations under the License.
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenWhen distributing Covered Code, include this CDDL HEADER in each
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenfile and include the License file at LICENSE.txt.
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenIf applicable, add the following below this CDDL HEADER, with the
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenfields enclosed by brackets "[]" replaced with your own identifying
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Siraineninformation: Portions Copyright [yyyy] [name of copyright owner]
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo SirainenCDDL HEADER END
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenCopyright 2008 Sun Microsystems, Inc. All rights reserved.
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenUse is subject to license terms.
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen--%><%@page import="java.io.File,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenjava.io.InputStream,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenjava.io.OutputStream,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenjava.io.FileInputStream,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenjava.io.FileNotFoundException,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenorg.opensolaris.opengrok.configuration.RuntimeEnvironment,
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenorg.opensolaris.opengrok.history.HistoryGuru"%><%
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenString path = request.getPathInfo();
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenif (path == null) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen path = "";
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenRuntimeEnvironment env = RuntimeEnvironment.getInstance();
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo SirainenFile file = new File(env.getSourceRootPath(), path);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainentry {
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen path = env.getPathRelativeToSourceRoot(file, 0);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen} catch (FileNotFoundException e) {
38505846b6d083e19f0a7d1373761bdda5d9a5a9Timo Sirainen response.sendError(response.SC_NOT_FOUND);
38505846b6d083e19f0a7d1373761bdda5d9a5a9Timo Sirainen return;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
d8702d15ee7721ed1fcfc8f00a589970bd6b3598Timo Sirainenif (!file.exists() || !file.canRead() || RuntimeEnvironment.getInstance().getIgnoredNames().ignore(file)) {
d8702d15ee7721ed1fcfc8f00a589970bd6b3598Timo Sirainen response.sendError(response.SC_NOT_FOUND);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen} else if (file.isDirectory()) {
2087543679f0b35a8f70e1277b234e66ffdf96edTimo Sirainen response.sendError(response.SC_NOT_FOUND, "Can't download a directory");
2087543679f0b35a8f70e1277b234e66ffdf96edTimo Sirainen return;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
f016dec9837e6a41867708e4b89ca5308dedab05Timo Sirainen
f016dec9837e6a41867708e4b89ca5308dedab05Timo SirainenString mimeType = getServletContext().getMimeType(file.getAbsolutePath());
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainenresponse.setContentType(mimeType);
f016dec9837e6a41867708e4b89ca5308dedab05Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenString revision = request.getParameter("r");
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenif (revision != null && revision.length() == 0) {
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen revision = null;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo SirainenInputStream in = null;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenif (revision != null) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen try{
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen in = HistoryGuru.getInstance().getRevision(file.getParent(), file.getName(), revision);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen } catch (Exception e) {
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen response.sendError(404, "Revision not found");
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen return ;
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen }
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen} else {
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen response.setContentLength((int)file.length());
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen response.setDateHeader("Last-Modified", file.lastModified());
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen in = new FileInputStream(file);
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen}
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainentry {
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen response.setHeader("content-disposition", "attachment; filename=" + file.getName());
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen OutputStream o = response.getOutputStream();
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen byte[] buffer = new byte[8192];
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen int nr;
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen while ((nr = in.read(buffer)) > 0) {
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen o.write(buffer, 0, nr);
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen }
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen o.flush();
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen o.close();
fe791e96fdf796f7d8997ee0515b163dc5eddd72Aki Tuomi} finally {
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi in.close();
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi}
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi%>
0b808b62a679c81f59958392507fd0f6b5bd4e34Aki Tuomi