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.core.util.HttpUtils; 027import org.ametys.runtime.config.Config; 028import org.ametys.runtime.plugin.component.AbstractLogEnabled; 029 030import fr.pcscol.pegase.odf.ApiClient; 031import fr.pcscol.pegase.odf.api.EspacesExterneApi; 032import fr.pcscol.pegase.odf.api.MaquettesApi; 033import fr.pcscol.pegase.odf.api.MaquettesExterneApi; 034import fr.pcscol.pegase.odf.api.ObjetsMaquetteApi; 035import fr.pcscol.pegase.odf.api.ObjetsMaquetteExterneApi; 036 037/** 038 * Manager for Pégase API. 039 * Get or create API objects and configure it. 040 */ 041public class PegaseApiManager extends AbstractLogEnabled implements Component, Serviceable, Initializable 042{ 043 /** Role */ 044 public static final String ROLE = PegaseApiManager.class.getName(); 045 046 /** The Pégase token manager */ 047 protected PegaseTokenManager _pegaseTokenManager; 048 049 /* Pégase APIs */ 050 private ObjetsMaquetteExterneApi _objetsMaquetteExterneApi; 051 private MaquettesExterneApi _maquettesExterneApi; 052 private ObjetsMaquetteApi _objetsMaquetteApi; 053 private MaquettesApi _maquettesApi; 054 private EspacesExterneApi _espacesExterneApi; 055 056 /* Configuration */ 057 private boolean _isActive; 058 private String _pegaseODFUrl; 059 060 public void service(ServiceManager manager) throws ServiceException 061 { 062 _pegaseTokenManager = (PegaseTokenManager) manager.lookup(PegaseTokenManager.ROLE); 063 } 064 065 public void initialize() throws Exception 066 { 067 _isActive = Config.getInstance().getValue("pegase.activate", true, false); 068 if (_isActive) 069 { 070 /* Configuration for API */ 071 _pegaseODFUrl = HttpUtils.sanitize(Config.getInstance().getValue("pegase.odf.url")); 072 } 073 } 074 075 private void _updateApiClient(ApiClient apiClient, boolean external) throws IOException 076 { 077 apiClient.setBearerToken(_pegaseTokenManager.getToken()); 078 apiClient.setServerUrl(_pegaseODFUrl); 079 apiClient.useExternalApi(external); 080 } 081 082 /** 083 * Getter for objetsMaquetteExterneApi 084 * @return objetsMaquetteExterneApi 085 * @throws IOException If an error occurs while retrieving the token 086 */ 087 public ObjetsMaquetteExterneApi getObjetsMaquetteExterneApi() throws IOException 088 { 089 if (_objetsMaquetteExterneApi == null) 090 { 091 _objetsMaquetteExterneApi = new ObjetsMaquetteExterneApi(); 092 } 093 094 _updateApiClient(_objetsMaquetteExterneApi.getApiClient(), true); 095 096 return _objetsMaquetteExterneApi; 097 } 098 099 /** 100 * Getter for MaquettesApi 101 * @return maquettesApi 102 * @throws IOException If an error occurs while retrieving the token 103 */ 104 public MaquettesExterneApi getMaquettesExterneApi() throws IOException 105 { 106 if (_maquettesExterneApi == null) 107 { 108 _maquettesExterneApi = new MaquettesExterneApi(); 109 } 110 111 _updateApiClient(_maquettesExterneApi.getApiClient(), true); 112 113 return _maquettesExterneApi; 114 } 115 116 /** 117 * Getter for ObjetsMaquetteApi 118 * @return objetsMaquetteApi 119 * @throws IOException If an error occurs while retrieving the token 120 */ 121 public ObjetsMaquetteApi getObjetsMaquetteApi() throws IOException 122 { 123 if (_objetsMaquetteApi == null) 124 { 125 _objetsMaquetteApi = new ObjetsMaquetteApi(); 126 } 127 128 _updateApiClient(_objetsMaquetteApi.getApiClient(), false); 129 130 return _objetsMaquetteApi; 131 } 132 133 /** 134 * Getter for ObjetsMaquetteApi 135 * @return objetsMaquetteApi 136 * @throws IOException If an error occurs while retrieving the token 137 */ 138 public MaquettesApi getMaquettesApi() throws IOException 139 { 140 if (_maquettesApi == null) 141 { 142 _maquettesApi = new MaquettesApi(); 143 } 144 145 _updateApiClient(_maquettesApi.getApiClient(), false); 146 147 return _maquettesApi; 148 } 149 150 /** 151 * Getter for EspacesExterneApi 152 * @return espacesExterneApi 153 * @throws IOException If an error occurs while retrieving the token 154 */ 155 public EspacesExterneApi getEspacesExterneApi() throws IOException 156 { 157 if (_espacesExterneApi == null) 158 { 159 _espacesExterneApi = new EspacesExterneApi(); 160 } 161 162 _updateApiClient(_espacesExterneApi.getApiClient(), true); 163 164 return _espacesExterneApi; 165 } 166}