Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -35,6 +34,7 @@
import org.codelibs.fess.llm.LlmClient;
import org.codelibs.fess.llm.LlmClientManager;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.mylasta.direction.FessProp;
import org.codelibs.fess.mylasta.mail.TestmailPostcard;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.ThemeSettingsUtil;
Expand Down Expand Up @@ -328,11 +328,11 @@ public static void updateConfig(final FessConfig fessConfig, final EditForm form
// which means the stored password was left untouched.
fessConfig.setSystemProperty("spnego.preauth.password", form.spnegoPreauthPassword);
}
fessConfig.setSystemProperty("spnego.allow.basic", String.valueOf(isCheckboxEnabled(form.spnegoAllowBasic)));
fessConfig.setSystemProperty("spnego.allow.unsecure.basic", String.valueOf(isCheckboxEnabled(form.spnegoAllowUnsecureBasic)));
fessConfig.setSystemProperty("spnego.prompt.ntlm", String.valueOf(isCheckboxEnabled(form.spnegoPromptNtlm)));
fessConfig.setSystemProperty("spnego.allow.localhost", String.valueOf(isCheckboxEnabled(form.spnegoAllowLocalhost)));
fessConfig.setSystemProperty("spnego.allow.delegation", String.valueOf(isCheckboxEnabled(form.spnegoAllowDelegation)));
fessConfig.setSystemPropertyAsBoolean("spnego.allow.basic", isCheckboxEnabled(form.spnegoAllowBasic));
fessConfig.setSystemPropertyAsBoolean("spnego.allow.unsecure.basic", isCheckboxEnabled(form.spnegoAllowUnsecureBasic));
fessConfig.setSystemPropertyAsBoolean("spnego.prompt.ntlm", isCheckboxEnabled(form.spnegoPromptNtlm));
fessConfig.setSystemPropertyAsBoolean("spnego.allow.localhost", isCheckboxEnabled(form.spnegoAllowLocalhost));
fessConfig.setSystemPropertyAsBoolean("spnego.allow.delegation", isCheckboxEnabled(form.spnegoAllowDelegation));
fessConfig.setSystemProperty("spnego.allowed.realms", form.spnegoAllowedRealms);
fessConfig.setSystemProperty("spnego.logger.level", form.spnegoLoggerLevel);

