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.cof.ApiClient; 030import fr.pcscol.pegase.cof.api.FormationsApi; 031import fr.pcscol.pegase.cof.api.GroupementsApi; 032import fr.pcscol.pegase.cof.api.ObjetsFormationApi; 033import fr.pcscol.pegase.cof.api.ObjetsMaquetteApi; 034 035/** 036 * Manager for Pégase API. 037 * Get or create API objects and configure it. 038 */ 039public class PegaseApiManager extends AbstractLogEnabled implements Component, Serviceable, Initializable 040{ 041 /** Role */ 042 public static final String ROLE = PegaseApiManager.class.getName(); 043 044 /** The Pégase token manager */ 045 protected PegaseTokenManager _pegaseTokenManager; 046 047 /* Pégase APIs */ 048 private ObjetsMaquetteApi _objetsMaquetteApi; 049 private FormationsApi _formationsApi; 050 private GroupementsApi _groupementsApi; 051 private ObjetsFormationApi _objetsFormationApi; 052 053 /* Configuration */ 054 private boolean _isActive; 055 private String _cofUrl; 056 057 public void service(ServiceManager manager) throws ServiceException 058 { 059 _pegaseTokenManager = (PegaseTokenManager) manager.lookup(PegaseTokenManager.ROLE); 060 } 061 062 public void initialize() throws Exception 063 { 064 _isActive = Config.getInstance().getValue("pegase.activate", true, false); 065 if (_isActive) 066 { 067 /* Configuration for API */ 068 _cofUrl = Config.getInstance().getValue("pegase.cof.url"); 069 } 070 } 071 072 /** 073 * Update the API client to set server URL and token. 074 * @param apiClient The API client to update 075 * @throws IOException if an error occurs 076 */ 077 protected void updateApiClient(ApiClient apiClient) throws IOException 078 { 079 apiClient.setBearerToken(_pegaseTokenManager.getToken()); 080 apiClient.setServerUrl(_cofUrl); 081 } 082 083 /** 084 * Getter for objetsMaquetteApi 085 * @return objetsMaquetteApi 086 * @throws IOException If an error occurs while retrieving the token 087 */ 088 public ObjetsMaquetteApi getObjetsMaquetteApi() throws IOException 089 { 090 if (_objetsMaquetteApi == null) 091 { 092 _objetsMaquetteApi = new ObjetsMaquetteApi(); 093 } 094 095 updateApiClient(_objetsMaquetteApi.getApiClient()); 096 097 return _objetsMaquetteApi; 098 } 099 100 /** 101 * Getter for FormationsApi 102 * @return FormationsApi 103 * @throws IOException If an error occurs while retrieving the token 104 */ 105 public FormationsApi getFormationsApi() throws IOException 106 { 107 if (_formationsApi == null) 108 { 109 _formationsApi = new FormationsApi(); 110 } 111 112 updateApiClient(_formationsApi.getApiClient()); 113 114 return _formationsApi; 115 } 116 117 /** 118 * Getter for GroupementsApi 119 * @return GroupementsApi 120 * @throws IOException If an error occurs while retrieving the token 121 */ 122 public GroupementsApi getGroupementsApi() throws IOException 123 { 124 if (_groupementsApi == null) 125 { 126 _groupementsApi = new GroupementsApi(); 127 } 128 129 updateApiClient(_groupementsApi.getApiClient()); 130 131 return _groupementsApi; 132 } 133 134 /** 135 * Getter for ObjetsFormationApi 136 * @return ObjetsFormationApi 137 * @throws IOException If an error occurs while retrieving the token 138 */ 139 public ObjetsFormationApi getObjetsFormationApi() throws IOException 140 { 141 if (_objetsFormationApi == null) 142 { 143 _objetsFormationApi = new ObjetsFormationApi(); 144 } 145 146 updateApiClient(_objetsFormationApi.getApiClient()); 147 148 return _objetsFormationApi; 149 } 150}