1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43 }
44
45 public static String getElasticsearchHttpUrl() {
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.getElasticsearchHttpUrl();
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("conf", names);
84 }
85
86 public static Path getClassesPath(final String... names) {
87 return getPath("classes", names);
88 }
89
90 public static Path getOrigPath(final String... names) {
91 return getPath("orig", names);
92 }
93
94 public static Path getMailTemplatePath(final String... names) {
95 return getPath("mail", names);
96 }
97
98 public static Path getViewTemplatePath(final String... names) {
99 return getPath("view", names);
100 }
101
102 public static Path getDictionaryPath(final String... names) {
103 return getPath("dict", names);
104 }
105
106 public static Path getThumbnailPath(final String... names) {
107 return getPath("thumbnails", names);
108 }
109
110 public static Path getSitePath(final String... names) {
111 return getPath("site", names);
112 }
113
114 public static Path getProjectPropertiesFile() {
115 return getPath("", "project.properties");
116 }
117
118 protected static Path getPath(final String base, final String... names) {
119
120 try {
121 final ServletContext servletContext = ComponentUtil.getComponent(ServletContext.class);
122 final String webinfPath = servletContext.getRealPath("/WEB-INF/" + base);
123 if (webinfPath != null) {
124 if (Files.exists(Paths.get(webinfPath))) {
125 return Paths.get(webinfPath, names);
126 }
127 }
128 } catch (final Throwable e) {
129
130 }
131 final String webinfBase = "WEB-INF/" + base;
132 if (Files.exists(Paths.get(webinfBase))) {
133 return Paths.get(webinfBase, names);
134 }
135 final String srcWebInfBase = "src/main/webapps/WEB-INF/" + base;
136 if (Files.exists(Paths.get(srcWebInfBase))) {
137 return Paths.get(srcWebInfBase, names);
138 }
139 final String targetWebInfBase = "target/fess/WEB-INF/" + base;
140 if (Files.exists(Paths.get(targetWebInfBase))) {
141 return Paths.get(targetWebInfBase, names);
142 }
143 return Paths.get(webinfBase, names);
144 }
145
146 public static File[] getJarFiles(final String namePrefix) {
147 final ServletContext context = LaServletContextUtil.getServletContext();
148 if (context == null) {
149 return new File[0];
150 }
151 final String libPath = context.getRealPath("/WEB-INF/lib");
152 if (StringUtil.isBlank(libPath)) {
153 return new File[0];
154 }
155 final File libDir = new File(libPath);
156 return libDir.listFiles((FilenameFilter) (file, name) -> name.startsWith(namePrefix));
157 }
158
159 public static String resolve(final String value) {
160 if (value == null) {
161 return null;
162 }
163
164 final StringBuffer tunedText = new StringBuffer(value.length());
165 final Pattern pattern = Pattern.compile("(\\$\\{([\\w\\.]+)\\})");
166 final Matcher matcher = pattern.matcher(value);
167 while (matcher.find()) {
168 final String key = matcher.group(2);
169 String replacement = System.getProperty(key);
170 if (replacement == null) {
171 replacement = matcher.group(1);
172 }
173 matcher.appendReplacement(tunedText, replacement.replace("\\", "\\\\").replace("$", "\\$"));
174
175 }
176 matcher.appendTail(tunedText);
177 return tunedText.toString();
178 }
179 }