001/*
002 *  Copyright 2019 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.plugins.newsletter.testsending;
017
018import java.io.InputStream;
019
020import javax.jcr.Repository;
021import javax.jcr.Session;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.cms.data.NamedResource;
029import org.ametys.cms.data.RichText;
030import org.ametys.cms.transformation.ImageResolverHelper;
031import org.ametys.web.WebConstants;
032import org.ametys.web.WebHelper;
033import org.ametys.web.editor.LocalURIResolver;
034
035/**
036 * Resolver for local uri in newsletters
037 */
038public class NewsletterLocalURIResolver extends LocalURIResolver
039{
040    /** resolver data type for newsletter local */
041    public static final String NEWSLETTER_LOCAL_DATA_TYPE = "newsletter-local";
042    private Repository _repository;
043
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _repository = (Repository) manager.lookup(Repository.class.getName());
049    }
050    
051    @Override
052    public String getType()
053    {
054        return NEWSLETTER_LOCAL_DATA_TYPE;
055    }
056    
057    @Override
058    public String resolve(String uri, boolean download, boolean absolute, boolean internal)
059    {
060        return _resolve(uri, download, absolute, internal, null);
061    }
062    
063    @Override
064    public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal)
065    {
066        String suffix = height != 0 || width != 0 ? "_" + height + "x" + width : null;
067        return _resolve(uri, download, absolute, internal, suffix);
068    }
069    
070    @Override
071    public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
072    {
073        String suffix = maxHeight != 0 || maxWidth != 0 ? "_max" + maxHeight + "x" + maxWidth : null;
074        return _resolve(uri, download, absolute, internal, suffix);
075    }
076    
077    @Override
078    public String resolveCroppedImage(String uri, int cropHeight, int cropWidth, boolean download, boolean absolute, boolean internal)
079    {
080        String suffix = cropHeight != 0 || cropWidth != 0 ? "_crop" + cropHeight + "x" + cropWidth : null;
081        return _resolve(uri, download, absolute, internal, suffix);
082    }
083    
084    @Override
085    public String resolveImageAsBase64(String uri, int height, int width)
086    {
087        return _resolveImageAsBase64(uri, height, width, 0, 0, 0, 0);
088    }
089    
090    @Override
091    public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth)
092    {
093        return _resolveImageAsBase64(uri, 0, 0, maxHeight, maxWidth, 0, 0);
094    }
095    
096    @Override
097    public String resolveCroppedImageAsBase64(String uri, int cropHeight, int cropWidth)
098    {
099        return _resolveImageAsBase64(uri, 0, 0, 0, 0, cropHeight, cropWidth);
100    }
101    
102    /**
103     * Resolves a link URI for rendering purposes.<br>
104     * The output must be a properly encoded path, relative to the webapp context, accessible from a browser.
105     * @param uri the link URI.
106     * @param download true if the pointed resource is to be downloaded.
107     * @param absolute true if the url must be absolute
108     * @param internal true to get an internal URI.
109     * @param suffix The suffix to add to the resolved path
110     * @return the path to the resource.
111     */
112    @Override
113    protected String _resolve(String uri, boolean download, boolean absolute, boolean internal, String suffix)
114    {
115        Session liveSession = null;
116        try
117        {
118            liveSession = _repository.login(WebConstants.LIVE_WORKSPACE);
119            
120            URIInfo infos = getInfos(uri, true, liveSession);
121            
122            Request request = ContextHelper.getRequest(_context);
123            
124            String siteName = WebHelper.getSiteName(request, infos.getContent());
125            
126            return _resolve(infos, uri, siteName, download, absolute, internal, suffix);
127        }
128        catch (Exception e)
129        {
130            getLogger().warn("Cannot resolve link " + uri, e);
131            return "";
132        }
133        finally
134        {
135            if (liveSession != null)
136            {
137                liveSession.logout();
138            }
139        }
140    }
141    
142    /**
143     * Resolve image as base 64
144     * @param uri the link URI.
145     * @param height the specified height. Ignored if 0.
146     * @param width the specified width. Ignored if 0.
147     * @param maxHeight the maximum image height. Ignored if height or width is specified.
148     * @param maxWidth the maximum image width. Ignored if height or width is specified.
149     * @param cropHeight the cropping height. Ignored if 0.
150     * @param cropWidth the cropping width. Ignored if 0.
151     * @return a base64-encoded string representing the image.
152     */
153    protected String _resolveImageAsBase64(String uri, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth)
154    {
155        Session liveSession = null;
156        try
157        {
158            liveSession = _repository.login(WebConstants.LIVE_WORKSPACE);
159            URIInfo infos = getInfos(uri, true, liveSession);
160            
161            RichText richText = infos.getContent().getValue(infos.getAttribute());
162            NamedResource attachment = richText.getAttachment(infos.getFilename());
163            
164            try (InputStream dataIs = attachment.getInputStream())
165            {
166                return ImageResolverHelper.resolveImageAsBase64(dataIs, attachment.getMimeType(), height, width, maxHeight, maxWidth, cropHeight, cropWidth);
167            }
168        }
169        catch (Exception e)
170        {
171            getLogger().warn("Cannot resolve link " + uri);
172            return "";
173        }
174        finally
175        {
176            if (liveSession != null)
177            {
178                liveSession.logout();
179            }
180        }
181    }
182}