Search in sources :

Example 1 with Method

use of org.codelibs.elasticsearch.runner.net.Curl.Method 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 (path.startsWith("/_plugin/") || path.equals("/_plugin")) {
        processPluginRequest(request, response, path.replaceFirst("^/_plugin", StringUtil.EMPTY));
        return;
    }
    final Method httpMethod = Method.valueOf(request.getMethod().toUpperCase(Locale.ROOT));
    final CurlRequest curlRequest = new CurlRequest(httpMethod, ResourceUtil.getElasticsearchHttpUrl() + path);
    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]);
        }
    });
    curlRequest.onConnect((req, con) -> {
        con.setDoOutput(true);
        if (httpMethod != Method.GET) {
            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(con -> {
        try (ServletOutputStream out = response.getOutputStream()) {
            try (InputStream in = con.getInputStream()) {
                response.setStatus(con.getResponseCode());
                CopyUtil.copy(in, out);
            } catch (final Exception e) {
                response.setStatus(con.getResponseCode());
                try (InputStream err = con.getErrorStream()) {
                    CopyUtil.copy(err, out);
                }
            }
        } catch (final ClientAbortException e) {
            logger.debug("Client aborts this request.", e);
        } catch (final Exception e) {
            if (e.getCause() instanceof ClientAbortException) {
                logger.debug("Client aborts this request.", e);
            } else {
                throw new WebApiException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 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) LoggerFactory(org.slf4j.LoggerFactory) ClientAbortException(org.apache.catalina.connector.ClientAbortException) SessionManager(org.lastaflute.web.servlet.session.SessionManager) WebApiException(org.codelibs.fess.exception.WebApiException) 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) Logger(org.slf4j.Logger) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) UUID(java.util.UUID) ComponentUtil(org.codelibs.fess.util.ComponentUtil) CurlRequest(org.codelibs.elasticsearch.runner.net.CurlRequest) InputStream(java.io.InputStream) Method(org.codelibs.elasticsearch.runner.net.Curl.Method) ServletInputStream(javax.servlet.ServletInputStream) ServletOutputStream(javax.servlet.ServletOutputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CurlRequest(org.codelibs.elasticsearch.runner.net.CurlRequest) ClientAbortException(org.apache.catalina.connector.ClientAbortException) Method(org.codelibs.elasticsearch.runner.net.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)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Locale (java.util.Locale)1 UUID (java.util.UUID)1 FilterChain (javax.servlet.FilterChain)1 ServletException (javax.servlet.ServletException)1 ServletInputStream (javax.servlet.ServletInputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ClientAbortException (org.apache.catalina.connector.ClientAbortException)1 CopyUtil (org.codelibs.core.io.CopyUtil)1 StringUtil (org.codelibs.core.lang.StringUtil)1 Method (org.codelibs.elasticsearch.runner.net.Curl.Method)1 CurlRequest (org.codelibs.elasticsearch.runner.net.CurlRequest)1 Constants (org.codelibs.fess.Constants)1 BaseApiManager (org.codelibs.fess.api.BaseApiManager)1