1
+ /*
2
+ * CDDL HEADER START
3
+ *
4
+ * The contents of this file are subject to the terms of the
5
+ * Common Development and Distribution License (the "License").
6
+ * You may not use this file except in compliance with the License.
7
+ *
8
+ * See LICENSE.txt included in this distribution for the specific
9
+ * language governing permissions and limitations under the License.
10
+ *
11
+ * When distributing Covered Code, include this CDDL HEADER in each
12
+ * file and include the License file at LICENSE.txt.
13
+ * If applicable, add the following below this CDDL HEADER, with the
14
+ * fields enclosed by brackets "[]" replaced with your own identifying
15
+ * information: Portions Copyright [yyyy] [name of copyright owner]
16
+ *
17
+ * CDDL HEADER END
18
+ */
19
+
20
+ package org .opengrok .web ;
21
+
22
+ /*
23
+ * Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
24
+ *
25
+ * Portions Copyright 2011 Jens Elkner.
26
+ */
27
+
28
+ import java .io .File ;
29
+ import java .io .FileInputStream ;
30
+ import java .io .IOException ;
31
+ import java .io .InputStream ;
32
+ import java .io .OutputStream ;
33
+
34
+ import org .opengrok .indexer .history .HistoryGuru ;
35
+ import org .opengrok .indexer .web .PageConfig ;
36
+ import org .opengrok .indexer .web .Prefix ;
37
+
38
+ import javax .servlet .http .HttpServlet ;
39
+ import javax .servlet .http .HttpServletRequest ;
40
+ import javax .servlet .http .HttpServletResponse ;
41
+
42
+ public class GetFile extends HttpServlet {
43
+
44
+ public static final long serialVersionUID = -1 ;
45
+
46
+ @ Override
47
+ protected void service (final HttpServletRequest request , final HttpServletResponse response ) throws IOException {
48
+ PageConfig cfg = PageConfig .get (request );
49
+ cfg .checkSourceRootExistence ();
50
+
51
+ String redir = cfg .canProcess ();
52
+ if (redir == null || redir .length () > 0 ) {
53
+ if (redir != null ) {
54
+ response .sendRedirect (redir );
55
+ } else {
56
+ response .sendError (HttpServletResponse .SC_NOT_FOUND );
57
+ }
58
+ return ;
59
+ }
60
+
61
+ File f = cfg .getResourceFile ();
62
+ String revision = cfg .getRequestedRevision ();
63
+ if (revision .length () == 0 ) {
64
+ revision = null ;
65
+ }
66
+
67
+ InputStream in = null ;
68
+ try {
69
+ if (revision != null ) {
70
+ in = HistoryGuru .getInstance ().getRevision (f .getParent (),
71
+ f .getName (), revision );
72
+ } else {
73
+ long flast = cfg .getLastModified ();
74
+ if (request .getDateHeader ("If-Modified-Since" ) >= flast ) {
75
+ response .setStatus (HttpServletResponse .SC_NOT_MODIFIED );
76
+ return ;
77
+ }
78
+ in = new FileInputStream (f );
79
+ response .setContentLength ((int ) f .length ());
80
+ response .setDateHeader ("Last-Modified" , f .lastModified ());
81
+ }
82
+ } catch (
83
+ Exception e ) {
84
+ response .sendError (HttpServletResponse .SC_NOT_FOUND );
85
+ return ;
86
+ }
87
+
88
+ String mimeType = getServletContext ().getMimeType (f .getAbsolutePath ());
89
+ response .setContentType (mimeType );
90
+
91
+ try {
92
+ if (cfg .getPrefix () == Prefix .DOWNLOAD_P ) {
93
+ response .setHeader ("content-disposition" , "attachment; filename="
94
+ + f .getName ());
95
+ } else {
96
+ response .setHeader ("content-type" , "text/plain" );
97
+ }
98
+ OutputStream o = response .getOutputStream ();
99
+ byte [] buffer = new byte [8192 ];
100
+ int nr ;
101
+ while ((nr = in .read (buffer )) > 0 ) {
102
+ o .write (buffer , 0 , nr );
103
+ }
104
+ o .flush ();
105
+ o .close ();
106
+ } finally {
107
+ in .close ();
108
+ }
109
+ }
110
+ }
0 commit comments