View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import java.io.File;
19  import java.io.FilenameFilter;
20  import java.nio.file.Files;
21  import java.nio.file.Path;
22  import java.nio.file.Paths;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import javax.servlet.ServletContext;
27  
28  import org.codelibs.core.lang.StringUtil;
29  import org.codelibs.fess.Constants;
30  import org.codelibs.fess.mylasta.direction.FessConfig;
31  import org.dbflute.optional.OptionalEntity;
32  import org.lastaflute.web.util.LaServletContextUtil;
33  
34  public class ResourceUtil {
35      private static final String FESS_OVERRIDE_CONF_PATH = "FESS_OVERRIDE_CONF_PATH";
36  
37      private static final String FESS_APP_TYPE = "FESS_APP_TYPE";
38  
39      private static final String FESS_APP_DOCKER = "docker";
40  
41      protected ResourceUtil() {
42          // nothing
43      }
44  
45      public static String getFesenHttpUrl() {
46          final String url = System.getProperty("fess.es.http_address");
47          if (url != null) {
48              return url;
49          }
50          final FessConfig fessConfig = ComponentUtil.getFessConfig();
51          return fessConfig.getFesenHttpUrl();
52      }
53  
54      public static String getAppType() {
55          final String appType = System.getenv(FESS_APP_TYPE);
56          if (StringUtil.isNotBlank(appType)) {
57              return appType;
58          }
59          return StringUtil.EMPTY;
60      }
61  
62      public static OptionalEntity<String> getOverrideConfPath() {
63          if (FESS_APP_DOCKER.equalsIgnoreCase(getAppType())) {
64              final String confPath = System.getenv(FESS_OVERRIDE_CONF_PATH);
65              if (StringUtil.isNotBlank(confPath)) {
66                  return OptionalEntity.of(confPath);
67              }
68          }
69          return OptionalEntity.empty();
70      }
71  
72      public static Path getConfPath(final String... names) {
73          if (FESS_APP_DOCKER.equalsIgnoreCase(getAppType())) {
74              final Path confPath = Paths.get("/opt/fess", names);
75              if (Files.exists(confPath)) {
76                  return confPath;
77              }
78          }
79          final String confPath = System.getProperty(Constants.FESS_CONF_PATH);
80          if (StringUtil.isNotBlank(confPath)) {
81              return Paths.get(confPath, names);
82          }
83          return getPath("WEB-INF/", "conf", names);
84      }
85  
86      public static Path getConfOrClassesPath(final String... names) {
87          final Path confPath = getConfPath(names);
88          if (Files.exists(confPath)) {
89              return confPath;
90          }
91          return org.codelibs.core.io.ResourceUtil.getResourceAsFile(String.join("/", names)).toPath();
92      }
93  
94      public static Path getClassesPath(final String... names) {
95          return getPath("WEB-INF/", "classes", names);
96      }
97  
98      public static Path getOrigPath(final String... names) {
99          return getPath("WEB-INF/", "orig", names);
100     }
101 
102     public static Path getMailTemplatePath(final String... names) {
103         return getPath("WEB-INF/", "mail", names);
104     }
105 
106     public static Path getViewTemplatePath(final String... names) {
107         return getPath("WEB-INF/", "view", names);
108     }
109 
110     public static Path getDictionaryPath(final String... names) {
111         return getPath("WEB-INF/", "dict", names);
112     }
113 
114     public static Path getThumbnailPath(final String... names) {
115         return getPath("WEB-INF/", "thumbnails", names);
116     }
117 
118     public static Path getSitePath(final String... names) {
119         return getPath("WEB-INF/", "site", names);
120     }
121 
122     public static Path getPluginPath(final String... names) {
123         return getPath("WEB-INF/", "plugin", names);
124     }
125 
126     public static Path getProjectPropertiesFile() {
127         return getPath("WEB-INF/", StringUtil.EMPTY, "project.properties");
128     }
129 
130     public static Path getImagePath(final String... names) {
131         return getPath(StringUtil.EMPTY, "images", names);
132     }
133 
134     public static Path getCssPath(final String... names) {
135         return getPath(StringUtil.EMPTY, "css", names);
136     }
137 
138     public static Path getJavaScriptPath(final String... names) {
139         return getPath(StringUtil.EMPTY, "js", names);
140     }
141 
142     public static Path getEnvPath(final String envName, final String... names) {
143         return getPath("WEB-INF/", "env/" + envName, names);
144     }
145 
146     protected static Path getPath(final String root, final String base, final String... names) {
147 
148         try {
149             final ServletContext servletContext = ComponentUtil.getComponent(ServletContext.class);
150             final String webinfPath = servletContext.getRealPath("/" + root + base);
151             if ((webinfPath != null) && Files.exists(Paths.get(webinfPath))) {
152                 return Paths.get(webinfPath, names);
153             }
154         } catch (final Throwable e) {
155             // ignore
156         }
157         final String webinfBase = root + base;
158         if (Files.exists(Paths.get(webinfBase))) {
159             return Paths.get(webinfBase, names);
160         }
161         final String srcWebInfBase = "src/main/webapps" + root + base;
162         if (Files.exists(Paths.get(srcWebInfBase))) {
163             return Paths.get(srcWebInfBase, names);
164         }
165         final String targetWebInfBase = "target/fess/" + root + base;
166         if (Files.exists(Paths.get(targetWebInfBase))) {
167             return Paths.get(targetWebInfBase, names);
168         }
169         return Paths.get(webinfBase, names);
170     }
171 
172     public static File[] getJarFiles(final String namePrefix) {
173         final ServletContext context = LaServletContextUtil.getServletContext();
174         if (context == null) {
175             return new File[0];
176         }
177         final String libPath = context.getRealPath("/WEB-INF/lib");
178         if (StringUtil.isBlank(libPath)) {
179             return new File[0];
180         }
181         final File libDir = new File(libPath);
182         if (!libDir.exists()) {
183             return new File[0];
184         }
185         return libDir.listFiles((file, name) -> name.startsWith(namePrefix));
186     }
187 
188     public static File[] getPluginJarFiles(final String namePrefix) {
189         return getPluginJarFiles((file, name) -> name.startsWith(namePrefix));
190     }
191 
192     public static File[] getPluginJarFiles(final FilenameFilter filter) {
193         final ServletContext context = LaServletContextUtil.getServletContext();
194         if (context == null) {
195             return new File[0];
196         }
197         final String libPath = context.getRealPath("/WEB-INF/plugin");
198         if (StringUtil.isBlank(libPath)) {
199             return new File[0];
200         }
201         final File libDir = new File(libPath);
202         if (!libDir.exists()) {
203             return new File[0];
204         }
205         return libDir.listFiles(filter);
206     }
207 
208     public static String resolve(final String value) {
209         if (value == null) {
210             return null;
211         }
212 
213         final StringBuffer tunedText = new StringBuffer(value.length());
214         final Pattern pattern = Pattern.compile("(\\$\\{([\\w\\.]+)\\})");
215         final Matcher matcher = pattern.matcher(value);
216         while (matcher.find()) {
217             final String key = matcher.group(2);
218             String replacement = System.getProperty(key);
219             if (replacement == null) {
220                 replacement = matcher.group(1);
221             }
222             matcher.appendReplacement(tunedText, replacement.replace("\\", "\\\\").replace("$", "\\$"));
223 
224         }
225         matcher.appendTail(tunedText);
226         return tunedText.toString();
227     }
228 }