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.odf.cdmfr; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.util.ArrayList; 021import java.util.Collections; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025 026import org.apache.avalon.framework.activity.Initializable; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.commons.lang.StringUtils; 030import org.apache.excalibur.source.Source; 031import org.apache.excalibur.source.SourceResolver; 032 033import org.ametys.cms.repository.WorkflowAwareContent; 034import org.ametys.cms.workflow.AbstractContentWorkflowComponent; 035import org.ametys.odf.CallWSHelper; 036import org.ametys.plugins.workflow.EnhancedFunction; 037import org.ametys.runtime.config.Config; 038import org.ametys.runtime.i18n.I18nizableText; 039 040import com.opensymphony.module.propertyset.PropertySet; 041import com.opensymphony.workflow.WorkflowException; 042 043/** 044 * Send CDM-fr to a distant server 045 * 046 */ 047public class SendCDMFRFunction extends AbstractContentWorkflowComponent implements EnhancedFunction, Initializable 048{ 049 private boolean _isActive; 050 private SourceResolver _sourceResolver; 051 052 @Override 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 super.service(manager); 056 _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 057 } 058 059 @Override 060 public void initialize() throws Exception 061 { 062 _isActive = Config.getInstance().getValue("odf.publish.server"); 063 } 064 065 @Override 066 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 067 { 068 if (!_isActive) 069 { 070 return; 071 } 072 073 // Retrieve current content 074 WorkflowAwareContent content = getContent(transientVars); 075 076 try 077 { 078 // Generate CDM-fr file 079 Source cdmfrSource = _sourceResolver.resolveURI(getExportUri() + "?id=" + content.getId() + "&" + ExportCDMfrHelper.REQUEST_PARAM_VALID_LABEL + "=true&" + ExportCDMfrHelper.REQUEST_PARAM_EXPORT_FOR_AMETYS + "=true"); 080 081 // Send to remote server 082 try (InputStream is = cdmfrSource.getInputStream()) 083 { 084 Set<String> failedUrl = CallWSHelper.callWS("_odf-sync/upload-cdm", is, _logger); 085 if (failedUrl == null || failedUrl.size() > 0) 086 { 087 List<String> params = new ArrayList<>(); 088 params.add(content.getTitle()); 089 params.add(StringUtils.join(failedUrl, ", ")); 090 addWorkflowWarning(transientVars, new I18nizableText("plugin.odf", "PLUGINS_ODF_PUBLISH_PROGRAM_PORTAL_ERROR", params)); 091 092 _logger.error("The program " + content.getId() + " can't be synchronized with portals" + (failedUrl != null ? " " + StringUtils.join(failedUrl, ", ") : "")); 093 } 094 } 095 } 096 catch (IOException e) 097 { 098 addWorkflowError(transientVars, new I18nizableText("plugin.odf", "PLUGINS_ODF_PUBLISH_PROGRAM_REMOTE_ERROR", Collections.singletonList(content.getTitle()))); 099 _logger.error("Unable to publish CDM-fr on distant server", e); 100 } 101 } 102 103 /** 104 * Get the URI to use to export CDMfr 105 * @return The uri location 106 */ 107 protected String getExportUri () 108 { 109 return "cocoon://_plugins/odf/export-cdmfr.xml"; 110 } 111 112 @Override 113 public FunctionType getFunctionExecType() 114 { 115 return FunctionType.POST; 116 } 117 118 @Override 119 public I18nizableText getLabel() 120 { 121 return new I18nizableText("plugin.odf", "PLUGINS_ODF_SEND_CDMFR_FUNCTION_LABEL"); 122 } 123}