GoogleSpeechTranscriptionService extends AbstractJobProducer implements TranscriptionService { boolean getAndSaveJobResults(String jobId) throws TranscriptionServiceException, IOException { CloseableHttpClient httpClient = makeHttpClient(); CloseableHttpResponse response = null; String mpId = "unknown"; JSONArray resultsArray = null; String token = getRefreshAccessToken(); if (token.equals(INVALID_TOKEN)) { return false; } try { HttpGet httpGet = new HttpGet(UrlSupport.concat(GOOGLE_SPEECH_URL, RESULT_PATH, jobId)); logger.debug("Url to invoke Google speech service: {}", httpGet.getURI().toString()); httpGet.addHeader("Authorization", "Bearer " + token); response = httpClient.execute(httpGet); int code = response.getStatusLine().getStatusCode(); switch (code) { case HttpStatus.SC_OK: HttpEntity entity = response.getEntity(); String jsonString = EntityUtils.toString(entity); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString); Boolean jobDone = (Boolean) jsonObject.get("done"); if (jobDone) { resultsArray = getTranscriptionResult(jsonObject); } TranscriptionJobControl jc = database.findByJob(jobId); if (jc != null) { mpId = jc.getMediaPackageId(); } logger.info("Recognitions job {} has been found, completed status {}", jobId, jobDone.toString()); EntityUtils.consume(entity); if (jobDone && resultsArray != null) { transcriptionDone(mpId, jsonObject); return true; } return false; case HttpStatus.SC_NOT_FOUND: logger.warn("Job not found: {}", jobId); break; case HttpStatus.SC_SERVICE_UNAVAILABLE: logger.warn("Service unavailable returned, status: {}", code); break; default: logger.warn("Error return status: {}.", code); break; } throw new TranscriptionServiceException( String.format("Could not check recognition job for media package %s, job id %s. Status returned: %d", mpId, jobId, code), code); } catch (TranscriptionServiceException e) { throw e; } catch (Exception e) { String msg = String.format("Exception when calling the recognitions endpoint for media package %s, job id %s", mpId, jobId); logger.warn(msg, e); throw new TranscriptionServiceException(String.format( "Exception when calling the recognitions endpoint for media package %s, job id %s", mpId, jobId), e); } finally { try { httpClient.close(); if (response != null) { response.close(); } } catch (IOException e) { } } } GoogleSpeechTranscriptionService(); void activate(ComponentContext cc); @Override Job startTranscription(String mpId, Track track, String language); @Override Job startTranscription(String mpId, Track track); @Override void transcriptionDone(String mpId, Object obj); @Override String getLanguage(); @Override void transcriptionError(String mpId, Object obj); String getTranscriptionResults(String jobId); @Override MediaPackageElement getGeneratedTranscription(String mpId, String jobId); String getTranscriptionStatus(String mpId); void setServiceRegistry(ServiceRegistry serviceRegistry); void setSecurityService(SecurityService securityService); void setUserDirectoryService(UserDirectoryService userDirectoryService); void setOrganizationDirectoryService(OrganizationDirectoryService organizationDirectoryService); void setSmtpService(SmtpService service); void setWorkspace(Workspace ws); void setWorkingFileRepository(WorkingFileRepository wfr); void setDatabase(TranscriptionDatabase service); void setAssetManager(AssetManager service); void setWorkflowService(WorkflowService service); static final String DEFAULT_WF_DEF; static final String ENABLED_CONFIG; static final String GOOGLE_SPEECH_LANGUAGE; static final String PROFANITY_FILTER; static final String WORKFLOW_CONFIG; static final String DISPATCH_WORKFLOW_INTERVAL_CONFIG; static final String COMPLETION_CHECK_BUFFER_CONFIG; static final String MAX_PROCESSING_TIME_CONFIG; static final String NOTIFICATION_EMAIL_CONFIG; static final String CLEANUP_RESULTS_DAYS_CONFIG; static final String GOOGLE_CLOUD_CLIENT_ID; static final String GOOGLE_CLOUD_CLIENT_SECRET; static final String GOOGLE_CLOUD_REFRESH_TOKEN; static final String GOOGLE_CLOUD_BUCKET; static final String GOOGLE_CLOUD_TOKEN_ENDPOINT_URL; static final String ENCODING_EXTENSION; }
@Test public void testGetAndSaveJobResults() throws Exception { InputStream stream = GoogleSpeechTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE); database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.InProgress.name(), TRACK_DURATION, DATE_EXPECTED, PROVIDER); Capture<String> capturedCollection = Capture.newInstance(); Capture<String> capturedFileName = Capture.newInstance(); EasyMock.expect(workspace.putInCollection(EasyMock.capture(capturedCollection), EasyMock.capture(capturedFileName), EasyMock.anyObject(InputStream.class))).andReturn(new URI("http: EasyMock.replay(workspace); HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class); EasyMock.expect(httpEntity.getContent()).andReturn(stream); CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class); StatusLine status = EasyMock.createNiceMock(StatusLine.class); EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes(); EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes(); EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes(); EasyMock.replay(httpEntity, response, status); Capture<HttpGet> capturedGet = Capture.newInstance(); EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response).anyTimes(); EasyMock.replay(httpClient); long before = System.currentTimeMillis(); service.getAndSaveJobResults(JOB_ID); long after = System.currentTimeMillis(); Assert.assertEquals(RESULT_URL + JOB_ID, capturedGet.getValue().getURI().toString()); TranscriptionJobControl job = database.findByJob(JOB_ID); Assert.assertNotNull(job); Assert.assertEquals(TranscriptionJobControl.Status.TranscriptionComplete.name(), job.getStatus()); Assert.assertNotNull(job.getDateCompleted()); Assert.assertTrue(before <= job.getDateCompleted().getTime() && job.getDateCompleted().getTime() <= after); Assert.assertEquals(GoogleSpeechTranscriptionService.TRANSCRIPT_COLLECTION, capturedCollection.getValue()); Assert.assertEquals(JOB_ID + ".json", capturedFileName.getValue()); }
@Test public void testGetAndSaveJobResultsError503() throws Exception { CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class); StatusLine status = EasyMock.createNiceMock(StatusLine.class); EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes(); EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_SERVICE_UNAVAILABLE).anyTimes(); EasyMock.replay(response, status); Capture<HttpGet> capturedGet = Capture.newInstance(); EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response).anyTimes(); EasyMock.replay(httpClient); try { service.getAndSaveJobResults(JOB_ID); } catch (TranscriptionServiceException e) { Assert.assertEquals(503, e.getCode()); } }