fix download/upload

This commit is contained in:
Shinsuke Sugaya 2015-11-21 23:09:23 +09:00
parent 4ab847671e
commit d75ced16a2
8 changed files with 140 additions and 152 deletions

View file

@ -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
// =========

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -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.
* <pre>
* message: Failed to download the Elevate file.
* </pre>
* @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.
* <pre>
* message: Failed to upload the Elevate file.
* </pre>
* @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.
* <pre>
* message: Failed to download the Badword file.
* </pre>
* @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.
* <pre>
* message: Failed to upload the Badword file.
* </pre>
* @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.
* <pre>

View file

@ -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.

View file

@ -59,7 +59,7 @@
</div>
<!-- /.box-header -->
<div class="box-body">
<la:form action="/admin/badword/" enctype="multipart/form-data">
<la:form action="/admin/badword/upload/" enctype="multipart/form-data">
<table class="table table-bordered">
<tbody>
<tr>

View file

@ -59,7 +59,7 @@
</div>
<!-- /.box-header -->
<div class="box-body">
<la:form action="/admin/elevateword/"
<la:form action="/admin/elevateword/upload/"
enctype="multipart/form-data">
<table class="table table-bordered">
<tbody>