use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.
the class GitBucketDataStoreImpl method getGitRef.
protected String getGitRef(final String rootURL, final String authToken, final String owner, final String name, final String branch) {
final String url = encode(rootURL, "api/v3/repos/" + owner + "/" + name + "/git/refs/heads/" + branch, null);
try (CurlResponse curlResponse = Curl.get(url).header("Authorization", "token " + authToken).execute()) {
final Map<String, Object> map = curlResponse.getContentAsMap();
assert (map.containsKey("object"));
@SuppressWarnings("unchecked") final Map<String, String> objmap = (Map<String, String>) map.get("object");
assert (objmap.containsKey("sha"));
return objmap.get("sha");
} catch (final Exception e) {
logger.warn("Failed to access to " + url, e);
return branch;
}
}
use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.
the class GitBucketDataStoreImpl method getRepositoryList.
protected List<Map<String, Object>> getRepositoryList(final String rootURL, final String authToken) {
final String url = rootURL + "api/v3/fess/repos";
// initialize with dummy value
int totalCount = -1;
final List<Map<String, Object>> repoList = new ArrayList<>();
do {
final String urlWithOffset = url + "?offset=" + repoList.size();
try (CurlResponse curlResponse = Curl.get(urlWithOffset).header("Authorization", "token " + authToken).execute()) {
final Map<String, Object> map = curlResponse.getContentAsMap();
assert (map.containsKey("total_count"));
assert (map.containsKey("response_count"));
assert (map.containsKey("repositories"));
totalCount = (int) map.get("total_count");
int responseCount = (int) map.get("response_count");
if (responseCount == 0)
break;
@SuppressWarnings("unchecked") List<Map<String, Object>> repos = (ArrayList<Map<String, Object>>) map.get("repositories");
repoList.addAll(repos);
} catch (final Exception e) {
logger.warn("Failed to access to " + urlWithOffset, e);
break;
}
} while (repoList.size() < totalCount);
logger.info("There exist " + repoList.size() + " repositories");
return repoList;
}
use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.
the class FessEsClient method sendConfigFiles.
protected void sendConfigFiles(final String index) {
configListMap.getOrDefault(index, Collections.emptyList()).forEach(path -> {
String source = null;
final String filePath = indexConfigPath + "/" + index + "/" + path;
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()) {
if (response.getHttpStatusCode() == 200) {
logger.info("Register " + path + " to " + index);
} else {
if (response.getContentException() != null) {
logger.warn("Invalid request for " + path + ".", response.getContentException());
} else {
logger.warn("Invalid request for " + path + ". The response is " + response.getContentAsString());
}
}
}
} catch (final Exception e) {
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()) {
if (response.getHttpStatusCode() == 200) {
logger.info("Flushed config files.");
} else {
logger.warn("Failed to flush config files.");
}
} catch (final Exception e) {
logger.warn("Failed to flush config files.", e);
}
}
use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.
the class GitBucketDataStoreImpl method crawlFileContents.
protected void crawlFileContents(final String rootURL, final String authToken, final String owner, final String name, final String refStr, final String path, final int depth, final long readInterval, final Consumer<String> consumer) {
if (MAX_DEPTH <= depth) {
return;
}
final String url = encode(rootURL, "api/v3/repos/" + owner + "/" + name + "/contents/" + path, "ref=" + refStr);
try (CurlResponse curlResponse = Curl.get(url).header("Authorization", "token " + authToken).execute()) {
final InputStream iStream = curlResponse.getContentAsStream();
final List<Object> fileList = parseList(iStream);
for (int i = 0; i < fileList.size(); ++i) {
@SuppressWarnings("unchecked") final Map<String, String> file = (Map<String, String>) fileList.get(i);
final String newPath = path.isEmpty() ? file.get("name") : path + "/" + file.get("name");
switch(file.get("type")) {
case "file":
consumer.accept(newPath);
break;
case "dir":
if (readInterval > 0) {
sleep(readInterval);
}
crawlFileContents(rootURL, authToken, owner, name, refStr, newPath, depth + 1, readInterval, consumer);
break;
}
}
} catch (final Exception e) {
logger.warn("Failed to access to " + url, e);
}
}
use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.
the class ApiAdminBackupAction method get$file.
// GET /api/admin/backup/file/{id}
@Execute
public StreamResponse get$file(final String id) {
FessConfig fessConfig = ComponentUtil.getFessConfig();
if (stream(fessConfig.getIndexBackupAllTargets()).get(stream -> stream.anyMatch(s -> s.equals(id)))) {
if (id.equals("system.properties")) {
return asStream(id).contentTypeOctetStream().stream(out -> {
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ComponentUtil.getSystemProperties().store(baos, id);
try (final InputStream in = new ByteArrayInputStream(baos.toByteArray())) {
out.write(in);
}
}
});
} else if (id.endsWith(CSV_EXTENTION)) {
final String name = id.substring(0, id.length() - CSV_EXTENTION.length());
if ("search_log".equals(name)) {
return writeCsvResponse(id, getSearchLogCsvWriteCall());
} else if ("search_field_log".equals(name)) {
return writeCsvResponse(id, getSearchFieldLogCsvWriteCall());
} else if ("user_info".equals(name)) {
return writeCsvResponse(id, getUserInfoCsvWriteCall());
} else if ("click_log".equals(name)) {
return writeCsvResponse(id, getClickLogCsvWriteCall());
} else if ("favorite_log".equals(name)) {
return writeCsvResponse(id, getFavoriteLogCsvWriteCall());
}
} else {
final String index;
final String filename;
if (id.endsWith(".bulk")) {
index = id.substring(0, id.length() - 5);
filename = id;
} else {
index = id;
filename = id + ".bulk";
}
return asStream(filename).contentTypeOctetStream().stream(out -> {
try (CurlResponse response = Curl.get(ResourceUtil.getElasticsearchHttpUrl() + "/" + index + "/_data").header("Content-Type", "application/json").param("format", "json").execute()) {
out.write(response.getContentAsStream());
}
});
}
}
throwValidationErrorApi(messages -> messages.addErrorsCouldNotFindBackupIndex(GLOBAL));
// no-op
return StreamResponse.asEmptyBody();
}
Aggregations