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.web.workflow; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.components.source.impl.SitemapSource; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.apache.cocoon.xml.AttributesImpl; 028import org.apache.cocoon.xml.XMLUtils; 029import org.apache.excalibur.source.SourceResolver; 030import org.xml.sax.SAXException; 031 032import org.ametys.cms.repository.Content; 033import org.ametys.cms.repository.RequestAttributeWorkspaceSelector; 034import org.ametys.core.util.IgnoreRootHandler; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.plugins.repository.UnknownAmetysObjectException; 037import org.ametys.plugins.repository.version.VersionableAmetysObject; 038import org.ametys.runtime.parameter.ParameterHelper; 039import org.ametys.web.WebConstants; 040import org.ametys.web.renderingcontext.RenderingContext; 041import org.ametys.web.renderingcontext.RenderingContextHandler; 042import org.ametys.web.repository.site.SiteManager; 043 044/** 045 * SAX the contents with id passed from request parameters 046 * 047 */ 048public class GetLastPublishedContents extends ServiceableGenerator 049{ 050 private SourceResolver _sourceResolver; 051 private AmetysObjectResolver _resolver; 052 private SiteManager _siteManager; 053 private RenderingContextHandler _renderingContextHandler; 054 055 @Override 056 public void service(ServiceManager smanager) throws ServiceException 057 { 058 _sourceResolver = (SourceResolver) smanager.lookup(SourceResolver.ROLE); 059 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 060 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 061 _renderingContextHandler = (RenderingContextHandler) smanager.lookup(RenderingContextHandler.ROLE); 062 } 063 064 @Override 065 public void generate() throws IOException, SAXException, ProcessingException 066 { 067 Request request = ObjectModelHelper.getRequest(objectModel); 068 String[] contentIds = request.getParameterValues("contentId"); 069 String metadataSetName = request.getParameter ("metadataSetName"); 070 071 contentHandler.startDocument(); 072 XMLUtils.startElement(contentHandler, "contents"); 073 074 String originalWorkspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 075 076 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE); 077 078 for (String id : contentIds) 079 { 080 try 081 { 082 Content content = _resolver.resolveById(id); 083 if (content instanceof VersionableAmetysObject) 084 { 085 saxContent(request, content, metadataSetName != null ? metadataSetName : "main"); 086 } 087 } 088 catch (UnknownAmetysObjectException e) 089 { 090 // The content just doesn't have a live version: ignore. 091 } 092 } 093 094 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, originalWorkspace); 095 096 XMLUtils.endElement(contentHandler, "contents"); 097 contentHandler.endDocument(); 098 } 099 100 /** 101 * SAX a content in its specific view 102 * @param request the request 103 * @param content The content to SAX 104 * @param metadataSetName The metadata set to use 105 * @throws SAXException If an error occurs while SAXing 106 * @throws IOException If an error occurs while retrieving content. 107 */ 108 protected void saxContent (Request request, Content content, String metadataSetName) throws SAXException, IOException 109 { 110 RenderingContext currentContext = _renderingContextHandler.getRenderingContext(); 111 _renderingContextHandler.setRenderingContext(RenderingContext.FRONT); 112 113 // FIXME CMS-2611 Force absolute 114 // FIXME CMS-4059 Force base64 encoding. 115 String siteName = (String) request.getAttribute("siteName"); 116 String baseUrl = _siteManager.getSite(siteName).getUrl(); 117 request.setAttribute("_baseServerPath", baseUrl); 118 request.setAttribute("_contextPath", ""); 119 request.setAttribute("forceAbsoluteUrl", true); 120 request.setAttribute("forceBase64Encoding", true); 121 122 AttributesImpl attrs = new AttributesImpl(); 123 attrs.addCDATAAttribute("id", content.getId()); 124 attrs.addCDATAAttribute("name", content.getName()); 125 attrs.addCDATAAttribute("title", content.getTitle()); 126 attrs.addCDATAAttribute("lastModifiedAt", ParameterHelper.valueToString(content.getLastModified())); 127 128 XMLUtils.startElement(contentHandler, "content", attrs); 129 130 String uri = "cocoon://_content.html?contentId=" + content.getId() + "&metadataSetName=" + metadataSetName; 131 SitemapSource src = null; 132 133 try 134 { 135 src = (SitemapSource) _sourceResolver.resolveURI(uri); 136 src.toSAX(new IgnoreRootHandler(contentHandler)); 137 } 138 finally 139 { 140 _sourceResolver.release(src); 141 } 142 143 XMLUtils.endElement(contentHandler, "content"); 144 145 _renderingContextHandler.setRenderingContext(currentContext); 146 request.removeAttribute("forceAbsoluteUrl"); 147 request.removeAttribute("forceBase64Encoding"); 148 request.removeAttribute("_baseServerPath"); 149 request.removeAttribute("_contextPath"); 150 } 151}