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.util.Collections;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
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.xslt.ResolveURIComponent;
030import org.ametys.core.util.FilenameUtils;
031import org.ametys.core.util.URIUtils;
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/_resource")
084                    .append(FilenameUtils.encodePath(fullPath));
085            
086            return URIUtils.encodePath(result.toString());
087        }
088        else if ("page".equals(type))
089        {
090            // Force absolute
091            return ResolveURIComponent.resolve(type, uri, download, true, false);
092        }
093        else
094        {
095            // Resolve URI forcing not absolute nor internal
096            String resolvedUri = ResolveURIComponent.resolve(type, uri, download, false, false);
097            return _resolve(resolvedUri);
098        }
099    }
100    
101    /**
102     * Resolve the URI of a resource image
103     * @param type Type name (defined by the extension to use)
104     * @param uri URI depending on the type
105     * @param height the height
106     * @param width the width
107     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
108     */
109    public static String resolveImage(String type, String uri, int height, int width)
110    {
111        return resolveImage(type, uri, height, width, false);
112    }
113    
114    /**
115     * Resolve the absolute URI of a resource image
116     * @param type Type name (defined by the extension to use)
117     * @param uri URI depending on the type
118     * @param height the height
119     * @param width the width
120     * @param download Is this uri for download purposes.
121     * @return The uri resolved, the empty string if the uri could not be resolved, or the uri itself if there is no resolver adapted
122     */
123    public static String resolveImage(String type, String uri, int height, int width, boolean download)
124    {
125        if ("explorer".equals(type))
126        {
127            if (height == 0 && width == 0)
128            {
129                return _resolveResource(uri, download, null);
130            }
131            
132            return _resolveResource(uri, download, "_" + height + "x" + width);
133        }
134        else
135        {
136            // Resolve URI forcing not absolute nor internal
137            String resolvedUri = ResolveURIComponent.resolveImage(type, uri, height, width, download, false, false);
138            return _resolve(resolvedUri);
139        }
140    }
141    
142    private static String _resolve (String resolvedUri)
143    {
144        // Redirect to "odf" workspace
145        Request request = ContextHelper.getRequest(_context);
146        String contextPath = request.getContextPath();
147        
148        StringBuilder result = new StringBuilder();
149        result.append((String) Config.getInstance().getValue("cms.url"))
150            .append("/_odf")
151            .append(org.apache.commons.lang.StringUtils.substringAfter(resolvedUri, contextPath));
152            
153        return result.toString();
154    }
155    
156    private static String _resolveResource (String uri, boolean download, String suffix)
157    {
158        Resource resource = (Resource) _ametysObjectResolver.resolveById(uri);
159        String fullPath = resource.getPath();
160        
161        int i = fullPath.lastIndexOf(".");
162        String extension = i != -1 ? fullPath.substring(i) : null;
163        fullPath = i != -1 ? fullPath.substring(0, i) : fullPath; 
164        
165        StringBuilder result = new StringBuilder();
166        
167        // Always use absolute url
168        result.append((String) Config.getInstance().getValue("cms.url"))
169                .append("/_odf/_resource")
170                .append(FilenameUtils.encodePath(fullPath));
171        
172        if (suffix != null)
173        {
174            result.append(suffix);
175        }
176        
177        if (extension != null)
178        {
179            result.append(extension);
180        }
181        
182        return URIUtils.encodeURI(result.toString(), download ? Collections.singletonMap("download", "true") : null);
183    }
184}