ESCatalogAction.java

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.httpv2.restv2;

import org.apache.doris.catalog.Env;
import org.apache.doris.common.Config;
import org.apache.doris.common.util.JsonUtil;
import org.apache.doris.connector.api.rest.ConnectorRestPassthrough;
import org.apache.doris.datasource.CatalogIf;
import org.apache.doris.datasource.plugin.PluginDrivenExternalCatalog;
import org.apache.doris.httpv2.entity.ResponseEntityBuilder;
import org.apache.doris.httpv2.rest.RestBaseController;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Maps;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rest/v2/api/es_catalog")
public class ESCatalogAction extends RestBaseController {

    private static final Logger LOG = LogManager.getLogger(ESCatalogAction.class);
    private static final String CATALOG = "catalog";
    private static final String TABLE = "table";

    private Object handleRequest(HttpServletRequest request, HttpServletResponse response,
            BiFunction<ConnectorRestPassthrough, String, String> action) {
        if (Config.enable_all_http_auth) {
            executeCheckPassword(request, response);
        }

        if (needRedirect(request.getScheme())) {
            return redirectToHttps(request);
        }

        Map<String, Object> resultMap = Maps.newHashMap();
        Env env = Env.getCurrentEnv();
        String catalogName = request.getParameter(CATALOG);
        String tableName = request.getParameter(TABLE);
        CatalogIf catalog = env.getCatalogMgr().getCatalog(catalogName);
        if (!(catalog instanceof PluginDrivenExternalCatalog)
                || !"es".equals(((PluginDrivenExternalCatalog) catalog).getType())) {
            return ResponseEntityBuilder.badRequest("unknown ES Catalog: " + catalogName);
        }
        PluginDrivenExternalCatalog esCatalog = (PluginDrivenExternalCatalog) catalog;
        esCatalog.makeSureInitialized();
        // These endpoints emulate the ES HTTP API, so they need the connector to forward the request. A
        // connector that cannot is not usable here; probe rather than call and catch.
        ConnectorRestPassthrough rest = esCatalog.getConnector().getRestPassthrough();
        if (rest == null) {
            return ResponseEntityBuilder.badRequest("unknown ES Catalog: " + catalogName);
        }
        String result = action.apply(rest, tableName);
        ObjectNode jsonResult = JsonUtil.parseObject(result);

        resultMap.put("catalog", catalogName);
        resultMap.put("table", tableName);
        resultMap.put("result", jsonResult);

        return ResponseEntityBuilder.ok(resultMap);
    }

    @RequestMapping(path = "/get_mapping", method = RequestMethod.GET)
    public Object getMapping(HttpServletRequest request, HttpServletResponse response) {
        return handleRequest(request, response,
                (rest, tableName) -> rest.executeRestRequest(tableName + "/_mapping", null));
    }

    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public Object search(HttpServletRequest request, HttpServletResponse response) {
        String body;
        try {
            body = getRequestBody(request);
        } catch (IOException e) {
            return ResponseEntityBuilder.okWithCommonError(e.getMessage());
        }
        return handleRequest(request, response,
                (rest, tableName) -> rest.executeRestRequest(tableName + "/_search", body));
    }

    private String getRequestBody(HttpServletRequest request) throws IOException {
        BufferedReader reader = request.getReader();
        return reader.lines().collect(Collectors.joining(System.lineSeparator()));
    }
}