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.inspection.remote;
23
24 import org.opencastproject.inspection.api.MediaInspectionException;
25 import org.opencastproject.inspection.api.MediaInspectionService;
26 import org.opencastproject.inspection.api.util.Options;
27 import org.opencastproject.job.api.Job;
28 import org.opencastproject.job.api.JobParser;
29 import org.opencastproject.mediapackage.MediaPackageElement;
30 import org.opencastproject.mediapackage.MediaPackageElementParser;
31 import org.opencastproject.security.api.TrustedHttpClient;
32 import org.opencastproject.serviceregistry.api.RemoteBase;
33 import org.opencastproject.serviceregistry.api.ServiceRegistry;
34
35 import org.apache.http.HttpResponse;
36 import org.apache.http.NameValuePair;
37 import org.apache.http.client.entity.UrlEncodedFormEntity;
38 import org.apache.http.client.methods.HttpGet;
39 import org.apache.http.client.methods.HttpPost;
40 import org.apache.http.client.utils.URLEncodedUtils;
41 import org.apache.http.message.BasicNameValuePair;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import java.net.URI;
48 import java.util.ArrayList;
49 import java.util.List;
50 import java.util.Map;
51
52
53
54
55 @Component(
56 property = {
57 "service.description=Media Inspection Remote Service Proxy"
58 },
59 immediate = true,
60 service = { MediaInspectionService.class }
61 )
62 public class MediaInspectionServiceRemoteImpl extends RemoteBase implements MediaInspectionService {
63
64
65 private static final Logger logger = LoggerFactory.getLogger(MediaInspectionServiceRemoteImpl.class);
66
67
68
69
70 public MediaInspectionServiceRemoteImpl() {
71 super(JOB_TYPE);
72 }
73
74
75
76
77
78
79 @Override
80 @Reference
81 public void setTrustedHttpClient(TrustedHttpClient client) {
82 super.setTrustedHttpClient(client);
83 }
84
85
86
87
88
89
90 @Override
91 @Reference
92 public void setRemoteServiceManager(ServiceRegistry remoteServiceManager) {
93 super.setRemoteServiceManager(remoteServiceManager);
94 }
95
96
97
98
99
100
101 @Override
102 public Job inspect(URI uri) throws MediaInspectionException {
103 return inspect(uri, Options.NO_OPTION);
104 }
105
106
107
108
109
110
111 @Override
112 public Job inspect(URI uri, final Map<String, String> options) throws MediaInspectionException {
113 assert (options != null);
114 List<NameValuePair> params = new ArrayList<NameValuePair>();
115 params.add(new BasicNameValuePair("uri", uri.toString()));
116 params.add(new BasicNameValuePair("options", Options.toJson(options)));
117 String url = "/inspect?" + URLEncodedUtils.format(params, "UTF-8");
118 logger.info("Inspecting media file at {} using a remote media inspection service", uri);
119 HttpResponse response = null;
120 try {
121 HttpGet get = new HttpGet(url);
122 response = getResponse(get);
123 if (response != null) {
124 Job job = JobParser.parseJob(response.getEntity().getContent());
125 logger.info("Completing inspection of media file at {} using a remote media inspection service", uri);
126 return job;
127 }
128 } catch (Exception e) {
129 throw new MediaInspectionException("Unable to inspect " + uri + " using a remote inspection service", e);
130 } finally {
131 closeConnection(response);
132 }
133 throw new MediaInspectionException("Unable to inspect " + uri + " using a remote inspection service");
134 }
135
136
137
138
139 @Override
140 public Job enrich(MediaPackageElement original, boolean override) throws MediaInspectionException {
141 return enrich(original, override, Options.NO_OPTION);
142 }
143
144
145
146
147 @Override
148 public Job enrich(MediaPackageElement original, boolean override, final Map<String, String> options)
149 throws MediaInspectionException {
150 assert (options != null);
151 List<NameValuePair> params = new ArrayList<NameValuePair>();
152 try {
153 params.add(new BasicNameValuePair("mediaPackageElement", MediaPackageElementParser.getAsXml(original)));
154 params.add(new BasicNameValuePair("override", new Boolean(override).toString()));
155 params.add(new BasicNameValuePair("options", Options.toJson(options)));
156 } catch (Exception e) {
157 throw new MediaInspectionException(e);
158 }
159 logger.info("Enriching {} using a remote media inspection service", original);
160 HttpResponse response = null;
161 try {
162 HttpPost post = new HttpPost("/enrich");
163 post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
164 response = getResponse(post);
165 if (response != null) {
166 Job receipt = JobParser.parseJob(response.getEntity().getContent());
167 logger.info("Completing inspection of media file at {} using a remote media inspection service",
168 original.getURI());
169 return receipt;
170 }
171 } catch (Exception e) {
172 throw new MediaInspectionException("Unable to enrich " + original + " using a remote inspection service", e);
173 } finally {
174 closeConnection(response);
175 }
176 throw new MediaInspectionException("Unable to enrich " + original + " using a remote inspection service");
177 }
178
179 }