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.explorer.resources.actions; 017 018import java.util.Enumeration; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 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; 030 031import org.ametys.core.cocoon.JSonReader; 032import org.ametys.core.observation.AbstractNotifierAction; 033import org.ametys.plugins.explorer.resources.Resource; 034import org.ametys.plugins.explorer.resources.actions.AddOrUpdateResourceHelper.ResourceOperationMode; 035import org.ametys.plugins.explorer.resources.actions.AddOrUpdateResourceHelper.ResourceOperationResult; 036import org.ametys.plugins.repository.RepositoryConstants; 037import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector; 038 039/** 040 * Action for adding or updating a resource file. 041 */ 042public class AddOrUpdateResourceAction extends AbstractNotifierAction 043{ 044 /** resource operation helper */ 045 protected AddOrUpdateResourceHelper _addOrUpdateResourceHelper; 046 047 @Override 048 public void service(ServiceManager serviceManager) throws ServiceException 049 { 050 super.service(serviceManager); 051 _addOrUpdateResourceHelper = (AddOrUpdateResourceHelper) serviceManager.lookup(AddOrUpdateResourceHelper.ROLE); 052 } 053 054 @Override 055 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 056 { 057 Request request = ObjectModelHelper.getRequest(objectModel); 058 059 // Retrieve the current workspace. 060 String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 061 062 try 063 { 064 // Force the default workspace. 065 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE); 066 067 _parameters2Attributes(request); 068 069 Part part = (Part) request.get("filename"); 070 String parentId = request.getParameter("collection"); 071 072 boolean unzip = Boolean.valueOf(request.getParameter("unzip")); 073 String rawMode = parameters.getParameter("mode", "add"); 074 075 if (unzip) 076 { 077 rawMode = "add-unzip"; 078 } 079 080 ResourceOperationMode mode = ResourceOperationMode.createsFromRawMode(rawMode); 081 082 // perform the add/update operation 083 ResourceOperationResult operationResult = _addOrUpdateResourceHelper.performResourceOperation(part, parentId, mode); 084 085 Map<String, Object> result = new HashMap<>(); 086 087 if (!operationResult.isSuccess()) 088 { 089 result.put("success", false); 090 result.put("message", operationResult.getErrorMessage()); 091 } 092 else 093 { 094 result.put("parentID", parentId); 095 result.put("success", true); 096 097 if (operationResult.isUnzip()) 098 { 099 result.put("unzip", true); 100 result.put("reload", true); 101 } 102 else 103 { 104 Resource resource = operationResult.getResource(); 105 result.put("id", resource.getId()); 106 result.put("name", resource.getName()); 107 } 108 } 109 110 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 111 return EMPTY_MAP; 112 } 113 finally 114 { 115 // Restore context 116 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp); 117 } 118 } 119 120 private void _parameters2Attributes(Request request) 121 { 122 Enumeration paramNames = request.getParameterNames(); 123 while (paramNames.hasMoreElements()) 124 { 125 String paramName = (String) paramNames.nextElement(); 126 if (paramName.startsWith("context_")) 127 { 128 request.setAttribute(paramName.substring("context_".length()), request.getParameter(paramName)); 129 } 130 } 131 } 132}