001/* 002 * Copyright 2012 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.core.upload; 017 018import java.io.IOException; 019import java.util.HashMap; 020import java.util.LinkedHashMap; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.cocoon.acting.Action; 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.commons.lang.exception.ExceptionUtils; 031 032import org.ametys.core.cocoon.JSonReader; 033import org.ametys.core.upload.Upload; 034import org.ametys.core.upload.UploadManager; 035import org.ametys.core.util.cocoon.AbstractCurrentUserProviderServiceableAction; 036 037/** 038 * {@link Action} for uploading a file and store it using the {@link UploadManager}. 039 */ 040public class UploadAction extends AbstractCurrentUserProviderServiceableAction 041{ 042 private UploadManager _uploadManager; 043 044 @Override 045 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 046 { 047 Request request = ObjectModelHelper.getRequest(objectModel); 048 049 //Lazy initialize the upload manager because it cannot be found if the config is not complete 050 if (_uploadManager == null) 051 { 052 _uploadManager = (UploadManager) manager.lookup(UploadManager.ROLE); 053 } 054 055 Part partUploaded = (Part) request.get("file"); 056 Map<String, Object> result = new LinkedHashMap<>(); 057 058 if (partUploaded == null) 059 { 060 throw new Exception("Missing request parameter file"); 061 } 062 063 if (partUploaded.isRejected()) 064 { 065 result.put("success", false); 066 result.put("error", "rejected"); 067 } 068 else 069 { 070 Upload upload = null; 071 072 try 073 { 074 upload = _uploadManager.storeUpload(_getCurrentUser(), partUploaded.getFileName(), partUploaded.getInputStream()); 075 076 result.put("success", true); 077 result.put("id", upload.getId()); 078 result.put("filename", upload.getFilename()); 079 result.put("size", upload.getLength()); 080 result.put("viewHref", _getUrlForView(upload)); 081 result.put("downloadHref", _getUrlForDownload(upload)); 082 } 083 catch (IOException e) 084 { 085 getLogger().error("Unable to store uploaded file: " + partUploaded, e); 086 087 result.put("success", false); 088 089 Map<String, String> ex = new HashMap<>(); 090 ex.put("message", e.getMessage()); 091 ex.put("stacktrace", ExceptionUtils.getFullStackTrace(e)); 092 093 result.put("error", ex); 094 } 095 } 096 097 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 098 099 return EMPTY_MAP; 100 } 101 102 /** 103 * Get the url for view the uploaded file 104 * @param upload The file uploaded 105 * @return The url for view 106 */ 107 protected String _getUrlForView (Upload upload) 108 { 109 return "/plugins/core/upload/file?id=" + upload.getId(); 110 } 111 112 /** 113 * Get the url for download the uploaded file 114 * @param upload The file uploaded 115 * @return The url for view 116 */ 117 protected String _getUrlForDownload (Upload upload) 118 { 119 return "/plugins/core/upload/file?id=" + upload.getId() + "&download=true"; 120 } 121}