001/*
002 *  Copyright 2021 Anyware Services
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 */
016package org.ametys.cms.indexing.solr;
017
018import java.util.Map;
019
020import org.apache.solr.client.solrj.SolrClient;
021import org.apache.solr.client.solrj.SolrRequest;
022import org.apache.solr.client.solrj.request.CollectionRequiringSolrRequest;
023import org.apache.solr.client.solrj.response.SolrResponseBase;
024import org.apache.solr.common.params.ModifiableSolrParams;
025import org.apache.solr.common.params.SolrParams;
026
027import org.ametys.plugins.repository.AmetysObject;
028
029import com.fasterxml.jackson.core.JsonProcessingException;
030import com.fasterxml.jackson.databind.ObjectMapper;
031
032/**
033 * {@link SolrRequest} to partially update the ACL Solr cache
034 */
035public class UpdateAclCacheRequest extends CollectionRequiringSolrRequest<SolrResponseBase>
036{
037    private Map<String, Map<String, Object>> _objects;
038
039    /**
040     * Default constructor
041     * @param objects the {@link AmetysObject} mapped to their corresponding ACL
042     */
043    public UpdateAclCacheRequest(Map<String, Map<String, Object>> objects)
044    {
045        super(METHOD.POST, "/updateAclCache");
046        _objects = objects;
047    }
048    
049    @Override
050    public SolrParams getParams()
051    {
052        ModifiableSolrParams params = new ModifiableSolrParams();
053        try
054        {
055            params.set("objects", new ObjectMapper().writeValueAsString(_objects));
056        }
057        catch (JsonProcessingException e)
058        {
059            throw new IllegalArgumentException("Unable to convert objects to JSON for updating Solr ACL cache", e);
060        }
061        
062        return params;
063    }
064
065    @Override
066    protected SolrResponseBase createResponse(SolrClient client)
067    {
068        return new SolrResponseBase();
069    }
070    
071    @Override
072    public String getRequestType()
073    {
074        return SolrRequestType.ADMIN.toString();
075    }
076}