fix #2683 add api.gsa.response.exception.included

This commit is contained in:
Shinsuke Sugaya 2022-09-04 21:10:43 +09:00
parent c64e957ce2
commit 909f505425
4 changed files with 72 additions and 16 deletions

View file

@ -24,6 +24,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.Supplier;
import javax.servlet.http.HttpServletResponse;
@ -56,25 +57,28 @@ public abstract class BaseJsonApiManager extends BaseApiManager {
response.setHeader("WWW-Authenticate", "Bearer error=\"" + e.getType() + "\"");
}
final StringBuilder sb = new StringBuilder();
if (StringUtil.isBlank(t.getMessage())) {
sb.append(t.getClass().getName());
} else {
sb.append(t.getMessage());
}
try (final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw)) {
t.printStackTrace(pw);
pw.flush();
sb.append(" [ ").append(sw.toString()).append(" ]");
} catch (final IOException ignore) {}
final Supplier<String> stacktraceString = () -> {
final StringBuilder sb = new StringBuilder();
if (StringUtil.isBlank(t.getMessage())) {
sb.append(t.getClass().getName());
} else {
sb.append(t.getMessage());
}
try (final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw)) {
t.printStackTrace(pw);
pw.flush();
sb.append(" [ ").append(sw.toString()).append(" ]");
} catch (final IOException ignore) {}
return sb.toString();
};
final String message;
if (Constants.TRUE.equalsIgnoreCase(ComponentUtil.getFessConfig().getApiJsonResponseExceptionIncluded())) {
message = sb.toString();
message = stacktraceString.get();
} else {
final String errorCode = UUID.randomUUID().toString();
message = "error_code:" + errorCode;
if (logger.isDebugEnabled()) {
logger.debug("[{}] {}", errorCode, sb.toString().replace("\n", "\\n"));
logger.debug("[{}] {}", errorCode, stacktraceString.get().replace("\n", "\\n"));
} else {
logger.warn("[{}] {}", errorCode, t.getMessage());
}

View file

@ -16,6 +16,8 @@
package org.codelibs.fess.api.gsa;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
@ -30,6 +32,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -314,9 +318,30 @@ public class GsaApiManager extends BaseApiManager {
}
} catch (final Exception e) {
status = 1;
errMsg = e.getMessage();
if (errMsg == null) {
errMsg = e.getClass().getName();
final Supplier<String> stacktraceString = () -> {
final StringBuilder sb = new StringBuilder();
if (StringUtil.isBlank(e.getMessage())) {
sb.append(e.getClass().getName());
} else {
sb.append(e.getMessage());
}
try (final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw)) {
e.printStackTrace(pw);
pw.flush();
sb.append(" [ ").append(sw.toString()).append(" ]");
} catch (final IOException ignore) {}
return sb.toString();
};
if (Constants.TRUE.equalsIgnoreCase(ComponentUtil.getFessConfig().getApiGsaResponseExceptionIncluded())) {
errMsg = stacktraceString.get();
} else {
final String errorCode = UUID.randomUUID().toString();
errMsg = "error_code:" + errorCode;
if (logger.isDebugEnabled()) {
logger.debug("[{}] {}", errorCode, stacktraceString.get().replace("\n", "\\n"));
} else {
logger.warn("[{}] {}", errorCode, e.getMessage());
}
}
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a search request.", e);

View file

@ -256,6 +256,9 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
/** The key of the configuration. e.g. */
String API_GSA_RESPONSE_HEADERS = "api.gsa.response.headers";
/** The key of the configuration. e.g. false */
String API_GSA_RESPONSE_EXCEPTION_INCLUDED = "api.gsa.response.exception.included";
/** The key of the configuration. e.g. */
String API_DASHBOARD_RESPONSE_HEADERS = "api.dashboard.response.headers";
@ -2333,6 +2336,20 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
*/
Integer getApiGsaResponseHeadersAsInteger();
/**
* Get the value for the key 'api.gsa.response.exception.included'. <br>
* The value is, e.g. false <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
*/
String getApiGsaResponseExceptionIncluded();
/**
* Is the property for the key 'api.gsa.response.exception.included' true? <br>
* The value is, e.g. false <br>
* @return The determination, true or false. (if not found, exception but basically no way)
*/
boolean isApiGsaResponseExceptionIncluded();
/**
* Get the value for the key 'api.dashboard.response.headers'. <br>
* The value is, e.g. <br>
@ -7688,6 +7705,14 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
return getAsInteger(FessConfig.API_GSA_RESPONSE_HEADERS);
}
public String getApiGsaResponseExceptionIncluded() {
return get(FessConfig.API_GSA_RESPONSE_EXCEPTION_INCLUDED);
}
public boolean isApiGsaResponseExceptionIncluded() {
return is(FessConfig.API_GSA_RESPONSE_EXCEPTION_INCLUDED);
}
public String getApiDashboardResponseHeaders() {
return get(FessConfig.API_DASHBOARD_RESPONSE_HEADERS);
}
@ -10467,6 +10492,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
defaultMap.put(FessConfig.API_JSON_RESPONSE_HEADERS, "");
defaultMap.put(FessConfig.API_JSON_RESPONSE_EXCEPTION_INCLUDED, "false");
defaultMap.put(FessConfig.API_GSA_RESPONSE_HEADERS, "");
defaultMap.put(FessConfig.API_GSA_RESPONSE_EXCEPTION_INCLUDED, "false");
defaultMap.put(FessConfig.API_DASHBOARD_RESPONSE_HEADERS, "");
defaultMap.put(FessConfig.API_CORS_ALLOW_ORIGIN, "*");
defaultMap.put(FessConfig.API_CORS_ALLOW_METHODS, "GET, POST, OPTIONS, DELETE, PUT");

View file

@ -172,6 +172,7 @@ api.search.scroll=false
api.json.response.headers=
api.json.response.exception.included=false
api.gsa.response.headers=
api.gsa.response.exception.included=false
api.dashboard.response.headers=
api.cors.allow.origin=*
api.cors.allow.methods=GET, POST, OPTIONS, DELETE, PUT