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  package org.opencastproject.transcription.googlespeech;
22  
23  import org.apache.http.HttpHeaders;
24  import org.apache.http.HttpStatus;
25  import org.apache.http.client.methods.CloseableHttpResponse;
26  import org.apache.http.client.methods.HttpDelete;
27  import org.apache.http.client.methods.HttpPost;
28  import org.apache.http.client.methods.HttpPut;
29  import org.apache.http.entity.FileEntity;
30  import org.apache.http.impl.client.CloseableHttpClient;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import java.io.File;
35  import java.io.IOException;
36  
37  public class GoogleSpeechTranscriptionServiceStorage {
38  
39    /**
40     * The logger
41     */
42    private static final Logger logger = LoggerFactory.getLogger(GoogleSpeechTranscriptionService.class);
43  
44    private static final String GOOGLE_STORAGE_LINK = "https://www.googleapis.com/upload/storage/v1/b/";
45    private static final String GOOGLE_STORAGE_MEDIA = "https://www.googleapis.com/storage/v1/b/";
46    private static final int DEFAULT_CODE = 0;
47  
48    /**
49     * Upload to Google Cloud Storage using Json API.
50     * https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload
51     *
52     * @param httpClient
53     * @param bucket Google storage bucket name
54     * @param mpId mediapackage ID
55     * @param fileExtension
56     * @param file
57     * @param byteSize
58     * @param contentType
59     * @param accessToken
60     * @return response code
61     * @throws java.io.IOException
62     */
63    public int startUpload(CloseableHttpClient httpClient, String bucket, String mpId,
64            String fileExtension, File file, String byteSize, String contentType, String accessToken) throws IOException {
65      CloseableHttpResponse postResponse = null;
66      CloseableHttpResponse putResponse = null;
67      String location = null;
68  
69      try {
70        HttpPost httpPost = new HttpPost(GOOGLE_STORAGE_LINK + String.format(
71                "%s/o?uploadType=resumable&name=%s.%s", bucket, mpId, fileExtension));
72        logger.debug("Url to store media file on Google Cloud Storage : {}", httpPost.getURI().toString());
73        httpPost.addHeader("Authorization", "Bearer " + accessToken);
74        httpPost.addHeader("X-Upload-Content-Type", contentType);
75        httpPost.addHeader("X-Upload-Content-Length", byteSize);
76        httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
77        postResponse = httpClient.execute(httpPost);
78        int postCode = postResponse.getStatusLine().getStatusCode();
79        if (postCode == HttpStatus.SC_OK) { // 200
80          logger.info("Upload session has been successfully created");
81          // Get upload session stored in location
82          try {
83            location = postResponse.getLastHeader("Location").getValue();
84          } catch (Exception ex) {
85            logger.warn("Exception when uploading file to Google Storage", ex);
86            return DEFAULT_CODE;
87          }
88          try {
89            HttpPut httpPut = new HttpPut(location);
90            httpPut.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
91            FileEntity entity = new FileEntity(file);
92            httpPut.setEntity(entity);
93            putResponse = httpClient.execute(httpPut);
94            int putCode = putResponse.getStatusLine().getStatusCode();
95  
96            switch (putCode) {
97              case HttpStatus.SC_OK: // 200
98              case HttpStatus.SC_CREATED: // 201
99                logger.info("File {} uploaded to Google Storage", mpId + "." + fileExtension);
100               return putCode;
101             default:
102               logger.warn("Unable to upload file to Google Storage, returned code: {}.", putCode);
103               return putCode;
104           }
105         } catch (Exception e) {
106           logger.warn("Exception when uploading file to Google Storage", e);
107         }
108       } else {
109         logger.warn("Uploading file to Google Storage failed and returned, status: {}.", postCode);
110         return postCode;
111       }
112     } catch (Exception e) {
113       logger.warn("Exception when uploading file to Google Storage", e);
114     } finally {
115       try {
116         httpClient.close();
117         if (postResponse != null) {
118           postResponse.close();
119         }
120         if (putResponse != null) {
121           putResponse.close();
122         }
123       } catch (IOException e) {
124       }
125     }
126     return DEFAULT_CODE;
127   }
128 
129   /**
130    * Delete file from Google Cloud Storage using json API.
131    * https://cloud.google.com/storage/docs/deleting-objects
132    *
133    * @param httpClient
134    * @param bucket
135    * @param objectName
136    * @param accessToken
137    * @throws java.io.IOException
138    */
139   public void deleteGoogleStorageFile(
140       CloseableHttpClient httpClient,
141       String bucket,
142       String objectName,
143       String accessToken
144   ) throws IOException {
145     CloseableHttpResponse response = null;
146     try {
147       HttpDelete httpdelete = new HttpDelete(GOOGLE_STORAGE_MEDIA + String.format("%s/o/%s", bucket, objectName));
148       logger.debug("Url to delete media file from Google Cloud Storage : {}", httpdelete.getURI().toString());
149       httpdelete.addHeader("Authorization", "Bearer " + accessToken); // add the authorization header to the request;
150       response = httpClient.execute(httpdelete);
151       int code = response.getStatusLine().getStatusCode();
152 
153       switch (code) {
154         case HttpStatus.SC_NO_CONTENT: // 204
155           logger.info("Media file: {} deleted from Google storage", objectName);
156           break;
157         default:
158           logger.warn("Unable to delete meida file: {} from Google Storage", objectName);
159           break;
160       }
161     } catch (Exception e) {
162       logger.warn("Unable to delete meida file: {} from Google Storage", objectName, e);
163     } finally {
164       try {
165         httpClient.close();
166         if (response != null) {
167           response.close();
168         }
169       } catch (IOException e) {
170       }
171     }
172   }
173 
174 }