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.lastaflute.web.util.LaServletContextUtil;
32
33 public class ResourceUtil {
34 protected ResourceUtil() {
35
36 }
37
38 public static String getElasticsearchHttpUrl() {
39 final String url = System.getProperty("fess.es.http_address");
40 if (url != null) {
41 return url;
42 }
43 final FessConfig fessConfig = ComponentUtil.getFessConfig();
44 return fessConfig.getElasticsearchHttpUrl();
45 }
46
47 public static Path getConfPath(final String... names) {
48 final String confPath = System.getProperty(Constants.FESS_CONF_PATH);
49 if (StringUtil.isNotBlank(confPath)) {
50 return Paths.get(confPath, names);
51 }
52 return getPath("conf", names);
53 }
54
55 public static Path getClassesPath(final String... names) {
56 return getPath("classes", names);
57 }
58
59 public static Path getOrigPath(final String... names) {
60 return getPath("orig", names);
61 }
62
63 public static Path getMailTemplatePath(final String... names) {
64 return getPath("mail", names);
65 }
66
67 public static Path getViewTemplatePath(final String... names) {
68 return getPath("view", names);
69 }
70
71 public static Path getDictionaryPath(final String... names) {
72 return getPath("dict", names);
73 }
74
75 public static Path getThumbnailPath(final String... names) {
76 return getPath("thumbnails", names);
77 }
78
79 public static Path getSitePath(final String... names) {
80 return getPath("site", names);
81 }
82
83 protected static Path getPath(final String base, final String... names) {
84
85 try {
86 final ServletContext servletContext = ComponentUtil.getComponent(ServletContext.class);
87 final String webinfPath = servletContext.getRealPath("/WEB-INF/" + base);
88 if (webinfPath != null) {
89 if (Files.exists(Paths.get(webinfPath))) {
90 return Paths.get(webinfPath, names);
91 }
92 }
93 } catch (final Throwable e) {
94
95 }
96 final String webinfBase = "WEB-INF/" + base;
97 if (Files.exists(Paths.get(webinfBase))) {
98 return Paths.get(webinfBase, names);
99 }
100 final String srcWebInfBase = "src/main/webapps/WEB-INF/" + base;
101 if (Files.exists(Paths.get(srcWebInfBase))) {
102 return Paths.get(srcWebInfBase, names);
103 }
104 final String targetWebInfBase = "target/fess/WEB-INF/" + base;
105 if (Files.exists(Paths.get(targetWebInfBase))) {
106 return Paths.get(targetWebInfBase, names);
107 }
108 return Paths.get(webinfBase, names);
109 }
110
111 public static File[] getJarFiles(final String namePrefix) {
112 final ServletContext context = LaServletContextUtil.getServletContext();
113 if (context == null) {
114 return new File[0];
115 }
116 final String libPath = context.getRealPath("/WEB-INF/lib");
117 if (StringUtil.isBlank(libPath)) {
118 return new File[0];
119 }
120 final File libDir = new File(libPath);
121 return libDir.listFiles((FilenameFilter) (file, name) -> name.startsWith(namePrefix));
122 }
123
124 public static String resolve(final String value) {
125 if (value == null) {
126 return null;
127 }
128
129 final StringBuffer tunedText = new StringBuffer(value.length());
130 final Pattern pattern = Pattern.compile("(\\$\\{([\\w\\.]+)\\})");
131 final Matcher matcher = pattern.matcher(value);
132 while (matcher.find()) {
133 final String key = matcher.group(2);
134 String replacement = System.getProperty(key);
135 if (replacement == null) {
136 replacement = matcher.group(1);
137 }
138 matcher.appendReplacement(tunedText, replacement.replace("\\", "\\\\").replace("$", "\\$"));
139
140 }
141 matcher.appendTail(tunedText);
142 return tunedText.toString();
143 }
144 }