001/*
002 *  Copyright 2015 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.odf.xslt;
017
018import java.net.URI;
019import java.net.URISyntaxException;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.cocoon.environment.Request;
029
030import org.ametys.cms.transformation.xslt.ResolveURIComponent;
031import org.ametys.core.util.URLEncoder;
032import org.ametys.plugins.explorer.resources.Resource;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.runtime.config.Config;
035
036/**
037 * Helper component to be used from XSL stylesheets for CDM-fr export
038 */
039public class CDMFrXSLTHelper implements Serviceable, Contextualizable
040{
041    private static AmetysObjectResolver _ametysObjectResolver;
042    private static Context _context;
043    
044    @Override
045    public void contextualize(Context context) throws ContextException
046    {
047        _context = context;
048    }
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        _ametysObjectResolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
054    }
055    
056    /**
057     * Resolve the URI of a resource
058     * @param type Type name (defined by the extension to use)
059     * @param uri URI depending on the type
060     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
061     */
062    public static String resolve(String type, String uri)
063    {
064        return resolve(type, uri, false);
065    }
066    
067    /**
068     * Resolve the URI of a resource
069     * @param type Type name (defined by the extension to use)
070     * @param uri URI depending on the type
071     * @param download Is this uri for download purposes.
072     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
073     */
074    public static String resolve(String type, String uri, boolean download)
075    {
076        if ("explorer".equals(type))
077        {
078            Resource resource = (Resource) _ametysObjectResolver.resolveById(uri);
079            String fullPath = resource.getPath();
080            
081            StringBuilder result = new StringBuilder();
082            result.append((String) Config.getInstance().getValue("cms.url"))
083                    .append("/_odf/")
084                    .append("_resources")
085                    .append(URLEncoder.encodePath(fullPath).replaceAll(":", "%3A"));
086            
087            return result.toString();
088        }
089        else if ("page".equals(type))
090        {
091            // Force absolute
092            return ResolveURIComponent.resolve(type, uri, download, true, false);
093        }
094        else
095        {
096            // Resolve URI forcing not abosolute nor internal
097            String resolvedUri = ResolveURIComponent.resolve(type, uri, download, false, false);
098            return _resolve(resolvedUri);
099        }
100    }
101    
102    /**
103     * Resolve the URI of a resource image
104     * @param type Type name (defined by the extension to use)
105     * @param uri URI depending on the type
106     * @param height the height
107     * @param width the width
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 resolveImage(String type, String uri, int height, int width)
111    {
112        return resolveImage(type, uri, height, width, false);
113    }
114    
115    /**
116     * Resolve the absolute URI of a resource image
117     * @param type Type name (defined by the extension to use)
118     * @param uri URI depending on the type
119     * @param height the height
120     * @param width the width
121     * @param download Is this uri for download purposes.
122     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
123     */
124    public static String resolveImage(String type, String uri, int height, int width, boolean download)
125    {
126        if ("explorer".equals(type))
127        {
128            if (height == 0 && width == 0)
129            {
130                return _resolveResource(uri, download, "_resources", null);
131            }
132            
133            return _resolveResource(uri, download, "_resources-images", "_" + height + "x" + width);
134        }
135        else
136        {
137            // Resolve URI forcing not absolute nor internal
138            String resolvedUri = ResolveURIComponent.resolveImage(type, uri, height, width, download, false, false);
139            return _resolve(resolvedUri);
140        }
141    }
142    
143    /**
144     * Resolve the URI of a resource image
145     * @param type Type name (defined by the extension to use)
146     * @param uri URI depending on the type
147     * @param maxHeight the max height
148     * @param maxWidth the max width
149     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
150     */
151    public String resolveBoundedImage(String type, String uri, int maxHeight, int maxWidth)
152    {
153        return resolveBoundedImage(type, uri, maxHeight, maxWidth, false);
154    }
155    
156    /**
157     * Resolve the asbsolute URI of a resource image
158     * @param type Type name (defined by the extension to use)
159     * @param uri URI depending on the type
160     * @param maxHeight the max height
161     * @param maxWidth the max width
162     * @param download Is this uri for download purposes.
163     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
164     */
165    public String resolveBoundedImage(String type, String uri, int maxHeight, int maxWidth, boolean download)
166    {
167        if ("explorer".equals(type))
168        {
169            if (maxHeight == 0 && maxWidth == 0)
170            {
171                return _resolveResource(uri, download, "_resources", null);
172            }
173            
174            return _resolveResource(uri, download, "_resources-images", "_max" + maxHeight + "x" + maxWidth);
175        }
176        else
177        {
178            // Resolve URI forcing not absolute nor internal
179            String resolvedUri = ResolveURIComponent.resolveBoundedImage(type, uri, maxHeight, maxWidth, download, false, false);
180            return _resolve(resolvedUri);
181        }
182    }
183    
184    private static String _resolve (String resolvedUri)
185    {
186        // Redirect to "odf" workspace
187        Request request = ContextHelper.getRequest(_context);
188        String contextPath = request.getContextPath();
189        
190        StringBuilder result = new StringBuilder();
191        result.append((String) Config.getInstance().getValue("cms.url"))
192            .append("/_odf")
193            .append(org.apache.commons.lang.StringUtils.substringAfter(resolvedUri, contextPath));
194            
195        return result.toString();
196    }
197    
198    private static String _resolveResource (String uri, boolean download, String prefix, String suffix)
199    {
200        Resource resource = (Resource) _ametysObjectResolver.resolveById(uri);
201        String fullPath = resource.getPath();
202        
203        int i = fullPath.lastIndexOf(".");
204        String extension = i != -1 ? fullPath.substring(i) : null;
205        fullPath = i != -1 ? fullPath.substring(0, i) : fullPath; 
206        
207        StringBuilder result = new StringBuilder();
208        
209        // Always use absolute url
210        result.append((String) Config.getInstance().getValue("cms.url"))
211                .append("/_odf/")
212                .append(prefix)
213                .append(URLEncoder.encodePath(fullPath).replaceAll(":", "%3A"));
214        
215        if (suffix != null)
216        {
217            result.append(suffix);
218        }
219        
220        if (extension != null)
221        {
222            result.append(extension);
223        }
224        
225        try
226        {
227            return new URI(null, null, result.toString(), download ? "download=true" : null, null).toASCIIString();
228        }
229        catch (URISyntaxException e)
230        {
231            throw new RuntimeException(e);
232        }
233    }
234}