001/* 002 * Copyright 2015 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.plugins.odfweb.catalog; 017 018import java.io.IOException; 019import java.nio.file.DirectoryStream; 020import java.nio.file.DirectoryStream.Filter; 021import java.nio.file.Files; 022import java.nio.file.Path; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.avalon.framework.parameters.Parameters; 029import org.apache.cocoon.acting.AbstractAction; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Redirector; 032import org.apache.cocoon.environment.Request; 033import org.apache.cocoon.environment.SourceResolver; 034import org.apache.commons.lang.StringUtils; 035 036import org.ametys.core.cocoon.JSonReader; 037import org.ametys.web.source.SkinSource; 038 039/** 040 * Get available InDesign template files from the current skin 041 */ 042public class GetInDesignTemplatesAction extends AbstractAction 043{ 044 private static InDesignPathFilter __PATH_FILTER = new InDesignPathFilter(); 045 046 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 047 { 048 Request request = ObjectModelHelper.getRequest(objectModel); 049 050 Map<String, Object> result = new HashMap<>(); 051 052 List<Map<String, Object>> templates = new ArrayList<>(); 053 054 String skinName = request.getParameter("skinName"); 055 if (StringUtils.isNotEmpty(skinName)) 056 { 057 SkinSource indesignDirSource = (SkinSource) resolver.resolveURI("skin:" + skinName + "://indesign"); 058 059 if (indesignDirSource.exists() && indesignDirSource.isCollection()) 060 { 061 // Use getFile() to get only the first existing path 062 try (DirectoryStream<Path> models = Files.newDirectoryStream(indesignDirSource.getFile(), __PATH_FILTER)) 063 { 064 for (Path model : models) 065 { 066 String fileName = model.getFileName().toString(); 067 068 Map<String, Object> info = new HashMap<>(); 069 info.put("filepath", fileName); 070 info.put("filename", fileName.substring(0, fileName.lastIndexOf("."))); 071 072 templates.add(info); 073 } 074 } 075 } 076 } 077 078 result.put("templates", templates); 079 080 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 081 return EMPTY_MAP; 082 } 083 084 static class InDesignPathFilter implements Filter<Path> 085 { 086 public boolean accept(Path entry) throws IOException 087 { 088 return Files.isRegularFile(entry) && entry.getFileName().toString().toLowerCase().endsWith(".indt"); 089 } 090 } 091}