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 public Job execute(String exec, String params, MediaPackageElement inElement, String outFileName, Type type, float load)
79 throws ExecuteException {
80 HttpPost post = null;
81 HttpResponse response = null;
82
83 try {
84 String inElementStr = MediaPackageElementParser.getAsXml(inElement);
85 List<NameValuePair> formStringParams = new ArrayList<NameValuePair>();
86 formStringParams.add(new BasicNameValuePair(EXEC_FORM_PARAM, exec));
87 formStringParams.add(new BasicNameValuePair(PARAMS_FORM_PARAM, params));
88 formStringParams.add(new BasicNameValuePair(LOAD_FORM_PARAM, String.valueOf(load)));
89 formStringParams.add(new BasicNameValuePair(INPUT_ELEM_FORM_PARAM, inElementStr));
90 if (outFileName != null)
91 formStringParams.add(new BasicNameValuePair(OUTPUT_NAME_FORM_PARAMETER, outFileName));
92 if (type != null)
93 formStringParams.add(new BasicNameValuePair(TYPE_FORM_PARAMETER, type.toString()));
94
95 logger.info("Executing command {} using a remote execute service", exec);
96
97 post = new HttpPost("/" + ExecuteService.ENDPOINT_NAME);
98 post.setEntity(new UrlEncodedFormEntity(formStringParams, "UTF-8"));
99 response = getResponse(post);
100
101 if (response != null) {
102 Job job = JobParser.parseJob(response.getEntity().getContent());
103 logger.info("Completing execution of command {} using a remote execute service", exec);
104 return job;
105 } else
106 throw new ExecuteException(String.format("Failed to execute the command %s using a remote execute service", exec));
107
108 } catch (MediaPackageException e) {
109 throw new ExecuteException("Error serializing the MediaPackage element", e);
110 } catch (IllegalStateException e) {
111 throw new ExecuteException(e);
112 } catch (IOException e) {
113 throw new ExecuteException(e);
114 } finally {
115 closeConnection(response);
116 }
117 }
118
119
120
121
122 @Override
123 public Job execute(String exec, String params, MediaPackage mp, String outFileName, Type type, float load)
124 throws ExecuteException {
125 HttpPost post = null;
126 HttpResponse response = null;
127
128 try {
129 String mpStr = MediaPackageParser.getAsXml(mp);
130 List<NameValuePair> formStringParams = new ArrayList<NameValuePair>();
131 formStringParams.add(new BasicNameValuePair(EXEC_FORM_PARAM, exec));
132 formStringParams.add(new BasicNameValuePair(PARAMS_FORM_PARAM, params));
133 formStringParams.add(new BasicNameValuePair(LOAD_FORM_PARAM, String.valueOf(load)));
134 formStringParams.add(new BasicNameValuePair(INPUT_MP_FORM_PARAM, mpStr));
135 if (outFileName != null)
136 formStringParams.add(new BasicNameValuePair(OUTPUT_NAME_FORM_PARAMETER, outFileName));
137 if (type != null)
138 formStringParams.add(new BasicNameValuePair(TYPE_FORM_PARAMETER, type.toString()));
139
140 logger.info("Executing command {} using a remote execute service", exec);
141
142 post = new HttpPost("/" + ExecuteService.ENDPOINT_NAME);
143 post.setEntity(new UrlEncodedFormEntity(formStringParams, "UTF-8"));
144 response = getResponse(post);
145
146 if (response != null) {
147 Job job = JobParser.parseJob(response.getEntity().getContent());
148 logger.info("Completing execution of command {} using a remote execute service", exec);
149 return job;
150 } else {
151 logger.error("Failed to execute the command {} using a remote execute service", exec);
152 throw new ExecuteException(String.format("Failed to execute the command %s using a remote execute service", exec));
153 }
154 } catch (IllegalStateException e) {
155 throw new ExecuteException(e);
156 } catch (IOException e) {
157 throw new ExecuteException(e);
158 } finally {
159 closeConnection(response);
160 }
161 }
162
163 @Reference
164 @Override
165 public void setTrustedHttpClient(TrustedHttpClient trustedHttpClient) {
166 super.setTrustedHttpClient(trustedHttpClient);
167 }
168
169 @Reference
170 @Override
171 public void setRemoteServiceManager(ServiceRegistry serviceRegistry) {
172 super.setRemoteServiceManager(serviceRegistry);
173 }
174
175 }