Selaa lähdekoodia

fix #539 : add esreq page

Shinsuke Sugaya 9 vuotta sitten
vanhempi
commit
701b56c0b6

+ 153 - 0
src/main/java/org/codelibs/fess/app/web/admin/esreq/AdminEsreqAction.java

@@ -0,0 +1,153 @@
+/*
+ * 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.esreq;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Locale;
+
+import org.codelibs.core.io.CopyUtil;
+import org.codelibs.core.io.ReaderUtil;
+import org.codelibs.core.lang.StringUtil;
+import org.codelibs.elasticsearch.runner.net.Curl;
+import org.codelibs.elasticsearch.runner.net.CurlRequest;
+import org.codelibs.elasticsearch.runner.net.CurlResponse;
+import org.codelibs.fess.Constants;
+import org.codelibs.fess.app.web.base.FessAdminAction;
+import org.codelibs.fess.util.ResourceUtil;
+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 AdminEsreqAction extends FessAdminAction {
+
+    private static final Logger logger = LoggerFactory.getLogger(AdminEsreqAction.class);
+
+    @Override
+    protected void setupHtmlData(final ActionRuntime runtime) {
+        super.setupHtmlData(runtime);
+        runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameEsreq()));
+    }
+
+    @Execute
+    public HtmlResponse index() {
+        return asListHtml(() -> saveToken());
+    }
+
+    @Execute
+    public ActionResponse upload(final UploadForm form) {
+        validate(form, messages -> {}, () -> asListHtml(null));
+        verifyToken(() -> asListHtml(() -> saveToken()));
+
+        String header = null;
+        final StringBuilder buf = new StringBuilder(1000);
+        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(form.requestFile.getInputStream(), Constants.UTF_8))) {
+            header = ReaderUtil.readLine(reader);
+            String line;
+            while ((line = ReaderUtil.readLine(reader)) != null) {
+                buf.append(line);
+            }
+        } catch (final Exception e) {
+            throwValidationError(messages -> messages.addErrorsFailedToReadRequestFile(GLOBAL, e.getMessage()), () -> {
+                return asListHtml(() -> saveToken());
+            });
+        }
+
+        final CurlRequest curlRequest = getCurlRequest(header);
+        if (curlRequest == null) {
+            final String msg = header;
+            throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, msg), () -> {
+                return asListHtml(() -> saveToken());
+            });
+        }
+
+        try (final CurlResponse response = curlRequest.body(buf.toString()).execute()) {
+            final File tempFile = File.createTempFile("esreq_", ".json");
+            try (final InputStream in = response.getContentAsStream()) {
+                CopyUtil.copy(in, tempFile);
+            } catch (final Exception e1) {
+                if (tempFile != null && tempFile.exists() && !tempFile.delete()) {
+                    logger.warn("Failed to delete " + tempFile.getAbsolutePath());
+                }
+                throw e1;
+            }
+            return asStream("es_" + System.currentTimeMillis() + ".json").contentTypeOctetStream().stream(out -> {
+                try (final InputStream in = new FileInputStream(tempFile)) {
+                    out.write(in);
+                } finally {
+                    if (tempFile.exists() && !tempFile.delete()) {
+                        logger.warn("Failed to delete " + tempFile.getAbsolutePath());
+                    }
+                }
+            });
+        } catch (final Exception e) {
+            logger.warn("Failed to process request file: " + form.requestFile.getFileName(), e);
+            throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, e.getMessage()), () -> {
+                return asListHtml(() -> saveToken());
+            });
+        }
+        return redirect(getClass()); // no-op
+    }
+
+    private CurlRequest getCurlRequest(String header) {
+        if (StringUtil.isBlank(header)) {
+            return null;
+        }
+
+        final String[] values = header.split(" ");
+        if (values.length != 2) {
+            return null;
+        }
+
+        final String url;
+        if (values[1].startsWith("/")) {
+            url = ResourceUtil.getElasticsearchHttpUrl() + values[1];
+        } else {
+            url = ResourceUtil.getElasticsearchHttpUrl() + "/" + values[1];
+        }
+
+        switch (values[0].toUpperCase(Locale.ROOT)) {
+        case "GET":
+            return Curl.get(url);
+        case "POST":
+            return Curl.post(url);
+        case "PUT":
+            return Curl.put(url);
+        case "DELETE":
+            return Curl.delete(url);
+        default:
+            break;
+        }
+        return null;
+    }
+
+    private HtmlResponse asListHtml(Runnable runnable) {
+        if (runnable != null) {
+            runnable.run();
+        }
+        return asHtml(path_AdminEsreq_AdminEsreqJsp).useForm(UploadForm.class);
+    }
+
+}

+ 25 - 0
src/main/java/org/codelibs/fess/app/web/admin/esreq/UploadForm.java

@@ -0,0 +1,25 @@
+/*
+ * 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.esreq;
+
+import org.lastaflute.web.ruts.multipart.MultipartFormFile;
+import org.lastaflute.web.validation.Required;
+
+public class UploadForm {
+
+    @Required
+    public MultipartFormFile requestFile;
+}

+ 3 - 0
src/main/java/org/codelibs/fess/mylasta/action/FessHtmlPath.java

@@ -149,6 +149,9 @@ public interface FessHtmlPath {
     /** The path of the HTML: /admin/error/admin_error.jsp */
     /** The path of the HTML: /admin/error/admin_error.jsp */
     HtmlNext path_AdminError_AdminErrorJsp = new HtmlNext("/admin/error/admin_error.jsp");
     HtmlNext path_AdminError_AdminErrorJsp = new HtmlNext("/admin/error/admin_error.jsp");
 
 
