use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class QueryStringBuilder method appendConditions.
protected void appendConditions(final StringBuilder queryBuf, final Map<String, String[]> conditions) {
if (conditions == null) {
return;
}
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final int maxQueryLength = fessConfig.getQueryMaxLengthAsInteger();
stream(conditions.get(SearchRequestParams.AS_OCCURRENCE)).of(stream -> stream.filter(this::isOccurrence).findFirst().ifPresent(q -> queryBuf.insert(0, q + ":")));
stream(conditions.get(SearchRequestParams.AS_Q)).of(stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= maxQueryLength).forEach(q -> queryBuf.append(' ').append(q)));
stream(conditions.get(SearchRequestParams.AS_EPQ)).of(stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= maxQueryLength).forEach(q -> queryBuf.append(" \"").append(escape(q, "\"")).append('"')));
stream(conditions.get(SearchRequestParams.AS_OQ)).of(stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= maxQueryLength).forEach(oq -> split(oq, " ").get(s -> s.filter(StringUtil::isNotBlank).reduce((q1, q2) -> escape(q1, "(", ")") + OR + escape(q2, "(", ")"))).ifPresent(q -> {
appendQuery(queryBuf, q);
})));
stream(conditions.get(SearchRequestParams.AS_NQ)).of(stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= maxQueryLength).forEach(eq -> {
final String nq = split(eq, " ").get(s -> s.filter(StringUtil::isNotBlank).map(q -> "NOT " + q).collect(Collectors.joining(" ")));
queryBuf.append(' ').append(nq);
}));
stream(conditions.get(SearchRequestParams.AS_FILETYPE)).of(stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= maxQueryLength).forEach(q -> queryBuf.append(" filetype:\"").append(q.trim()).append('"')));
stream(conditions.get(SearchRequestParams.AS_SITESEARCH)).of(stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= maxQueryLength).forEach(q -> queryBuf.append(" site:").append(q.trim())));
stream(conditions.get(SearchRequestParams.AS_TIMESTAMP)).of(stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= maxQueryLength).forEach(q -> queryBuf.append(" timestamp:").append(q.trim())));
}
use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class ApiAdminElevatewordAction method createEditBody.
protected EditBody createEditBody(final ElevateWord entity) {
final EditBody body = new EditBody();
copyBeanToBean(entity, body, copyOp -> {
copyOp.excludeNull();
copyOp.exclude(Constants.PERMISSIONS);
});
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
body.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s)).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
return body;
}
use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class ApiAdminFileconfigAction method createEditBody.
protected EditBody createEditBody(final FileConfig entity) {
final EditBody body = new EditBody();
copyBeanToBean(entity, body, copyOp -> {
copyOp.excludeNull();
copyOp.exclude(Constants.PERMISSIONS, Constants.VIRTUAL_HOSTS);
});
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
body.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s)).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
body.virtualHosts = stream(entity.getVirtualHosts()).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().map(String::trim).collect(Collectors.joining("\n")));
return body;
}
use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class ApiAdminDataconfigAction method createEditBody.
protected EditBody createEditBody(final DataConfig entity) {
final EditBody body = new EditBody();
copyBeanToBean(entity, body, copyOp -> {
copyOp.excludeNull();
copyOp.exclude(Constants.PERMISSIONS, Constants.VIRTUAL_HOSTS);
});
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
body.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s)).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
body.virtualHosts = stream(entity.getVirtualHosts()).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().map(String::trim).collect(Collectors.joining("\n")));
return body;
}
use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class SuggestJob method executeSuggestCreator.
protected void executeSuggestCreator() {
final List<String> cmdList = new ArrayList<>();
final String cpSeparator = SystemUtils.IS_OS_WINDOWS ? ";" : ":";
final ServletContext servletContext = ComponentUtil.getComponent(ServletContext.class);
final ProcessHelper processHelper = ComponentUtil.getProcessHelper();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
cmdList.add(fessConfig.getJavaCommandPath());
// -cp
cmdList.add("-cp");
final StringBuilder buf = new StringBuilder(100);
ResourceUtil.getOverrideConfPath().ifPresent(p -> {
buf.append(p);
buf.append(cpSeparator);
});
final String confPath = System.getProperty(Constants.FESS_CONF_PATH);
if (StringUtil.isNotBlank(confPath)) {
buf.append(confPath);
buf.append(cpSeparator);
}
// WEB-INF/env/suggest/resources
buf.append("WEB-INF");
buf.append(File.separator);
buf.append("env");
buf.append(File.separator);
buf.append(getExecuteType());
buf.append(File.separator);
buf.append("resources");
buf.append(cpSeparator);
// WEB-INF/classes
buf.append("WEB-INF");
buf.append(File.separator);
buf.append("classes");
// target/classes
final String userDir = System.getProperty("user.dir");
final File targetDir = new File(userDir, "target");
final File targetClassesDir = new File(targetDir, "classes");
if (targetClassesDir.isDirectory()) {
buf.append(cpSeparator);
buf.append(targetClassesDir.getAbsolutePath());
}
// WEB-INF/lib
appendJarFile(cpSeparator, buf, new File(servletContext.getRealPath("/WEB-INF/lib")), "WEB-INF" + File.separator + "lib" + File.separator);
// WEB-INF/env/suggest/lib
appendJarFile(cpSeparator, buf, new File(servletContext.getRealPath("/WEB-INF/env/" + getExecuteType() + "/lib")), "WEB-INF" + File.separator + "env" + File.separator + getExecuteType() + File.separator + "lib" + File.separator);
// WEB-INF/plugin
appendJarFile(cpSeparator, buf, new File(servletContext.getRealPath("/WEB-INF/plugin")), "WEB-INF" + File.separator + "plugin" + File.separator);
final File targetLibDir = new File(targetDir, "fess" + File.separator + "WEB-INF" + File.separator + "lib");
if (targetLibDir.isDirectory()) {
appendJarFile(cpSeparator, buf, targetLibDir, targetLibDir.getAbsolutePath() + File.separator);
}
cmdList.add(buf.toString());
if (useLocalFesen) {
final String httpAddress = System.getProperty(Constants.FESS_ES_HTTP_ADDRESS);
if (StringUtil.isNotBlank(httpAddress)) {
cmdList.add("-D" + Constants.FESS_ES_HTTP_ADDRESS + "=" + httpAddress);
}
}
final String systemLastaEnv = System.getProperty("lasta.env");
if (StringUtil.isNotBlank(systemLastaEnv)) {
if ("web".equals(systemLastaEnv)) {
cmdList.add("-Dlasta.env=" + getExecuteType());
} else {
cmdList.add("-Dlasta.env=" + systemLastaEnv);
}
} else if (StringUtil.isNotBlank(lastaEnv)) {
cmdList.add("-Dlasta.env=" + lastaEnv);
} else {
cmdList.add("-Dlasta.env=" + getExecuteType());
}
addFessConfigProperties(cmdList);
addFessSystemProperties(cmdList);
addSystemProperty(cmdList, Constants.FESS_CONF_PATH, null, null);
cmdList.add("-Dfess." + getExecuteType() + ".process=true");
if (logFilePath == null) {
final String value = System.getProperty("fess.log.path");
logFilePath = value != null ? value : new File(targetDir, "logs").getAbsolutePath();
}
cmdList.add("-Dfess.log.path=" + logFilePath);
addSystemProperty(cmdList, "fess.log.name", "fess-" + getExecuteType(), "-" + getExecuteType());
if (logLevel == null) {
addSystemProperty(cmdList, "fess.log.level", null, null);
} else {
cmdList.add("-Dfess.log.level=" + logLevel);
}
stream(fessConfig.getJvmSuggestOptionsAsArray()).of(stream -> stream.filter(StringUtil::isNotBlank).forEach(value -> cmdList.add(value)));
File ownTmpDir = null;
final String tmpDir = System.getProperty("java.io.tmpdir");
if (fessConfig.isUseOwnTmpDir() && StringUtil.isNotBlank(tmpDir)) {
ownTmpDir = new File(tmpDir, "fessTmpDir_" + sessionId);
if (ownTmpDir.mkdirs()) {
cmdList.add("-Djava.io.tmpdir=" + ownTmpDir.getAbsolutePath());
} else {
ownTmpDir = null;
}
}
if (!jvmOptions.isEmpty()) {
jvmOptions.stream().filter(StringUtil::isNotBlank).forEach(cmdList::add);
}
cmdList.add(SuggestCreator.class.getCanonicalName());
cmdList.add("--sessionId");
cmdList.add(sessionId);
final File propFile = ComponentUtil.getSystemHelper().createTempFile(getExecuteType() + "_", ".properties");
try {
cmdList.add("-p");
cmdList.add(propFile.getAbsolutePath());
createSystemProperties(cmdList, propFile);
final File baseDir = new File(servletContext.getRealPath("/WEB-INF")).getParentFile();
if (logger.isInfoEnabled()) {
logger.info("SuggestCreator: \nDirectory={}\nOptions={}", baseDir, cmdList);
}
final JobProcess jobProcess = processHelper.startProcess(sessionId, cmdList, pb -> {
pb.directory(baseDir);
pb.redirectErrorStream(true);
});
final InputStreamThread it = jobProcess.getInputStreamThread();
it.start();
final Process currentProcess = jobProcess.getProcess();
currentProcess.waitFor();
it.join(5000);
final int exitValue = currentProcess.exitValue();
if (logger.isInfoEnabled()) {
logger.info("SuggestCreator: Exit Code={} - Process Output:\n{}", exitValue, it.getOutput());
}
if (exitValue != 0) {
final StringBuilder out = new StringBuilder();
if (processTimeout) {
out.append("Process is terminated due to ").append(timeout).append(" second exceeded.\n");
}
out.append("Exit Code: ").append(exitValue).append("\nOutput:\n").append(it.getOutput());
throw new JobProcessingException(out.toString());
}
ComponentUtil.getPopularWordHelper().clearCache();
} catch (final JobProcessingException e) {
throw e;
} catch (final Exception e) {
throw new JobProcessingException("SuggestCreator Process terminated.", e);
} finally {
try {
processHelper.destroyProcess(sessionId);
} finally {
if (propFile != null && !propFile.delete()) {
logger.warn("Failed to delete {}.", propFile.getAbsolutePath());
}
deleteTempDir(ownTmpDir);
}
}
}
Aggregations