use of org.opencastproject.search.api.SearchResult in project opencast by opencast.
the class LiveScheduleServiceImplTest method testGetMediaPackageFromSearchNotFound.
@Test
public void testGetMediaPackageFromSearchNotFound() throws Exception {
URI searchResultURI = LiveScheduleServiceImplTest.class.getResource("/search-result-empty.xml").toURI();
SearchResult searchResult = SearchResultImpl.valueOf(searchResultURI.toURL().openStream());
EasyMock.expect(searchService.getByQuery((SearchQuery) EasyMock.anyObject())).andReturn(searchResult);
replayServices();
MediaPackage mp = service.getMediaPackageFromSearch(MP_ID);
Assert.assertNull(mp);
}
use of org.opencastproject.search.api.SearchResult in project opencast by opencast.
the class MediaPackagePostOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
// get configuration
WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
Configuration config = new Configuration(currentOperation);
MediaPackage workflowMP = workflowInstance.getMediaPackage();
MediaPackage mp = workflowMP;
/* Check if we need to replace the Mediapackage we got with the published
* Mediapackage from the Search Service */
if (config.mpFromSearch()) {
SearchQuery searchQuery = new SearchQuery();
searchQuery.withId(mp.getIdentifier().toString());
SearchResult result = searchService.getByQuery(searchQuery);
if (result.size() != 1) {
throw new WorkflowOperationException("Received multiple results for identifier" + "\"" + mp.getIdentifier().toString() + "\" from search service. ");
}
logger.info("Getting Mediapackage from Search Service");
mp = result.getItems()[0].getMediaPackage();
}
logger.info("Submitting \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") as " + config.getFormat().name() + " to " + config.getUrl().toString());
try {
// serialize MediaPackage to target format
String mpStr;
if (config.getFormat() == Configuration.Format.JSON) {
mpStr = MediaPackageParser.getAsJSON(mp);
} else {
mpStr = MediaPackageParser.getAsXml(mp);
}
// Log mediapackge
if (config.debug()) {
logger.info(mpStr);
}
// constrcut message body
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("mediapackage", mpStr));
data.addAll(config.getAdditionalFields());
// construct POST
HttpPost post = new HttpPost(config.getUrl());
post.setEntity(new UrlEncodedFormEntity(data, config.getEncoding()));
// execute POST
DefaultHttpClient client = new DefaultHttpClient();
// Handle authentication
if (config.authenticate()) {
URL targetUrl = config.getUrl().toURL();
client.getCredentialsProvider().setCredentials(new AuthScope(targetUrl.getHost(), targetUrl.getPort()), config.getCredentials());
}
HttpResponse response = client.execute(post);
// throw Exception if target host did not return 200
int status = response.getStatusLine().getStatusCode();
if ((status >= 200) && (status < 300)) {
if (config.debug()) {
logger.info("Successfully submitted \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") to " + config.getUrl().toString() + ": " + status);
}
} else if (status == 418) {
logger.warn("Submitted \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") to " + config.getUrl().toString() + ": The target claims to be a teapot. " + "The Reason for this is probably an insane programmer.");
} else {
throw new WorkflowOperationException("Faild to submit \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + "), " + config.getUrl().toString() + " answered with: " + Integer.toString(status));
}
} catch (Exception e) {
if (e instanceof WorkflowOperationException) {
throw (WorkflowOperationException) e;
} else {
logger.error("Submitting mediapackage failed: {}", e.toString());
throw new WorkflowOperationException(e);
}
}
return createResult(workflowMP, Action.CONTINUE);
}
use of org.opencastproject.search.api.SearchResult in project opencast by opencast.
the class PublishEngageWorkflowOperationHandler method getDistributedMediapackage.
protected MediaPackage getDistributedMediapackage(String mediaPackageID) throws WorkflowOperationException {
MediaPackage mediaPackage = null;
SearchQuery query = new SearchQuery().withId(mediaPackageID);
query.includeEpisodes(true);
query.includeSeries(false);
SearchResult result = searchService.getByQuery(query);
if (result.size() == 0) {
logger.info("The search service doesn't know mediapackage {}.", mediaPackageID);
// i.e. null
return mediaPackage;
} else if (result.size() > 1) {
logger.warn("More than one mediapackage with id {} returned from search service", mediaPackageID);
throw new WorkflowOperationException("More than one mediapackage with id " + mediaPackageID + " found");
} else {
// else, merge the new with the existing (new elements will overwrite existing elements)
mediaPackage = result.getItems()[0].getMediaPackage();
}
return mediaPackage;
}
use of org.opencastproject.search.api.SearchResult in project opencast by opencast.
the class RetractEngageWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(WorkflowInstance, JobContext)
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
try {
List<Job> jobs = new ArrayList<Job>();
SearchQuery query = new SearchQuery().withId(mediaPackage.getIdentifier().toString());
SearchResult result = searchService.getByQuery(query);
if (result.size() == 0) {
logger.info("The search service doesn't know mediapackage {}", mediaPackage);
return createResult(mediaPackage, Action.SKIP);
} else if (result.size() > 1) {
logger.warn("More than one mediapackage with id {} returned from search service", mediaPackage.getIdentifier());
throw new WorkflowOperationException("More than one mediapackage with id " + mediaPackage.getIdentifier() + " found");
} else {
Set<String> retractElementIds = new HashSet<String>();
MediaPackage searchMediaPackage = result.getItems()[0].getMediaPackage();
logger.info("Retracting media package {} from download/streaming distribution channel", searchMediaPackage);
for (MediaPackageElement element : searchMediaPackage.getElements()) {
retractElementIds.add(element.getIdentifier());
}
if (retractElementIds.size() > 0) {
Job retractDownloadDistributionJob = downloadDistributionService.retract(CHANNEL_ID, searchMediaPackage, retractElementIds);
if (retractDownloadDistributionJob != null) {
jobs.add(retractDownloadDistributionJob);
}
}
if (distributeStreaming) {
for (MediaPackageElement element : searchMediaPackage.getElements()) {
if (distributeStreaming) {
Job retractStreamingJob = streamingDistributionService.retract(CHANNEL_ID, searchMediaPackage, element.getIdentifier());
if (retractStreamingJob != null) {
jobs.add(retractStreamingJob);
}
}
}
}
}
// Wait for retraction to finish
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
throw new WorkflowOperationException("One of the download/streaming retract job did not complete successfully");
}
logger.debug("Retraction operation complete");
logger.info("Removing media package {} from the search index", mediaPackage);
Job deleteFromSearch = searchService.delete(mediaPackage.getIdentifier().toString());
if (!waitForStatus(deleteFromSearch).isSuccess())
throw new WorkflowOperationException("Removing media package from search did not complete successfully");
logger.debug("Remove from search operation complete");
// Remove publication element
logger.info("Removing engage publication element from media package {}", mediaPackage);
Publication[] publications = mediaPackage.getPublications();
for (Publication publication : publications) {
if (CHANNEL_ID.equals(publication.getChannel())) {
mediaPackage.remove(publication);
logger.debug("Remove engage publication element '{}' complete", publication);
}
}
return createResult(mediaPackage, Action.CONTINUE);
} catch (Throwable t) {
throw new WorkflowOperationException(t);
}
}
use of org.opencastproject.search.api.SearchResult in project opencast by opencast.
the class SearchServiceImplTest method testAddSeriesMediaPackage.
/**
* Adds a media package with a dublin core catalog for episode and series. Verifies series catalog can be retrieved
* via search service.
*/
@Test
public void testAddSeriesMediaPackage() throws Exception {
String seriesId = "foobar-series";
MediaPackage mediaPackage = getMediaPackage("/manifest-full.xml");
mediaPackage.setSeries(seriesId);
// Add the media package to the search index
Job job = service.add(mediaPackage);
JobBarrier barrier = new JobBarrier(null, serviceRegistry, 1000, job);
barrier.waitForJobs();
assertEquals("Job to add mediapckage did not finish", Job.Status.FINISHED, job.getStatus());
User adminUser = new JaxbUser("admin", "test", defaultOrganization, new JaxbRole(defaultOrganization.getAdminRole(), defaultOrganization));
userResponder.setResponse(adminUser);
// Make sure it's properly indexed and returned
SearchQuery q = new SearchQuery();
q.includeEpisodes(false);
q.includeSeries(true);
SearchResult result = service.getByQuery(q);
assertEquals(1, result.size());
assertEquals(seriesId, result.getItems()[0].getId());
}
Aggregations