/*
* This file is part of ReadonlyREST.
*
* ReadonlyREST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReadonlyREST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReadonlyREST. If not, see http://www.gnu.org/licenses/
*/
package org.elasticsearch.plugin.readonlyrest.es;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.support.ActionFilter;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.http.HttpServerTransport;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.plugin.readonlyrest.ESContext;
import org.elasticsearch.plugin.readonlyrest.es.rradmin.RRAdminAction;
import org.elasticsearch.plugin.readonlyrest.es.rradmin.TransportRRAdminAction;
import org.elasticsearch.plugin.readonlyrest.es.rradmin.rest.RestRRAdminAction;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.IngestPlugin;
import org.elasticsearch.plugins.NetworkPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.threadpool.ThreadPool;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
public class ReadonlyRestPlugin extends Plugin
implements ScriptPlugin, ActionPlugin, IngestPlugin, NetworkPlugin {
private final ESContext context;
public ReadonlyRestPlugin(Settings s) {
this.context = new ESContextImpl();
}
@Override
public List<Class<? extends ActionFilter>> getActionFilters() {
return Collections.singletonList(IndexLevelActionFilter.class);
}
@Override
public Map<String, Supplier<HttpServerTransport>> getHttpTransports(
Settings settings,
ThreadPool threadPool,
BigArrays bigArrays,
CircuitBreakerService circuitBreakerService,
NamedWriteableRegistry namedWriteableRegistry,
NamedXContentRegistry xContentRegistry,
NetworkService networkService,
HttpServerTransport.Dispatcher dispatcher) {
return Collections.singletonMap(
"ssl_netty4", () ->
new SSLTransportNetty4(
context, settings, networkService, bigArrays, threadPool, xContentRegistry, dispatcher
));
}
@Override
public List<Setting<?>> getSettings() {
return AllowedSettings.INSTANCE.list();
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
return Collections.singletonList(
new ActionHandler(RRAdminAction.INSTANCE, TransportRRAdminAction.class));
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public List<RestHandler> getRestHandlers(
Settings settings, RestController restController, ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter,
IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
return Collections.singletonList(new RestRRAdminAction(settings, restController));
}
@Override
public UnaryOperator<RestHandler> getRestHandlerWrapper(ThreadContext threadContext) {
return restHandler -> (RestHandler) (request, channel, client) -> {
// Need to make sure we've fetched cluster-wide configuration at least once. This is super fast, so NP.
ThreadRepo.channel.set(channel);
restHandler.handleRequest(request, channel, client);
};
}
}