MtasRequestHandler.java.html
12.4 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasRequestHandler.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.solr.handler</a> > <span class="el_source">MtasRequestHandler.java</span></div><h1>MtasRequestHandler.java</h1><pre class="source lang-java linenums">package mtas.solr.handler;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.noggit.JSONParser;
import org.noggit.ObjectBuilder;
import mtas.analysis.MtasTokenizer;
import mtas.analysis.util.MtasFetchData;
import mtas.analysis.util.MtasParserException;
/**
* The Class MtasRequestHandler.
*/
<span class="fc" id="L30">public class MtasRequestHandler extends RequestHandlerBase {</span>
/** The log. */
<span class="fc" id="L33"> private static Log log = LogFactory.getLog(MtasRequestHandler.class);</span>
/** The Constant ERROR. */
private static final String ERROR = "error";
/** The Constant ACTION_CONFIG_FILES. */
private static final String ACTION_CONFIG_FILES = "files";
/** The Constant ACTION_CONFIG_FILE. */
private static final String ACTION_CONFIG_FILE = "file";
/** The Constant ACTION_MAPPING. */
private static final String ACTION_MAPPING = "mapping";
/** The Constant PARAM_ACTION. */
private static final String PARAM_ACTION = "action";
/** The Constant PARAM_CONFIG_FILE. */
private static final String PARAM_CONFIG_FILE = "file";
/** The Constant PARAM_MAPPING_CONFIGURATION. */
private static final String PARAM_MAPPING_CONFIGURATION = "configuration";
/** The Constant PARAM_MAPPING_DOCUMENT. */
private static final String PARAM_MAPPING_DOCUMENT = "document";
/** The Constant PARAM_MAPPING_DOCUMENT_URL. */
private static final String PARAM_MAPPING_DOCUMENT_URL = "url";
/*
* (non-Javadoc)
*
* @see
* org.apache.solr.handler.RequestHandlerBase#handleRequestBody(org.apache.
* solr.request.SolrQueryRequest, org.apache.solr.response.SolrQueryResponse)
*/
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
throws IOException {
<span class="nc" id="L73"> String configDir = req.getCore().getResourceLoader().getConfigDir();</span>
// generate list of files
<span class="nc" id="L75"> if (req.getParams().get(PARAM_ACTION, "false")</span>
<span class="nc bnc" id="L76" title="All 2 branches missed."> .equals(ACTION_CONFIG_FILES)) {</span>
<span class="nc" id="L77"> rsp.add(ACTION_CONFIG_FILES, getFiles(configDir, null));</span>
// get file
<span class="nc" id="L79"> } else if (req.getParams().get(PARAM_ACTION, "false")</span>
<span class="nc bnc" id="L80" title="All 2 branches missed."> .equals(ACTION_CONFIG_FILE)) {</span>
<span class="nc" id="L81"> String file = req.getParams().get(PARAM_CONFIG_FILE, null);</span>
<span class="nc bnc" id="L82" title="All 4 branches missed."> if (file != null && !file.contains("..")) {</span>
InputStream is;
try {
<span class="nc" id="L85"> is = req.getCore().getResourceLoader().openResource(file);</span>
<span class="nc" id="L86"> rsp.add(ACTION_CONFIG_FILE,</span>
<span class="nc" id="L87"> IOUtils.toString(is, StandardCharsets.UTF_8));</span>
<span class="nc" id="L88"> } catch (IOException e) {</span>
<span class="nc" id="L89"> log.debug(e);</span>
<span class="nc" id="L90"> rsp.add(ERROR, e.getMessage());</span>
<span class="nc" id="L91"> }</span>
}
// test mapping
<span class="nc" id="L94"> } else if (req.getParams().get(PARAM_ACTION, "false")</span>
<span class="nc bnc" id="L95" title="All 2 branches missed."> .equals(ACTION_MAPPING)) {</span>
<span class="nc" id="L96"> String configuration = null;</span>
<span class="nc" id="L97"> String document = null;</span>
<span class="nc" id="L98"> String documentUrl = null;</span>
<span class="nc bnc" id="L99" title="All 2 branches missed."> if (req.getContentStreams() != null) {</span>
<span class="nc" id="L100"> Iterator<ContentStream> it = req.getContentStreams().iterator();</span>
<span class="nc bnc" id="L101" title="All 2 branches missed."> if (it.hasNext()) {</span>
<span class="nc" id="L102"> ContentStream cs = it.next();</span>
<span class="nc" id="L103"> Map<String, String> params = new HashMap<>();</span>
<span class="nc" id="L104"> getParamsFromJSON(params, IOUtils.toString(cs.getReader()));</span>
<span class="nc" id="L105"> configuration = params.get(PARAM_MAPPING_CONFIGURATION);</span>
<span class="nc" id="L106"> document = params.get(PARAM_MAPPING_DOCUMENT);</span>
<span class="nc" id="L107"> documentUrl = params.get(PARAM_MAPPING_DOCUMENT_URL);</span>
}
<span class="nc" id="L109"> } else {</span>
<span class="nc" id="L110"> configuration = req.getParams().get(PARAM_MAPPING_CONFIGURATION);</span>
<span class="nc" id="L111"> document = req.getParams().get(PARAM_MAPPING_DOCUMENT);</span>
<span class="nc" id="L112"> documentUrl = req.getParams().get(PARAM_MAPPING_DOCUMENT_URL);</span>
}
<span class="nc bnc" id="L114" title="All 4 branches missed."> if (configuration != null && documentUrl != null) {</span>
<span class="nc" id="L115"> InputStream stream = IOUtils.toInputStream(configuration,</span>
StandardCharsets.UTF_8);
<span class="nc" id="L117"> try (MtasTokenizer tokenizer = new MtasTokenizer(stream);) {</span>
<span class="nc" id="L118"> MtasFetchData fetchData = new MtasFetchData(</span>
new StringReader(documentUrl));
<span class="nc" id="L120"> rsp.add(ACTION_MAPPING,</span>
<span class="nc" id="L121"> tokenizer.getList(fetchData.getUrl(null, null)));</span>
<span class="nc" id="L122"> tokenizer.close();</span>
<span class="nc bnc" id="L123" title="All 8 branches missed."> } catch (IOException | MtasParserException e) {</span>
<span class="nc" id="L124"> log.debug(e);</span>
<span class="nc" id="L125"> rsp.add(ERROR, e.getMessage());</span>
} finally {
<span class="nc" id="L127"> stream.close();</span>
<span class="nc" id="L128"> }</span>
<span class="nc bnc" id="L129" title="All 4 branches missed."> } else if (configuration != null && document != null) {</span>
<span class="nc" id="L130"> InputStream stream = IOUtils.toInputStream(configuration,</span>
StandardCharsets.UTF_8);
<span class="nc" id="L132"> try (MtasTokenizer tokenizer = new MtasTokenizer(stream);) {</span>
<span class="nc" id="L133"> rsp.add(ACTION_MAPPING,</span>
<span class="nc" id="L134"> tokenizer.getList(new StringReader(document)));</span>
<span class="nc" id="L135"> tokenizer.close();</span>
<span class="nc bnc" id="L136" title="All 8 branches missed."> } catch (IOException e) {</span>
<span class="nc" id="L137"> log.debug(e);</span>
<span class="nc" id="L138"> rsp.add(ERROR, e.getMessage());</span>
} finally {
<span class="nc" id="L140"> stream.close();</span>
<span class="nc" id="L141"> }</span>
}
}
<span class="nc" id="L144"> }</span>
/**
* Gets the files.
*
* @param dir the dir
* @param subDir the sub dir
* @return the files
*/
private ArrayList<String> getFiles(String dir, String subDir) {
<span class="nc" id="L154"> ArrayList<String> files = new ArrayList<>();</span>
<span class="nc bnc" id="L155" title="All 2 branches missed."> String fullDir = subDir == null ? dir : dir + File.separator + subDir;</span>
<span class="nc" id="L156"> File[] listOfFiles = (new File(fullDir)).listFiles();</span>
<span class="nc bnc" id="L157" title="All 2 branches missed."> if(listOfFiles!=null) {</span>
<span class="nc bnc" id="L158" title="All 2 branches missed."> for (File file : listOfFiles) {</span>
<span class="nc bnc" id="L159" title="All 2 branches missed."> String fullName = subDir == null ? file.getName()</span>
<span class="nc" id="L160"> : subDir + File.separator + file.getName();</span>
<span class="nc bnc" id="L161" title="All 2 branches missed."> if (file.isFile()) {</span>
<span class="nc" id="L162"> files.add(fullName);</span>
<span class="nc bnc" id="L163" title="All 2 branches missed."> } else if (file.isDirectory()) {</span>
<span class="nc" id="L164"> files.addAll(getFiles(dir, fullName));</span>
}
}
}
<span class="nc" id="L168"> return files;</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.solr.handler.RequestHandlerBase#getDescription()
*/
@Override
public String getDescription() {
<span class="fc" id="L178"> return "Mtas Request Handler";</span>
}
/**
* Gets the params from JSON.
*
* @param params the params
* @param json the json
* @return the params from JSON
*/
@SuppressWarnings("unchecked")
private static void getParamsFromJSON(Map<String, String> params,
String json) {
<span class="nc" id="L191"> JSONParser parser = new JSONParser(json);</span>
try {
<span class="nc" id="L193"> Object o = ObjectBuilder.getVal(parser);</span>
<span class="nc bnc" id="L194" title="All 2 branches missed."> if (!(o instanceof Map))</span>
<span class="nc" id="L195"> return;</span>
<span class="nc" id="L196"> Map<String, Object> map = (Map<String, Object>) o;</span>
// To make consistent with json.param handling, we should make query
// params come after json params (i.e. query params should
// appear to overwrite json params.
// Solr params are based on String though, so we need to convert
<span class="nc bnc" id="L202" title="All 2 branches missed."> for (Map.Entry<String, Object> entry : map.entrySet()) {</span>
<span class="nc" id="L203"> String key = entry.getKey();</span>
<span class="nc" id="L204"> Object val = entry.getValue();</span>
<span class="nc bnc" id="L205" title="All 2 branches missed."> if (params.get(key) != null) {</span>
<span class="nc" id="L206"> continue;</span>
}
<span class="nc bnc" id="L209" title="All 2 branches missed."> if (val == null) {</span>
<span class="nc" id="L210"> params.remove(key);</span>
<span class="nc bnc" id="L211" title="All 2 branches missed."> } else if (val instanceof String) {</span>
<span class="nc" id="L212"> params.put(key, (String) val);</span>
}
<span class="nc" id="L214"> }</span>
<span class="nc" id="L215"> } catch (Exception e) {</span>
<span class="nc" id="L216"> log.debug(</span>
"ignore parse exceptions at this stage, they may be caused by incomplete macro expansions",
e);
<span class="nc" id="L219"> return;</span>
<span class="nc" id="L220"> }</span>
<span class="nc" id="L221"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>