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 .project
# vim swp file # # vim swp file #
*.swp *.swp
# for idea
.idea/
*.iml

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,14 +1,15 @@
package org.lionsoul.jcseg.elasticsearch.util; package org.lionsoul.jcseg.elasticsearch.util;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.lionsoul.jcseg.elasticsearch.plugin.AnalysisJcsegPlugin;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig; import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.File;
import java.nio.file.Path;
public class CommonUtil 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 * get the sementation mode and default to COMPLEX_MODE
@ -25,7 +26,7 @@ public class CommonUtil
* get the segmentation mode * get the segmentation mode
* *
* @param settings * @param settings
* @param int * @param default_mode
* @return int * @return int
*/ */
public static int getSegMode(Settings settings, int default_mode) public static int getSegMode(Settings settings, int default_mode)
@ -53,4 +54,16 @@ public class CommonUtil
return mode; 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();
}
} }