View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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   * Remote implementation of the execute service
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    /** The logger */
65    private static final Logger logger = LoggerFactory.getLogger(ExecuteServiceRemoteImpl.class);
66  
67  
68    /**
69     * Constructs a new execute service proxy
70     */
71    public ExecuteServiceRemoteImpl() {
72      super(JOB_TYPE);
73    }
74  
75    /**
76     * @see org.opencastproject.execute.api.ExecuteService#execute(java.lang.String, java.lang.String,
77     *      org.opencastproject.mediapackage.MediaPackageElement, java.lang.String,
78     *      org.opencastproject.mediapackage.MediaPackageElement.Type, float)
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    * @see org.opencastproject.execute.api.ExecuteService#execute(java.lang.String, java.lang.String,
128    *      org.opencastproject.mediapackage.MediaPackage, java.lang.String,
129    *      org.opencastproject.mediapackage.MediaPackageElement.Type, float)
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 }