View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  package org.opencastproject.workflow.handler.distribution;
22  
23  import static org.opencastproject.util.RequireUtil.notNull;
24  
25  import org.opencastproject.distribution.api.DownloadDistributionService;
26  import org.opencastproject.distribution.api.StreamingDistributionService;
27  import org.opencastproject.job.api.JobContext;
28  import org.opencastproject.mediapackage.MediaPackage;
29  import org.opencastproject.serviceregistry.api.ServiceRegistry;
30  import org.opencastproject.workflow.api.WorkflowInstance;
31  import org.opencastproject.workflow.api.WorkflowOperationException;
32  import org.opencastproject.workflow.api.WorkflowOperationHandler;
33  import org.opencastproject.workflow.api.WorkflowOperationInstance;
34  import org.opencastproject.workflow.api.WorkflowOperationResult;
35  import org.opencastproject.workflow.api.WorkflowOperationResult.Action;
36  
37  import org.apache.commons.lang3.BooleanUtils;
38  import org.apache.commons.lang3.StringUtils;
39  import org.osgi.service.component.annotations.Component;
40  import org.osgi.service.component.annotations.Reference;
41  
42  /**
43   * WOH that retracts elements from an internal distribution channel and removes the reflective publication elements from
44   * the media package.
45   */
46  @Component(
47      immediate = true,
48      service = WorkflowOperationHandler.class,
49      property = {
50          "service.description=Configurable Retraction Workflow Handler",
51          "workflow.operation=retract-configure"
52      }
53  )
54  public class ConfigurableRetractWorkflowOperationHandler extends ConfigurableWorkflowOperationHandlerBase {
55  
56    private static final String CHANNEL_ID_KEY = "channel-id";
57  
58    static final String RETRACT_STREAMING = "retract-streaming";
59    static final boolean RETRACT_STREAMING_DEFAULT = false;
60  
61    // service references
62    private DownloadDistributionService downloadDistributionService;
63    private StreamingDistributionService streamingDistributionService;
64  
65    /** OSGi DI */
66    @Reference(target = "(distribution.channel=download)")
67    void setDownloadDistributionService(DownloadDistributionService distributionService) {
68      this.downloadDistributionService = distributionService;
69    }
70  
71    @Reference(target = "(distribution.channel=streaming)")
72    void setStreamingDistributionService(StreamingDistributionService distributionService) {
73      this.streamingDistributionService = distributionService;
74    }
75  
76    @Override
77    protected DownloadDistributionService getDownloadDistributionService() {
78      assert (downloadDistributionService != null);
79      return downloadDistributionService;
80    }
81  
82    @Override
83    protected StreamingDistributionService getStreamingDistributionService() {
84      assert (streamingDistributionService != null);
85      return streamingDistributionService;
86    }
87  
88    @Override
89    public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context)
90            throws WorkflowOperationException {
91      notNull(workflowInstance, "workflowInstance");
92  
93      boolean retractStreaming = RETRACT_STREAMING_DEFAULT;
94      final WorkflowOperationInstance op = workflowInstance.getCurrentOperation();
95      String retractStreamingString = op.getConfiguration(RETRACT_STREAMING);
96      if (retractStreamingString != null) {
97        retractStreaming = BooleanUtils.toBoolean(StringUtils.trimToEmpty(retractStreamingString));
98      }
99  
100     final MediaPackage mp = workflowInstance.getMediaPackage();
101     final String channelId = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(
102             CHANNEL_ID_KEY));
103     if (StringUtils.isBlank((channelId))) {
104       throw new WorkflowOperationException("Unable to retract this mediapackage as the configuration key "
105               + CHANNEL_ID_KEY + " is missing. Unable to determine from where to retract these elements.");
106     }
107 
108     retract(mp, channelId, retractStreaming);
109 
110     return createResult(mp, Action.CONTINUE);
111   }
112 
113   @Reference
114   @Override
115   public void setServiceRegistry(ServiceRegistry serviceRegistry) {
116     super.setServiceRegistry(serviceRegistry);
117   }
118 
119 }