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.authentication.AuthenticateAction; 033import org.ametys.core.user.CurrentUserProvider; 034import org.ametys.core.user.UserIdentity; 035import org.ametys.core.user.population.UserPopulationDAO; 036import org.ametys.core.util.AvalonLoggerAdapter; 037import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 038import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper; 039import org.ametys.plugins.core.user.UserDAO; 040 041/** 042 * Import a CDM-fr file from a remote source. 043 */ 044public class UploadCDMFrAction extends ServiceableAction 045{ 046 /** The remote CDMFr SCC model id */ 047 public static final String REMOTE_CDMFR_SCC_MODEL_ID = "org.ametys.plugins.odfsync.remote.cdmfr.scc"; 048 049 /** The name of the key to get the CDMFr input stream */ 050 public static final String CDMFR_IMPUT_STREAM_KEY_NAME = "cdmfr-input-stream"; 051 052 /** The SCC helper */ 053 protected SynchronizableContentsCollectionHelper _sccHelper; 054 055 /** Current user provider. */ 056 protected CurrentUserProvider _currentUserProvider; 057 058 @Override 059 public void service(ServiceManager sManager) throws ServiceException 060 { 061 super.service(sManager); 062 _sccHelper = (SynchronizableContentsCollectionHelper) sManager.lookup(SynchronizableContentsCollectionHelper.ROLE); 063 _currentUserProvider = (CurrentUserProvider) sManager.lookup(CurrentUserProvider.ROLE); 064 } 065 066 @Override 067 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 068 { 069 Request request = ObjectModelHelper.getRequest(objectModel); 070 if (request instanceof HttpRequest) 071 { 072 try (InputStream is = ((HttpRequest) request).getInputStream()) 073 { 074 SynchronizableContentsCollection scc = _sccHelper.getSCCFromModelId(REMOTE_CDMFR_SCC_MODEL_ID); 075 if (scc != null) 076 { 077 UserIdentity user = _currentUserProvider.getUser(); 078 if (user == null) 079 { 080 AuthenticateAction.setUserIdentityInSession(request, UserPopulationDAO.SYSTEM_USER_IDENTITY, new UserDAO.ImpersonateCredentialProvider(), true); 081 } 082 083 getLogger().info("Start of upload CDM-FR file"); 084 085 Map<String, Object> importParams = new HashMap<>(); 086 importParams.put(CDMFR_IMPUT_STREAM_KEY_NAME, is); 087 scc.importContent(null, importParams, new AvalonLoggerAdapter(getLogger())); 088 089 getLogger().info("End of upload CDM-FR file"); 090 } 091 else 092 { 093 String message = "Can't find synchronizable contents collections for the remote CDMFr import with model id " + REMOTE_CDMFR_SCC_MODEL_ID; 094 getLogger().error(message); 095 throw new IllegalStateException(message); 096 } 097 } 098 } 099 100 return EMPTY_MAP; 101 } 102}