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