+    /** The path of the HTML: /admin/esreq/admin_esreq.jsp */
+    HtmlNext path_AdminEsreq_AdminEsreqJsp = new HtmlNext("/admin/esreq/admin_esreq.jsp");
+
     /** The path of the HTML: /admin/failureurl/admin_failureurl.jsp */
     /** The path of the HTML: /admin/failureurl/admin_failureurl.jsp */
     HtmlNext path_AdminFailureurl_AdminFailureurlJsp = new HtmlNext("/admin/failureurl/admin_failureurl.jsp");
     HtmlNext path_AdminFailureurl_AdminFailureurlJsp = new HtmlNext("/admin/failureurl/admin_failureurl.jsp");
 
 

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

@@ -2355,6 +2355,18 @@ public class FessLabels extends ActionMessages {
     /** The key of the message: Version From */
     /** The key of the message: Version From */
     public static final String LABELS_target_version = "{labels.target_version}";
     public static final String LABELS_target_version = "{labels.target_version}";
 
 
+    /** The key of the message: Request to elasticsearch */
+    public static final String LABELS_esreq_configuration = "{labels.esreq_configuration}";
+
+    /** The key of the message: Request File */
+    public static final String LABELS_esreq_request_file = "{labels.esreq_request_file}";
+
+    /** The key of the message: Request File */
+    public static final String LABELS_REQUEST_FILE = "{labels.requestFile}";
+
+    /** The key of the message: Send */
+    public static final String LABELS_esreq_button_upload = "{labels.esreq_button_upload}";
+
     /**
     /**
      * Assert the property is not null.
      * Assert the property is not null.
      * @param property The value of the property. (NotNull)
      * @param property The value of the property. (NotNull)

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

@@ -287,6 +287,12 @@ public class FessMessages extends FessLabels {
     /** The key of the message: Failed to upgrade from {0}: {1} */
     /** The key of the message: Failed to upgrade from {0}: {1} */
     public static final String ERRORS_failed_to_upgrade_from = "{errors.failed_to_upgrade_from}";
     public static final String ERRORS_failed_to_upgrade_from = "{errors.failed_to_upgrade_from}";
 
 
+    /** The key of the message: Failed to read request file: {0} */
+    public static final String ERRORS_failed_to_read_request_file = "{errors.failed_to_read_request_file}";
+
+    /** The key of the message: Invalid header: {0} */
+    public static final String ERRORS_invalid_header_for_request_file = "{errors.invalid_header_for_request_file}";
+
     /** The key of the message: The given query has unknown condition. */
     /** The key of the message: The given query has unknown condition. */
     public static final String ERRORS_invalid_query_unknown = "{errors.invalid_query_unknown}";
     public static final String ERRORS_invalid_query_unknown = "{errors.invalid_query_unknown}";
 
 
@@ -1677,6 +1683,36 @@ public class FessMessages extends FessLabels {
         return this;
         return this;
     }
     }
 
 
