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, org.opencastproject.mediapackage.MediaPackageElement, java.lang.String, org.opencastproject.mediapackage.MediaPackageElement.Type, float)
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    * @see org.opencastproject.execute.api.ExecuteService#execute(java.lang.String, java.lang.String, org.opencastproject.mediapackage.MediaPackage, java.lang.String, org.opencastproject.mediapackage.MediaPackageElement.Type, float)
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 }