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;
025
026import org.ametys.cms.transformation.URIResolver;
027import org.ametys.core.util.FilenameUtils;
028import org.ametys.core.util.URIUtils;
029import org.ametys.plugins.repository.UnknownAmetysObjectException;
030import org.ametys.plugins.repository.metadata.RichText;
031import org.ametys.web.URIPrefixHandler;
032import org.ametys.web.WebHelper;
033
034/**
035 * {@link URIResolver} for type "local".resources local to a Content
036 * These links or images point resources local to a Content.
037 */
038public class LocalURIResolver extends org.ametys.cms.transformation.LocalURIResolver
039{
040    /** The URI prefix handler */
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, "/_contents", "");
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, "/_contents-images", "_" + 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, "/_contents-images", "_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 prefix a prefix to be prepended to the generated path. Should not be null.
106     * @param suffix a suffix to be appended to the generated path. Should not be null. 
107     * @return the path to the resource.
108     */
109    protected String _resolve(String uri, boolean download, boolean absolute, boolean internal, String prefix, String suffix)
110    {
111        URIInfo infos = getInfos(uri, true);
112        
113        Request request = ContextHelper.getRequest(_context);
114        
115        String siteName = WebHelper.getSiteName(request, infos.getContent());
116        if (siteName == null)
117        {
118            return null;
119        }
120        
121        RichText richText = _getMeta(infos.getContent().getMetadataHolder(), infos.getMetadata());
122        try
123        {
124            richText.getAdditionalDataFolder().getFile(infos.getPath());
125        }
126        catch (UnknownAmetysObjectException e)
127        {
128            getLogger().warn("Cannot resolve link " + uri);
129            return "";
130        }
131        
132        Map<String, String> params = new HashMap<>();
133        
134        if (download)
135        {
136            params.put("download", "true");
137        }
138        
139        if (infos.getContentVersion() != null)
140        {
141            params.put("contentVersion", infos.getContentVersion());
142        }
143        
144        StringBuilder resultPath = new StringBuilder();
145        
146        if (internal)
147        {
148            resultPath.append("cocoon://").append(siteName);
149        }
150        else if (absolute)
151        {
152            resultPath.append(_prefixHandler.getAbsoluteUriPrefix(siteName));
153        }
154        else
155        {
156            resultPath.append(_prefixHandler.getUriPrefix(siteName));
157        }
158        
159        resultPath.append(prefix)
160                  .append(FilenameUtils.encodePath(infos.getContent().getPath()))
161                  .append("/_metadata/").append(infos.getMetadata())
162                  .append("/_data/")
163                  .append(FilenameUtils.encodePath(infos.getPath()))
164                  .append(suffix);
165        
166        return URIUtils.encodeURI(resultPath.toString(), params);
167    }
168}