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.content;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ResourceNotFoundException;
025import org.apache.cocoon.acting.ServiceableAction;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.SourceResolver;
030import org.apache.commons.lang.StringUtils;
031import org.apache.commons.lang3.ArrayUtils;
032
033import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
034import org.ametys.cms.contenttype.ContentTypesHelper;
035import org.ametys.cms.repository.Content;
036import org.ametys.plugins.repository.AmetysObject;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.repository.UnknownAmetysObjectException;
039import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
040
041/**
042 * This action export the plugin name and content type for the current content.
043 */
044public class GetContentAction extends ServiceableAction
045{
046    /** The key for plugin's name */
047    public static final String RESULT_PLUGINNAME = "plugin";
048    /** The key for content type */
049    public static final String RESULT_CONTENTTYPE = "contentType";
050    /** The key for content version */
051    public static final String RESULT_CONTENTVERSION = "contentVersion";
052    /** The key for rendering  language */
053    public static final String RESULT_RENDERINGLANGUAGE = "renderingLanguage";
054    
055    /** The content type extension point */
056    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
057    /** The ametys object resolver */
058    protected AmetysObjectResolver _resolver;
059    /** Helper for content types */
060    protected ContentTypesHelper _contentTypeshelper;
061    
062    @Override
063    public void service(ServiceManager serviceManager) throws ServiceException
064    {
065        super.service(serviceManager);
066        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
067        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
068        _contentTypeshelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
069    }
070
071    public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
072    {
073        Map<String, String> result = new HashMap<>();
074        Request request = ObjectModelHelper.getRequest(objectModel);
075        
076        AmetysObject ametysObject;
077        String id = parameters.getParameter("id", null);
078        String path = parameters.getParameter("path", null);
079        
080        try
081        {
082            if (id != null)
083            {
084                ametysObject = _resolver.resolveById(id);
085            }
086            else if (path != null)
087            {
088                path = path.replaceAll("%3A", ":");
089                ametysObject = _resolver.resolveByPath(path);
090            }
091            else
092            {
093                Content content = (Content) request.getAttribute(Content.class.getName());
094                ametysObject = content != null ? content : (AmetysObject) request.getAttribute(AmetysObject.class.getName());
095            }
096        }
097        catch (UnknownAmetysObjectException e)
098        {
099            // Transorm a 500 error into a 404
100            throw new ResourceNotFoundException("Content not found", e);
101        }
102        
103        if (ametysObject instanceof Content)
104        {
105            String versionLabel = parameters.getParameter("versionLabel", null);
106            String revision = parameters.getParameter("contentVersion", null);
107            
108            if (StringUtils.isNotEmpty(versionLabel) && ametysObject instanceof VersionAwareAmetysObject versionAO)
109            {
110                String[] allLabels = versionAO.getAllLabels();
111                if (ArrayUtils.contains(allLabels, versionLabel))
112                {
113                    versionAO.switchToLabel(versionLabel);
114                }
115            }
116            else if (StringUtils.isNotEmpty(revision) && ametysObject instanceof VersionAwareAmetysObject versionAO)
117            {
118                versionAO.switchToRevision(revision);
119            }
120
121            Content content = (Content) ametysObject;
122            request.setAttribute(Content.class.getName(), content);
123            if (request.getAttribute(RESULT_RENDERINGLANGUAGE) == null)
124            {
125                String lang = content.getLanguage();
126                if (lang == null)
127                {
128                    lang = request.getParameter("lang");
129                }
130                if (lang == null)
131                {
132                    lang = "en";
133                }
134                
135                request.setAttribute(RESULT_RENDERINGLANGUAGE, lang);
136                result.put(RESULT_RENDERINGLANGUAGE, lang);
137            }
138            else
139            {
140                result.put(RESULT_RENDERINGLANGUAGE, (String) request.getAttribute(RESULT_RENDERINGLANGUAGE));
141            }
142            
143            String contentTypeId = _contentTypeshelper.getContentTypeIdForRendering(content);
144            
145            request.setAttribute(RESULT_CONTENTTYPE, contentTypeId);
146            
147            result.put(RESULT_CONTENTTYPE, contentTypeId);
148            result.put(RESULT_PLUGINNAME, _contentTypeshelper.getContentTypePluginForRendering(content));
149            result.put(RESULT_CONTENTVERSION, revision);
150        }
151        else
152        {
153            request.setAttribute(AmetysObject.class.getName(), ametysObject);
154        }
155        
156        return result;
157    }
158}
159