Browse Source

fix #519 : add bulk file upload

Shinsuke Sugaya 9 years ago
parent
commit
6b5925c3ef

+ 44 - 4
src/main/java/org/codelibs/fess/app/web/admin/backup/AdminBackupAction.java

@@ -17,27 +17,42 @@ package org.codelibs.fess.app.web.admin.backup;
 
 import static org.codelibs.core.stream.StreamUtil.stream;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
+import javax.annotation.Resource;
+
+import org.codelibs.core.exception.IORuntimeException;
+import org.codelibs.core.io.CopyUtil;
 import org.codelibs.core.lang.StringUtil;
 import org.codelibs.elasticsearch.runner.net.Curl;
 import org.codelibs.elasticsearch.runner.net.CurlResponse;
 import org.codelibs.fess.app.web.base.FessAdminAction;
 import org.codelibs.fess.util.RenderDataUtil;
 import org.codelibs.fess.util.ResourceUtil;
+import org.lastaflute.core.magic.async.AsyncManager;
 import org.lastaflute.web.Execute;
 import org.lastaflute.web.response.ActionResponse;
 import org.lastaflute.web.response.HtmlResponse;
 import org.lastaflute.web.ruts.process.ActionRuntime;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author shinsuke
  */
 public class AdminBackupAction extends FessAdminAction {
 
+    private static final Logger logger = LoggerFactory.getLogger(AdminBackupAction.class);
+
+    @Resource
+    private AsyncManager asyncManager;
+
     @Override
     protected void setupHtmlData(final ActionRuntime runtime) {
         super.setupHtmlData(runtime);
@@ -46,7 +61,32 @@ public class AdminBackupAction extends FessAdminAction {
 
     @Execute
     public HtmlResponse index() {
-        return asIndexHtml();
+        saveToken();
+        return asListHtml();
+    }
+
+    @Execute
+    public HtmlResponse upload(final UploadForm form) {
+        validate(form, messages -> {}, () -> asListHtml());
+        verifyToken(() -> asListHtml());
+        asyncManager.async(() -> {
+            try (CurlResponse response = Curl.post(ResourceUtil.getElasticsearchHttpUrl() + "/_bulk").onConnect((req, con) -> {
+                con.setDoOutput(true);
+                try (InputStream in = form.bulkFile.getInputStream(); OutputStream out = con.getOutputStream()) {
+                    CopyUtil.copy(in, out);
+                } catch (IOException e) {
+                    throw new IORuntimeException(e);
+                }
+            }).execute()) {
+                if (logger.isDebugEnabled()) {
+                    logger.debug("Bulk Response:\n" + response.getContentAsString());
+                }
+            } catch (final Exception e) {
+                logger.warn("Failed to process bulk file: " + form.bulkFile.getFileName(), e);
+            }
+        });
+        saveInfo(messages -> messages.addSuccessBulkProcessStarted(GLOBAL));
+        return redirect(getClass()); // no-op
     }
 
     @Execute
@@ -61,7 +101,7 @@ public class AdminBackupAction extends FessAdminAction {
                     });
         }
         throwValidationError(messages -> messages.addErrorsCouldNotFindBackupIndex(GLOBAL), () -> {
-            return asIndexHtml();
+            return asListHtml();
         });
         return redirect(getClass()); // no-op
     }
@@ -75,8 +115,8 @@ public class AdminBackupAction extends FessAdminAction {
         }).collect(Collectors.toList()));
     }
 