+    /**
+     * Add the created action message for the key 'errors.failed_to_read_request_file' with parameters.
+     * <pre>
+     * message: Failed to read request file: {0}
+     * </pre>
+     * @param property The property name for the message. (NotNull)
+     * @param arg0 The parameter arg0 for message. (NotNull)
+     * @return this. (NotNull)
+     */
+    public FessMessages addErrorsFailedToReadRequestFile(String property, String arg0) {
+        assertPropertyNotNull(property);
+        add(property, new ActionMessage(ERRORS_failed_to_read_request_file, arg0));
+        return this;
+    }
+
+    /**
+     * Add the created action message for the key 'errors.invalid_header_for_request_file' with parameters.
+     * <pre>
+     * message: Invalid header: {0}
+     * </pre>
+     * @param property The property name for the message. (NotNull)
+     * @param arg0 The parameter arg0 for message. (NotNull)
+     * @return this. (NotNull)
+     */
+    public FessMessages addErrorsInvalidHeaderForRequestFile(String property, String arg0) {
+        assertPropertyNotNull(property);
+        add(property, new ActionMessage(ERRORS_invalid_header_for_request_file, arg0));
+        return this;
+    }
+
     /**
     /**
      * Add the created action message for the key 'errors.invalid_query_unknown' with parameters.
      * Add the created action message for the key 'errors.invalid_query_unknown' with parameters.
      * <pre>
      * <pre>

+ 14 - 0
src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java

@@ -694,6 +694,9 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
     /** The key of the configuration. e.g. upgrade */
     /** The key of the configuration. e.g. upgrade */
     String ONLINE_HELP_NAME_UPGRADE = "online.help.name.upgrade";
     String ONLINE_HELP_NAME_UPGRADE = "online.help.name.upgrade";
 
 
+    /** The key of the configuration. e.g. esreq */
+    String ONLINE_HELP_NAME_ESREQ = "online.help.name.esreq";
+
     /** The key of the configuration. e.g. ja */
     /** The key of the configuration. e.g. ja */
     String ONLINE_HELP_SUPPORTED_LANGS = "online.help.supported.langs";
     String ONLINE_HELP_SUPPORTED_LANGS = "online.help.supported.langs";
 
 
@@ -3020,6 +3023,13 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
      */
      */
     String getOnlineHelpNameUpgrade();
     String getOnlineHelpNameUpgrade();
 
 
