001/*
002 *  Copyright 2020 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 */
016
017package org.ametys.plugins.workspaces.documents.onlyoffice;
018
019import java.text.SimpleDateFormat;
020import java.util.Date;
021import java.util.Map;
022import java.util.concurrent.ConcurrentHashMap;
023
024import org.apache.avalon.framework.activity.Initializable;
025import org.apache.avalon.framework.component.Component;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030
031/**
032 * Manager for key/token used in OnlyOffice
033 */
034public class OnlyOfficeKeyManager extends AbstractLogEnabled implements Component, Initializable
035{
036    /** The Avalon role */
037    public static final String ROLE = OnlyOfficeKeyManager.class.getName();
038    
039    private Map<String, String> _onlyOfficeKeyStorage;
040    
041    public void initialize() throws Exception
042    {
043        _onlyOfficeKeyStorage = new ConcurrentHashMap<>();
044    }
045    
046    /**
047     * Get the OnlyOffice key associated with the resource
048     * If a key exist for the resourceId, return it and increment _nbHolders
049     * If no key exist for the resourceId, create a new key, put at 1 the _nbHolders and return the key
050     * @param resourceId id of the resource to look
051     * @return the OnlyOffice key
052     */
053    @Callable
054    public synchronized String getKey(String resourceId)
055    {
056        String tokenKey = _resourceIdToTokenKey(resourceId);
057        return _onlyOfficeKeyStorage.computeIfAbsent(tokenKey, this::createKey);
058    }
059    
060    /**
061     * Create the OnlyOffice key
062     * @param tokenKey id of the resource associate to the key
063     * @return the new key
064     */
065    protected String createKey(String tokenKey)
066    {
067        String currentTime = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
068        return tokenKey + "." + currentTime;
069    }
070    
071    /**
072     * Remove OnlyOffice key
073     * @param resourceId id of the resource
074     */
075    public void removeKey(String resourceId)
076    {
077        String tokenKey = _resourceIdToTokenKey(resourceId);
078        _onlyOfficeKeyStorage.remove(tokenKey);
079    }
080    
081    private String _resourceIdToTokenKey(String ressourceId)
082    {
083        return StringUtils.substringAfter(ressourceId, "://");
084    }
085}