Search in sources :

Example 1 with CurlResponse

use of org.codelibs.curl.CurlResponse in project fess by codelibs.

the class EsApiManager method processRequest.

protected void processRequest(final HttpServletRequest request, final HttpServletResponse response, final String path) {
    if (StringUtil.isNotBlank(path)) {
        final String lowerPath = path.toLowerCase(Locale.ROOT);
        if (lowerPath.endsWith(".html")) {
            response.setContentType("text/html;charset=utf-8");
        } else if (lowerPath.endsWith(".txt")) {
            response.setContentType("text/plain");
        } else if (lowerPath.endsWith(".css")) {
            response.setContentType("text/css");
        }
    }
    if ("/_plugin".equals(path) || path.startsWith("/_plugin/")) {
        processPluginRequest(request, response, path.replaceFirst("^/_plugin", StringUtil.EMPTY));
        return;
    }
    final Method httpMethod = Method.valueOf(request.getMethod().toUpperCase(Locale.ROOT));
    final CurlRequest curlRequest = ComponentUtil.getCurlHelper().request(httpMethod, path);
    final String contentType = request.getHeader("Content-Type");
    if (StringUtil.isNotEmpty(contentType)) {
        curlRequest.header("Content-Type", contentType);
    }
    request.getParameterMap().entrySet().stream().forEach(entry -> {
        if (entry.getValue().length > 1) {
            curlRequest.param(entry.getKey(), String.join(",", entry.getValue()));
        } else if (entry.getValue().length == 1) {
            curlRequest.param(entry.getKey(), entry.getValue()[0]);
        }
    });
    try (final CurlResponse curlResponse = curlRequest.onConnect((req, con) -> {
        con.setDoOutput(true);
        if (httpMethod != Method.GET && request.getContentLength() > 2) {
            try (ServletInputStream in = request.getInputStream();
                OutputStream out = con.getOutputStream()) {
                CopyUtil.copy(in, out);
            } catch (final IOException e) {
                throw new WebApiException(HttpServletResponse.SC_BAD_REQUEST, e);
            }
        }
    }).execute()) {
        try (ServletOutputStream out = response.getOutputStream();
            InputStream in = curlResponse.getContentAsStream()) {
            response.setStatus(curlResponse.getHttpStatusCode());
            writeHeaders(response);
            CopyUtil.copy(in, out);
        } catch (final ClientAbortException e) {
            logger.debug("Client aborts this request.", e);
        }
    } catch (final Exception e) {
        if (!(e.getCause() instanceof ClientAbortException)) {
            throw new WebApiException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        }
        logger.debug("Client aborts this request.", e);
    }
}
Also used : FilterChain(javax.servlet.FilterChain) Constants(org.codelibs.fess.Constants) ServletException(javax.servlet.ServletException) FessSystemException(org.codelibs.fess.exception.FessSystemException) ServletInputStream(javax.servlet.ServletInputStream) BaseApiManager(org.codelibs.fess.api.BaseApiManager) CurlRequest(org.codelibs.curl.CurlRequest) ClientAbortException(org.apache.catalina.connector.ClientAbortException) SessionManager(org.lastaflute.web.servlet.session.SessionManager) WebApiException(org.codelibs.fess.exception.WebApiException) Method(org.codelibs.curl.Curl.Method) FessUserBean(org.codelibs.fess.mylasta.action.FessUserBean) ResourceUtil(org.codelibs.fess.util.ResourceUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestManager(org.lastaflute.web.servlet.request.RequestManager) ServletOutputStream(javax.servlet.ServletOutputStream) Locale(java.util.Locale) CopyUtil(org.codelibs.core.io.CopyUtil) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) UUID(java.util.UUID) Logger(org.apache.logging.log4j.Logger) ComponentUtil(org.codelibs.fess.util.ComponentUtil) PostConstruct(javax.annotation.PostConstruct) LogManager(org.apache.logging.log4j.LogManager) CurlResponse(org.codelibs.curl.CurlResponse) InputStream(java.io.InputStream) CurlResponse(org.codelibs.curl.CurlResponse) ServletOutputStream(javax.servlet.ServletOutputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CurlRequest(org.codelibs.curl.CurlRequest) Method(org.codelibs.curl.Curl.Method) IOException(java.io.IOException) WebApiException(org.codelibs.fess.exception.WebApiException) ServletException(javax.servlet.ServletException) FessSystemException(org.codelibs.fess.exception.FessSystemException) ClientAbortException(org.apache.catalina.connector.ClientAbortException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) ServletInputStream(javax.servlet.ServletInputStream) ClientAbortException(org.apache.catalina.connector.ClientAbortException)

Example 2 with CurlResponse

use of org.codelibs.curl.CurlResponse in project fess by codelibs.

the class DictionaryManager method getDictionaryFiles.

public DictionaryFile<? extends DictionaryItem>[] getDictionaryFiles() {
    try (CurlResponse response = ComponentUtil.getCurlHelper().get("/_configsync/file").param("fields", "path,@timestamp").param("size", ComponentUtil.getFessConfig().getPageDictionaryMaxFetchSize()).execute()) {
        final Map<String, Object> contentMap = response.getContent(OpenSearchCurl.jsonParser());
        @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) Date(java.util.Date) OptionalEntity(org.dbflute.optional.OptionalEntity) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) Logger(org.apache.logging.log4j.Logger) FileUtil(org.codelibs.core.io.FileUtil) ComponentUtil(org.codelibs.fess.util.ComponentUtil) OpenSearchCurl(org.codelibs.opensearch.runner.net.OpenSearchCurl) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct) LogManager(org.apache.logging.log4j.LogManager) CurlResponse(org.codelibs.curl.CurlResponse) CurlResponse(org.codelibs.curl.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)