+    /**
+     * Get the value for the key 'online.help.name.esreq'. <br>
+     * The value is, e.g. esreq <br>
+     * @return The value of found property. (NotNull: if not found, exception but basically no way)
+     */
+    String getOnlineHelpNameEsreq();
+
     /**
     /**
      * Get the value for the key 'online.help.supported.langs'. <br>
      * Get the value for the key 'online.help.supported.langs'. <br>
      * The value is, e.g. ja <br>
      * The value is, e.g. ja <br>
@@ -4771,6 +4781,10 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
             return get(FessConfig.ONLINE_HELP_NAME_UPGRADE);
             return get(FessConfig.ONLINE_HELP_NAME_UPGRADE);
         }
         }
 
 
+        public String getOnlineHelpNameEsreq() {
+            return get(FessConfig.ONLINE_HELP_NAME_ESREQ);
+        }
+
         public String getOnlineHelpSupportedLangs() {
         public String getOnlineHelpSupportedLangs() {
             return get(FessConfig.ONLINE_HELP_SUPPORTED_LANGS);
             return get(FessConfig.ONLINE_HELP_SUPPORTED_LANGS);
         }
         }

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

@@ -373,6 +373,7 @@ online.help.name.scheduler=scheduler
 online.help.name.crawlinginfo=crawlinginfo
 online.help.name.crawlinginfo=crawlinginfo
 online.help.name.backup=backup
 online.help.name.backup=backup
 online.help.name.upgrade=upgrade
 online.help.name.upgrade=upgrade
+online.help.name.esreq=esreq
 
 
 online.help.supported.langs=ja
 online.help.supported.langs=ja
 
 

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

@@ -775,3 +775,7 @@ labels.upgrade_data_migration=Data Migration
 labels.upgrade_start_button=Start
 labels.upgrade_start_button=Start
 labels.targetVersion=Version
 labels.targetVersion=Version
 labels.target_version=Version From
 labels.target_version=Version From
+labels.esreq_configuration=Request to elasticsearch
+labels.esreq_request_file=Request File
+labels.requestFile=Request File
+labels.esreq_button_upload=Send

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

@@ -775,3 +775,7 @@ labels.upgrade_data_migration=Data Migration
 labels.upgrade_start_button=Start
 labels.upgrade_start_button=Start
 labels.targetVersion=Version
 labels.targetVersion=Version
 labels.target_version=Version From
 labels.target_version=Version From
+labels.esreq_configuration=Request to elasticsearch
+labels.esreq_request_file=Request File
+labels.requestFile=Request File
+labels.esreq_button_upload=Send

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

@@ -775,3 +775,7 @@ labels.upgrade_data_migration=\u30c7\u30fc\u30bf\u79fb\u884c
 labels.upgrade_start_button=\u958b\u59cb
 labels.upgrade_start_button=\u958b\u59cb
 labels.targetVersion=\u5bfe\u8c61\u30d0\u30fc\u30b8\u30e7\u30f3
 labels.targetVersion=\u5bfe\u8c61\u30d0\u30fc\u30b8\u30e7\u30f3
 labels.target_version=\u5bfe\u8c61\u30d0\u30fc\u30b8\u30e7\u30f3
 labels.target_version=\u5bfe\u8c61\u30d0\u30fc\u30b8\u30e7\u30f3
+labels.esreq_configuration=elasticsearch\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8
+labels.esreq_request_file=\u30ea\u30af\u30a8\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb
+labels.requestFile=\u30ea\u30af\u30a8\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb
+labels.esreq_button_upload=\u9001\u4fe1

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

@@ -117,6 +117,8 @@ errors.no_user_for_changing_password=The current password is incorrect.
 errors.failed_to_change_password=Failed to change your password.
 errors.failed_to_change_password=Failed to change your password.
 errors.unknown_version_for_upgrade=Unknown version information.
 errors.unknown_version_for_upgrade=Unknown version information.
 errors.failed_to_upgrade_from=Failed to upgrade from {0}: {1}
 errors.failed_to_upgrade_from=Failed to upgrade from {0}: {1}
+errors.failed_to_read_request_file=Failed to read request file: {0}
+errors.invalid_header_for_request_file=Invalid header: {0}
 
 
 errors.invalid_query_unknown=The given query has unknown condition.
 errors.invalid_query_unknown=The given query has unknown condition.
 errors.invalid_query_parse_error=The given query is invalid.
 errors.invalid_query_parse_error=The given query is invalid.

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

@@ -117,6 +117,8 @@ errors.no_user_for_changing_password=The current password is incorrect.
 errors.failed_to_change_password=Failed to change your password.
 errors.failed_to_change_password=Failed to change your password.
 errors.unknown_version_for_upgrade=Unknown version information.
 errors.unknown_version_for_upgrade=Unknown version information.
 errors.failed_to_upgrade_from=Failed to upgrade from {0}.
 errors.failed_to_upgrade_from=Failed to upgrade from {0}.
+errors.failed_to_read_request_file=Failed to read request file: {0}
+errors.invalid_header_for_request_file=Invalid header: {0}
 
 
 errors.invalid_query_unknown=The given query has unknown condition.
 errors.invalid_query_unknown=The given query has unknown condition.
 errors.invalid_query_parse_error=The given query is invalid.
 errors.invalid_query_parse_error=The given query is invalid.

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

@@ -124,6 +124,8 @@ errors.no_user_for_changing_password=\u73fe\u5728\u306e\u30d1\u30b9\u30ef\u30fc\
 errors.failed_to_change_password=\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u5909\u66f4\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002
 errors.failed_to_change_password=\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u5909\u66f4\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002
 errors.unknown_version_for_upgrade=\u4e0d\u660e\u306a\u30d0\u30fc\u30b8\u30e7\u30f3\u60c5\u5831\u3067\u3059\u3002
 errors.unknown_version_for_upgrade=\u4e0d\u660e\u306a\u30d0\u30fc\u30b8\u30e7\u30f3\u60c5\u5831\u3067\u3059\u3002
 errors.failed_to_upgrade_from={0}\u304b\u3089\u306e\u30a2\u30c3\u30d7\u30b0\u30ec\u30fc\u30c9\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002
 errors.failed_to_upgrade_from={0}\u304b\u3089\u306e\u30a2\u30c3\u30d7\u30b0\u30ec\u30fc\u30c9\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002
+errors.failed_to_read_request_file=\u30ea\u30af\u30a8\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u306b\u5931\u6557\u3057\u307e\u3057\u305f: {0}
+errors.invalid_header_for_request_file=\u30d8\u30c3\u30c0\u30fc\u884c\u304c\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093: {0}
 
 
 success.update_crawler_params = \u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002
 success.update_crawler_params = \u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002
 success.delete_doc_from_index = \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304b\u3089\u6587\u66f8\u3092\u524a\u9664\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3092\u958b\u59cb\u3057\u307e\u3057\u305f\u3002
 success.delete_doc_from_index = \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304b\u3089\u6587\u66f8\u3092\u524a\u9664\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3092\u958b\u59cb\u3057\u307e\u3057\u305f\u3002

+ 72 - 0
src/main/webapp/WEB-INF/view/admin/esreq/admin_esreq.jsp

@@ -0,0 +1,72 @@
+<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%><!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8">
+<title><la:message key="labels.admin_brand_title" /> | <la:message
+		key="labels.esreq_configuration" /></title>
+<jsp:include page="/WEB-INF/view/common/admin/head.jsp"></jsp:include>
+</head>
+<body class="skin-blue sidebar-mini">
+	<div class="wrapper">
+		<jsp:include page="/WEB-INF/view/common/admin/header.jsp"></jsp:include>
+		<jsp:include page="/WEB-INF/view/common/admin/sidebar.jsp">
+			<jsp:param name="menuCategoryType" value="system" />
+			<jsp:param name="menuType" value="wizard" />
+		</jsp:include>
+		<div class="content-wrapper">
+			<section class="content-header">
+				<h1>
+					<la:message key="labels.esreq_configuration" />
+				</h1>
+				<jsp:include page="/WEB-INF/view/common/admin/crud/breadcrumb.jsp"></jsp:include>
+			</section>
+			<section class="content">
+				<div class="row">
+					<div class="col-md-12">
+						<div class="box box-primary">
+							<div class="box-header with-border">
+								<h3 class="box-title">
+									<la:message key="labels.esreq_configuration" />
+								</h3>
+							</div>
+							<!-- /.box-header -->
+							<div class="box-body">
+								<%-- Message --%>
+								<div>
+									<la:info id="msg" message="true">
+										<div class="alert alert-info">${msg}</div>
+									</la:info>
+									<la:errors />
+								</div>
+								<%-- List --%>
+								<div class="data-wrapper">
+									<div class="row">
+										<div class="col-sm-12">
+											<la:form action="/admin/esreq/upload/" enctype="multipart/form-data" styleClass="form-inline">
+												<div class="form-group">
+													<label for="requestFile"> <la:message key="labels.esreq_request_file" />
+													</label> <input type="file" name="requestFile" class="form-control" />
+												</div>
+												<button type="submit" class="btn btn-success" name="upload">
+													<i class="fa fa-upload"></i>
+													<la:message key="labels.esreq_button_upload" />
+												</button>
+											</la:form>
+										</div>
+									</div>
+								</div>
+								<!-- /.data-wrapper -->
+							</div>
+							<!-- /.box-body -->
+						</div>
+						<!-- /.box -->
+					</div>
+				</div>
+			</section>
+		</div>
+		<jsp:include page="/WEB-INF/view/common/admin/footer.jsp"></jsp:include>
+	</div>
+	<jsp:include page="/WEB-INF/view/common/admin/foot.jsp"></jsp:include>
+</body>
+</html>
+