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.execute.remote;
23
24 import org.opencastproject.execute.api.ExecuteException;
25 import org.opencastproject.execute.api.ExecuteService;
26 import org.opencastproject.job.api.Job;
27 import org.opencastproject.job.api.JobParser;
28 import org.opencastproject.mediapackage.MediaPackage;
29 import org.opencastproject.mediapackage.MediaPackageElement;
30 import org.opencastproject.mediapackage.MediaPackageElement.Type;
31 import org.opencastproject.mediapackage.MediaPackageElementParser;
32 import org.opencastproject.mediapackage.MediaPackageException;
33 import org.opencastproject.mediapackage.MediaPackageParser;
34 import org.opencastproject.security.api.TrustedHttpClient;
35 import org.opencastproject.serviceregistry.api.RemoteBase;
36 import org.opencastproject.serviceregistry.api.ServiceRegistry;
37
38 import org.apache.http.HttpResponse;
39 import org.apache.http.NameValuePair;
40 import org.apache.http.client.entity.UrlEncodedFormEntity;
41 import org.apache.http.client.methods.HttpPost;
42 import org.apache.http.message.BasicNameValuePair;
43 import org.osgi.service.component.annotations.Component;
44 import org.osgi.service.component.annotations.Reference;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.List;
51
52
53
54
55 @Component(
56 immediate = true,
57 service = ExecuteService.class,
58 property = {
59 "service.description=Execute Service Remote Service Proxy"
60 }
61 )
62 public class ExecuteServiceRemoteImpl extends RemoteBase implements ExecuteService {
63
64
65 private static final Logger logger = LoggerFactory.getLogger(ExecuteServiceRemoteImpl.class);
66
67
68
69
70
71 public ExecuteServiceRemoteImpl() {
72 super(JOB_TYPE);
73 }
74
75
76
77
78
79
80 public Job execute(String exec, String params, MediaPackageElement inElement, String outFileName, Type type,
81 float load)
82 throws ExecuteException {
83 HttpPost post = null;
84 HttpResponse response = null;
85
86 try {
87 String inElementStr = MediaPackageElementParser.getAsXml(inElement);
88 List<NameValuePair> formStringParams = new ArrayList<NameValuePair>();
89 formStringParams.add(new BasicNameValuePair(EXEC_FORM_PARAM, exec));
90 formStringParams.add(new BasicNameValuePair(PARAMS_FORM_PARAM, params));
91 formStringParams.add(new BasicNameValuePair(LOAD_FORM_PARAM, String.valueOf(load)));
92 formStringParams.add(new BasicNameValuePair(INPUT_ELEM_FORM_PARAM, inElementStr));
93 if (outFileName != null) {
94 formStringParams.add(new BasicNameValuePair(OUTPUT_NAME_FORM_PARAMETER, outFileName));
95 }
96 if (type != null) {
97 formStringParams.add(new BasicNameValuePair(TYPE_FORM_PARAMETER, type.toString()));
98 }
99
100 logger.info("Executing command {} using a remote execute service", exec);
101
102 post = new HttpPost("/" + ExecuteService.ENDPOINT_NAME);
103 post.setEntity(new UrlEncodedFormEntity(formStringParams, "UTF-8"));
104 response = getResponse(post);
105
106 if (response != null) {
107 Job job = JobParser.parseJob(response.getEntity().getContent());
108 logger.info("Completing execution of command {} using a remote execute service", exec);
109 return job;
110 } else {
111 throw new ExecuteException(String.format("Failed to execute the command %s using a remote execute service",
112 exec));
113 }
114
115 } catch (MediaPackageException e) {
116 throw new ExecuteException("Error serializing the MediaPackage element", e);
117 } catch (IllegalStateException e) {
118 throw new ExecuteException(e);
119 } catch (IOException e) {
120 throw new ExecuteException(e);
121 } finally {
122 closeConnection(response);
123 }
124 }
125
126
127
128
129
130
131 @Override
132 public Job execute(String exec, String params, MediaPackage mp, String outFileName, Type type, float load)
133 throws ExecuteException {
134 HttpPost post = null;
135 HttpResponse response = null;
136
137 try {
138 String mpStr = MediaPackageParser.getAsXml(mp);
139 List<NameValuePair> formStringParams = new ArrayList<NameValuePair>();
140 formStringParams.add(new BasicNameValuePair(EXEC_FORM_PARAM, exec));
141 formStringParams.add(new BasicNameValuePair(PARAMS_FORM_PARAM, params));
142 formStringParams.add(new BasicNameValuePair(LOAD_FORM_PARAM, String.valueOf(load)));
143 formStringParams.add(new BasicNameValuePair(INPUT_MP_FORM_PARAM, mpStr));
144 if (outFileName != null) {
145 formStringParams.add(new BasicNameValuePair(OUTPUT_NAME_FORM_PARAMETER, outFileName));
146 }
147 if (type != null) {
148 formStringParams.add(new BasicNameValuePair(TYPE_FORM_PARAMETER, type.toString()));
149 }
150
151 logger.info("Executing command {} using a remote execute service", exec);
152
153 post = new HttpPost("/" + ExecuteService.ENDPOINT_NAME);
154 post.setEntity(new UrlEncodedFormEntity(formStringParams, "UTF-8"));
155 response = getResponse(post);
156
157 if (response != null) {
158 Job job = JobParser.parseJob(response.getEntity().getContent());
159 logger.info("Completing execution of command {} using a remote execute service", exec);
160 return job;
161 } else {
162 logger.error("Failed to execute the command {} using a remote execute service", exec);
163 throw new ExecuteException(String.format("Failed to execute the command %s using a remote execute service",
164 exec));
165 }
166 } catch (IllegalStateException e) {
167 throw new ExecuteException(e);
168 } catch (IOException e) {
169 throw new ExecuteException(e);
170 } finally {
171 closeConnection(response);
172 }
173 }
174
175 @Reference
176 @Override
177 public void setTrustedHttpClient(TrustedHttpClient trustedHttpClient) {
178 super.setTrustedHttpClient(trustedHttpClient);
179 }
180
181 @Reference
182 @Override
183 public void setRemoteServiceManager(ServiceRegistry serviceRegistry) {
184 super.setRemoteServiceManager(serviceRegistry);
185 }
186
187 }