001/*
002 *  Copyright 2020 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.cms.transformation;
017
018import org.apache.avalon.framework.logger.AbstractLogEnabled;
019
020/**
021 * Abstract superclass for simple {@link URIResolver}.
022 */
023public abstract class AbstractURIResolver extends AbstractLogEnabled implements URIResolver
024{
025    @Override
026    public String resolve(String uri, boolean download, boolean absolute, boolean internal)
027    {
028        return _resolve(uri, "", download, absolute, internal);
029    }
030    
031    @Override
032    public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal)
033    {
034        if (height == 0 && width == 0)
035        {
036            return resolve(uri, download, absolute, internal);
037        }
038        
039        StringBuilder uriArgument = new StringBuilder();
040        uriArgument.append("_").append(height).append("x").append(width);
041        return _resolve(uri, uriArgument.toString(), download, absolute, internal);
042    }
043    
044    @Override
045    public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
046    {
047        if (maxHeight == 0 && maxWidth == 0)
048        {
049            return resolve(uri, download, absolute, internal);
050        }
051        
052        StringBuilder uriArgument = new StringBuilder();
053        uriArgument.append("_max").append(maxHeight).append("x").append(maxWidth);
054        return _resolve(uri, uriArgument.toString(), download, absolute, internal);
055    }
056    
057    
058    @Override
059    public String resolveCroppedImage(String uri, int cropHeight, int cropWidth, boolean download, boolean absolute, boolean internal)
060    {
061        if (cropHeight == 0 && cropWidth == 0)
062        {
063            return resolve(uri, download, absolute, internal);
064        }
065        
066        StringBuilder uriArgument = new StringBuilder();
067        uriArgument.append("_crop").append(cropHeight).append("x").append(cropWidth);
068        return _resolve(uri, uriArgument.toString(), download, absolute, internal);
069    }
070    
071    @Override
072    public String resolveImageAsBase64(String uri, int height, int width)
073    {
074        return resolveImageAsBase64(uri, height, width, 0, 0, 0, 0);
075    }
076    
077    @Override
078    public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth)
079    {
080        return resolveImageAsBase64(uri, 0, 0, maxHeight, maxWidth, 0, 0);
081    }
082    
083    @Override
084    public String resolveCroppedImageAsBase64(String uri, int cropHeight, int cropWidth)
085    {
086        return resolveImageAsBase64(uri, 0, 0, 0, 0, cropHeight, cropWidth);
087    }
088
089    /**
090     * Resolves a link URI for rendering.<br>
091     * The output must be a properly encoded path, relative to the webapp context, accessible from a browser.
092     * @param uri the link URI.
093     * @param uriArgument the argument to append to the uri
094     * @param download true if the pointed resource is to be downloaded.
095     * @param absolute true if the url must be absolute
096     * @param internal true to get an internal URI.
097     * @return the path to the image.
098     */
099    protected abstract String _resolve(String uri, String uriArgument, boolean download, boolean absolute, boolean internal);
100    
101    /**
102     * Get an image's bytes encoded as base64, optionally resized. 
103     * @param uri the image URI.
104     * @param height the specified height. Ignored if negative.
105     * @param width the specified width. Ignored if negative.
106     * @param maxHeight the maximum image height. Ignored if height or width is specified.
107     * @param maxWidth the maximum image width. Ignored if height or width is specified.
108     * @param cropHeight The cropping height. Ignored if negative.
109     * @param cropWidth The cropping width. Ignored if negative.
110     * @return the image bytes encoded as base64.
111     */
112    protected abstract String resolveImageAsBase64(String uri, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth);
113}