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.content;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Map;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.context.Contextualizable;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.avalon.framework.thread.ThreadSafe;
029import org.apache.cocoon.components.ContextHelper;
030import org.apache.cocoon.environment.Request;
031import org.apache.excalibur.source.Source;
032import org.apache.excalibur.source.SourceFactory;
033
034import org.ametys.cms.data.Binary;
035import org.ametys.cms.data.BinarySource;
036import org.ametys.cms.repository.Content;
037import org.ametys.core.util.FilenameUtils;
038import org.ametys.plugins.repository.AmetysObjectResolver;
039import org.ametys.runtime.plugin.component.AbstractLogEnabled;
040
041/**
042 * {@link SourceFactory} for content attributes.
043 */
044public class AttributeSourceFactory extends AbstractLogEnabled implements SourceFactory, ThreadSafe, Serviceable, Contextualizable
045{
046    /** source scheme */
047    public static final String ATTRIBUTE_SCHEME = "attribute";
048    
049    private Context _context;
050    private AmetysObjectResolver _resolver;
051    
052    @Override
053    public void contextualize(Context context) throws ContextException
054    {
055        _context = context;
056    }
057    
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
061    }
062    
063    public Source getSource(String location, Map parameters) throws IOException, MalformedURLException
064    {
065        String uri = location.substring((ATTRIBUTE_SCHEME + "://").length());
066        
067        // uri are like <contentPath>@<attributePath>;<filename>
068        int i = uri.indexOf('@');
069        int j = uri.indexOf(';', i);
070        String path = uri.substring(0, i);
071        String attribute = uri.substring(i + 1, j);
072        String filename = uri.substring(j + 1);
073        
074        Request request = ContextHelper.getRequest(_context);
075        Content content = (Content) request.getAttribute(Content.class.getName());
076        
077        try
078        {
079            if (content == null) 
080            {
081                content = _resolver.resolveByPath(FilenameUtils.decode(path));
082            }
083            
084            Binary binary = content.getValue(attribute);
085    
086            return new BinarySource(binary, FilenameUtils.decode(filename), location, ATTRIBUTE_SCHEME);
087        }
088        catch (Exception e)
089        {
090            getLogger().error("Unable to resolve binary attribute for uri " + location, e);
091            
092            // returns an unexisting Source
093            return new BinarySource(null, FilenameUtils.decode(filename), location, ATTRIBUTE_SCHEME);
094        }
095    }
096
097    public void release(Source source)
098    {
099        // nothing to do
100    }
101}