1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.helper;
17
18 import static org.codelibs.core.stream.StreamUtil.stream;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.regex.Pattern;
28 import java.util.stream.Collectors;
29
30 import javax.annotation.PostConstruct;
31
32 import org.codelibs.core.lang.StringUtil;
33 import org.codelibs.fess.Constants;
34 import org.codelibs.fess.app.service.LabelTypeService;
35 import org.codelibs.fess.entity.SearchRequestParams.SearchRequestType;
36 import org.codelibs.fess.es.config.exentity.LabelType;
37 import org.codelibs.fess.util.ComponentUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class LabelTypeHelper {
42 private static final Logger logger = LoggerFactory.getLogger(LabelTypeHelper.class);
43
44 protected volatile List<LabelTypeItem> labelTypeItemList;
45
46 protected volatile List<LabelTypePattern> labelTypePatternList;
47
48 @PostConstruct
49 public void init() {
50 update();
51 }
52
53 public int update() {
54 final List<LabelType> labelTypeList = ComponentUtil.getComponent(LabelTypeService.class).getLabelTypeList();
55 buildLabelTypeItems(labelTypeList);
56 buildLabelTypePatternList(labelTypeList);
57 return labelTypeList.size();
58 }
59
60 public void refresh(final List<LabelType> labelTypeList) {
61 buildLabelTypeItems(labelTypeList);
62 buildLabelTypePatternList(labelTypeList);
63 }
64
65 protected void buildLabelTypeItems(final List<LabelType> labelTypeList) {
66 final List<LabelTypeItem> itemList = new ArrayList<>();
67 for (final LabelType labelType : labelTypeList) {
68 final LabelTypeItem item = new LabelTypeItem();
69 item.setLabel(labelType.getName());
70 item.setValue(labelType.getValue());
71 item.setPermissions(labelType.getPermissions());
72 item.setVirtualHost(labelType.getVirtualHost());
73 itemList.add(item);
74 }
75 labelTypeItemList = itemList;
76 }
77
78 public List<Map<String, String>> getLabelTypeItemList(final SearchRequestType searchRequestType) {
79 if (labelTypeItemList == null) {
80 init();
81 }
82
83 final String virtualHostKey = ComponentUtil.getVirtualHostHelper().getVirtualHostKey();
84 final List<LabelTypeItem> labelList;
85 if (StringUtil.isBlank(virtualHostKey)) {
86 labelList = labelTypeItemList;
87 } else {
88 labelList =
89 labelTypeItemList.stream().filter(item -> virtualHostKey.equals(item.getVirtualHost())).collect(Collectors.toList());
90 }
91
92 final List<Map<String, String>> itemList = new ArrayList<>();
93 final Set<String> roleSet = ComponentUtil.getRoleQueryHelper().build(searchRequestType);
94 if (roleSet.isEmpty()) {
95 for (final LabelTypeItem item : labelList) {
96 if (item.getPermissions().length == 0) {
97 final Map<String, String> map = new HashMap<>(2);
98 map.put(Constants.ITEM_LABEL, item.getLabel());
99 map.put(Constants.ITEM_VALUE, item.getValue());
100 itemList.add(map);
101 }
102 }
103 } else {
104 for (final LabelTypeItem item : labelList) {
105 final Set<String> permissions = stream(item.getPermissions()).get(stream -> stream.collect(Collectors.toSet()));
106 for (final String roleValue : roleSet) {
107 if (permissions.contains(roleValue)) {
108 final Map<String, String> map = new HashMap<>(2);
109 map.put(Constants.ITEM_LABEL, item.getLabel());
110 map.put(Constants.ITEM_VALUE, item.getValue());
111 itemList.add(map);
112 break;
113 }
114 }
115 }
116 }
117
118 return itemList;
119 }
120
121 public Set<String> getMatchedLabelValueSet(final String path) {
122 if (labelTypePatternList == null) {
123 synchronized (this) {
124 if (labelTypePatternList == null) {
125 buildLabelTypePatternList(ComponentUtil.getComponent(LabelTypeService.class).getLabelTypeList());
126 }
127 }
128 }
129
130 if (labelTypePatternList.isEmpty()) {
131 return Collections.emptySet();
132 }
133
134 final Set<String> valueSet = new HashSet<>();
135 for (final LabelTypePattern pattern : labelTypePatternList) {
136 if (pattern.match(path)) {
137 valueSet.add(pattern.getValue());
138 }
139 }
140 return valueSet;
141 }
142
143 protected void buildLabelTypePatternList(final List<LabelType> labelTypeList) {
144 final List<LabelTypePattern> list = new ArrayList<>();
145 for (final LabelType labelType : labelTypeList) {
146 final String includedPaths = labelType.getIncludedPaths();
147 final String excludedPaths = labelType.getExcludedPaths();
148 if (StringUtil.isNotBlank(includedPaths) || StringUtil.isNotBlank(excludedPaths)) {
149 try {
150 list.add(new LabelTypePattern(labelType.getValue(), includedPaths, excludedPaths));
151 } catch (final Exception e) {
152 logger.warn("Failed to create a matching pattern of a label: " + labelType.getValue() + ", includedPaths:"
153 + includedPaths + ", excludedPaths:" + excludedPaths, e);
154 }
155 }
156 }
157 labelTypePatternList = list;
158 }
159
160 protected static class LabelTypeItem {
161 private String label;
162
163 private String value;
164
165 private String[] permissions;
166
167 private String virtualHost;
168
169 public String getLabel() {
170 return label;
171 }
172
173 public void setLabel(final String label) {
174 this.label = label;
175 }
176
177 public String getValue() {
178 return value;
179 }
180
181 public void setValue(final String value) {
182 this.value = value;
183 }
184
185 public String[] getPermissions() {
186 return permissions;
187 }
188
189 public void setPermissions(final String[] permissions) {
190 this.permissions = permissions;
191 }
192
193 public String getVirtualHost() {
194 return virtualHost;
195 }
196
197 public void setVirtualHost(final String virtualHost) {
198 this.virtualHost = virtualHost;
199 }
200 }
201
202 public static class LabelTypePattern {
203
204 private final String value;
205
206 private Pattern includedPaths;
207
208 private Pattern excludedPaths;
209
210 public LabelTypePattern(final String value, final String includedPaths, final String excludedPaths) {
211 this.value = value;
212
213 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
214 if (StringUtil.isNotBlank(includedPaths)) {
215 final StringBuilder buf = new StringBuilder(100);
216 char split = 0;
217 for (final String path : includedPaths.split("\n")) {
218 if (split == 0) {
219 split = '|';
220 } else {
221 buf.append(split);
222 }
223 final String normalizePath = systemHelper.normalizeConfigPath(path);
224 if (StringUtil.isNotBlank(normalizePath)) {
225 buf.append(normalizePath);
226 }
227 }
228 this.includedPaths = Pattern.compile(buf.toString());
229 }
230
231 if (StringUtil.isNotBlank(excludedPaths)) {
232 final StringBuilder buf = new StringBuilder(100);
233 char split = 0;
234 for (final String path : excludedPaths.split("\n")) {
235 if (split == 0) {
236 split = '|';
237 } else {
238 buf.append(split);
239 }
240 final String normalizePath = systemHelper.normalizeConfigPath(path);
241 if (StringUtil.isNotBlank(normalizePath)) {
242 buf.append(normalizePath);
243 }
244 }
245 this.excludedPaths = Pattern.compile(buf.toString());
246 }
247 }
248
249 public String getValue() {
250 return value;
251 }
252
253 public boolean match(final String path) {
254 if (includedPaths != null) {
255 if (includedPaths.matcher(path).matches()) {
256 if (excludedPaths != null && excludedPaths.matcher(path).matches()) {
257 if (logger.isDebugEnabled()) {
258 logger.debug("Path " + path + " matched against the excludes paths expression " + excludedPaths.toString());
259 }
260 return false;
261 }
262 return true;
263 }
264 if (logger.isDebugEnabled()) {
265 logger.debug("Path " + path + " wasn't matched against the include paths expression " + includedPaths.toString());
266 }
267 return false;
268 } else {
269 if (excludedPaths != null && excludedPaths.matcher(path).matches()) {
270 if (logger.isDebugEnabled()) {
271 logger.debug("Path " + path + " matched against the excludes paths expression " + excludedPaths.toString());
272 }
273 return false;
274 }
275 return true;
276 }
277 }
278
279 }
280 }