1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.distribution.download.remote;
23
24 import static java.lang.String.format;
25 import static org.opencastproject.util.HttpUtil.param;
26 import static org.opencastproject.util.HttpUtil.post;
27
28 import org.opencastproject.distribution.api.DistributionException;
29 import org.opencastproject.distribution.api.DistributionService;
30 import org.opencastproject.distribution.api.DownloadDistributionService;
31 import org.opencastproject.job.api.Job;
32 import org.opencastproject.mediapackage.MediaPackage;
33 import org.opencastproject.mediapackage.MediaPackageElement;
34 import org.opencastproject.mediapackage.MediaPackageException;
35 import org.opencastproject.mediapackage.MediaPackageParser;
36 import org.opencastproject.security.api.TrustedHttpClient;
37 import org.opencastproject.serviceregistry.api.RemoteBase;
38 import org.opencastproject.serviceregistry.api.ServiceRegistry;
39 import org.opencastproject.util.JobUtil;
40 import org.opencastproject.util.OsgiUtil;
41
42 import com.google.gson.Gson;
43
44 import org.apache.http.client.methods.HttpPost;
45 import org.osgi.service.component.ComponentContext;
46 import org.osgi.service.component.annotations.Activate;
47 import org.osgi.service.component.annotations.Component;
48 import org.osgi.service.component.annotations.Reference;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 import java.util.HashSet;
53 import java.util.List;
54 import java.util.Optional;
55 import java.util.Set;
56
57
58 @Component(
59 immediate = true,
60 service = { DistributionService.class,DownloadDistributionService.class },
61 property = {
62 "service.description=Distribution (Download) Remote Service Proxy",
63 "distribution.channel=download"
64 }
65 )
66 public class DownloadDistributionServiceRemoteImpl extends RemoteBase
67 implements DistributionService, DownloadDistributionService {
68
69 private static final Logger logger = LoggerFactory.getLogger(DownloadDistributionServiceRemoteImpl.class);
70
71
72 private static final String PARAM_CHANNEL_ID = "channelId";
73 private static final String PARAM_MEDIAPACKAGE = "mediapackage";
74 private static final String PARAM_ELEMENT_ID = "elementId";
75 private static final String PARAM_CHECK_AVAILABILITY = "checkAvailability";
76 private static final String PARAM_PRESERVE_REFERENCE = "preserveReference";
77
78 private final Gson gson = new Gson();
79
80
81 private String distributionChannel;
82
83 public DownloadDistributionServiceRemoteImpl() {
84
85 super("waiting for activation");
86 }
87
88 public String getDistributionType() {
89 return this.distributionChannel;
90 }
91
92
93 @Activate
94 protected void activate(ComponentContext cc) {
95 this.distributionChannel = OsgiUtil.getComponentContextProperty(cc, CONFIG_KEY_STORE_TYPE);
96 super.serviceType = JOB_TYPE_PREFIX + this.distributionChannel;
97 }
98
99 @Override
100 public Job distribute(String channelId, MediaPackage mediaPackage, String elementId) throws DistributionException {
101 return distribute(channelId, mediaPackage, elementId, true);
102 }
103
104 @Override
105 public Job distribute(String channelId, MediaPackage mediaPackage, String elementId, boolean checkAvailability)
106 throws DistributionException {
107 Set<String> elementIds = new HashSet<String>();
108 elementIds.add(elementId);
109 return distribute(channelId, mediaPackage, elementIds, checkAvailability);
110 }
111
112 @Override
113 public Job distribute(String channelId, final MediaPackage mediaPackage, Set<String> elementIds,
114 boolean checkAvailability)
115 throws DistributionException {
116 return distribute(channelId, mediaPackage, elementIds, checkAvailability, false);
117 }
118 @Override
119 public Job distribute(String channelId, final MediaPackage mediaPackage, Set<String> elementIds,
120 boolean checkAvailability, boolean preserveReference)
121 throws DistributionException {
122 logger.info("Distributing {} elements to {}@{}", elementIds.size(), channelId, distributionChannel);
123 final HttpPost req = post(param(PARAM_CHANNEL_ID, channelId),
124 param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediaPackage)),
125 param(PARAM_ELEMENT_ID, gson.toJson(elementIds)),
126 param(PARAM_CHECK_AVAILABILITY, Boolean.toString(checkAvailability)),
127 param(PARAM_PRESERVE_REFERENCE, Boolean.toString(preserveReference)));
128 Optional<Job> job = runRequest(req, JobUtil::jobFromHttpResponse);
129 if (job.isPresent()) {
130 return job.get();
131 }
132 throw new DistributionException(format("Unable to distribute '%s' elements of "
133 + "mediapackage '%s' using a remote destribution service proxy",
134 elementIds.size(), mediaPackage.getIdentifier().toString()));
135 }
136
137 @Override
138 public Job retract(String channelId, MediaPackage mediaPackage, String elementId) throws DistributionException {
139 Set<String> elementIds = new HashSet<String>();
140 elementIds.add(elementId);
141 return retract(channelId, mediaPackage, elementIds);
142 }
143
144 @Override
145 public Job retract(String channelId, MediaPackage mediaPackage, Set<String> elementIds) throws DistributionException {
146 logger.info("Retracting {} elements from {}@{}", elementIds.size(), channelId, distributionChannel);
147 final HttpPost req = post("/retract",
148 param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediaPackage)),
149 param(PARAM_ELEMENT_ID, gson.toJson(elementIds)),
150 param(PARAM_CHANNEL_ID, channelId));
151 Optional<Job> job = runRequest(req, JobUtil::jobFromHttpResponse);
152 if (job.isPresent()) {
153 return job.get();
154 }
155 throw new DistributionException(format("Unable to retract '%s' elements of "
156 + "mediapackage '%s' using a remote destribution service proxy",
157 elementIds.size(), mediaPackage.getIdentifier().toString()));
158 }
159
160 @Override
161 public List<MediaPackageElement> distributeSync(String channelId, MediaPackage mediapackage, String elementId)
162 throws DistributionException, MediaPackageException {
163 Set<String> elementIds = new HashSet<String>();
164 elementIds.add(elementId);
165 return distributeSync(channelId, mediapackage, elementIds, true);
166 }
167
168 @Override
169 public List<MediaPackageElement> distributeSync(String channelId, MediaPackage mediapackage, Set<String> elementIds,
170 boolean checkAvailability) throws DistributionException {
171 logger.info("Distributing {} elements to {}@{}", elementIds.size(), channelId, distributionChannel);
172 final HttpPost req = post("/distributesync", param(PARAM_CHANNEL_ID, channelId),
173 param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediapackage)),
174 param(PARAM_ELEMENT_ID, gson.toJson(elementIds)),
175 param(PARAM_CHECK_AVAILABILITY, Boolean.toString(checkAvailability)));
176 Optional<List<MediaPackageElement>> elements = runRequest(req, RemoteBase::elementsFromHttpResponse);
177 if (elements.isPresent()) {
178 return elements.get();
179 }
180 throw new DistributionException(format("Unable to distribute '%s' elements of "
181 + "mediapackage '%s' using a remote destribution service proxy",
182 elementIds.size(), mediapackage.getIdentifier().toString()));
183 }
184
185 @Override
186 public List<MediaPackageElement> retractSync(String channelId, MediaPackage mediaPackage, String elementId)
187 throws DistributionException {
188 Set<String> elementIds = new HashSet<String>();
189 elementIds.add(elementId);
190 return retractSync(channelId, mediaPackage, elementIds);
191 }
192
193 @Override
194 public List<MediaPackageElement> retractSync(String channelId, MediaPackage mediaPackage, Set<String> elementIds)
195 throws DistributionException {
196 logger.info("Retracting {} elements from {}@{}", elementIds.size(), channelId, distributionChannel);
197 final HttpPost req = post("/retractsync",
198 param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediaPackage)),
199 param(PARAM_ELEMENT_ID, gson.toJson(elementIds)),
200 param(PARAM_CHANNEL_ID, channelId));
201 Optional<List<MediaPackageElement>> elements = runRequest(req, RemoteBase::elementsFromHttpResponse);
202 if (elements.isPresent()) {
203 return elements.get();
204 }
205 throw new DistributionException(format("Unable to retract '%s' elements of "
206 + "mediapackage '%s' using a remote destribution service proxy",
207 elementIds.size(), mediaPackage.getIdentifier().toString()));
208 }
209
210 @Reference
211 @Override
212 public void setTrustedHttpClient(TrustedHttpClient trustedHttpClient) {
213 super.setTrustedHttpClient(trustedHttpClient);
214 }
215
216 @Reference
217 @Override
218 public void setRemoteServiceManager(ServiceRegistry serviceRegistry) {
219 super.setRemoteServiceManager(serviceRegistry);
220 }
221
222 }