001/*
002 *  Copyright 2010 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 java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.cms.data.RichText;
028import org.ametys.cms.transformation.URIResolver;
029import org.ametys.core.util.FilenameUtils;
030import org.ametys.core.util.URIUtils;
031import org.ametys.web.URIPrefixHandler;
032import org.ametys.web.WebHelper;
033
034/**
035 * {@link URIResolver} for type "local".
036 * These links or images point to resources local to a rich text.
037 */
038public class LocalURIResolver extends org.ametys.cms.transformation.LocalURIResolver
039{
040    /** the {@link URIPrefixHandler} s*/
041    protected URIPrefixHandler _prefixHandler;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
048    }
049    
050    @Override
051    public String resolve(String uri, boolean download, boolean absolute, boolean internal)
052    {
053        String result = _resolve(uri, download, absolute, internal, "");
054        
055        if (result == null)
056        {
057            super.resolve(uri, download, absolute, internal);
058        }
059        
060        return result;
061    }
062    
063    @Override
064    public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal)
065    {
066        if (height == 0 && width == 0)
067        {
068            return resolve(uri, download, absolute, internal);
069        }
070        
071        String result = _resolve(uri, download, absolute, internal, "_" + height + "x" + width);
072        
073        if (result == null)
074        {
075            super.resolveImage(uri, height, width, download, absolute, internal);
076        }
077        
078        return result;
079    }
080    
081    @Override
082    public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
083    {
084        if (maxHeight == 0 && maxWidth == 0)
085        {
086            return resolve(uri, download, absolute, internal);
087        }
088        
089        String result = _resolve(uri, download, absolute, internal, "_max" + maxWidth + "x" + maxWidth);
090        
091        if (result == null)
092        {
093            super.resolveBoundedImage(uri, maxHeight, maxWidth, download, absolute, internal);
094        }
095        
096        return result;
097    }
098    
099    /**
100     * Actually resolves the URI.
101     * @param uri the link URI.
102     * @param download true if the pointed resource is to be downloaded.
103     * @param absolute true if the url must be absolute
104     * @param internal true to get an internal URI.
105     * @param suffix a suffix to be appended to the generated path. Should not be null. 
106     * @return the path to the resource.
107     */
108    protected String _resolve(String uri, boolean download, boolean absolute, boolean internal, String suffix)
109    {
110        URIInfo infos = getInfos(uri, true, null);
111        
112        Request request = ContextHelper.getRequest(_context);
113        
114        String siteName = WebHelper.getSiteName(request, infos.getContent());
115        if (siteName == null)
116        {
117            return null;
118        }
119        
120        return _resolve(infos, uri, siteName, download, absolute, internal, suffix);
121    }
122    
123    /**
124     * Actually resolves the URI.
125     * @param infos info about parsed uri.
126     * @param uri the link URI.
127     * @param siteName the siteName, if any.
128     * @param download true if the pointed resource is to be downloaded.
129     * @param absolute true if the url must be absolute
130     * @param internal true to get an internal URI.
131     * @param suffix a suffix to be appended to the generated path. Should not be null. 
132     * @return the path to the resource.
133     */
134    protected String _resolve(URIInfo infos, String uri, String siteName, boolean download, boolean absolute, boolean internal, String suffix)
135    {
136        RichText richText = infos.getContent().getValue(infos.getAttribute());
137        boolean hasAttachment = richText != null && richText.hasAttachment(infos.getFilename());
138        if (!hasAttachment)
139        {
140            getLogger().warn("Cannot resolve link " + uri);
141            return StringUtils.EMPTY;
142        }
143        
144        StringBuilder resultPath = new StringBuilder();
145        
146        String filename = FilenameUtils.encodeName(infos.getFilename());
147        
148        String baseName = org.apache.commons.io.FilenameUtils.getBaseName(filename);
149        String extension = org.apache.commons.io.FilenameUtils.getExtension(filename);
150        
151        resultPath.append(_prefixHandler.computeUriPrefix(siteName, absolute, internal))
152                  .append("/_richText-file")
153                  .append(FilenameUtils.encodePath(infos.getContent().getPath()))
154                  .append("/_attribute/").append(infos.getAttribute())
155                  .append("/_data/")
156                  .append(baseName)
157                  .append(suffix)
158                  .append(extension.isEmpty() ? "" : "." + extension);
159        
160        Map<String, String> params = new HashMap<>();
161        
162        if (download)
163        {
164            params.put("download", "true");
165        }
166        
167        infos.getContentVersion()
168             .ifPresent(version -> params.put("contentVersion", version));
169        
170        return URIUtils.encodeURI(resultPath.toString(), params);
171    }
172}