-    private HtmlResponse asIndexHtml() {
-        return asHtml(path_AdminBackup_AdminBackupJsp).renderWith(data -> {
+    private HtmlResponse asListHtml() {
+        return asHtml(path_AdminBackup_AdminBackupJsp).useForm(UploadForm.class).renderWith(data -> {
             RenderDataUtil.register(data, "backupItems", getBackupItems());
         });
     }

+ 29 - 0
src/main/java/org/codelibs/fess/app/web/admin/backup/UploadForm.java

@@ -0,0 +1,29 @@
+/*
+ * Copyright 2012-2016 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.app.web.admin.backup;
+
+import java.io.Serializable;
+
+import org.lastaflute.web.ruts.multipart.MultipartFormFile;
+import org.lastaflute.web.validation.Required;
+
+public class UploadForm implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Required
+    public MultipartFormFile bulkFile;
+}

+ 2 - 2
src/main/java/org/codelibs/fess/app/web/admin/upgrade/AdminUpgradeAction.java

@@ -339,10 +339,10 @@ public class AdminUpgradeAction extends FessAdminAction {
         }
     }
 
-    private void uploadResource(String indexConfigPath, String indexName, String path) {
+    private void uploadResource(final String indexConfigPath, final String indexName, final String path) {
         final String filePath = indexConfigPath + "/" + indexName + "/" + path;
         try {
-            String source = FileUtil.readUTF8(filePath);
+            final String source = FileUtil.readUTF8(filePath);
             try (CurlResponse response =
                     Curl.post(org.codelibs.fess.util.ResourceUtil.getElasticsearchHttpUrl() + "/_configsync/file").param("path", path)
                             .body(source).execute()) {

+ 9 - 0
src/main/java/org/codelibs/fess/mylasta/action/FessLabels.java

@@ -158,6 +158,9 @@ public class FessLabels extends ActionMessages {
     /** The key of the message: Upload File */
     public static final String LABELS_DESIGN_FILE = "{labels.designFile}";
 
+    /** The key of the message: Bulk File */
+    public static final String LABELS_BULK_FILE = "{labels.bulkFile}";
+
     /** The key of the message: Additional Query Parameters */
     public static final String LABELS_APPEND_QUERY_PARAMETER = "{labels.appendQueryParameter}";
 
@@ -2103,6 +2106,12 @@ public class FessLabels extends ActionMessages {
     /** The key of the message: Name */
     public static final String LABELS_backup_name = "{labels.backup_name}";
 
+    /** The key of the message: Bulk File */
+    public static final String LABELS_backup_bulk_file = "{labels.backup_bulk_file}";
+
+    /** The key of the message: Upload */
+    public static final String LABELS_backup_button_upload = "{labels.backup_button_upload}";
+
     /** The key of the message: The limit of a search time was exceeded. The partial result might be displayed. */
     public static final String LABELS_process_time_is_exceeded = "{labels.process_time_is_exceeded}";
 

+ 17 - 0
src/main/java/org/codelibs/fess/mylasta/action/FessMessages.java

@@ -377,6 +377,9 @@ public class FessMessages extends FessLabels {
     /** The key of the message: Upgraded data. */
     public static final String SUCCESS_upgrade_from = "{success.upgrade_from}";
 
+    /** The key of the message: Bulk process is started. */
+    public static final String SUCCESS_bulk_process_started = "{success.bulk_process_started}";
+
     /** The key of the message: Created data. */
     public static final String SUCCESS_crud_create_crud_table = "{success.crud_create_crud_table}";
 
@@ -2109,6 +2112,20 @@ public class FessMessages extends FessLabels {
         return this;
     }
 
+    /**
+     * Add the created action message for the key 'success.bulk_process_started' with parameters.
+     * <pre>
+     * message: Bulk process is started.
+     * </pre>
+     * @param property The property name for the message. (NotNull)
+     * @return this. (NotNull)
+     */
+    public FessMessages addSuccessBulkProcessStarted(String property) {
+        assertPropertyNotNull(property);
+        add(property, new ActionMessage(SUCCESS_bulk_process_started));
+        return this;
+    }
+
     /**
      * Add the created action message for the key 'success.crud_create_crud_table' with parameters.
      * <pre>

+ 3 - 0
src/main/resources/fess_label.properties

@@ -42,6 +42,7 @@ labels.crawlingConfigPath=Crawling Path
 labels.processType=Process Type
 labels.parameters=Parameters
 labels.designFile=Upload File
+labels.bulkFile=Bulk File
 labels.appendQueryParameter=Additional Query Parameters
 labels.configId=Config ID
 labels.configParameter=Config Parameters
@@ -691,6 +692,8 @@ labels.notification_search_top=Search top page
 labels.send_testmail=Send TestMail
 labels.backup_configuration=Back Up
 labels.backup_name=Name
+labels.backup_bulk_file=Bulk File
+labels.backup_button_upload=Upload
 labels.process_time_is_exceeded=The limit of a search time was exceeded. The partial result might be displayed.
 labels.user_given_name=First Name
 labels.givenName=First Name

+ 3 - 0
src/main/resources/fess_label_en.properties

@@ -42,6 +42,7 @@ labels.crawlingConfigPath=Crawling Path
 labels.processType=Process Type
 labels.parameters=Parameters
 labels.designFile=Upload File
+labels.bulkFile=Bulk File
 labels.appendQueryParameter=Additional Query Parameters
 labels.configId=Config ID
 labels.configParameter=Config Parameters
@@ -691,6 +692,8 @@ labels.notification_search_top=Search top page
 labels.send_testmail=Send TestMail
 labels.backup_configuration=Back Up
 labels.backup_name=Name
+labels.backup_bulk_file=Bulk File
+labels.backup_button_upload=Upload
 labels.process_time_is_exceeded=The limit of a search time was exceeded. The partial result might be displayed.
 labels.user_given_name=First Name
 labels.givenName=First Name

+ 3 - 0
src/main/resources/fess_label_ja.properties

@@ -42,6 +42,7 @@ labels.crawlingConfigPath=\u30af\u30ed\u30fc\u30eb\u3059\u308b\u30d1\u30b9
 labels.processType=\u51e6\u7406\u306e\u7a2e\u985e
 labels.parameters=\u30d1\u30e9\u30e1\u30fc\u30bf
 labels.designFile=\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u30d5\u30a1\u30a4\u30eb
+labels.bulkFile=\u30d0\u30eb\u30af\u30d5\u30a1\u30a4\u30eb
 labels.appendQueryParameter=\u691c\u7d22\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u8ffd\u52a0
 labels.configId=\u8a2d\u5b9aID
 labels.configParameter=\u8a2d\u5b9a\u30d1\u30e9\u30e1\u30fc\u30bf
@@ -691,6 +692,8 @@ labels.notification_search_top=\u691c\u7d22\u30c8\u30c3\u30d7\u30da\u30fc\u30b8
 labels.send_testmail=\u30c6\u30b9\u30c8\u30e1\u30fc\u30eb\u306e\u9001\u4fe1
 labels.backup_configuration=\u30d0\u30c3\u30af\u30a2\u30c3\u30d7
 labels.backup_name=\u540d\u524d
+labels.backup_bulk_file=\u30d0\u30eb\u30af\u30d5\u30a1\u30a4\u30eb
+labels.backup_button_upload=\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9
 labels.process_time_is_exceeded=\u691c\u7d22\u5f85\u3061\u6642\u9593\u306e\u4e0a\u9650\u3092\u8d85\u3048\u307e\u3057\u305f\u3002\u8868\u793a\u3055\u308c\u305f\u7d50\u679c\u306f\u691c\u7d22\u7d50\u679c\u306e\u4e00\u90e8\u3067\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
 labels.user_given_name=\u540d\u524d(\u540d)
 labels.givenName=\u540d\u524d(\u540d)

+ 1 - 0
src/main/resources/fess_message.properties

@@ -150,6 +150,7 @@ success.send_testmail=Sent the test mail.
 success.job_log_delete_all=Deleted job logs.
 success.changed_password=Changed your password.
 success.upgrade_from=Upgraded data.
+success.bulk_process_started=Bulk process is started.
 
 success.crud_create_crud_table=Created data.
 success.crud_update_crud_table=Updated data.

+ 1 - 0
src/main/resources/fess_message_en.properties

@@ -150,6 +150,7 @@ success.send_testmail=Sent the test mail.
 success.job_log_delete_all=Deleted job logs.
 success.changed_password=Changed your password.
 success.upgrade_from=Upgraded data.
+success.bulk_process_started=Bulk process is started.
 
 success.crud_create_crud_table=Created data.
 success.crud_update_crud_table=Updated data.

+ 1 - 0
src/main/resources/fess_message_ja.properties

@@ -144,6 +144,7 @@ success.send_testmail=\u30c6\u30b9\u30c8\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u30
 success.job_log_delete_all=\u30b8\u30e7\u30d6\u30ed\u30b0\u3092\u524a\u9664\u3057\u307e\u3057\u305f\u3002
 success.changed_password=\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5909\u66f4\u3057\u307e\u3057\u305f\u3002
 success.upgrade_from=\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002
+success.bulk_process_started=\u30d0\u30eb\u30af\u51e6\u7406\u3092\u958b\u59cb\u3057\u307e\u3057\u305f\u3002
 
 success.crud_create_crud_table = \u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002
 success.crud_update_crud_table = \u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002

+ 12 - 0
src/main/webapp/WEB-INF/view/admin/backup/admin_backup.jsp

@@ -41,6 +41,18 @@
 								<%-- List --%>
 								<div class="data-wrapper">
 									<div class="row">
+										<div class="col-sm-12">
+											<la:form action="/admin/backup/upload/" enctype="multipart/form-data" styleClass="form-inline">
+												<div class="form-group">
+													<label for="bulkFile"> <la:message key="labels.backup_bulk_file" />
+													</label> <input type="file" name="bulkFile" class="form-control" />
+												</div>
+												<button type="submit" class="btn btn-success" name="upload">
+													<i class="fa fa-upload"></i>
+													<la:message key="labels.backup_button_upload" />
+												</button>
+											</la:form>
+										</div>
 										<div class="col-sm-12">
 											<table class="table table-bordered table-striped dataTable">
 												<tbody>