for elasticsearch safe file resource access

This commit is contained in:
lionsoul 2018-09-30 14:46:07 +08:00
parent 3de9501505
commit 70dcc7ef37
11 changed files with 255 additions and 234 deletions

4
.gitignore vendored
View File

@ -15,3 +15,7 @@ target/
.project
# vim swp file #
*.swp
# for idea
.idea/
*.iml

View File

@ -1,6 +1,7 @@
package org.lionsoul.jcseg.elasticsearch.index.analysis;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
@ -9,6 +10,7 @@ import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractIndexAnalyzerProvider;
import org.lionsoul.jcseg.analyzer.JcsegAnalyzer;
import org.lionsoul.jcseg.elasticsearch.util.CommonUtil;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
/**
* JcsegAnalyzerProvider
@ -22,14 +24,12 @@ public abstract class JcsegAnalyzerProvider extends AbstractIndexAnalyzerProvide
@Inject
public JcsegAnalyzerProvider(
IndexSettings indexSettings, Environment env, String name, Settings settings)
{
IndexSettings indexSettings, Environment env, String name, Settings settings) throws IOException {
super(indexSettings, name, settings);
File proFile = new File(settings.get("config_file", CommonUtil.JcsegConfigFile));
analyzer = proFile.exists() ?
new JcsegAnalyzer(this.getSegMode(), proFile.getPath()) :
new JcsegAnalyzer(this.getSegMode());
JcsegTaskConfig config = new JcsegTaskConfig(new FileInputStream(CommonUtil.getPluginSafeFile("jcseg.properties")));
config.setAutoload(false); // disable the autoload of the lexicon
analyzer = new JcsegAnalyzer(this.getSegMode(), config);
}
protected abstract int getSegMode();

View File

@ -5,6 +5,8 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.IOException;
/**
* Jcseg simple Analyzer Provider
*
@ -13,8 +15,7 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
public class JcsegComplexAnalyzerProvider extends JcsegAnalyzerProvider
{
public JcsegComplexAnalyzerProvider(IndexSettings indexSettings,
Environment env, String name, Settings settings)
{
Environment env, String name, Settings settings) throws IOException {
super(indexSettings, env, name, settings);
}

View File

@ -5,6 +5,8 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.IOException;
/**
* Jcseg simple Analyzer Provider
*
@ -13,8 +15,7 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
public class JcsegDelimiterAnalyzerProvider extends JcsegAnalyzerProvider
{
public JcsegDelimiterAnalyzerProvider(IndexSettings indexSettings,
Environment env, String name, Settings settings)
{
Environment env, String name, Settings settings) throws IOException {
super(indexSettings, env, name, settings);
}

View File

@ -5,6 +5,8 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.IOException;
/**
* Jcseg simple Analyzer Provider
*
@ -13,8 +15,7 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
public class JcsegDetectAnalyzerProvider extends JcsegAnalyzerProvider
{
public JcsegDetectAnalyzerProvider(IndexSettings indexSettings,
Environment env, String name, Settings settings)
{
Environment env, String name, Settings settings) throws IOException {
super(indexSettings, env, name, settings);
}

View File

@ -5,6 +5,8 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.IOException;
/**
* Jcseg simple Analyzer Provider
*
@ -13,8 +15,7 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
public class JcsegNLPAnalyzerProvider extends JcsegAnalyzerProvider
{
public JcsegNLPAnalyzerProvider(IndexSettings indexSettings,
Environment env, String name, Settings settings)
{
Environment env, String name, Settings settings) throws IOException {
super(indexSettings, env, name, settings);
}

View File

@ -5,6 +5,8 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.IOException;
/**
* Jcseg simple Analyzer Provider
*
@ -13,8 +15,7 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
public class JcsegSearchAnalyzerProvider extends JcsegAnalyzerProvider
{
public JcsegSearchAnalyzerProvider(IndexSettings indexSettings,
Environment env, String name, Settings settings)
{
Environment env, String name, Settings settings) throws IOException {
super(indexSettings, env, name, settings);
}

View File

@ -5,6 +5,8 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.IOException;
/**
* Jcseg simple Analyzer Provider
*
@ -13,8 +15,7 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
public class JcsegSimpleAnalyzerProvider extends JcsegAnalyzerProvider
{
public JcsegSimpleAnalyzerProvider(IndexSettings indexSettings,
Environment env, String name, Settings settings)
{
Environment env, String name, Settings settings) throws IOException {
super(indexSettings, env, name, settings);
}

View File

@ -1,6 +1,6 @@
package org.lionsoul.jcseg.elasticsearch.index.analysis;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.lucene.analysis.Tokenizer;
@ -17,16 +17,16 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
public class JcsegTokenizerTokenizerFactory extends AbstractTokenizerFactory
{
private JcsegTaskConfig config;
private ADictionary dic;
private int mode;
private final JcsegTaskConfig config;
private final ADictionary dic;
private final int mode;
public JcsegTokenizerTokenizerFactory(
IndexSettings indexSettings, Environment environment, String name, Settings settings) {
IndexSettings indexSettings, Environment environment, String name, Settings settings) throws IOException {
super(indexSettings, name, settings);
File proFile = new File(settings.get("config_file", CommonUtil.JcsegConfigFile));
config = proFile.exists() ? new JcsegTaskConfig(proFile.getPath()) : new JcsegTaskConfig(true);
config = new JcsegTaskConfig(new FileInputStream(CommonUtil.getPluginSafeFile("jcseg.properties")));
config.setAutoload(false); // disable the autoload of the lexicon
mode = CommonUtil.getSegMode(settings, JcsegTaskConfig.SEARCH_MODE);
dic = DictionaryFactory.createSingletonDictionary(config);
}
@ -36,9 +36,7 @@ public class JcsegTokenizerTokenizerFactory extends AbstractTokenizerFactory
{
try {
return new JcsegTokenizer(mode, config, dic);
} catch (JcsegException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (JcsegException | IOException e) {
e.printStackTrace();
}

View File

@ -1,14 +1,15 @@
package org.lionsoul.jcseg.elasticsearch.util;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.settings.Settings;
import org.lionsoul.jcseg.elasticsearch.plugin.AnalysisJcsegPlugin;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.File;
import java.nio.file.Path;
public class CommonUtil
{
/**
* The default jcseg configuration file
*/
public static final String JcsegConfigFile = "plugins/jcseg/jcseg.properties";
/**
* get the sementation mode and default to COMPLEX_MODE
@ -25,7 +26,7 @@ public class CommonUtil
* get the segmentation mode
*
* @param settings
* @param int
* @param default_mode
* @return int
*/
public static int getSegMode(Settings settings, int default_mode)
@ -53,4 +54,16 @@ public class CommonUtil
return mode;
}
/**
* Quick interface to get a safe file path
*
* @param file
*/
private static final String pluginBase = AnalysisJcsegPlugin.class.getProtectionDomain().getCodeSource().getLocation().getPath();
private static final Path safePath = PathUtils.get(new File(pluginBase).getParent()).toAbsolutePath();
public static File getPluginSafeFile(String file)
{
return safePath.resolve(file).toFile();
}
}