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.plugins.skineditor.resources; 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.core.cocoon.JSonReader; 033import org.ametys.core.file.FileHelper; 034import org.ametys.plugins.skincommons.SkinEditionHelper; 035import org.ametys.plugins.skincommons.SkinLockManager; 036 037/** 038 * This action creates or updates a resource file. 039 */ 040public class AddOrUpdateFileAction extends ServiceableAction 041{ 042 /** Constant for skin editor tool id */ 043 public static final String SKIN_EDITOR_TOOL_ID = "uitool-skineditor"; 044 045 /** The lock manager */ 046 protected SkinLockManager _lockManager; 047 /** The skin edition helper */ 048 protected SkinEditionHelper _skinHelper; 049 050 private org.apache.excalibur.source.SourceResolver _srcResolver; 051 052 private FileHelper _fileHelper; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(smanager); 058 _skinHelper = (SkinEditionHelper) smanager.lookup(SkinEditionHelper.ROLE); 059 _lockManager = (SkinLockManager) smanager.lookup(SkinLockManager.ROLE); 060 _srcResolver = (org.apache.excalibur.source.SourceResolver) smanager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); 061 _fileHelper = (FileHelper) smanager.lookup(FileHelper.ROLE); 062 } 063 064 @Override 065 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 066 { 067 Map<String, Object> result = new HashMap<>(); 068 069 Request request = ObjectModelHelper.getRequest(objectModel); 070 String path = request.getParameter("parentPath"); 071 072 boolean unzip = Boolean.valueOf(request.getParameter("unzip")); 073 String mode = parameters.getParameter("mode", "add"); 074 075 Part part = (Part) request.get("filename"); 076 077 String tempDirURI = _skinHelper.getTempDirectoryURI(source); 078 FileSource tempDir = (FileSource) _srcResolver.resolveURI(tempDirURI); 079 080 String parentURI = tempDirURI + (path.length() > 0 ? "/" + path : ""); 081 FileSource parentDir = (FileSource) _srcResolver.resolveURI(parentURI); 082 083 result.putAll(_fileHelper.addOrUpdateFile(part, parentDir, mode, unzip)); 084 085 // Update lock 086 _lockManager.updateLockFile(tempDir.getFile().toPath(), SKIN_EDITOR_TOOL_ID); 087 088 if (result.containsKey("uri")) 089 { 090 String fileUri = (String) result.get("uri"); 091 String filePath = fileUri.substring(tempDir.getURI().length()); 092 result.put("path", filePath.endsWith("/") ? filePath.substring(0, filePath.length() - 1) : filePath); 093 String parentPath = parentDir.getURI().substring(tempDir.getURI().length()); 094 result.put("parentPath", parentPath.endsWith("/") ? parentPath.substring(0, parentPath.length() - 1) : parentPath); 095 } 096 else if (result.containsKey("unzip")) 097 { 098 String parentPath = parentDir.getURI().substring(parentDir.getURI().length()); 099 result.put("parentPath", parentPath.endsWith("/") ? parentPath.substring(0, parentPath.length() - 1) : parentPath); 100 } 101 102 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 103 return EMPTY_MAP; 104 } 105 106}