Search in sources :

Example 6 with SearchResult

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);
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) SearchResult(org.opencastproject.search.api.SearchResult) URI(java.net.URI) Test(org.junit.Test)

Example 7 with SearchResult

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);
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) SearchResult(org.opencastproject.search.api.SearchResult) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) URL(java.net.URL) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) AuthScope(org.apache.http.auth.AuthScope)

Example 8 with SearchResult

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;
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) SearchResult(org.opencastproject.search.api.SearchResult)

Example 9 with SearchResult

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);
    }
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Publication(org.opencastproject.mediapackage.Publication) SearchResult(org.opencastproject.search.api.SearchResult) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job)

Example 10 with SearchResult

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());
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbRole(org.opencastproject.security.api.JaxbRole) MediaPackage(org.opencastproject.mediapackage.MediaPackage) JaxbUser(org.opencastproject.security.api.JaxbUser) SearchResult(org.opencastproject.search.api.SearchResult) Job(org.opencastproject.job.api.Job) JobBarrier(org.opencastproject.job.api.JobBarrier) Test(org.junit.Test)

Aggregations

SearchResult (org.opencastproject.search.api.SearchResult)15 SearchQuery (org.opencastproject.search.api.SearchQuery)10 MediaPackage (org.opencastproject.mediapackage.MediaPackage)9 Test (org.junit.Test)6 Job (org.opencastproject.job.api.Job)5 JobBarrier (org.opencastproject.job.api.JobBarrier)4 URI (java.net.URI)3 SearchResultItem (org.opencastproject.search.api.SearchResultItem)3 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)3 ArrayList (java.util.ArrayList)2 SearchException (org.opencastproject.search.api.SearchException)2 User (org.opencastproject.security.api.User)2 NotFoundException (org.opencastproject.util.NotFoundException)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 HttpResponse (org.apache.http.HttpResponse)1 NameValuePair (org.apache.http.NameValuePair)1