Search in sources :

Example 1 with CurlResponse

use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.

the class AdminBackupAction method upload.

@Execute
public HtmlResponse upload(final UploadForm form) {
    validate(form, messages -> {
    }, () -> asListHtml());
    verifyToken(() -> asListHtml());
    asyncManager.async(() -> {
        final String fileName = form.bulkFile.getFileName();
        if (fileName.startsWith("system") && fileName.endsWith(".properties")) {
            try (final InputStream in = form.bulkFile.getInputStream()) {
                ComponentUtil.getSystemProperties().load(in);
            } catch (final IOException e) {
                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()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Bulk Response:\n" + response.getContentAsString());
                }
                systemHelper.reloadConfiguration();
            } catch (final Exception e) {
                logger.warn("Failed to process bulk file: " + form.bulkFile.getFileName(), e);
            }
        }
    });
    saveInfo(messages -> messages.addSuccessBulkProcessStarted(GLOBAL));
    // no-op
    return redirect(getClass());
}
Also used : IORuntimeException(org.codelibs.core.exception.IORuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AsyncManager(org.lastaflute.core.magic.async.AsyncManager) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) Curl(org.codelibs.elasticsearch.runner.net.Curl) RenderDataUtil(org.codelibs.fess.util.RenderDataUtil) ArrayList(java.util.ArrayList) ResourceUtil(org.codelibs.fess.util.ResourceUtil) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StreamResponse(org.lastaflute.web.response.StreamResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) FessAdminAction(org.codelibs.fess.app.web.base.FessAdminAction) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) FavoriteLogBhv(org.codelibs.fess.es.log.exbhv.FavoriteLogBhv) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) CopyUtil(org.codelibs.core.io.CopyUtil) HtmlResponse(org.lastaflute.web.response.HtmlResponse) OutputStream(java.io.OutputStream) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Logger(org.slf4j.Logger) UserInfoBhv(org.codelibs.fess.es.log.exbhv.UserInfoBhv) BufferedWriter(java.io.BufferedWriter) ClickLogBhv(org.codelibs.fess.es.log.exbhv.ClickLogBhv) SearchFieldLogBhv(org.codelibs.fess.es.log.exbhv.SearchFieldLogBhv) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) CsvWriter(com.orangesignal.csv.CsvWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) RuntimeIOException(com.healthmarketscience.jackcess.RuntimeIOException) CsvConfig(com.orangesignal.csv.CsvConfig) ActionResponse(org.lastaflute.web.response.ActionResponse) List(java.util.List) SearchLogBhv(org.codelibs.fess.es.log.exbhv.SearchLogBhv) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DateTimeFormatter(java.time.format.DateTimeFormatter) Execute(org.lastaflute.web.Execute) InputStream(java.io.InputStream) IORuntimeException(org.codelibs.core.exception.IORuntimeException) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RuntimeIOException(com.healthmarketscience.jackcess.RuntimeIOException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) IOException(java.io.IOException) RuntimeIOException(com.healthmarketscience.jackcess.RuntimeIOException) Execute(org.lastaflute.web.Execute)

Example 2 with CurlResponse

use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.

the class GitBucketDataStoreImpl method getIssueComments.

private List<String> getIssueComments(final String issueUrl, final String authToken) {
    final String commentsUrl = issueUrl + "/comments";
    final List<String> commentList = new ArrayList<String>();
    try (CurlResponse curlResponse = Curl.get(commentsUrl).header("Authorization", "token " + authToken).execute()) {
        final String commentsJson = curlResponse.getContentAsString();
        List<Map<String, Object>> comments = new ObjectMapper().readValue(commentsJson, new TypeReference<List<Map<String, Object>>>() {
        });
        for (Map<String, Object> comment : comments) {
            if (comment.containsKey("body")) {
                commentList.add((String) comment.get("body"));
            }
        }
    } catch (final Exception e) {
        logger.warn("Failed to access to " + issueUrl, e);
    }
    return commentList;
}
Also used : CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URISyntaxException(java.net.URISyntaxException)

Example 3 with CurlResponse

use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.

the class GitBucketDataStoreImpl method storeIssueById.

