// Copyright 2020 Google LLC
 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
 
//     https://www.apache.org/licenses/LICENSE-2.0
 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
package com.google.speech.tools.voxetta;
 
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/** 
* Servlet that returns a Blobstore URL. 
*/
@WebServlet("/blobstore-link")
public class BlobstoreLinkServlet extends HttpServlet {
 
  private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
 
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Redirect to Datastore processing after Blobstore processing
    String uploadUrl = blobstoreService.createUploadUrl("/upload-utterance");
 
    response.setContentType("text/html");
    response.getWriter().println(uploadUrl);
  }

  /** 
  * Allow the servlet's Blobstore service to be set for mocking purposes
  */
  public void setBlobstoreService(BlobstoreService inputService) {
      blobstoreService = inputService; 
  }
}

@@ -22,17 +22,18 @@
 gle LLC%0A
- 
+//
 %0A// Lice
@@ -203,16 +203,18 @@
 ense at%0A
+//
  %0A//    
@@ -258,17 +258,18 @@
 NSE-2.0%0A
- 
+//
 %0A// Unle
@@ -623,19 +623,27 @@
 .voxetta
+.servlets
 ;%0A
- 
 %0Aimport 
@@ -686,23 +686,32 @@
 lobstore
-Service
+FailureException
 ;%0Aimport
@@ -726,55 +726,63 @@
 gle.
-appengine.api.blobstore.BlobstoreServiceFactory
+speech.tools.voxetta.services.DatastoreUtteranceService
 ;%0Aim
@@ -990,16 +990,17 @@
 %0A %0A/** %0A
+ 
 * Servle
@@ -1032,16 +1032,17 @@
 e URL. %0A
+ 
 */%0A@WebS
@@ -1060,16 +1060,33 @@
 obstore-
+utterance-upload-
 link%22)%0Ap
@@ -1140,17 +1140,16 @@
 rvlet %7B%0A
- 
 %0A  priva
@@ -1155,79 +1155,65 @@
 ate 
-Blob
+Data
 store
-Service blobstoreService = BlobstoreServiceFactory.getBlobstor
+UtteranceService service = new DatastoreUtteranc
 eSer
@@ -1219,16 +1219,17 @@
 rvice();
+ 
 %0A %0A  @Ov
@@ -1342,151 +1342,163 @@
     
-// Redirect to Datastore processing after Blobstore processing%0A    String uploadUrl = blobstoreService.createUploadUrl(%22/upload-utterance%22
+response.setContentType(%22application/json%22);%0A%0A    // Create and return a Blobstore Upload URL  %0A    try %7B%0A      String uploadUrl = service.getFormUrl(
 );%0A 
-%0A
+ 
     
@@ -1510,81 +1510,263 @@
 nse.
-setContentType(%22text/html%22);%0A    response.getWriter().println(uploadUrl);
+getWriter().println(%22%7B %5C%22success%5C%22: true, %5C%22url%5C%22: %5C%22%22 + uploadUrl + %22%5C%22 %7D%22); %0A    %7D catch (BlobstoreFailureException e) %7B%0A      response.getWriter().println(%22%7B %5C%22success%5C%22: false, %5C%22error%5C%22: %5C%22Error: Failed to upload audio file to Blobstore.%5C%22 %7D%22);%0A    %7D
 %0A  %7D
@@ -1774,16 +1774,17 @@
 %0A  /** %0A
+ 
   * Allo
@@ -1803,19 +1803,29 @@
 t's 
-Blob
+Data
 store 
-s
+Utterance S
 ervi
@@ -1857,17 +1857,19 @@
 purposes
-%0A
+.%0A 
   */%0A  p
@@ -1886,25 +1886,16 @@
  set
-Blobstore
 Service(
 Blob
@@ -1890,25 +1890,34 @@
 Service(
-Blob
+Data
 store
+Utterance
 Service 
@@ -1940,20 +1940,9 @@
     
-  blobstoreS
+s
 ervi

remove external API logic to service file