001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.elasticsearch.http.readonly.service;
022
023import java.util.Map;
024import java.util.concurrent.ConcurrentHashMap;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.elasticsearch.http.readonly.filter.SearchRequestFilter;
029import org.nuxeo.runtime.model.ComponentContext;
030import org.nuxeo.runtime.model.ComponentInstance;
031import org.nuxeo.runtime.model.ComponentName;
032import org.nuxeo.runtime.model.DefaultComponent;
033
034/**
035 * @since 7.4
036 */
037public class RequestFilterService extends DefaultComponent {
038
039    public static final ComponentName NAME = new ComponentName(ComponentName.DEFAULT_TYPE,
040            "org.nuxeo.elasticsearch.http.readonly.RequestFilterService");
041
042    private static final Log log = LogFactory.getLog(RequestFilterService.class);
043
044    protected static final String FILTER_EXT_POINT = "filters";
045
046    protected Map<String, Class<? extends SearchRequestFilter>> requestFilters;
047
048    @Override
049    public void activate(ComponentContext context) {
050        requestFilters = new ConcurrentHashMap<>();
051    }
052
053    @Override
054    public void deactivate(ComponentContext context) {
055        requestFilters.clear();
056        requestFilters = null;
057    }
058
059    public Map<String, Class<? extends SearchRequestFilter>> getRequestFilters() {
060        return requestFilters;
061
062    }
063
064    public SearchRequestFilter getRequestFilters(String indices) throws ReflectiveOperationException {
065        Class<? extends SearchRequestFilter> clazz = requestFilters.get(indices);
066        if (clazz == null) {
067            return null;
068        }
069        return clazz.getDeclaredConstructor().newInstance();
070    }
071
072    @Override
073    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
074        if (FILTER_EXT_POINT.equals(extensionPoint)) {
075            RequestFilterDescriptor des = (RequestFilterDescriptor) contribution;
076            requestFilters.put(des.getIndex(), des.getFilterClass());
077            log.info("Registered filter: " + des.getFilterClass() + " for index " + des.getIndex());
078        }
079    }
080
081    @Override
082    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
083        if (FILTER_EXT_POINT.equals(extensionPoint)) {
084            RequestFilterDescriptor des = (RequestFilterDescriptor) contribution;
085            Class<? extends SearchRequestFilter> filter = requestFilters.remove(des.getIndex());
086            if (filter != null) {
087                log.info("Unregistered filter: " + filter + " for index " + des.getIndex());
088            }
089        }
090    }
091
092}