private void storeIssueById(final String rootURL, final String authToken, final String issueLabel, final String owner, final String name, final Integer issueId, final List<String> roleList, final CrawlingConfig crawlingConfig, final IndexUpdateCallback callback, final Map<String, String> paramMap, final Map<String, String> scriptMap, final Map<String, Object> defaultDataMap) {
    final String issueUrl = rootURL + "api/v3/repos/" + owner + "/" + name + "/issues/" + issueId.toString();
    final String viewUrl = rootURL + owner + "/" + name + "/issues/" + issueId.toString();
    if (logger.isInfoEnabled()) {
        logger.info("Get a content from " + issueUrl);
    }
    final Map<String, Object> dataMap = new HashMap<>();
    String contentStr = "";
    dataMap.putAll(defaultDataMap);
    // FIXME: Use `ComponentUtil.getDocumentHelper().processRequest` instead of `Curl.get`
    try (CurlResponse curlResponse = Curl.get(issueUrl).header("Authorization", "token " + authToken).execute()) {
        final Map<String, Object> map = curlResponse.getContentAsMap();
        dataMap.put("title", map.getOrDefault("title", ""));
        contentStr = (String) map.getOrDefault("body", "");
    } catch (final Exception e) {
        logger.warn("Failed to access to " + issueUrl, e);
    }
    final String commentsStr = String.join("\n", getIssueComments(issueUrl, authToken));
    contentStr += "\n" + commentsStr;
    dataMap.put("content", contentStr);
    dataMap.put("url", viewUrl);
    dataMap.put("role", roleList);
    dataMap.put("label", Collections.singletonList(issueLabel));
    // TODO scriptMap
    callback.store(paramMap, dataMap);
    return;
}
Also used : HashMap(java.util.HashMap) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) URISyntaxException(java.net.URISyntaxException)

Example 4 with CurlResponse

use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.

the class GitBucketDataStoreImpl method storeWikiContents.

@SuppressWarnings("unchecked")
private void storeWikiContents(final String rootURL, final String authToken, final String wikiLabel, final String owner, final String name, final List<String> roleList, final CrawlingConfig crawlingConfig, final IndexUpdateCallback callback, final Map<String, String> paramMap, final Map<String, String> scriptMap, final Map<String, Object> defaultDataMap, final long readInterval) {
    final String wikiUrl = rootURL + "api/v3/fess/" + owner + "/" + name + "/wiki";
    List<String> pageList = Collections.emptyList();
    // Get list of pages
    try (CurlResponse curlResponse = Curl.get(wikiUrl).header("Authorization", "token " + authToken).execute()) {
        final Map<String, Object> map = curlResponse.getContentAsMap();
        pageList = (List<String>) map.get("pages");
    } catch (final Exception e) {
        logger.warn("Failed to access to " + wikiUrl, e);
    }
    for (final String page : pageList) {
        // FIXME: URL encoding (e.g. page name that contains spaces)
        final String pageUrl = wikiUrl + "/contents/" + page + ".md";
        final String viewUrl = rootURL + owner + "/" + name + "/wiki/" + page;
        if (logger.isInfoEnabled()) {
            logger.info("Get a content from " + pageUrl);
        }
        final Map<String, Object> dataMap = new HashMap<>();
        dataMap.putAll(defaultDataMap);
        dataMap.putAll(ComponentUtil.getDocumentHelper().processRequest(crawlingConfig, paramMap.get("crawlingInfoId"), pageUrl));
        dataMap.put("url", viewUrl);
        dataMap.put("role", roleList);
        dataMap.put("label", Collections.singletonList(wikiLabel));
        // TODO scriptMap
        callback.store(paramMap, dataMap);
        logger.info("Stored " + pageUrl);
        if (readInterval > 0) {
            sleep(readInterval);
        }
    }
}
Also used : CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) HashMap(java.util.HashMap) URISyntaxException(java.net.URISyntaxException)

Example 5 with CurlResponse

use of org.codelibs.elasticsearch.runner.net.CurlResponse in project fess by codelibs.

the class DictionaryManager method getDictionaryFiles.

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()) {
        final Map<String, Object> contentMap = response.getContentAsMap();
        @SuppressWarnings("unchecked") final List<Map<String, Object>> fileList = (List<Map<String, Object>>) contentMap.get("file");
        return fileList.stream().map(fileMap -> {
            try {
                final String path = fileMap.get("path").toString();
                final Date timestamp = new SimpleDateFormat(Constants.DATE_FORMAT_ISO_8601_EXTEND_UTC).parse(fileMap.get("@timestamp").toString());
                for (final DictionaryCreator creator : creatorList) {
                    final DictionaryFile<? extends DictionaryItem> file = creator.create(path, timestamp);
                    if (file != null) {
                        return file;
                    }
                }
            } catch (final Exception e) {
                logger.warn("Failed to load " + fileMap, e);
            }
            return null;
        }).filter(file -> file != null).toArray(n -> new DictionaryFile<?>[n]);
    } catch (final IOException e) {
        throw new DictionaryException("Failed to access dictionaries", e);
    }
}
Also used : Constants(org.codelibs.fess.Constants) Logger(org.slf4j.Logger) Date(java.util.Date) OptionalEntity(org.dbflute.optional.OptionalEntity) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) File(java.io.File) Curl(org.codelibs.elasticsearch.runner.net.Curl) ArrayList(java.util.ArrayList) ResourceUtil(org.codelibs.fess.util.ResourceUtil) List(java.util.List) FileUtil(org.codelibs.core.io.FileUtil) ComponentUtil(org.codelibs.fess.util.ComponentUtil) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) InputStream(java.io.InputStream) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

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