001/* 002 * Copyright 2014 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.contentio.in; 017 018import java.io.InputStream; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Set; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Redirector; 026import org.apache.cocoon.environment.Request; 027import org.apache.cocoon.environment.SourceResolver; 028import org.apache.cocoon.environment.http.HttpRequest; 029import org.apache.commons.lang.StringUtils; 030 031import org.ametys.plugins.contentio.ContentImportManager.ImportResult; 032 033/** 034 * Import contents from a file transmitted as a POST entity. 035 */ 036public class ImportPostBodyAction extends ImportFileAction 037{ 038 039 @Override 040 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 041 { 042 Request request = ObjectModelHelper.getRequest(objectModel); 043 044 boolean sendMail = parameters.getParameterAsBoolean("send-mail", false); 045 046 boolean success = false; 047 048 Map<String, String> result = new HashMap<>(); 049 050 ImportResult importResult = null; 051 052 if (request instanceof HttpRequest) 053 { 054 InputStream inputStream = ((HttpRequest) request).getInputStream(); 055 String name = request.getParameter("name"); 056 057 importResult = _importManager.importContents(inputStream, name); 058 059 if (!importResult.isImporterFound()) 060 { 061 result.put("error", "no-importer"); 062 } 063 else 064 { 065 Set<String> contentIds = importResult.getImportedContentIds(); 066 success = true; 067 068 result.put("importedCount", Integer.toString(contentIds.size())); 069 result.put("importedIds", StringUtils.join(contentIds, '|')); 070 } 071 } 072 073 result.put("success", String.valueOf(success)); 074 075 if (sendMail) 076 { 077 sendMail(success, importResult); 078 } 079 080 return result; 081 } 082 083}