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 import static org.opencastproject.util.JobUtil.jobFromHttpResponse;
28 import static org.opencastproject.util.data.functions.Options.join;
29
30 import org.opencastproject.distribution.api.DistributionException;
31 import org.opencastproject.distribution.api.DistributionService;
32 import org.opencastproject.distribution.api.DownloadDistributionService;
33 import org.opencastproject.job.api.Job;
34 import org.opencastproject.mediapackage.MediaPackage;
35 import org.opencastproject.mediapackage.MediaPackageElement;
36 import org.opencastproject.mediapackage.MediaPackageException;
37 import org.opencastproject.mediapackage.MediaPackageParser;
38 import org.opencastproject.security.api.TrustedHttpClient;
39 import org.opencastproject.serviceregistry.api.RemoteBase;
40 import org.opencastproject.serviceregistry.api.ServiceRegistry;
41 import org.opencastproject.util.OsgiUtil;
42
43 import com.google.gson.Gson;
44
45 import org.apache.http.client.methods.HttpPost;
46 import org.osgi.service.component.ComponentContext;
47 import org.osgi.service.component.annotations.Activate;
48 import org.osgi.service.component.annotations.Component;
49 import org.osgi.service.component.annotations.Reference;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import java.util.HashSet;
54 import java.util.List;
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 for (Job job : join(runRequest(req, jobFromHttpResponse))) {
129 return job;
130 }
131 throw new DistributionException(format("Unable to distribute '%s' elements of "
132 + "mediapackage '%s' using a remote destribution service proxy",
133 elementIds.size(), mediaPackage.getIdentifier().toString()));
134 }
135
136 @Override
137 public Job retract(String channelId, MediaPackage mediaPackage, String elementId) throws DistributionException {
138 Set<String> elementIds = new HashSet<String>();
139 elementIds.add(elementId);
140 return retract(channelId, mediaPackage, elementIds);
141 }
142
143 @Override
144 public Job retract(String channelId, MediaPackage mediaPackage, Set<String> elementIds) throws DistributionException {
145 logger.info("Retracting {} elements from {}@{}", elementIds.size(), channelId, distributionChannel);
146 final HttpPost req = post("/retract",
147 param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediaPackage)),
148 param(PARAM_ELEMENT_ID, gson.toJson(elementIds)),
149 param(PARAM_CHANNEL_ID, channelId));
150 for (Job job : join(runRequest(req, jobFromHttpResponse))) {
151 return job;
152 }
153 throw new DistributionException(format("Unable to retract '%s' elements of "
154 + "mediapackage '%s' using a remote destribution service proxy",
155 elementIds.size(), mediaPackage.getIdentifier().toString()));
156 }
157
158 @Override
159 public List<MediaPackageElement> distributeSync(String channelId, MediaPackage mediapackage, String elementId)
160 throws DistributionException, MediaPackageException {
161 Set<String> elementIds = new HashSet<String>();
162 elementIds.add(elementId);
163 return distributeSync(channelId, mediapackage, elementIds, true);
164 }
165
166 @Override
167 public List<MediaPackageElement> distributeSync(String channelId, MediaPackage mediapackage, Set<String> elementIds,
168 boolean checkAvailability) throws DistributionException {
169 logger.info("Distributing {} elements to {}@{}", elementIds.size(), channelId, distributionChannel);
170 final HttpPost req = post("/distributesync", param(PARAM_CHANNEL_ID, channelId),
171 param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediapackage)),
172 param(PARAM_ELEMENT_ID, gson.toJson(elementIds)),
173 param(PARAM_CHECK_AVAILABILITY, Boolean.toString(checkAvailability)));
174 for (List<MediaPackageElement> elements : join(runRequest(req, elementsFromHttpResponse))) {
175 return elements;
176 }
177 throw new DistributionException(format("Unable to distribute '%s' elements of "
178 + "mediapackage '%s' using a remote destribution service proxy",
179 elementIds.size(), mediapackage.getIdentifier().toString()));
180 }
181
182 @Override
183 public List<MediaPackageElement> retractSync(String channelId, MediaPackage mediaPackage, String elementId)
184 throws DistributionException {
185 Set<String> elementIds = new HashSet<String>();
186 elementIds.add(elementId);
187 return retractSync(channelId, mediaPackage, elementIds);
188 }
189
190 @Override
191 public List<MediaPackageElement> retractSync(String channelId, MediaPackage mediaPackage, Set<String> elementIds)
192 throws DistributionException {
193 logger.info("Retracting {} elements from {}@{}", elementIds.size(), channelId, distributionChannel);
194 final HttpPost req = post("/retractsync",
195 param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediaPackage)),
196 param(PARAM_ELEMENT_ID, gson.toJson(elementIds)),
197 param(PARAM_CHANNEL_ID, channelId));
198 for (List<MediaPackageElement> elements : join(runRequest(req, elementsFromHttpResponse))) {
199 return elements;
200 }
201 throw new DistributionException(format("Unable to retract '%s' elements of "
202 + "mediapackage '%s' using a remote destribution service proxy",
203 elementIds.size(), mediaPackage.getIdentifier().toString()));
204 }
205
206 @Reference
207 @Override
208 public void setTrustedHttpClient(TrustedHttpClient trustedHttpClient) {
209 super.setTrustedHttpClient(trustedHttpClient);
210 }
211
212 @Reference
213 @Override
214 public void setRemoteServiceManager(ServiceRegistry serviceRegistry) {
215 super.setRemoteServiceManager(serviceRegistry);
216 }
217
218 }