001/* 002 * Copyright 2013 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.parameters; 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.cocoon.servlet.multipart.Part; 030import org.apache.excalibur.source.impl.FileSource; 031 032import org.ametys.cms.file.FileHelper; 033import org.ametys.core.cocoon.JSonReader; 034 035/** 036 * Create or update a file 037 * 038 */ 039public class AddOrUpdateFileAction extends ServiceableAction 040{ 041 private org.apache.excalibur.source.SourceResolver _sResolver; 042 private FileHelper _fileHelper; 043 044 @Override 045 public void service(ServiceManager smanager) throws ServiceException 046 { 047 _sResolver = (org.apache.excalibur.source.SourceResolver) smanager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); 048 _fileHelper = (FileHelper) smanager.lookup(FileHelper.ROLE); 049 } 050 051 @Override 052 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 053 { 054 Map<String, Object> result = new HashMap<>(); 055 056 Request request = ObjectModelHelper.getRequest(objectModel); 057 String filePath = request.getParameter("parentPath"); 058 String mode = parameters.getParameter("mode", "add"); 059 060 FileSource paramsDir = (FileSource) _sResolver.resolveURI("context://WEB-INF/param"); 061 FileSource parentDir = (FileSource) _sResolver.resolveURI("context://WEB-INF/param" + (filePath.length() > 0 ? "/" + filePath : "")); 062 063 Part part = (Part) request.get("filename"); 064 065 result.putAll(_fileHelper.addOrUpdateFile(part, parentDir, mode, false)); 066 067 if (result.containsKey("uri")) 068 { 069 String fileUrl = (String) result.get("uri"); 070 String path = fileUrl.substring(paramsDir.getURI().length()); 071 result.put("path", path.endsWith("/") ? path.substring(0, path.length() - 1) : path); 072 String parentPath = parentDir.getURI().substring(paramsDir.getURI().length()); 073 result.put("parentPath", parentPath.endsWith("/") ? parentPath.substring(0, parentPath.length() - 1) : parentPath); 074 } 075 076 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 077 return EMPTY_MAP; 078 } 079}