Search in sources :

Example 6 with CurlResponse

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;
    }
}
Also used : CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) HashMap(java.util.HashMap) Map(java.util.Map) URISyntaxException(java.net.URISyntaxException)

Example 7 with CurlResponse

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;
}
Also used : CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) URISyntaxException(java.net.URISyntaxException)

Example 8 with CurlResponse

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);
    }
}
Also used : CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) IllegalBehaviorStateException(org.dbflute.exception.IllegalBehaviorStateException) FessSystemException(org.codelibs.fess.exception.FessSystemException) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) ResourceNotFoundRuntimeException(org.codelibs.core.exception.ResourceNotFoundRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ElasticsearchException(org.elasticsearch.ElasticsearchException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) SearchQueryException(org.codelibs.fess.exception.SearchQueryException) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException)

Example 9 with CurlResponse

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);
    }
}
Also used : CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) InputStream(java.io.InputStream) HashMap(java.util.HashMap) Map(java.util.Map) URISyntaxException(java.net.URISyntaxException)

Example 10 with CurlResponse

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();
}
Also used : AdminBackupAction.getClickLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getClickLogCsvWriteCall) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApiBackupFilesResponse(org.codelibs.fess.app.web.api.ApiResult.ApiBackupFilesResponse) ApiResult(org.codelibs.fess.app.web.api.ApiResult) Curl(org.codelibs.elasticsearch.runner.net.Curl) ResourceUtil(org.codelibs.fess.util.ResourceUtil) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StreamResponse(org.lastaflute.web.response.StreamResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) AdminBackupAction.getSearchLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getSearchLogCsvWriteCall) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) FessApiAdminAction(org.codelibs.fess.app.web.api.admin.FessApiAdminAction) BufferedWriter(java.io.BufferedWriter) AdminBackupAction.getBackupItems(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getBackupItems) CsvWriter(com.orangesignal.csv.CsvWriter) JsonResponse(org.lastaflute.web.response.JsonResponse) Consumer(java.util.function.Consumer) CsvConfig(com.orangesignal.csv.CsvConfig) AdminBackupAction.getFavoriteLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getFavoriteLogCsvWriteCall) List(java.util.List) CSV_EXTENTION(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.CSV_EXTENTION) ComponentUtil(org.codelibs.fess.util.ComponentUtil) AdminBackupAction.getUserInfoCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getUserInfoCsvWriteCall) Execute(org.lastaflute.web.Execute) AdminBackupAction.getSearchFieldLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getSearchFieldLogCsvWriteCall) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Execute(org.lastaflute.web.Execute)

Aggregations

CurlResponse (org.codelibs.elasticsearch.runner.net.CurlResponse)11 HashMap (java.util.HashMap)7 Map (java.util.Map)7 URISyntaxException (java.net.URISyntaxException)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Curl (org.codelibs.elasticsearch.runner.net.Curl)4 ResourceUtil (org.codelibs.fess.util.ResourceUtil)4 IOException (java.io.IOException)3 CsvConfig (com.orangesignal.csv.CsvConfig)2 CsvWriter (com.orangesignal.csv.CsvWriter)2 BufferedWriter (java.io.BufferedWriter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Consumer (java.util.function.Consumer)2 CopyUtil (org.codelibs.core.io.CopyUtil)2 StringUtil (org.codelibs.core.lang.StringUtil)2