Example 3 with CurlResponse

use of org.codelibs.curl.CurlResponse in project fess by codelibs.

the class NotificationHelper method sendToGoogleChat.

protected void sendToGoogleChat(final CardView cardView, final SMailPostingDiscloser discloser) {
    // https://developers.google.com/hangouts/chat/how-tos/webhooks
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String googleChatWebhookUrls = fessConfig.getGoogleChatWebhookUrls();
    if (StringUtil.isBlank(googleChatWebhookUrls)) {
        return;
    }
    final String body = toGoogleChatMessage(discloser);
    StreamUtil.split(googleChatWebhookUrls, "[,\\s]").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(url -> {
        try (CurlResponse response = Curl.post(url).header("Content-Type", "application/json").body(body).execute()) {
            if (response.getHttpStatusCode() == 200) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Sent {} to {}.", body, url);
                }
            } else {
                logger.warn("Failed to send {} to {}. HTTP Status is {}. {}", body, url, response.getHttpStatusCode(), response.getContentAsString());
            }
        } catch (final IOException e) {
            logger.warn("Failed to send {} to {}.", body, url, e);
        }
    }));
}
Also used : FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) StreamUtil(org.codelibs.core.stream.StreamUtil) ComponentUtil(org.codelibs.fess.util.ComponentUtil) CardView(org.dbflute.mail.CardView) SMailPostingDiscloser(org.dbflute.mail.send.supplement.SMailPostingDiscloser) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) LogManager(org.apache.logging.log4j.LogManager) CurlResponse(org.codelibs.curl.CurlResponse) Curl(org.codelibs.curl.Curl) CurlResponse(org.codelibs.curl.CurlResponse) IOException(java.io.IOException) StringUtil(org.codelibs.core.lang.StringUtil) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 4 with CurlResponse

use of org.codelibs.curl.CurlResponse in project fess by codelibs.

the class NotificationHelper method sendToSlack.

