001/*
002 *  Copyright 2022 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.plugins.odfsync.pegase.ws;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.activity.Initializable;
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.runtime.config.Config;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028
029import fr.pcscol.pegase.odf.ApiClient;
030import fr.pcscol.pegase.odf.api.MaquettesExterneApi;
031import fr.pcscol.pegase.odf.api.ObjetsMaquetteExterneApi;
032
033/**
034 * Manager for Pégase API.
035 * Get or create API objects and configure it.
036 */
037public class PegaseApiManager extends AbstractLogEnabled implements Component, Serviceable, Initializable
038{
039    /** Role */
040    public static final String ROLE = PegaseApiManager.class.getName();
041
042    /** The Pégase token manager */
043    protected PegaseTokenManager _pegaseTokenManager;
044    
045    /* Pégase APIs */
046    private ObjetsMaquetteExterneApi _objetsMaquetteExterneApi;
047    private MaquettesExterneApi _maquettesExterneApi;
048    
049    /* Configuration */
050    private boolean _isActive;
051    private String _pegaseODFUrl;
052
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        _pegaseTokenManager = (PegaseTokenManager) manager.lookup(PegaseTokenManager.ROLE);
056    }
057    
058    public void initialize() throws Exception
059    {
060        _isActive = Config.getInstance().getValue("pegase.activate", true, false);
061        if (_isActive)
062        {
063            /* Configuration for API */
064            _pegaseODFUrl = Config.getInstance().getValue("pegase.odf.url");
065        }
066    }
067    
068    private void _updateApiClient(ApiClient apiClient, boolean external) throws IOException
069    {
070        apiClient.setBearerToken(_pegaseTokenManager.getToken());
071        apiClient.setServerUrl(_pegaseODFUrl);
072        apiClient.useExternalApi(external);
073    }
074    
075    /**
076     * Getter for objetsMaquetteApi
077     * @return objetsMaquetteApi
078     * @throws IOException If an error occurs while retrieving the token
079     */
080    public ObjetsMaquetteExterneApi getObjetsMaquetteExterneApi() throws IOException
081    {
082        if (_objetsMaquetteExterneApi == null)
083        {
084            _objetsMaquetteExterneApi = new ObjetsMaquetteExterneApi();
085        }
086        
087        _updateApiClient(_objetsMaquetteExterneApi.getApiClient(), true);
088        
089        return _objetsMaquetteExterneApi;
090    }
091
092    /**
093     * Getter for MaquettesApi
094     * @return maquettesApi
095     * @throws IOException If an error occurs while retrieving the token
096     */
097    public MaquettesExterneApi getMaquettesExterneApi() throws IOException
098    {
099        if (_maquettesExterneApi == null)
100        {
101            _maquettesExterneApi = new MaquettesExterneApi();
102        }
103        
104        _updateApiClient(_maquettesExterneApi.getApiClient(), true);
105        
106        return _maquettesExterneApi;
107    }
108}