Expand All @@ -351,7 +351,7 @@ public static void updateConfig(final FessConfig fessConfig, final EditForm form
fessConfig.setSystemProperty("entraid.default.groups", form.entraidDefaultGroups);
fessConfig.setSystemProperty("entraid.default.roles", form.entraidDefaultRoles);
fessConfig.setSystemProperty("entraid.permission.fields", form.entraidPermissionFields);
fessConfig.setSystemProperty("entraid.use.ds", String.valueOf(isCheckboxEnabled(form.entraidUseDs)));
fessConfig.setSystemPropertyAsBoolean("entraid.use.ds", isCheckboxEnabled(form.entraidUseDs));

fessConfig.storeSystemProperties();
ComponentUtil.getLdapManager().updateConfig();
Expand Down Expand Up @@ -570,8 +570,7 @@ private List<String> getDayItems() {
* @return the normalized value; empty when no entry is left
*/
static String normalizeCommaSeparatedValue(final String value) {
return stream(value.split(","))
.get(stream -> stream.map(String::trim).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining(",")));
return FessProp.normalizeCommaSeparatedValue(value);
}

private boolean isRagSectionVisible() {
Expand Down
83 changes: 77 additions & 6 deletions src/main/java/org/codelibs/fess/mylasta/direction/FessProp.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
Expand All @@ -62,6 +63,7 @@
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.ConfigEnvUtil;
import org.codelibs.fess.util.LogUtil;
import org.codelibs.fess.util.SecretKeyUtil;
import org.codelibs.fess.util.ThemeSettingsUtil;
import org.codelibs.fess.util.JvmUtil;
import org.codelibs.fess.util.PrunedTag;
Expand Down Expand Up @@ -205,11 +207,77 @@ private static String getOverrideOrDefault(final String key, final String defaul
default void setSystemProperty(final String key, final String value) {
if (value == null) {
ComponentUtil.getSystemProperties().remove(key);
} else if (!isOverrideValue(key, value::equals)) {
} else if (!isOverrideValue(key, v -> isSameText(key, v, value))) {
ComponentUtil.getSystemProperties().setProperty(key, value);
}
}

/**
* Trims a text and turns its CRLF and CR line breaks into LF, which is what the form mapping of Fess does to a
* text field before a screen's action gets it.
*
* @param value the text, not null
* @return the text as a form sends it
*/
static String normalizeFormText(final String value) {
return value.trim().replace("\r\n", "\n").replace('\r', '\n');
}

/**
* Normalizes a comma-separated setting: trims every entry and drops empty and repeated ones.
*
* @param value the value, not null
* @return the normalized value; empty when no entry is left
*/
static String normalizeCommaSeparatedValue(final String value) {
return stream(value.split(","))
.get(stream -> stream.map(String::trim).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining(",")));
}

/**
* Tells whether a text to be set is the one of {@code -Dfess.system.<key>} or {@code FESS_SYSTEM_<KEY>}, either
* as it is or as a screen sends back the text it showed.
*
* @param key the key of the setting
* @param override the value of the option or the variable
* @param value the value to be set
* @return true if both are the same setting
*/
private static boolean isSameText(final String key, final String override, final String value) {
return override.equals(value) || isShownAsText(key) && normalizeFormText(override).equals(value);
}

/**
* Tells whether the screens show the value of a setting in a text field, so that it comes back unless edited.
* A secret is never shown, and a list cannot show a value that is none of its choices, so what is set for those
* was typed or chosen and is compared as it is: retyping or choosing it without a stray line break is stored.
*
* @param key the key of the setting
* @return true if the value is shown in a text field
*/
private static boolean isShownAsText(final String key) {
if (SecretKeyUtil.isSsoClientCredential(key)) {
return false;
}
return switch (key) {
case "spnego.preauth.password", Constants.STORAGE_ACCESS_KEY, Constants.STORAGE_SECRET_KEY -> false;
case Constants.SSO_TYPE_PROPERTY, Constants.STORAGE_TYPE, Constants.RAG_LLM_NAME, Constants.LOG_NOTIFICATION_LEVEL_PROPERTY, "entraid.response.mode" -> false;
default -> true;
};
}

/**
* Stores a comma-separated setting unless it only is the normalized form of the option or the variable.
*
* @param key the key of the setting
* @param value the normalized value, or the value as it is
*/
private void setCommaSeparatedSystemProperty(final String key, final String value) {
if (value == null || !isOverrideValue(key, v -> normalizeCommaSeparatedValue(v).equals(value))) {
setSystemProperty(key, value);
}
}

/**
* Tells whether a value to be set is the one that a key not stored in system.properties already gets from
* {@code -Dfess.system.<key>} or {@code FESS_SYSTEM_<KEY>}. The admin screens show that value and send it back on
Expand All @@ -234,7 +302,7 @@ default boolean getSystemPropertyAsBoolean(final String key, final boolean defau

default void setSystemPropertyAsBoolean(final String key, final boolean value) {
if (!isOverrideValue(key, v -> Constants.TRUE.equalsIgnoreCase(v) == value)) {
setSystemProperty(key, value ? Constants.TRUE : Constants.FALSE);
ComponentUtil.getSystemProperties().setProperty(key, value ? Constants.TRUE : Constants.FALSE);
}
}

Expand All @@ -258,7 +326,7 @@ default void setSystemPropertyAsInt(final String key, final int value) {
return false;
}
})) {
setSystemProperty(key, Integer.toString(value));
ComponentUtil.getSystemProperties().setProperty(key, Integer.toString(value));
}
}

Expand Down Expand Up @@ -788,7 +856,7 @@ default String getRagLlmName() {
* @param value the comma-separated permissions
*/
default void setRagChatPermissions(final String value) {
setSystemProperty(Constants.RAG_CHAT_PERMISSIONS, value);
setCommaSeparatedSystemProperty(Constants.RAG_CHAT_PERMISSIONS, value);
}

/**
Expand Down Expand Up @@ -828,7 +896,7 @@ default Set<String> getRagChatPermissionSet() {
* @param value the comma-separated label values
*/
default void setRagChatLabels(final String value) {
setSystemProperty(Constants.RAG_CHAT_LABELS, value);
setCommaSeparatedSystemProperty(Constants.RAG_CHAT_LABELS, value);
}

/**
Expand Down Expand Up @@ -949,7 +1017,10 @@ default String getNotificationSearchTop() {
* @param value the settings, one key=value per line
*/
default void setThemeSettings(final String value) {
setSystemProperty(Constants.THEME_SETTINGS_PROPERTY, value);
// The screen sends the settings of the option or the variable back as the lines to store, not as written.
if (!isOverrideValue(Constants.THEME_SETTINGS_PROPERTY, v -> Objects.equals(ThemeSettingsUtil.toStoredValue(v), value))) {
setSystemProperty(Constants.THEME_SETTINGS_PROPERTY, value);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.codelibs.core.misc.Pair;
import org.codelibs.core.stream.StreamUtil;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.mylasta.direction.FessProp;
import org.codelibs.fess.util.ComponentUtil;
import org.dbflute.util.DfTypeUtil;
import org.lastaflute.web.path.ActionAdjustmentProvider;
Expand Down Expand Up @@ -114,7 +115,7 @@ public FormMappingOption adjustFormMapping() {
if (meta.getPropertyName().toLowerCase(Locale.ROOT).endsWith("password")) {
return parameter;
}
return parameter.trim().replace("\r\n", "\n").replace('\r', '\n');
return FessProp.normalizeFormText(parameter);
});
}

Expand Down
Loading
Loading