IBMWatsonTranscriptionService extends AbstractJobProducer implements TranscriptionService { void createRecognitionsJob(String mpId, Track track) throws TranscriptionServiceException { if (!callbackAlreadyRegistered) registerCallback(); File audioFile = null; try { audioFile = workspace.get(track.getURI()); } catch (Exception e) { throw new TranscriptionServiceException("Error reading audio track", e); } CloseableHttpClient httpClient = makeHttpClient(); String additionalParms = ""; if (callbackAlreadyRegistered) { additionalParms = String.format("&callback_url=%s&events=%s,%s", callbackUrl, JobEvent.COMPLETED_WITH_RESULTS, JobEvent.FAILED); } if (!StringUtils.isEmpty(model)) { additionalParms += String.format("&model=%s", model); } CloseableHttpResponse response = null; try { HttpPost httpPost = new HttpPost(UrlSupport.concat(watsonServiceUrl, RECOGNITIONS) + String.format( "?user_token=%s&inactivity_timeout=-1&timestamps=true&smart_formatting=true%s", mpId, additionalParms)); logger.debug("Url to invoke ibm watson service: {}", httpPost.getURI().toString()); httpPost.setHeader(HttpHeaders.CONTENT_TYPE, track.getMimeType().toString()); FileEntity fileEntity = new FileEntity(audioFile); fileEntity.setChunked(true); httpPost.setEntity(fileEntity); response = httpClient.execute(httpPost); int code = response.getStatusLine().getStatusCode(); switch (code) { case HttpStatus.SC_CREATED: logger.info("Recognitions job has been successfully created"); HttpEntity entity = response.getEntity(); String jsonString = EntityUtils.toString(response.getEntity()); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString); String jobId = (String) jsonObject.get("id"); String jobStatus = (String) jsonObject.get("status"); String jobUrl = (String) jsonObject.get("url"); logger.info("Transcription for mp {} has been submitted. Job id: {}, job status: {}, job url: {}", mpId, jobId, jobStatus, jobUrl); database.storeJobControl(mpId, track.getIdentifier(), jobId, TranscriptionJobControl.Status.InProgress.name(), track.getDuration() == null ? 0 : track.getDuration().longValue(), null, PROVIDER); EntityUtils.consume(entity); return; case HttpStatus.SC_BAD_REQUEST: logger.info("Invalid argument returned, status: {}", code); break; case HttpStatus.SC_SERVICE_UNAVAILABLE: logger.info("Service unavailable returned, status: {}", code); break; default: logger.info("Unknown return status: {}.", code); break; } throw new TranscriptionServiceException("Could not create recognition job. Status returned: " + code); } catch (Exception e) { logger.warn("Exception when calling the recognitions endpoint", e); throw new TranscriptionServiceException("Exception when calling the recognitions endpoint", e); } finally { try { httpClient.close(); if (response != null) response.close(); } catch (IOException e) { } } } IBMWatsonTranscriptionService(); void activate(ComponentContext cc); @Override Job startTranscription(String mpId, Track track); @Override Job startTranscription(String mpId, Track track, String language); @Override void transcriptionDone(String mpId, Object obj); @Override void transcriptionError(String mpId, Object obj); @Override String getLanguage(); @Override MediaPackageElement getGeneratedTranscription(String mpId, String jobId); boolean isCallbackAlreadyRegistered(); 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); boolean isEnabled(); static final String DEFAULT_WF_DEF; static final String ADMIN_URL_PROPERTY; static final float DEFAULT_START_TRANSCRIPTION_JOB_LOAD; static final String START_TRANSCRIPTION_JOB_LOAD_KEY; static final String ENABLED_CONFIG; static final String IBM_WATSON_SERVICE_URL_CONFIG; static final String IBM_WATSON_USER_CONFIG; static final String IBM_WATSON_PSW_CONFIG; static final String IBM_WATSON_API_KEY_CONFIG; static final String IBM_WATSON_MODEL_CONFIG; 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 MAX_ATTEMPTS_CONFIG; static final String RETRY_WORKLFOW_CONFIG; }
@Test public void testCreateRecognitionsJob() throws Exception { service.activate(cc); EasyMock.expect(workspace.get(EasyMock.anyObject(URI.class))).andReturn(audioFile); EasyMock.replay(workspace); HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class); EasyMock.expect(httpEntity.getContent()).andReturn(new ByteArrayInputStream(new String("{\"id\": \"" + JOB_ID + "\", \"status\": \"waiting\", \"url\": \"" + WATSON_URL + "/v1/recognitions/" + JOB_ID + "\"}").getBytes(StandardCharsets.UTF_8))); 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_CREATED).anyTimes(); EasyMock.replay(httpEntity, response, status); Capture<HttpPost> capturedPost = Capture.newInstance(); EasyMock.expect(httpClient.execute(EasyMock.capture(capturedPost))).andReturn(response).anyTimes(); EasyMock.replay(httpClient); service.createRecognitionsJob(MP_ID, mediaPackage.getTrack("audioTrack1")); Assert.assertEquals(WATSON_URL + "/v1/recognitions?user_token=" + MP_ID + "&inactivity_timeout=-1&timestamps=true&smart_formatting=true" + "&callback_url=http: capturedPost.getValue().getURI().toString()); TranscriptionJobControl j = database.findByJob(JOB_ID); Assert.assertNotNull(j); Assert.assertEquals(MP_ID, j.getMediaPackageId()); Assert.assertEquals(TRACK_ID, j.getTrackId()); }