-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVulnAnalyzerMapper.java
186 lines (154 loc) · 5.6 KB
/
VulnAnalyzerMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.*;
import org.elasticsearch.search.highlight.HighlighterContext;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.index.mapper.MapperBuilders.analyzer;
/**
*
*/
public class AnalyzerMapper implements Mapper, InternalMapper, RootMapper {
public static final String NAME = "_analyzer";
public static final String CONTENT_TYPE = "_analyzer";
public static class Defaults {
public static final String PATH = "_analyzer";
}
public static class Builder extends Mapper.Builder<Builder, AnalyzerMapper> {
private String field = Defaults.PATH;
public Builder() {
super(CONTENT_TYPE);
this.builder = this;
}
public Builder field(String field) {
this.field = field;
return this;
}
@Override
public AnalyzerMapper build(BuilderContext context) {
return new AnalyzerMapper(field);
}
}
public static class TypeParser implements Mapper.TypeParser {
@Override
public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
AnalyzerMapper.Builder builder = analyzer();
for (Map.Entry<String, Object> entry : node.entrySet()) {
String fieldName = Strings.toUnderscoreCase(entry.getKey());
Object fieldNode = entry.getValue();
if (fieldName.equals("path")) {
builder.field(fieldNode.toString());
}
}
return builder;
}
}
private final String path;
public AnalyzerMapper() {
this(Defaults.PATH);
}
public AnalyzerMapper(String path) {
this.path = path.intern();
}
@Override
public String name() {
return CONTENT_TYPE;
}
@Override
public void preParse(ParseContext context) throws IOException {
}
@Override
public void postParse(ParseContext context) throws IOException {
Analyzer analyzer = context.docMapper().mappers().indexAnalyzer();
if (path != null) {
String value = null;
List<IndexableField> fields = context.doc().getFields();
for (int i = 0, fieldsSize = fields.size(); i < fieldsSize; i++) {
IndexableField field = fields.get(i);
if (field.name() == path) {
value = field.stringValue();
break;
}
}
if (value == null) {
value = context.ignoredValue(path);
}
if (value != null) {
analyzer = context.analysisService().analyzer(value);
if (analyzer == null) {
throw new MapperParsingException("No analyzer found for [" + value + "] from path [" + path + "]");
}
analyzer = context.docMapper().mappers().indexAnalyzer(analyzer);
}
}
context.analyzer(analyzer);
}
@Override
public boolean includeInObject() {
return false;
}
public Analyzer setAnalyzer(HighlighterContext context){
if (context.analyzer() != null){
return context.analyzer();
}
Analyzer analyzer = null;
if (path != null) {
String analyzerName = (String) context.context.lookup().source().extractValue(path);
analyzer = context.context.mapperService().analysisService().analyzer(analyzerName);
}
if (analyzer == null) {
analyzer = context.context.mapperService().documentMapper(context.hitContext.hit().type()).mappers().indexAnalyzer();
}
context.analyzer(analyzer);
return analyzer;
}
@Override
public void parse(ParseContext context) throws IOException {
}
@Override
public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappingException {
}
@Override
public void traverse(FieldMapperListener fieldMapperListener) {
}
@Override
public void traverse(ObjectMapperListener objectMapperListener) {
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (path.equals(Defaults.PATH)) {
return builder;
}
builder.startObject(CONTENT_TYPE);
if (!path.equals(Defaults.PATH)) {
builder.field("path", path);
}
builder.endObject();
return builder;
}
@Override
public void close() {
}
}