fix #2521 fix an exception handling

This commit is contained in:
Shinsuke Sugaya 2021-01-27 06:23:10 +09:00
parent 6d19b53299
commit 9eea4524ad
5 changed files with 67 additions and 35 deletions

View file

@ -39,6 +39,7 @@ import org.codelibs.fess.util.RenderDataUtil;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.HtmlResponse;
import org.lastaflute.web.ruts.process.ActionRuntime;
import org.lastaflute.web.validation.exception.ValidationErrorException;
public class AdminPluginAction extends FessAdminAction {
@ -82,47 +83,53 @@ public class AdminPluginAction extends FessAdminAction {
public HtmlResponse install(final InstallForm form) {
validate(form, messages -> {}, () -> asHtml(path_AdminPlugin_AdminPluginInstallpluginJsp));
verifyToken(() -> asHtml(path_AdminPlugin_AdminPluginInstallpluginJsp));
if (UPLOAD.equals(form.id)) {
if (form.jarFile == null) {
throwValidationError(messages -> messages.addErrorsPluginFileIsNotFound(GLOBAL, form.id), this::asListHtml);
}
if (!form.jarFile.getFileName().endsWith(".jar")) {
throwValidationError(messages -> messages.addErrorsFileIsNotSupported(GLOBAL, form.jarFile.getFileName()),
this::asListHtml);
}
final String filename = form.jarFile.getFileName();
final File tempFile = ComponentUtil.getSystemHelper().createTempFile("tmp-adminplugin-", ".jar");
try (final InputStream is = form.jarFile.getInputStream(); final OutputStream os = new FileOutputStream(tempFile)) {
CopyUtil.copy(is, os);
} catch (final Exception e) {
if (tempFile.exists() && !tempFile.delete()) {
logger.warn("Failed to delete {}.", tempFile.getAbsolutePath());
try {
if (UPLOAD.equals(form.id)) {
if (form.jarFile == null) {
throwValidationError(messages -> messages.addErrorsPluginFileIsNotFound(GLOBAL, form.id), this::asListHtml);
}
logger.debug("Failed to copy {}", filename, e);
throwValidationError(messages -> messages.addErrorsFailedToInstallPlugin(GLOBAL, filename), this::asListHtml);
}
new Thread(() -> {
try {
final PluginHelper pluginHelper = ComponentUtil.getPluginHelper();
final Artifact artifact =
pluginHelper.getArtifactFromFileName(ArtifactType.UNKNOWN, filename, tempFile.getAbsolutePath());
pluginHelper.installArtifact(artifact);
if (!form.jarFile.getFileName().endsWith(".jar")) {
throwValidationError(messages -> messages.addErrorsFileIsNotSupported(GLOBAL, form.jarFile.getFileName()),
this::asListHtml);
}
final String filename = form.jarFile.getFileName();
final File tempFile = ComponentUtil.getSystemHelper().createTempFile("tmp-adminplugin-", ".jar");
try (final InputStream is = form.jarFile.getInputStream(); final OutputStream os = new FileOutputStream(tempFile)) {
CopyUtil.copy(is, os);
} catch (final Exception e) {
logger.warn("Failed to install {}", filename, e);
} finally {
if (tempFile.exists() && !tempFile.delete()) {
logger.warn("Failed to delete {}.", tempFile.getAbsolutePath());
}
logger.debug("Failed to copy {}", filename, e);
throwValidationError(messages -> messages.addErrorsFailedToInstallPlugin(GLOBAL, filename), this::asListHtml);
}
}).start();
saveInfo(messages -> messages.addSuccessInstallPlugin(GLOBAL, form.jarFile.getFileName()));
} else {
final Artifact artifact = getArtifactFromInstallForm(form);
if (artifact == null) {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), this::asListHtml);
new Thread(() -> {
try {
final PluginHelper pluginHelper = ComponentUtil.getPluginHelper();
final Artifact artifact =
pluginHelper.getArtifactFromFileName(ArtifactType.UNKNOWN, filename, tempFile.getAbsolutePath());
pluginHelper.installArtifact(artifact);
} catch (final Exception e) {
logger.warn("Failed to install {}", filename, e);
} finally {
if (tempFile.exists() && !tempFile.delete()) {
logger.warn("Failed to delete {}.", tempFile.getAbsolutePath());
}
}
}).start();
saveInfo(messages -> messages.addSuccessInstallPlugin(GLOBAL, form.jarFile.getFileName()));
} else {
final Artifact artifact = getArtifactFromInstallForm(form);
if (artifact == null) {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), this::asListHtml);
}
installArtifact(artifact);
saveInfo(messages -> messages.addSuccessInstallPlugin(GLOBAL, artifact.getFileName()));
}
installArtifact(artifact);
saveInfo(messages -> messages.addSuccessInstallPlugin(GLOBAL, artifact.getFileName()));
} catch (final ValidationErrorException e) {
throw e;
} catch (final Exception e) {
throwValidationError(messages -> messages.addErrorsFailedToInstallPlugin(GLOBAL, form.id), this::asListHtml);
}
return redirect(getClass());
}
@ -138,7 +145,12 @@ public class AdminPluginAction extends FessAdminAction {
map.put("name", "");
map.put("version", "");
result.add(map);
result.addAll(getAllAvailableArtifacts());
try {
result.addAll(getAllAvailableArtifacts());
} catch (Exception e) {
saveError(messages -> messages.addErrorsFailedToFindPlugins(GLOBAL));
logger.warn("Failed to access a plugin repository.", e);
}
RenderDataUtil.register(data, "availableArtifactItems", result);
}).useForm(InstallForm.class, op -> op.setup(form -> {}));
}

