xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IncludeFiles.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
12ffbb0cfSVladimir Kotal /*
22ffbb0cfSVladimir Kotal  * CDDL HEADER START
32ffbb0cfSVladimir Kotal  *
42ffbb0cfSVladimir Kotal  * The contents of this file are subject to the terms of the
52ffbb0cfSVladimir Kotal  * Common Development and Distribution License (the "License").
62ffbb0cfSVladimir Kotal  * You may not use this file except in compliance with the License.
72ffbb0cfSVladimir Kotal  *
82ffbb0cfSVladimir Kotal  * See LICENSE.txt included in this distribution for the specific
92ffbb0cfSVladimir Kotal  * language governing permissions and limitations under the License.
102ffbb0cfSVladimir Kotal  *
112ffbb0cfSVladimir Kotal  * When distributing Covered Code, include this CDDL HEADER in each
122ffbb0cfSVladimir Kotal  * file and include the License file at LICENSE.txt.
132ffbb0cfSVladimir Kotal  * If applicable, add the following below this CDDL HEADER, with the
142ffbb0cfSVladimir Kotal  * fields enclosed by brackets "[]" replaced with your own identifying
152ffbb0cfSVladimir Kotal  * information: Portions Copyright [yyyy] [name of copyright owner]
162ffbb0cfSVladimir Kotal  *
172ffbb0cfSVladimir Kotal  * CDDL HEADER END
182ffbb0cfSVladimir Kotal  */
192ffbb0cfSVladimir Kotal 
202ffbb0cfSVladimir Kotal /*
212ffbb0cfSVladimir Kotal  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22*68c19b4cSAlexandrSergei4  * Portions Copyright (c) 2020, Aleksandr Kirillov <alexkirillovsamara@gmail.com>.
232ffbb0cfSVladimir Kotal  */
242ffbb0cfSVladimir Kotal package org.opengrok.indexer.configuration;
252ffbb0cfSVladimir Kotal 
262ffbb0cfSVladimir Kotal import java.io.File;
272ffbb0cfSVladimir Kotal 
282ffbb0cfSVladimir Kotal import static org.opengrok.indexer.util.IOUtils.getFileContent;
292ffbb0cfSVladimir Kotal 
302ffbb0cfSVladimir Kotal public class IncludeFiles {
312ffbb0cfSVladimir Kotal     /**
322ffbb0cfSVladimir Kotal      * Reload the content of all include files.
332ffbb0cfSVladimir Kotal      */
reloadIncludeFiles()342ffbb0cfSVladimir Kotal     public void reloadIncludeFiles() {
352ffbb0cfSVladimir Kotal         getBodyIncludeFileContent(true);
362ffbb0cfSVladimir Kotal         getHeaderIncludeFileContent(true);
372ffbb0cfSVladimir Kotal         getFooterIncludeFileContent(true);
382ffbb0cfSVladimir Kotal         getForbiddenIncludeFileContent(true);
39*68c19b4cSAlexandrSergei4         getHttpHeaderIncludeFileContent(true);
402ffbb0cfSVladimir Kotal     }
412ffbb0cfSVladimir Kotal 
422ffbb0cfSVladimir Kotal     private transient String footer = null;
432ffbb0cfSVladimir Kotal 
442ffbb0cfSVladimir Kotal     /**
452ffbb0cfSVladimir Kotal      * Get the contents of the footer include file.
462ffbb0cfSVladimir Kotal      *
472ffbb0cfSVladimir Kotal      * @param force if true, reload even if already set
482ffbb0cfSVladimir Kotal      * @return an empty string if it could not be read successfully, the
492ffbb0cfSVladimir Kotal      * contents of the file otherwise.
502ffbb0cfSVladimir Kotal      * @see Configuration#FOOTER_INCLUDE_FILE
512ffbb0cfSVladimir Kotal      */
getFooterIncludeFileContent(boolean force)522ffbb0cfSVladimir Kotal     public String getFooterIncludeFileContent(boolean force) {
532ffbb0cfSVladimir Kotal         if (footer == null || force) {
542ffbb0cfSVladimir Kotal             footer = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
552ffbb0cfSVladimir Kotal                     Configuration.FOOTER_INCLUDE_FILE));
562ffbb0cfSVladimir Kotal         }
572ffbb0cfSVladimir Kotal         return footer;
582ffbb0cfSVladimir Kotal     }
592ffbb0cfSVladimir Kotal 
602ffbb0cfSVladimir Kotal     private transient String header = null;
612ffbb0cfSVladimir Kotal 
622ffbb0cfSVladimir Kotal     /**
632ffbb0cfSVladimir Kotal      * Get the contents of the header include file.
642ffbb0cfSVladimir Kotal      *
652ffbb0cfSVladimir Kotal      * @param force if true, reload even if already set
662ffbb0cfSVladimir Kotal      * @return an empty string if it could not be read successfully, the
672ffbb0cfSVladimir Kotal      * contents of the file otherwise.
682ffbb0cfSVladimir Kotal      * @see Configuration#HEADER_INCLUDE_FILE
692ffbb0cfSVladimir Kotal      */
getHeaderIncludeFileContent(boolean force)702ffbb0cfSVladimir Kotal     public String getHeaderIncludeFileContent(boolean force) {
712ffbb0cfSVladimir Kotal         if (header == null || force) {
722ffbb0cfSVladimir Kotal             header = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
732ffbb0cfSVladimir Kotal                     Configuration.HEADER_INCLUDE_FILE));
742ffbb0cfSVladimir Kotal         }
752ffbb0cfSVladimir Kotal         return header;
762ffbb0cfSVladimir Kotal     }
772ffbb0cfSVladimir Kotal 
782ffbb0cfSVladimir Kotal     private transient String body = null;
792ffbb0cfSVladimir Kotal 
802ffbb0cfSVladimir Kotal     /**
812ffbb0cfSVladimir Kotal      * Get the contents of the body include file.
822ffbb0cfSVladimir Kotal      *
832ffbb0cfSVladimir Kotal      * @param force if true, reload even if already set
842ffbb0cfSVladimir Kotal      * @return an empty string if it could not be read successfully, the
852ffbb0cfSVladimir Kotal      * contents of the file otherwise.
862ffbb0cfSVladimir Kotal      * @see Configuration#BODY_INCLUDE_FILE
872ffbb0cfSVladimir Kotal      */
getBodyIncludeFileContent(boolean force)882ffbb0cfSVladimir Kotal     public String getBodyIncludeFileContent(boolean force) {
892ffbb0cfSVladimir Kotal         if (body == null || force) {
902ffbb0cfSVladimir Kotal             body = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
912ffbb0cfSVladimir Kotal                     Configuration.BODY_INCLUDE_FILE));
922ffbb0cfSVladimir Kotal         }
932ffbb0cfSVladimir Kotal         return body;
942ffbb0cfSVladimir Kotal     }
952ffbb0cfSVladimir Kotal 
962ffbb0cfSVladimir Kotal     private transient String eforbidden_content = null;
972ffbb0cfSVladimir Kotal 
982ffbb0cfSVladimir Kotal     /**
992ffbb0cfSVladimir Kotal      * Get the contents of the page for forbidden error page (403 Forbidden)
1002ffbb0cfSVladimir Kotal      * include file.
1012ffbb0cfSVladimir Kotal      *
1022ffbb0cfSVladimir Kotal      * @param force if true, reload even if already set
1032ffbb0cfSVladimir Kotal      * @return an empty string if it could not be read successfully, the
1042ffbb0cfSVladimir Kotal      * contents of the file otherwise.
1052ffbb0cfSVladimir Kotal      * @see Configuration#E_FORBIDDEN_INCLUDE_FILE
1062ffbb0cfSVladimir Kotal      */
getForbiddenIncludeFileContent(boolean force)1072ffbb0cfSVladimir Kotal     public String getForbiddenIncludeFileContent(boolean force) {
1082ffbb0cfSVladimir Kotal         if (eforbidden_content == null || force) {
1092ffbb0cfSVladimir Kotal             eforbidden_content = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
1102ffbb0cfSVladimir Kotal                     Configuration.E_FORBIDDEN_INCLUDE_FILE));
1112ffbb0cfSVladimir Kotal         }
1122ffbb0cfSVladimir Kotal         return eforbidden_content;
1132ffbb0cfSVladimir Kotal     }
114*68c19b4cSAlexandrSergei4 
115*68c19b4cSAlexandrSergei4     private transient String http_header = null;
116*68c19b4cSAlexandrSergei4 
117*68c19b4cSAlexandrSergei4     /**
118*68c19b4cSAlexandrSergei4      * Get the contents of the HTTP header include file.
119*68c19b4cSAlexandrSergei4      *
120*68c19b4cSAlexandrSergei4      * @param force if true, reload even if already set
121*68c19b4cSAlexandrSergei4      * @return an empty string if it could not be read successfully, the
122*68c19b4cSAlexandrSergei4      * contents of the file otherwise.
123*68c19b4cSAlexandrSergei4      * @see Configuration#HTTP_HEADER_INCLUDE_FILE
124*68c19b4cSAlexandrSergei4      */
getHttpHeaderIncludeFileContent(boolean force)125*68c19b4cSAlexandrSergei4     public String getHttpHeaderIncludeFileContent(boolean force) {
126*68c19b4cSAlexandrSergei4         if (http_header == null || force) {
127*68c19b4cSAlexandrSergei4             http_header = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
128*68c19b4cSAlexandrSergei4                     Configuration.HTTP_HEADER_INCLUDE_FILE));
129*68c19b4cSAlexandrSergei4         }
130*68c19b4cSAlexandrSergei4         return http_header;
131*68c19b4cSAlexandrSergei4     }
1322ffbb0cfSVladimir Kotal }
133