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