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