001/* 002 * Copyright 2018 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.odfsync.cdmfr.actions; 017 018import java.io.InputStream; 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.acting.ServiceableAction; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Redirector; 028import org.apache.cocoon.environment.Request; 029import org.apache.cocoon.environment.SourceResolver; 030import org.apache.cocoon.environment.http.HttpRequest; 031 032import org.ametys.core.util.AvalonLoggerAdapter; 033import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 034import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper; 035 036/** 037 * Import a CDM-fr file from a remote source. 038 */ 039public class UploadCDMFrAction extends ServiceableAction 040{ 041 /** The remote CDMFr SCC model id */ 042 public static final String REMOTE_CDMFR_SCC_MODEL_ID = "org.ametys.plugins.odfsync.remote.cdmfr.scc"; 043 044 /** The name of the key to get the CDMFr input stream */ 045 public static final String CDMFR_IMPUT_STREAM_KEY_NAME = "cdmfr-input-stream"; 046 047 /** The SCC helper */ 048 protected SynchronizableContentsCollectionHelper _sccHelper; 049 050 @Override 051 public void service(ServiceManager sManager) throws ServiceException 052 { 053 super.service(sManager); 054 _sccHelper = (SynchronizableContentsCollectionHelper) sManager.lookup(SynchronizableContentsCollectionHelper.ROLE); 055 } 056 057 @Override 058 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 059 { 060 Request request = ObjectModelHelper.getRequest(objectModel); 061 if (request instanceof HttpRequest) 062 { 063 try (InputStream is = ((HttpRequest) request).getInputStream()) 064 { 065 SynchronizableContentsCollection scc = _sccHelper.getSCCFromModelId(REMOTE_CDMFR_SCC_MODEL_ID); 066 if (scc != null) 067 { 068 getLogger().info("Start of upload CDM-FR file"); 069 070 Map<String, Object> importParams = new HashMap<>(); 071 importParams.put(CDMFR_IMPUT_STREAM_KEY_NAME, is); 072 scc.importContent(null, importParams, new AvalonLoggerAdapter(getLogger())); 073 074 getLogger().info("End of upload CDM-FR file"); 075 } 076 else 077 { 078 String message = "Can't find synchronizable contents collections for the remote CDMFr import with model id " + REMOTE_CDMFR_SCC_MODEL_ID; 079 getLogger().error(message); 080 throw new IllegalStateException(message); 081 } 082 } 083 } 084 085 return EMPTY_MAP; 086 } 087}