001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Nelson Silva 018 */ 019 020package org.nuxeo.ecm.media.publishing; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.core.api.CoreSession; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.NuxeoException; 027import org.nuxeo.ecm.core.api.event.DocumentEventCategories; 028import org.nuxeo.ecm.core.api.event.DocumentEventTypes; 029import org.nuxeo.ecm.core.event.Event; 030import org.nuxeo.ecm.core.event.EventProducer; 031import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 032import org.nuxeo.ecm.core.work.api.Work; 033import org.nuxeo.ecm.core.work.api.WorkManager; 034import org.nuxeo.ecm.media.publishing.adapter.PublishableMedia; 035import org.nuxeo.ecm.media.publishing.upload.MediaPublishingUploadWork; 036import org.nuxeo.runtime.api.Framework; 037import org.nuxeo.runtime.model.ComponentInstance; 038import org.nuxeo.runtime.model.DefaultComponent; 039 040import java.io.IOException; 041import java.util.Map; 042 043/** 044 * @since 7.3 045 */ 046public class MediaPublishingServiceImpl extends DefaultComponent implements MediaPublishingService { 047 048 protected static final Log log = LogFactory.getLog(MediaPublishingServiceImpl.class); 049 050 public static final String PROVIDER_EP = "providers"; 051 052 protected MediaPublishingProviderRegistry providers = new MediaPublishingProviderRegistry(); 053 054 @Override 055 public String[] getAvailableProviders(DocumentModel doc) { 056 return providers.getServices().toArray(new String[]{}); 057 } 058 059 public MediaPublishingProvider getProvider(String provider) { 060 MediaPublishingProviderDescriptor descriptor = providers.lookup(provider); 061 return (MediaPublishingProvider) Framework.getService(descriptor.getService()); 062 } 063 064 @Override 065 public String publish(DocumentModel doc, String serviceId, String account, Map<String, String> options) { 066 MediaPublishingProvider service = getProvider(serviceId); 067 WorkManager workManager = Framework.getService(WorkManager.class); 068 if (workManager == null) { 069 throw new RuntimeException("No WorkManager available"); 070 } 071 072 Work work = new MediaPublishingUploadWork(serviceId, service, doc.getRepositoryName(), doc.getId(), doc.getCoreSession(), account, options); 073 workManager.schedule(work); 074 return work.getId(); 075 } 076 077 @Override 078 public void unpublish(DocumentModel doc, String provider) { 079 MediaPublishingProvider service = getProvider(provider); 080 PublishableMedia media = doc.getAdapter(PublishableMedia.class); 081 try { 082 if (service.unpublish(media)) { 083 // Remove provider from the list of published providers 084 media.removeProvider(provider); 085 086 // Track unpublish in document history 087 CoreSession session = doc.getCoreSession(); 088 DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), doc); 089 ctx.setComment("Video unpublished from " + provider); 090 ctx.setCategory(DocumentEventCategories.EVENT_DOCUMENT_CATEGORY); 091 Event event = ctx.newEvent(DocumentEventTypes.DOCUMENT_PUBLISHED); 092 EventProducer evtProducer = Framework.getService(EventProducer.class); 093 evtProducer.fireEvent(event); 094 session.saveDocument(doc); 095 session.save(); 096 } 097 } catch (IOException e) { 098 throw new NuxeoException("Failed to unpublish media", e); 099 } 100 } 101 102 @Override 103 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 104 if (PROVIDER_EP.equals(extensionPoint)) { 105 MediaPublishingProviderDescriptor provider = (MediaPublishingProviderDescriptor) contribution; 106 providers.addContribution(provider); 107 } 108 } 109}