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