fix #1559 add CurlHelper

This commit is contained in:
Shinsuke Sugaya 2018-03-13 23:03:03 +09:00
parent 3b9a6114b1
commit 3f13e71159
10 changed files with 84 additions and 43 deletions

View file

@ -113,7 +113,7 @@ public class EsApiManager extends BaseApiManager {
}
final Method httpMethod = Method.valueOf(request.getMethod().toUpperCase(Locale.ROOT));
final CurlRequest curlRequest = new CurlRequest(httpMethod, ResourceUtil.getElasticsearchHttpUrl() + path);
final CurlRequest curlRequest = ComponentUtil.getCurlHelper().request(httpMethod, path);
final String contentType = request.getHeader("Content-Type");
if (StringUtil.isNotEmpty(contentType)) {

View file

@ -101,16 +101,14 @@ public class AdminBackupAction extends FessAdminAction {
logger.warn("Failed to process system.properties file: " + form.bulkFile.getFileName(), e);
}
} else {
try (CurlResponse response =
Curl.post(ResourceUtil.getElasticsearchHttpUrl() + "/_bulk").header("Content-Type", "application/json")
.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()) {
try (CurlResponse response = ComponentUtil.getCurlHelper().post("/_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());
}
@ -160,8 +158,7 @@ public class AdminBackupAction extends FessAdminAction {
return asStream(filename).contentTypeOctetStream().stream(
out -> {
try (CurlResponse response =
Curl.get(ResourceUtil.getElasticsearchHttpUrl() + "/" + index + "/_data")
.header("Content-Type", "application/json").param("format", "json").execute()) {
ComponentUtil.getCurlHelper().get("/" + index + "/_data").param("format", "json").execute()) {
out.write(response.getContentAsStream());
}
});

View file

@ -30,6 +30,8 @@ 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.helper.CurlHelper;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.ResourceUtil;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.ActionResponse;
@ -82,7 +84,7 @@ public class AdminEsreqAction extends FessAdminAction {
return asListHtml(() -> saveToken());
});
} else {
try (final CurlResponse response = curlRequest.header("Content-Type", "application/json").body(buf.toString()).execute()) {
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);
@ -121,22 +123,23 @@ public class AdminEsreqAction extends FessAdminAction {
return null;
}
final String url;
final String path;
if (values[1].startsWith("/")) {
url = ResourceUtil.getElasticsearchHttpUrl() + values[1];
path = values[1];
} else {
url = ResourceUtil.getElasticsearchHttpUrl() + "/" + values[1];
path = "/" + values[1];
}
final CurlHelper curlHelper = ComponentUtil.getCurlHelper();
switch (values[0].toUpperCase(Locale.ROOT)) {
case "GET":
return Curl.get(url);
return curlHelper.get(path);
case "POST":
return Curl.post(url);
return curlHelper.post(path);
case "PUT":
return Curl.put(url);
return curlHelper.put(path);
case "DELETE":
return Curl.delete(url);
return curlHelper.delete(path);
default:
break;
}

View file

@ -96,8 +96,7 @@ public class ApiAdminBackupAction extends FessApiAdminAction {
return asStream(filename).contentTypeOctetStream().stream(
out -> {
try (CurlResponse response =
Curl.get(ResourceUtil.getElasticsearchHttpUrl() + "/" + index + "/_data")
.header("Content-Type", "application/json").param("format", "json").execute()) {
ComponentUtil.getCurlHelper().get("/" + index + "/_data").param("format", "json").execute()) {
out.write(response.getContentAsStream());
}
});

View file

@ -50,9 +50,8 @@ public class DictionaryManager {
public DictionaryFile<? extends DictionaryItem>[] getDictionaryFiles() {
try (CurlResponse response =
Curl.get(ResourceUtil.getElasticsearchHttpUrl() + "/_configsync/file").header("Content-Type", "application/json")
.param("fields", "path,@timestamp").param("size", ComponentUtil.getFessConfig().getPageDictionaryMaxFetchSize())
.execute()) {
ComponentUtil.getCurlHelper().get("/_configsync/file").param("fields", "path,@timestamp")
.param("size", ComponentUtil.getFessConfig().getPageDictionaryMaxFetchSize()).execute()) {
final Map<String, Object> contentMap = response.getContentAsMap();
@SuppressWarnings("unchecked")
final List<Map<String, Object>> fileList = (List<Map<String, Object>>) contentMap.get("file");
@ -97,8 +96,8 @@ public class DictionaryManager {
// TODO use stream
try (CurlResponse response =
Curl.post(ResourceUtil.getElasticsearchHttpUrl() + "/_configsync/file").header("Content-Type", "application/json")
.param("path", dictFile.getPath()).body(FileUtil.readUTF8(file)).execute()) {
ComponentUtil.getCurlHelper().post("/_configsync/file").param("path", dictFile.getPath())
.body(FileUtil.readUTF8(file)).execute()) {
final Map<String, Object> contentMap = response.getContentAsMap();
if (!Constants.TRUE.equalsIgnoreCase(contentMap.get("acknowledged").toString())) {
throw new DictionaryException("Failed to update " + dictFile.getPath());
@ -114,8 +113,7 @@ public class DictionaryManager {
public InputStream getContentInputStream(final DictionaryFile<? extends DictionaryItem> dictFile) {
try {
return Curl.get(ResourceUtil.getElasticsearchHttpUrl() + "/_configsync/file").header("Content-Type", "application/json")
.param("path", dictFile.getPath()).execute().getContentAsStream();
return ComponentUtil.getCurlHelper().get("/_configsync/file").param("path", dictFile.getPath()).execute().getContentAsStream();
} catch (final IOException e) {
throw new DictionaryException("Failed to access " + dictFile.getPath(), e);
}

View file

@ -373,8 +373,7 @@ public class FessEsClient implements Client {
public boolean reindex(final String fromIndex, final String toIndex, final boolean waitForCompletion) {
final String source = "{\"source\":{\"index\":\"" + fromIndex + "\"},\"dest\":{\"index\":\"" + toIndex + "\"}}";
try (CurlResponse response =
Curl.post(org.codelibs.fess.util.ResourceUtil.getElasticsearchHttpUrl() + "/_reindex")
.header("Content-Type", "application/json").param("wait_for_completion", Boolean.toString(waitForCompletion))
ComponentUtil.getCurlHelper().post("/_reindex").param("wait_for_completion", Boolean.toString(waitForCompletion))
.body(source).execute()) {
if (response.getHttpStatusCode() == 200) {
return true;
@ -516,8 +515,7 @@ public class FessEsClient implements Client {
try {
source = FileUtil.readUTF8(filePath);
try (CurlResponse response =
Curl.post(org.codelibs.fess.util.ResourceUtil.getElasticsearchHttpUrl() + "/_configsync/file")
.header("Content-Type", "application/json").param("path", path).body(source).execute()) {
ComponentUtil.getCurlHelper().post("/_configsync/file").param("path", path).body(source).execute()) {
if (response.getHttpStatusCode() == 200) {
logger.info("Register " + path + " to " + index);
} else {
@ -532,9 +530,7 @@ public class FessEsClient implements Client {
logger.warn("Failed to register " + filePath, e);
}
});
try (CurlResponse response =
Curl.post(org.codelibs.fess.util.ResourceUtil.getElasticsearchHttpUrl() + "/_configsync/flush")
.header("Content-Type", "application/json").execute()) {
try (CurlResponse response = ComponentUtil.getCurlHelper().post("/_configsync/flush").execute()) {
if (response.getHttpStatusCode() == 200) {
logger.info("Flushed config files.");
} else {
@ -610,9 +606,7 @@ public class FessEsClient implements Client {
}
private void waitForConfigSyncStatus() {
try (CurlResponse response =
Curl.get(org.codelibs.fess.util.ResourceUtil.getElasticsearchHttpUrl() + "/_configsync/wait")
.header("Content-Type", "application/json").param("status", "green").execute()) {
try (CurlResponse response = ComponentUtil.getCurlHelper().get("/_configsync/wait").param("status", "green").execute()) {
if (response.getHttpStatusCode() == 200) {
logger.info("ConfigSync is ready.");
} else {

View file

@ -0,0 +1,43 @@
/*
* Copyright 2012-2018 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.helper;
import org.codelibs.elasticsearch.runner.net.Curl.Method;
import org.codelibs.elasticsearch.runner.net.CurlRequest;
import org.codelibs.fess.util.ResourceUtil;
public class CurlHelper {
public CurlRequest get(final String path) {
return request(Method.GET, path).header("Content-Type", "application/json");
}
public CurlRequest post(final String path) {
return request(Method.POST, path).header("Content-Type", "application/json");
}
public CurlRequest put(final String path) {
return request(Method.PUT, path).header("Content-Type", "application/json");
}
public CurlRequest delete(final String path) {
return request(Method.DELETE, path).header("Content-Type", "application/json");
}
public CurlRequest request(final Method method, final String path) {
return new CurlRequest(method, ResourceUtil.getElasticsearchHttpUrl() + path);
}
}

View file

@ -36,6 +36,7 @@ import org.codelibs.fess.helper.AccessTokenHelper;
import org.codelibs.fess.helper.ActivityHelper;
import org.codelibs.fess.helper.CrawlingConfigHelper;
import org.codelibs.fess.helper.CrawlingInfoHelper;
import org.codelibs.fess.helper.CurlHelper;
import org.codelibs.fess.helper.DocumentHelper;
import org.codelibs.fess.helper.DuplicateHostHelper;
import org.codelibs.fess.helper.FileTypeHelper;
@ -82,6 +83,8 @@ public final class ComponentUtil {
private static final Logger logger = LoggerFactory.getLogger(ComponentUtil.class);
private static final String CURL_HELPER = "curlHelper";
private static final String QUERY_STRING_BUILDER = "queryStringBuilder";
private static final String ACCESS_TOKEN_HELPER = "accessTokenHelper";
@ -427,6 +430,10 @@ public final class ComponentUtil {
return getComponent(QUERY_STRING_BUILDER);
}
public static CurlHelper getCurlHelper() {
return getComponent(CURL_HELPER);
}
public static <T> T getComponent(final Class<T> clazz) {
try {
return SingletonLaContainer.getComponent(clazz);

View file

@ -51,9 +51,7 @@ public final class UpgradeUtil {
final String filePath = indexConfigPath + "/" + indexName + "/" + path;
try {
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()) {
try (CurlResponse response = ComponentUtil.getCurlHelper().post("/_configsync/file").param("path", path).body(source).execute()) {
if (response.getHttpStatusCode() == 200) {
logger.info("Register " + path + " to " + indexName);
return true;

View file

@ -8,6 +8,8 @@
<include path="esflute_user.xml"/>
<include path="esflute_log.xml"/>
<component name="curlHelper" class="org.codelibs.fess.helper.CurlHelper">
</component>
<component name="searchLogHelper" class="org.codelibs.fess.helper.SearchLogHelper">
<!--
<property name="userCheckInterval">5 * 60 * 1000</property>