diff --git a/src/main/java/org/codelibs/fess/app/web/admin/badword/AdminBadwordAction.java b/src/main/java/org/codelibs/fess/app/web/admin/badword/AdminBadwordAction.java index bb53996c1..b6e447b4f 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/badword/AdminBadwordAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/badword/AdminBadwordAction.java @@ -15,22 +15,18 @@ */ package org.codelibs.fess.app.web.admin.badword; -import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; import javax.annotation.Resource; -import javax.servlet.http.HttpServletResponse; -import org.apache.commons.io.IOUtils; -import org.codelibs.core.io.CopyUtil; import org.codelibs.core.misc.DynamicProperties; import org.codelibs.fess.Constants; import org.codelibs.fess.app.pager.SuggestBadWordPager; @@ -45,9 +41,9 @@ import org.dbflute.optional.OptionalEntity; import org.dbflute.optional.OptionalThing; import org.lastaflute.web.Execute; import org.lastaflute.web.callback.ActionRuntime; +import org.lastaflute.web.response.ActionResponse; import org.lastaflute.web.response.HtmlResponse; import org.lastaflute.web.response.render.RenderData; -import org.lastaflute.web.util.LaResponseUtil; /** * @author Keiichi Watanabe @@ -187,22 +183,25 @@ public class AdminBadwordAction extends FessAdminAction { return asDownloadHtml(); } - // TODO refactoring @Execute - public HtmlResponse download(final DownloadForm form) { - validate(form, messages -> {}, () -> asDownloadHtml()); + public ActionResponse download(final DownloadForm form) { verifyToken(() -> asDownloadHtml()); - final HttpServletResponse response = LaResponseUtil.getResponse(); - response.setContentType("application/octet-stream"); - response.setHeader("Content-Disposition", "attachment; filename=\"" + "badword.csv" + "\""); - try (Writer writer = - new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), crawlerProperties.getProperty( - Constants.CSV_FILE_ENCODING_PROPERTY, Constants.UTF_8)))) { - suggestBadWordService.exportCsv(writer); - } catch (final Exception e) { - e.printStackTrace(); // TODO - } - return redirect(getClass()); + + return asStream("badword.csv").stream(out -> { + Path tempFile = Files.createTempFile(null, null); + try { + try (Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(tempFile), getCsvEncoding()))) { + suggestBadWordService.exportCsv(writer); + } catch (final Exception e) { + throwValidationError(messages -> messages.addErrorsFailedToDownloadBadwordFile(GLOBAL), () -> asDownloadHtml()); + } + try (InputStream in = Files.newInputStream(tempFile)) { + out.write(in); + } + } finally { + Files.delete(tempFile); + } + }); } // ----------------------------------------------------- @@ -267,56 +266,14 @@ public class AdminBadwordAction extends FessAdminAction { public HtmlResponse upload(final UploadForm form) { validate(form, messages -> {}, () -> asUploadHtml()); verifyToken(() -> asUploadHtml()); - BufferedInputStream is = null; - File tempFile = null; - FileOutputStream fos = null; - final byte[] b = new byte[20]; - try { - tempFile = File.createTempFile("suggestbadword-import-", ".csv"); - is = new BufferedInputStream(form.suggestBadWordFile.getInputStream()); - is.mark(20); - if (is.read(b, 0, 20) <= 0) { - // TODO + new Thread(() -> { + try (Reader reader = new BufferedReader(new InputStreamReader(form.suggestBadWordFile.getInputStream(), getCsvEncoding()))) { + suggestBadWordService.importCsv(reader); + suggestHelper.storeAllBadWords(); + } catch (final Exception e) { + throw new FessSystemException("Failed to import data.", e); } - is.reset(); - fos = new FileOutputStream(tempFile); - CopyUtil.copy(is, fos); - } catch (final Exception e) { - if (tempFile != null && !tempFile.delete()) { - // TODO - } - } finally { - IOUtils.closeQuietly(is); - IOUtils.closeQuietly(fos); - } - - final File oFile = tempFile; - try { - final String head = new String(b, Constants.UTF_8); - if (!(head.startsWith("\"SuggestWord\"") || head.startsWith("SuggestWord"))) { - // TODO - } - final String enc = crawlerProperties.getProperty(Constants.CSV_FILE_ENCODING_PROPERTY, Constants.UTF_8); - new Thread(() -> { - Reader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(oFile), enc)); - suggestBadWordService.importCsv(reader); - suggestHelper.storeAllBadWords(); - } catch (final Exception e) { - throw new FessSystemException("Failed to import data.", e); - } finally { - if (!oFile.delete()) { - // TODO - } - IOUtils.closeQuietly(reader); - } - } ).start(); - } catch (final Exception e) { - if (!oFile.delete()) { - // TODO - } - } + }).start(); saveInfo(messages -> messages.addSuccessUploadSuggestBadWord(GLOBAL)); return redirect(getClass()); } @@ -368,6 +325,10 @@ public class AdminBadwordAction extends FessAdminAction { } } + private String getCsvEncoding() { + return crawlerProperties.getProperty(Constants.CSV_FILE_ENCODING_PROPERTY, Constants.UTF_8); + } + // =================================================================================== // JSP // ========= diff --git a/src/main/java/org/codelibs/fess/app/web/admin/badword/DownloadForm.java b/src/main/java/org/codelibs/fess/app/web/admin/badword/DownloadForm.java index d88496604..36472482d 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/badword/DownloadForm.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/badword/DownloadForm.java @@ -17,8 +17,6 @@ package org.codelibs.fess.app.web.admin.badword; import java.io.Serializable; -import org.lastaflute.web.validation.Required; - /** * @author shinsuke */ @@ -26,6 +24,4 @@ public class DownloadForm implements Serializable { private static final long serialVersionUID = 1L; - @Required - public String id; } diff --git a/src/main/java/org/codelibs/fess/app/web/admin/elevateword/AdminElevatewordAction.java b/src/main/java/org/codelibs/fess/app/web/admin/elevateword/AdminElevatewordAction.java index 19fd27b37..fbc05258e 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/elevateword/AdminElevatewordAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/elevateword/AdminElevatewordAction.java @@ -15,22 +15,18 @@ */ package org.codelibs.fess.app.web.admin.elevateword; -import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; import javax.annotation.Resource; -import javax.servlet.http.HttpServletResponse; -import org.apache.commons.io.IOUtils; -import org.codelibs.core.io.CopyUtil; import org.codelibs.core.misc.DynamicProperties; import org.codelibs.fess.Constants; import org.codelibs.fess.app.pager.SuggestElevateWordPager; @@ -47,9 +43,9 @@ import org.dbflute.optional.OptionalEntity; import org.dbflute.optional.OptionalThing; import org.lastaflute.web.Execute; import org.lastaflute.web.callback.ActionRuntime; +import org.lastaflute.web.response.ActionResponse; import org.lastaflute.web.response.HtmlResponse; import org.lastaflute.web.response.render.RenderData; -import org.lastaflute.web.util.LaResponseUtil; /** * @author Keiichi Watanabe @@ -199,22 +195,25 @@ public class AdminElevatewordAction extends FessAdminAction { return asDownloadHtml(); } - // TODO refactoring @Execute - public HtmlResponse download(final DownloadForm form) { - validate(form, messages -> {}, () -> asDownloadHtml()); + public ActionResponse download(final DownloadForm form) { verifyToken(() -> asDownloadHtml()); - final HttpServletResponse response = LaResponseUtil.getResponse(); - response.setContentType("application/octet-stream"); - response.setHeader("Content-Disposition", "attachment; filename=\"" + "elevateword.csv" + "\""); - try (Writer writer = - new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), crawlerProperties.getProperty( - Constants.CSV_FILE_ENCODING_PROPERTY, Constants.UTF_8)))) { - suggestElevateWordService.exportCsv(writer); - } catch (final Exception e) { - e.printStackTrace(); // TODO - } - return asHtml(path_AdminElevateword_AdminElevatewordDownloadJsp); + + return asStream("elevate.csv").stream(out -> { + Path tempFile = Files.createTempFile(null, null); + try { + try (Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(tempFile), getCsvEncoding()))) { + suggestElevateWordService.exportCsv(writer); + } catch (final Exception e) { + throwValidationError(messages -> messages.addErrorsFailedToDownloadElevateFile(GLOBAL), () -> asDownloadHtml()); + } + try (InputStream in = Files.newInputStream(tempFile)) { + out.write(in); + } + } finally { + Files.delete(tempFile); + } + }); } // ----------------------------------------------------- @@ -282,55 +281,15 @@ public class AdminElevatewordAction extends FessAdminAction { public HtmlResponse upload(final UploadForm form) { validate(form, messages -> {}, () -> asUploadHtml()); verifyToken(() -> asUploadHtml()); - BufferedInputStream is = null; - File tempFile = null; - FileOutputStream fos = null; - final byte[] b = new byte[20]; - try { - tempFile = File.createTempFile("suggestelevateword-import-", ".csv"); - is = new BufferedInputStream(form.suggestElevateWordFile.getInputStream()); - is.mark(20); - if (is.read(b, 0, 20) <= 0) { - // TODO + new Thread(() -> { + Reader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(form.suggestElevateWordFile.getInputStream(), getCsvEncoding())); + suggestElevateWordService.importCsv(reader); + } catch (final Exception e) { + throw new FessSystemException("Failed to import data.", e); } - is.reset(); - fos = new FileOutputStream(tempFile); - CopyUtil.copy(is, fos); - } catch (final Exception e) { - if (tempFile != null && !tempFile.delete()) { - // TODO - } - } finally { - IOUtils.closeQuietly(is); - IOUtils.closeQuietly(fos); - } - - final File oFile = tempFile; - try { - final String head = new String(b, Constants.UTF_8); - if (!(head.startsWith("\"SuggestWord\"") || head.startsWith("SuggestWord"))) { - // TODO - } - final String enc = crawlerProperties.getProperty(Constants.CSV_FILE_ENCODING_PROPERTY, Constants.UTF_8); - new Thread(() -> { - Reader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(oFile), enc)); - suggestElevateWordService.importCsv(reader); - } catch (final Exception e) { - throw new FessSystemException("Failed to import data.", e); - } finally { - if (!oFile.delete()) { - // TODO - } - IOUtils.closeQuietly(reader); - } - } ).start(); - } catch (final Exception e) { - if (!oFile.delete()) { - // TODO - } - } + }).start(); saveInfo(messages -> messages.addSuccessUploadSuggestElevateWord(GLOBAL)); return redirect(getClass()); } @@ -386,6 +345,10 @@ public class AdminElevatewordAction extends FessAdminAction { } } + private String getCsvEncoding() { + return crawlerProperties.getProperty(Constants.CSV_FILE_ENCODING_PROPERTY, Constants.UTF_8); + } + // =================================================================================== // JSP // ========= @@ -412,6 +375,6 @@ public class AdminElevatewordAction extends FessAdminAction { } private HtmlResponse asDownloadHtml() { - return asHtml(path_AdminElevateword_AdminElevatewordDownloadJsp); + return asHtml(path_AdminElevateword_AdminElevatewordDownloadJsp).useForm(DownloadForm.class); } } diff --git a/src/main/java/org/codelibs/fess/app/web/admin/elevateword/DownloadForm.java b/src/main/java/org/codelibs/fess/app/web/admin/elevateword/DownloadForm.java index 87d0934fa..330a14311 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/elevateword/DownloadForm.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/elevateword/DownloadForm.java @@ -17,8 +17,6 @@ package org.codelibs.fess.app.web.admin.elevateword; import java.io.Serializable; -import org.lastaflute.web.validation.Required; - /** * @author shinsuke */ @@ -26,6 +24,4 @@ public class DownloadForm implements Serializable { private static final long serialVersionUID = 1L; - @Required - public String id; } diff --git a/src/main/java/org/codelibs/fess/mylasta/action/FessMessages.java b/src/main/java/org/codelibs/fess/mylasta/action/FessMessages.java index d6b5cbc1d..2793026bb 100644 --- a/src/main/java/org/codelibs/fess/mylasta/action/FessMessages.java +++ b/src/main/java/org/codelibs/fess/mylasta/action/FessMessages.java @@ -236,6 +236,18 @@ public class FessMessages extends FessLabels { /** The key of the message: Failed to upload the Kuromoji file. */ public static final String ERRORS_failed_to_upload_kuromoji_file = "{errors.failed_to_upload_kuromoji_file}"; + /** The key of the message: Failed to download the Elevate file. */ + public static final String ERRORS_failed_to_download_elevate_file = "{errors.failed_to_download_elevate_file}"; + + /** The key of the message: Failed to upload the Elevate file. */ + public static final String ERRORS_failed_to_upload_elevate_file = "{errors.failed_to_upload_elevate_file}"; + + /** The key of the message: Failed to download the Badword file. */ + public static final String ERRORS_failed_to_download_badword_file = "{errors.failed_to_download_badword_file}"; + + /** The key of the message: Failed to upload the Badword file. */ + public static final String ERRORS_failed_to_upload_badword_file = "{errors.failed_to_upload_badword_file}"; + /** The key of the message: "{1}" in "{0}" is invalid. */ public static final String ERRORS_invalid_str_is_included = "{errors.invalid_str_is_included}"; @@ -1373,6 +1385,62 @@ public class FessMessages extends FessLabels { return this; } + /** + * Add the created action message for the key 'errors.failed_to_download_elevate_file' with parameters. + *
+     * message: Failed to download the Elevate file.
+     * 
+ * @param property The property name for the message. (NotNull) + * @return this. (NotNull) + */ + public FessMessages addErrorsFailedToDownloadElevateFile(String property) { + assertPropertyNotNull(property); + add(property, new ActionMessage(ERRORS_failed_to_download_elevate_file)); + return this; + } + + /** + * Add the created action message for the key 'errors.failed_to_upload_elevate_file' with parameters. + *
+     * message: Failed to upload the Elevate file.
+     * 
+ * @param property The property name for the message. (NotNull) + * @return this. (NotNull) + */ + public FessMessages addErrorsFailedToUploadElevateFile(String property) { + assertPropertyNotNull(property); + add(property, new ActionMessage(ERRORS_failed_to_upload_elevate_file)); + return this; + } + + /** + * Add the created action message for the key 'errors.failed_to_download_badword_file' with parameters. + *
+     * message: Failed to download the Badword file.
+     * 
+ * @param property The property name for the message. (NotNull) + * @return this. (NotNull) + */ + public FessMessages addErrorsFailedToDownloadBadwordFile(String property) { + assertPropertyNotNull(property); + add(property, new ActionMessage(ERRORS_failed_to_download_badword_file)); + return this; + } + + /** + * Add the created action message for the key 'errors.failed_to_upload_badword_file' with parameters. + *
+     * message: Failed to upload the Badword file.
+     * 
+ * @param property The property name for the message. (NotNull) + * @return this. (NotNull) + */ + public FessMessages addErrorsFailedToUploadBadwordFile(String property) { + assertPropertyNotNull(property); + add(property, new ActionMessage(ERRORS_failed_to_upload_badword_file)); + return this; + } + /** * Add the created action message for the key 'errors.invalid_str_is_included' with parameters. *
diff --git a/src/main/resources/fess_message.properties b/src/main/resources/fess_message.properties
index 27d72a85b..10c64c32b 100644
--- a/src/main/resources/fess_message.properties
+++ b/src/main/resources/fess_message.properties
@@ -100,6 +100,10 @@ errors.failed_to_download_synonym_file=Failed to download the Synonym file.
 errors.failed_to_upload_synonym_file=Failed to upload the Synonym file.
 errors.failed_to_download_kuromoji_file=Failed to download the Kuromoji file.
 errors.failed_to_upload_kuromoji_file=Failed to upload the Kuromoji file.
+errors.failed_to_download_elevate_file=Failed to download the Elevate file.
+errors.failed_to_upload_elevate_file=Failed to upload the Elevate file.
+errors.failed_to_download_badword_file=Failed to download the Badword file.
+errors.failed_to_upload_badword_file=Failed to upload the Badword file.
 errors.invalid_str_is_included="{1}" in "{0}" is invalid.
 errors.blank_password=Password is required.
 errors.invalid_confirm_password=Confirm Password does not match.
diff --git a/src/main/webapp/WEB-INF/view/admin/badword/admin_badword_upload.jsp b/src/main/webapp/WEB-INF/view/admin/badword/admin_badword_upload.jsp
index 04ee2eb76..0dffc6652 100644
--- a/src/main/webapp/WEB-INF/view/admin/badword/admin_badword_upload.jsp
+++ b/src/main/webapp/WEB-INF/view/admin/badword/admin_badword_upload.jsp
@@ -59,7 +59,7 @@
 							
 							
 							
- + diff --git a/src/main/webapp/WEB-INF/view/admin/elevateword/admin_elevateword_upload.jsp b/src/main/webapp/WEB-INF/view/admin/elevateword/admin_elevateword_upload.jsp index 17afc4d69..ad39a3441 100644 --- a/src/main/webapp/WEB-INF/view/admin/elevateword/admin_elevateword_upload.jsp +++ b/src/main/webapp/WEB-INF/view/admin/elevateword/admin_elevateword_upload.jsp @@ -59,7 +59,7 @@
-