Skip to content

Commit

Permalink
fix #2848 Refactor code: Replace redundant conditions and enhance rea…
Browse files Browse the repository at this point in the history
…dability across several classes.
  • Loading branch information
marevol committed Oct 11, 2024
1 parent 173a05d commit 063a714
Show file tree
Hide file tree
Showing 70 changed files with 368 additions and 315 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/codelibs/fess/FessBoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public void setupServerConfigIfNeeds(final BootLogger logger, final Tomcat serve
});
doSetupServerConfig(logger, props, "sameSiteCookies", value -> {
for (final Container container : server.getHost().findChildren()) {
if ((container instanceof final Context context)
&& (context.getCookieProcessor() instanceof final CookieProcessorBase cookieProcessor)) {
if (container instanceof final Context context
&& context.getCookieProcessor() instanceof final CookieProcessorBase cookieProcessor) {
cookieProcessor.setSameSiteCookies(value);
}
}
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/codelibs/fess/api/json/SearchApiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ protected FormatType detectFormatType(final HttpServletRequest request) {
return FormatType.SEARCH;
}
final String type = value.toLowerCase(Locale.ROOT);
if ("documents".equals(type)) {
switch (type) {
case "documents":
if (values.length > 5 && "favorite".equals(values[5])) {
request.setAttribute(DOC_ID_FIELD, values[4]);
return FormatType.FAVORITE;
Expand All @@ -129,21 +130,18 @@ protected FormatType detectFormatType(final HttpServletRequest request) {
return FormatType.SCROLL;
}
return FormatType.SEARCH;
}
if ("labels".equals(type)) {
case "labels":
return FormatType.LABEL;
}
if ("popular-words".equals(type)) {
case "popular-words":
return FormatType.POPULARWORD;
}
if ("favorites".equals(type)) {
case "favorites":
return FormatType.FAVORITES;
}
if ("health".equals(type)) {
case "health":
return FormatType.PING;
}
if ("suggest-words".equals(type)) {
case "suggest-words":
return FormatType.SUGGEST;
default:
break;
}
// default
return FormatType.OTHER;
Expand Down Expand Up @@ -1247,8 +1245,8 @@ protected String escapeJson(final Object obj) {
buf.append(escapeJson(entry.getKey())).append(':').append(escapeJson(entry.getValue()));
}
buf.append('}');
} else if ((obj instanceof Integer) || (obj instanceof Long) || (obj instanceof Float) || (obj instanceof Double)) {
buf.append((obj));
} else if (obj instanceof Integer || obj instanceof Long || obj instanceof Float || obj instanceof Double) {
buf.append(obj);
} else if (obj instanceof Boolean) {
buf.append(obj.toString());
} else if (obj instanceof Date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,17 @@ public ActionResponse download(final String id) {
}
if (id.endsWith(NDJSON_EXTENTION)) {
final String name = id.substring(0, id.length() - NDJSON_EXTENTION.length());
if ("search_log".equals(name)) {
switch (name) {
case "search_log":
return writeNdjsonResponse(id, getSearchLogNdjsonWriteCall());
}
if ("user_info".equals(name)) {
case "user_info":
return writeNdjsonResponse(id, getUserInfoNdjsonWriteCall());
}
if ("click_log".equals(name)) {
case "click_log":
return writeNdjsonResponse(id, getClickLogNdjsonWriteCall());
}
if ("favorite_log".equals(name)) {
case "favorite_log":
return writeNdjsonResponse(id, getFavoriteLogNdjsonWriteCall());
default:
break;
}
} else if ("fess.json".equals(id)) {
return asStream(id).contentTypeOctetStream().stream(out -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -359,12 +358,12 @@ private HtmlResponse asEditHtml(final EditForm form) {

public static String decodeJsp(final String value) {
return value.replaceAll("<%(?![@-])([\\s\\S]*?)%>", "&lt;%$1%&gt;").replaceAll("<%=([\\s\\S]*?)%>", "&lt;%=$1%&gt;")
.replaceAll(TRY_STATEMENT, "<% try{ %>")
.replaceAll(CACHE_AND_SESSION_INVALIDATE_STATEMENT, "<% }catch(Exception e){session.invalidate();} %>");
.replace(TRY_STATEMENT, "<% try{ %>")
.replace(CACHE_AND_SESSION_INVALIDATE_STATEMENT, "<% }catch(Exception e){session.invalidate();} %>");
}

public static String encodeJsp(final String value) {
return value.replaceAll(Pattern.quote("<% try{ %>"), TRY_STATEMENT)
.replaceAll(Pattern.quote("<% }catch(Exception e){session.invalidate();} %>"), CACHE_AND_SESSION_INVALIDATE_STATEMENT);
return value.replace("<% try{ %>", TRY_STATEMENT).replace("<% }catch(Exception e){session.invalidate();} %>",
CACHE_AND_SESSION_INVALIDATE_STATEMENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ protected boolean startReindex(final boolean replaceAliases, final boolean reset
if (searchEngineClient.createIndex(docIndex, toIndex, numberOfShards, autoExpandReplicas, resetDictionaries)) {
searchEngineClient.admin().cluster().prepareHealth(toIndex).setWaitForYellowStatus().execute(ActionListener.wrap(response -> {
searchEngineClient.addMapping(docIndex, "doc", toIndex);
if (searchEngineClient.copyDocIndex(fromIndex, toIndex, replaceAliases)
&& (replaceAliases && !searchEngineClient.updateAlias(toIndex))) {
if (searchEngineClient.copyDocIndex(fromIndex, toIndex, replaceAliases) && replaceAliases
&& !searchEngineClient.updateAlias(toIndex)) {
logger.warn("Failed to update aliases for {} and {}", fromIndex, toIndex);
}
}, e -> logger.warn("Failed to reindex from {} to {}", fromIndex, toIndex, e)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,18 @@ public HtmlResponse createnewjob(final String type, final String id, final Strin
final String decodedName = new String(Base64.getUrlDecoder().decode(name), Constants.CHARSET_UTF_8);
scheduledJobForm.name = MessageFormat.format(fessConfig.getJobTemplateTitle(type), decodedName);
final String[] ids = { "", "", "" };
if (Constants.WEB_CRAWLER_TYPE.equals(type)) {
switch (type) {
case Constants.WEB_CRAWLER_TYPE:
ids[0] = "\"" + id + "\"";
} else if (Constants.FILE_CRAWLER_TYPE.equals(type)) {
break;
case Constants.FILE_CRAWLER_TYPE:
ids[1] = "\"" + id + "\"";
} else if (Constants.DATA_CRAWLER_TYPE.equals(type)) {
break;
case Constants.DATA_CRAWLER_TYPE:
ids[2] = "\"" + id + "\"";
break;
default:
break;
}
scheduledJobForm.scriptData =
MessageFormat.format(fessConfig.getJobTemplateScript(), ids[0], ids[1], ids[2], id.replace('-', '_'));
Expand Down
Loading

0 comments on commit 063a714

Please sign in to comment.