View file

@ -353,6 +353,9 @@ public class FessMessages extends FessLabels {
/** The key of the message: Failed to install {0}. */
public static final String ERRORS_failed_to_install_plugin = "{errors.failed_to_install_plugin}";
/** The key of the message: Failed to access available plugins. */
public static final String ERRORS_failed_to_find_plugins = "{errors.failed_to_find_plugins}";
/** The key of the message: Failed to process the request: {0} */
public static final String ERRORS_failed_to_process_sso_request = "{errors.failed_to_process_sso_request}";
@ -2097,6 +2100,20 @@ public class FessMessages extends FessLabels {
return this;
}
/**
* Add the created action message for the key 'errors.failed_to_find_plugins' with parameters.
* <pre>
* message: Failed to access available plugins.
* </pre>
* @param property The property name for the message. (NotNull)
* @return this. (NotNull)
*/
public FessMessages addErrorsFailedToFindPlugins(String property) {
assertPropertyNotNull(property);
add(property, new UserMessage(ERRORS_failed_to_find_plugins));
return this;
}
/**
* Add the created action message for the key 'errors.failed_to_process_sso_request' with parameters.
* <pre>

View file

@ -139,6 +139,7 @@ errors.failed_to_print_thread_dump=Failed to print thread dump.
errors.file_is_not_supported={0} is not supported.
errors.plugin_file_is_not_found={0} is not found.
errors.failed_to_install_plugin=Failed to install {0}.
errors.failed_to_find_plugins=Failed to access available plugins.
errors.failed_to_process_sso_request=Failed to process the request: {0}
errors.invalid_query_unknown=The given query has unknown condition.

View file

@ -135,6 +135,7 @@ errors.failed_to_print_thread_dump=Failed to print thread dump.
errors.file_is_not_supported={0} is not supported.
errors.plugin_file_is_not_found={0} is not found.
errors.failed_to_install_plugin=Failed to install {0}.
errors.failed_to_find_plugins=Failed to access available plugins.
errors.failed_to_process_sso_request=Failed to process the request: {0}
errors.invalid_query_unknown=The given query has unknown condition.

View file

@ -141,6 +141,7 @@ errors.failed_to_print_thread_dump=スレッドダンプの出力に失敗しま
errors.file_is_not_supported={0}はサポートされていません。
errors.plugin_file_is_not_found={0}が見つかりません。
errors.failed_to_install_plugin={0}のインストールに失敗しました。
errors.failed_to_find_plugins=利用可能なプラグインが見つまりませんでした。
errors.failed_to_process_sso_request=リクエストの処理に失敗しました: {0}
errors.property_required={0}は必須です。