1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.opensearch.log.exbhv;
17
18 import java.time.Instant;
19 import java.time.LocalDateTime;
20 import java.time.ZoneId;
21 import java.time.format.DateTimeFormatter;
22 import java.time.format.DateTimeParseException;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Pattern;
26
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.codelibs.core.misc.Pair;
30 import org.codelibs.fess.opensearch.log.bsbhv.BsSearchLogBhv;
31 import org.codelibs.fess.opensearch.log.exentity.SearchLog;
32 import org.codelibs.fess.util.ComponentUtil;
33 import org.dbflute.exception.IllegalBehaviorStateException;
34 import org.dbflute.util.DfTypeUtil;
35
36
37
38
39 public class SearchLogBhv extends BsSearchLogBhv {
40 private static final Logger logger = LogManager.getLogger(SearchLogBhv.class);
41
42 private String indexName = null;
43
44 @Override
45 protected String asEsIndex() {
46 if (indexName == null) {
47 final String name = ComponentUtil.getFessConfig().getIndexLogIndex();
48 indexName = super.asEsIndex().replaceFirst(Pattern.quote("fess_log"), name);
49 }
50 return indexName;
51 }
52
53 @Override
54 protected LocalDateTime toLocalDateTime(final Object value) {
55 if (value != null) {
56 try {
57 final Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value.toString()));
58 return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
59 } catch (final DateTimeParseException e) {
60 logger.debug("Invalid date format: {}", value, e);
61 }
62 }
63 return DfTypeUtil.toLocalDateTime(value);
64 }
65
66 @SuppressWarnings("unchecked")
67 @Override
68 protected <RESULT extends SearchLog> RESULT createEntity(final Map<String, Object> source, final Class<? extends RESULT> entityType) {
69 try {
70 final RESULT result = super.createEntity(source, entityType);
71 final Object searchFieldObj = source.get("searchField");
72 if (searchFieldObj instanceof Map) {
73 ((Map<String, ?>) searchFieldObj).entrySet().stream().forEach(e -> {
74 if (e.getValue() instanceof String[]) {
75 final String[] values = (String[]) e.getValue();
76 for (final String v : values) {
77 result.getSearchFieldLogList().add(new Pair<>(e.getKey(), v));
78 }
79 } else if (e.getValue() instanceof List) {
80 final List<String> values = (List<String>) e.getValue();
81 for (final String v : values) {
82 result.getSearchFieldLogList().add(new Pair<>(e.getKey(), v));
83 }
84 } else if (e.getValue() != null) {
85 result.getSearchFieldLogList().add(new Pair<>(e.getKey(), e.getValue().toString()));
86 }
87 });
88 }
89 final Object headersObj = source.get("headers");
90 if (headersObj instanceof Map) {
91 ((Map<String, ?>) headersObj).entrySet().stream().forEach(e -> {
92 if (e.getValue() instanceof String[]) {
93 final String[] values = (String[]) e.getValue();
94 for (final String v : values) {
95 result.getRequestHeaderList().add(new Pair<>(e.getKey(), v));
96 }
97 } else if (e.getValue() instanceof List) {
98 final List<String> values = (List<String>) e.getValue();
99 for (final String v : values) {
100 result.getRequestHeaderList().add(new Pair<>(e.getKey(), v));
101 }
102 } else if (e.getValue() != null) {
103 result.getRequestHeaderList().add(new Pair<>(e.getKey(), e.getValue().toString()));
104 }
105 });
106 }
107 return result;
108 } catch (final Exception e) {
109 final String msg = "Cannot create a new instance: " + entityType.getName();
110 throw new IllegalBehaviorStateException(msg, e);
111 }
112 }
113
114 }