001/*
002 *  Copyright 2023 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.odf.cdmfr;
017
018import org.ametys.cms.data.File;
019import org.ametys.cms.repository.Content;
020import org.ametys.core.util.FilenameUtils;
021import org.ametys.core.util.HttpUtils;
022import org.ametys.core.util.URIUtils;
023import org.ametys.runtime.config.Config;
024
025/**
026 * Helper class containing useful methods to implements CDM-fr extension.
027 */
028public abstract class AbstractCDMfrExtension implements CDMfrExtension
029{
030    /**
031     * Computes an external URL for retrieving files after export.
032     * @param content the current exported content
033     * @param file the file being exported
034     * @param dataPath the data path of the file
035     * @return an URL for retrieving the file
036     */
037    protected String _getFileAbsoluteUrl(Content content, File file, String dataPath)
038    {
039        StringBuilder uri = new StringBuilder();
040        uri.append("/_odf")
041           .append("/_contents")
042           .append(FilenameUtils.encodePath(content.getPath()))
043           .append("/_attribute/")
044           .append(dataPath)
045           .append("/").append(FilenameUtils.encodeName(file.getName()));
046        
047        StringBuilder resultPath = new StringBuilder();
048        resultPath.append(HttpUtils.sanitize(Config.getInstance().getValue("cms.url")))
049                  .append(URIUtils.encodePath(uri.toString()))
050                  .append("?objectId=" + URIUtils.encodeParameter(content.getId()));
051        
052        return resultPath.toString();
053    }
054}