xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IncludeFiles.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
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 /*
21  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2020, Aleksandr Kirillov <alexkirillovsamara@gmail.com>.
23  */
24 package org.opengrok.indexer.configuration;
25 
26 import java.io.File;
27 
28 import static org.opengrok.indexer.util.IOUtils.getFileContent;
29 
30 public class IncludeFiles {
31     /**
32      * Reload the content of all include files.
33      */
reloadIncludeFiles()34     public void reloadIncludeFiles() {
35         getBodyIncludeFileContent(true);
36         getHeaderIncludeFileContent(true);
37         getFooterIncludeFileContent(true);
38         getForbiddenIncludeFileContent(true);
39         getHttpHeaderIncludeFileContent(true);
40     }
41 
42     private transient String footer = null;
43 
44     /**
45      * Get the contents of the footer include file.
46      *
47      * @param force if true, reload even if already set
48      * @return an empty string if it could not be read successfully, the
49      * contents of the file otherwise.
50      * @see Configuration#FOOTER_INCLUDE_FILE
51      */
getFooterIncludeFileContent(boolean force)52     public String getFooterIncludeFileContent(boolean force) {
53         if (footer == null || force) {
54             footer = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
55                     Configuration.FOOTER_INCLUDE_FILE));
56         }
57         return footer;
58     }
59 
60     private transient String header = null;
61 
62     /**
63      * Get the contents of the header include file.
64      *
65      * @param force if true, reload even if already set
66      * @return an empty string if it could not be read successfully, the
67      * contents of the file otherwise.
68      * @see Configuration#HEADER_INCLUDE_FILE
69      */
getHeaderIncludeFileContent(boolean force)70     public String getHeaderIncludeFileContent(boolean force) {
71         if (header == null || force) {
72             header = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
73                     Configuration.HEADER_INCLUDE_FILE));
74         }
75         return header;
76     }
77 
78     private transient String body = null;
79 
80     /**
81      * Get the contents of the body include file.
82      *
83      * @param force if true, reload even if already set
84      * @return an empty string if it could not be read successfully, the
85      * contents of the file otherwise.
86      * @see Configuration#BODY_INCLUDE_FILE
87      */
getBodyIncludeFileContent(boolean force)88     public String getBodyIncludeFileContent(boolean force) {
89         if (body == null || force) {
90             body = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
91                     Configuration.BODY_INCLUDE_FILE));
92         }
93         return body;
94     }
95 
96     private transient String eforbidden_content = null;
97 
98     /**
99      * Get the contents of the page for forbidden error page (403 Forbidden)
100      * include file.
101      *
102      * @param force if true, reload even if already set
103      * @return an empty string if it could not be read successfully, the
104      * contents of the file otherwise.
105      * @see Configuration#E_FORBIDDEN_INCLUDE_FILE
106      */
getForbiddenIncludeFileContent(boolean force)107     public String getForbiddenIncludeFileContent(boolean force) {
108         if (eforbidden_content == null || force) {
109             eforbidden_content = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
110                     Configuration.E_FORBIDDEN_INCLUDE_FILE));
111         }
112         return eforbidden_content;
113     }
114 
115     private transient String http_header = null;
116 
117     /**
118      * Get the contents of the HTTP header include file.
119      *
120      * @param force if true, reload even if already set
121      * @return an empty string if it could not be read successfully, the
122      * contents of the file otherwise.
123      * @see Configuration#HTTP_HEADER_INCLUDE_FILE
124      */
getHttpHeaderIncludeFileContent(boolean force)125     public String getHttpHeaderIncludeFileContent(boolean force) {
126         if (http_header == null || force) {
127             http_header = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
128                     Configuration.HTTP_HEADER_INCLUDE_FILE));
129         }
130         return http_header;
131     }
132 }
133