001/*
002 *  Copyright 2021 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.web.editor;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.cocoon.components.ContextHelper;
021import org.apache.cocoon.environment.Request;
022
023import org.ametys.web.URIPrefixHandler;
024import org.ametys.web.WebHelper;
025import org.ametys.web.renderingcontext.RenderingContext;
026import org.ametys.web.renderingcontext.RenderingContextHandler;
027
028/**
029 * Handler to enhance the rich text attachments.
030 * Add the content id and the data path to the local attachment's attribute
031 */
032public class DocbookLocalMediaObjectEditionHandler extends org.ametys.cms.transformation.htmledition.DocbookLocalMediaObjectEditionHandler
033{
034    private URIPrefixHandler _prefixHandler;
035    private RenderingContextHandler _renderingContextHandler;
036
037    @Override
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        super.service(manager);
041        _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
042        _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
043    }
044    
045    @Override
046    protected String getRichTextByUUIDURL(String uuid)
047    {
048        if (_renderingContextHandler.getRenderingContext() == RenderingContext.BACK)
049        {
050            return super.getRichTextByUUIDURL(uuid);
051        }
052        else
053        {
054            StringBuilder resultPath = new StringBuilder();
055            
056            Request request = ContextHelper.getRequest(_context);
057            String currentSiteName = WebHelper.getSiteName(request);
058            
059            resultPath.append(_prefixHandler.computeUriPrefix(currentSiteName, false, false))
060                .append("/plugins/cms/richText-file-by-uuid/")
061                .append(uuid);
062            
063            return resultPath.toString();
064        }
065    }
066}