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.cms.transformation.xslt;
017
018import org.apache.avalon.framework.activity.Initializable;
019import org.apache.avalon.framework.context.Context;
020import org.apache.avalon.framework.context.ContextException;
021import org.apache.avalon.framework.context.Contextualizable;
022import org.apache.avalon.framework.logger.AbstractLogEnabled;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.cms.transformation.URIResolver;
030import org.ametys.cms.transformation.URIResolverExtensionPoint;
031
032/**
033 * This component resolve links and give a static hack access for xslt calls
034 */
035public class ResolveURIComponent extends AbstractLogEnabled implements Serviceable, Initializable, Contextualizable
036{
037    private static ResolveURIComponent _instance;
038    
039    private static Context _context;
040    
041    private URIResolverExtensionPoint _linkResolverExtensionPoint;
042    
043    @Override
044    public void contextualize(Context context) throws ContextException
045    {
046        _context = context;
047    }
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _linkResolverExtensionPoint = (URIResolverExtensionPoint) manager.lookup(URIResolverExtensionPoint.ROLE);
053    }
054    
055    @Override
056    public void initialize() throws Exception
057    {
058        _instance = this;
059    }
060    
061    /**
062     * Resolve an uri upon the LinkResolverExtensionPoint
063     * @param type Type name (defined by the extension to use)
064     * @param uri URI depending on the type
065     * @return The uri resolved, or the uri if there is no resolver adapted
066     */
067    public static String resolve(String type, String uri)
068    {
069        return resolve(type, uri, false);
070    }
071
072    /**
073     * Resolve an uri upon the LinkResolverExtensionPoint
074     * @param type Type name (defined by the extension to use)
075     * @param uri URI depending on the type
076     * @param download Is this uri for download purposes.
077     * @return The uri resolved, or the uri if there is no resolver adapted
078     */
079    public static String resolve(String type, String uri, boolean download)
080    {
081        // FIXME CMS-2611 Force absolute 
082        Request request = ContextHelper.getRequest(_context);
083        boolean absolute = request.getAttribute("forceAbsoluteUrl") != null ? (Boolean) request.getAttribute("forceAbsoluteUrl") : false;
084        
085        return resolve(type, uri, download, absolute);
086    }
087    
088    /**
089     * Resolve an uri upon the LinkResolverExtensionPoint
090     * @param type Type name (defined by the extension to use)
091     * @param uri URI depending on the type
092     * @param download Is this uri for download purposes.
093     * @param absolute true to generate absolute url
094     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
095     */
096    public static String resolve(String type, String uri, boolean download, boolean absolute)
097    {
098        return resolve(type, uri, download, absolute, false);
099    }
100    
101    /**
102     * Resolve an uri upon the LinkResolverExtensionPoint
103     * @param type Type name (defined by the extension to use)
104     * @param uri URI depending on the type
105     * @param download Is this uri for download purposes.
106     * @param absolute true to generate absolute url
107     * @param internal true to get an internal URI.
108     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
109     */
110    public static String resolve(String type, String uri, boolean download, boolean absolute, boolean internal)
111    {
112        URIResolver uriResolver = _instance._linkResolverExtensionPoint.getResolverForType(type);
113        if (uriResolver != null)
114        {
115            try
116            {
117                return uriResolver.resolve(uri, download, absolute, internal);
118            }
119            catch (Exception e)
120            {
121                _instance.getLogger().warn("Error resolving the uri '" + uri + "' with type " + type, e);
122                return "";
123            }
124        }
125        else
126        {
127            return uri;
128        }
129    }
130    
131    /**
132     * Resolve an uri upon the LinkResolverExtensionPoint
133     * @param type Type name (defined by the extension to use)
134     * @param uri URI depending on the type
135     * @param height the height
136     * @param width the width
137     * @return The uri resolved, or the uri if there is no resolver adapted
138     */
139    public static String resolveImage(String type, String uri, int height, int width)
140    {
141        return resolveImage (type, uri, height, width, false);
142    }
143    
144    /**
145     * Resolve an uri upon the LinkResolverExtensionPoint
146     * @param type Type name (defined by the extension to use)
147     * @param uri URI depending on the type
148     * @param height the height
149     * @param width the width
150     * @param download Is this uri for download purposes.
151     * @return The uri resolved, or the uri if there is no resolver adapted
152     */
153    public static String resolveImage(String type, String uri, int height, int width, boolean download)
154    {
155        // FIXME CMS-2611 Force absolute 
156        Request request = ContextHelper.getRequest(_context);
157        boolean absolute = request.getAttribute("forceAbsoluteUrl") != null ? (Boolean) request.getAttribute("forceAbsoluteUrl") : false;
158        
159        return resolveImage(type, uri, height, width, download, absolute);
160    }
161    
162    /**
163     * Resolve an uri upon the LinkResolverExtensionPoint
164     * @param type Type name (defined by the extension to use)
165     * @param uri URI depending on the type
166     * @param height the height
167     * @param width the width
168     * @param download Is this uri for download purposes.
169     * @param absolute true to generate absolute url
170     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
171     */
172    public static String resolveImage(String type, String uri, int height, int width, boolean download, boolean absolute)
173    {
174        return resolveImage(type, uri, height, width, download, absolute, false);
175    }
176    
177    /**
178     * Resolve an uri upon the LinkResolverExtensionPoint
179     * @param type Type name (defined by the extension to use)
180     * @param uri URI depending on the type
181     * @param height the height
182     * @param width the width
183     * @param download Is this uri for download purposes.
184     * @param absolute true to generate absolute url
185     * @param internal true to get an internal URI.
186     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
187     */
188    public static String resolveImage(String type, String uri, int height, int width, boolean download, boolean absolute, boolean internal)
189    {
190        // FIXME CMS-4059 Force base64 encoding. 
191        Request request = ContextHelper.getRequest(_context);
192        boolean encodeBase64 = request.getAttribute("forceBase64Encoding") != null ? (Boolean) request.getAttribute("forceBase64Encoding") : false;
193        
194        URIResolver uriResolver = _instance._linkResolverExtensionPoint.getResolverForType(type);
195        if (uriResolver != null)
196        {
197            try
198            {
199                if (encodeBase64)
200                {
201                    return uriResolver.resolveImageAsBase64(uri, height, width);
202                }
203                else
204                {
205                    return uriResolver.resolveImage(uri, height, width, download, absolute, internal);
206                }
207            }
208            catch (Exception e)
209            {
210                _instance.getLogger().warn("Error resolving the image of uri '" + uri + "' with type " + type, e);
211                return "";
212            }
213        }
214        else
215        {
216            return uri;
217        }
218    }
219    
220    /**
221     * Resolve an uri upon the LinkResolverExtensionPoint
222     * @param type Type name (defined by the extension to use)
223     * @param uri URI depending on the type
224     * @param maxHeight the max height
225     * @param maxWidth the max width
226     * @return The uri resolved, or the uri if there is no resolver adapted
227     */
228    public static String resolveBoundedImage(String type, String uri, int maxHeight, int maxWidth)
229    {
230        return resolveBoundedImage (type, uri, maxHeight, maxWidth, false);
231    }
232    
233    /**
234     * Resolve an uri upon the LinkResolverExtensionPoint
235     * @param type Type name (defined by the extension to use)
236     * @param uri URI depending on the type
237     * @param maxHeight the max height
238     * @param maxWidth the max width
239     * @param download Is this uri for download purposes.
240     * @return The uri resolved, or the uri if there is no resolver adapted
241     */
242    public static String resolveBoundedImage(String type, String uri, int maxHeight, int maxWidth, boolean download)
243    {
244        // FIXME CMS-2611 Force absolute 
245        Request request = ContextHelper.getRequest(_context);
246        boolean absolute = request.getAttribute("forceAbsoluteUrl") != null ? (Boolean) request.getAttribute("forceAbsoluteUrl") : false;
247        
248        return resolveBoundedImage(type, uri, maxHeight, maxWidth, download, absolute);
249    }
250    
251    /**
252     * Resolve an uri upon the LinkResolverExtensionPoint
253     * @param type Type name (defined by the extension to use)
254     * @param uri URI depending on the type
255     * @param maxHeight the max height
256     * @param maxWidth the max width
257     * @param download Is this uri for download purposes.
258     * @param absolute true to generate absolute url
259     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
260     */
261    public static String resolveBoundedImage(String type, String uri, int maxHeight, int maxWidth, boolean download, boolean absolute)
262    {
263        return resolveBoundedImage(type, uri, maxHeight, maxWidth, download, absolute, false);
264    }
265    
266    /**
267     * Resolve an uri upon the LinkResolverExtensionPoint
268     * @param type Type name (defined by the extension to use)
269     * @param uri URI depending on the type
270     * @param maxHeight the max height
271     * @param maxWidth the max width
272     * @param download Is this uri for download purposes.
273     * @param absolute true to generate absolute url
274     * @param internal true to get an internal URI.
275     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
276     */
277    public static String resolveBoundedImage(String type, String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
278    {
279        // FIXME CMS-4059 Force base64 encoding. 
280        Request request = ContextHelper.getRequest(_context);
281        boolean encodeBase64 = request.getAttribute("forceBase64Encoding") != null ? (Boolean) request.getAttribute("forceBase64Encoding") : false;
282        
283        URIResolver uriResolver = _instance._linkResolverExtensionPoint.getResolverForType(type);
284        if (uriResolver != null)
285        {
286            try
287            {
288                if (encodeBase64)
289                {
290                    return uriResolver.resolveBoundedImageAsBase64(uri, maxHeight, maxWidth);
291                }
292                else
293                {
294                    return uriResolver.resolveBoundedImage(uri, maxHeight, maxWidth, download, absolute, internal);
295                }
296            }
297            catch (Exception e)
298            {
299                _instance.getLogger().warn("Error resolving the image of uri '" + uri + "' with type " + type, e);
300                return "";
301            }
302        }
303        else
304        {
305            return uri;
306        }
307    }
308}