protected void sendToSlack(final CardView cardView, final SMailPostingDiscloser discloser) {
    // https://api.slack.com/messaging/webhooks#posting_with_webhooks
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String slackWebhookUrls = fessConfig.getSlackWebhookUrls();
    if (StringUtil.isBlank(slackWebhookUrls)) {
        return;
    }
    final String body = toSlackMessage(discloser);
    StreamUtil.split(slackWebhookUrls, "[,\\s]").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(url -> {
        try (CurlResponse response = Curl.post(url).header("Content-Type", "application/json").body(body).execute()) {
            if (response.getHttpStatusCode() == 200) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Sent {} to {}.", body, url);
                }
            } else {
                logger.warn("Failed to send {} to {}. HTTP Status is {}. {}", body, url, response.getHttpStatusCode(), response.getContentAsString());
            }
        } catch (final IOException e) {
            logger.warn("Failed to send {} to {}.", body, url, e);
        }
    }));
}
Also used : FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) StreamUtil(org.codelibs.core.stream.StreamUtil) ComponentUtil(org.codelibs.fess.util.ComponentUtil) CardView(org.dbflute.mail.CardView) SMailPostingDiscloser(org.dbflute.mail.send.supplement.SMailPostingDiscloser) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) LogManager(org.apache.logging.log4j.LogManager) CurlResponse(org.codelibs.curl.CurlResponse) Curl(org.codelibs.curl.Curl) CurlResponse(org.codelibs.curl.CurlResponse) IOException(java.io.IOException) StringUtil(org.codelibs.core.lang.StringUtil) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 5 with CurlResponse

use of org.codelibs.curl.CurlResponse in project fess by codelibs.

the class PluginHelper method install.

protected void install(final Artifact artifact) {
    final String fileName = artifact.getFileName();
    final String url = artifact.getUrl();
    if (StringUtil.isBlank(url)) {
        throw new PluginException("url is blank: " + artifact.getName());
    }
    if (url.startsWith("http:") || url.startsWith("https:")) {
        try (final CurlResponse response = createCurlRequest(url).execute()) {
            if (response.getHttpStatusCode() != 200) {
                throw new PluginException("HTTP Status " + response.getHttpStatusCode() + " : failed to get the artifact from " + url);
            }
            try (final InputStream in = response.getContentAsStream()) {
                CopyUtil.copy(in, ResourceUtil.getPluginPath(fileName).toFile());
            }
        } catch (final Exception e) {
            throw new PluginException("Failed to install the artifact " + artifact.getName(), e);
        }
    } else {
        try (final InputStream in = new FileInputStream(url)) {
            CopyUtil.copy(in, ResourceUtil.getPluginPath(fileName).toFile());
        } catch (final Exception e) {
            throw new PluginException("Failed to install the artifact " + artifact.getName(), e);
        }
    }
}
Also used : CurlResponse(org.codelibs.curl.CurlResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PluginException(org.codelibs.fess.exception.PluginException) PluginException(org.codelibs.fess.exception.PluginException) IORuntimeException(org.lastaflute.di.exception.IORuntimeException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) FileInputStream(java.io.FileInputStream)

Aggregations

CurlResponse (org.codelibs.curl.CurlResponse)6 IOException (java.io.IOException)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 ComponentUtil (org.codelibs.fess.util.ComponentUtil)5 StringUtil (org.codelibs.core.lang.StringUtil)4 InputStream (java.io.InputStream)3 Constants (org.codelibs.fess.Constants)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Locale (java.util.Locale)2 PostConstruct (javax.annotation.PostConstruct)2 StringEscapeUtils (org.apache.commons.text.StringEscapeUtils)2 CopyUtil (org.codelibs.core.io.CopyUtil)2 StreamUtil (org.codelibs.core.stream.StreamUtil)2 Curl (org.codelibs.curl.Curl)2 CurlRequest (org.codelibs.curl.CurlRequest)2 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)2 CardView (org.dbflute.mail.CardView)2 SMailPostingDiscloser (org.dbflute.mail.send.supplement.SMailPostingDiscloser)2