Search in sources :

Example 1 with SearchException

use of org.opencastproject.search.api.SearchException in project opencast by opencast.

the class SearchServiceRemoteImpl method add.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.search.api.SearchService#add(org.opencastproject.mediapackage.MediaPackage)
 */
@Override
public Job add(MediaPackage mediaPackage) throws SearchException {
    HttpPost post = new HttpPost("/add");
    try {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("mediapackage", MediaPackageParser.getAsXml(mediaPackage)));
        post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    } catch (Exception e) {
        throw new SearchException("Unable to assemble a remote search request for mediapackage " + mediaPackage, e);
    }
    HttpResponse response = getResponse(post);
    try {
        if (response != null) {
            Job job = JobParser.parseJob(response.getEntity().getContent());
            logger.info("Publishing mediapackage '{}' using a remote search service", mediaPackage.getIdentifier());
            return job;
        }
    } catch (Exception e) {
        throw new SearchException("Unable to publish " + mediaPackage.getIdentifier() + " using a remote search service", e);
    } finally {
        closeConnection(response);
    }
    throw new SearchException("Unable to publish " + mediaPackage.getIdentifier() + " using a remote search service");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) SearchException(org.opencastproject.search.api.SearchException) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Job(org.opencastproject.job.api.Job) SearchException(org.opencastproject.search.api.SearchException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException)

Example 2 with SearchException

use of org.opencastproject.search.api.SearchException in project opencast by opencast.

the class SearchServiceRemoteImpl method getByQuery.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.search.api.SearchService#getByQuery(java.lang.String, int, int)
 */
@Override
public SearchResult getByQuery(String query, int limit, int offset) throws SearchException {
    List<NameValuePair> queryStringParams = new ArrayList<NameValuePair>();
    queryStringParams.add(new BasicNameValuePair("q", query));
    queryStringParams.add(new BasicNameValuePair("limit", Integer.toString(limit)));
    queryStringParams.add(new BasicNameValuePair("offset", Integer.toString(offset)));
    queryStringParams.add(new BasicNameValuePair("admin", Boolean.TRUE.toString()));
    HttpGet get = new HttpGet("/lucene.xml?" + URLEncodedUtils.format(queryStringParams, "UTF-8"));
    logger.debug("Sending remote query '{}'", get.getRequestLine().toString());
    HttpResponse response = getResponse(get);
    try {
        if (response != null)
            return SearchResultImpl.valueOf(response.getEntity().getContent());
    } catch (Exception e) {
        throw new SearchException("Unable to parse getByQuery response from remote search index", e);
    } finally {
        closeConnection(response);
    }
    throw new SearchException("Unable to perform getByQuery from remote search index");
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) SearchException(org.opencastproject.search.api.SearchException) SearchException(org.opencastproject.search.api.SearchException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException)

Example 3 with SearchException

use of org.opencastproject.search.api.SearchException in project opencast by opencast.

the class SearchServiceRemoteImpl method getByQuery.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.search.api.SearchService#getByQuery(org.opencastproject.search.api.SearchQuery)
 */
@Override
public SearchResult getByQuery(SearchQuery q) throws SearchException {
    HttpGet get = new HttpGet(getSearchUrl(q, false));
    HttpResponse response = getResponse(get);
    try {
        if (response != null)
            return SearchResultImpl.valueOf(response.getEntity().getContent());
    } catch (Exception e) {
        throw new SearchException("Unable to parse results of a getByQuery request from remote search index: ", e);
    } finally {
        closeConnection(response);
    }
    throw new SearchException("Unable to perform getByQuery from remote search index");
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) SearchException(org.opencastproject.search.api.SearchException) SearchException(org.opencastproject.search.api.SearchException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException)

Example 4 with SearchException

use of org.opencastproject.search.api.SearchException in project opencast by opencast.

the class SeriesUpdatedEventHandler method handleEvent.

public void handleEvent(final SeriesItem seriesItem) {
    // A series or its ACL has been updated. Find any mediapackages with that series, and update them.
    logger.debug("Handling {}", seriesItem);
    String seriesId = seriesItem.getSeriesId();
    // We must be an administrative user to make this query
    final User prevUser = securityService.getUser();
    final Organization prevOrg = securityService.getOrganization();
    try {
        securityService.setUser(SecurityUtil.createSystemUser(systemAccount, prevOrg));
        SearchQuery q = new SearchQuery().withSeriesId(seriesId);
        SearchResult result = searchService.getForAdministrativeRead(q);
        for (SearchResultItem item : result.getItems()) {
            MediaPackage mp = item.getMediaPackage();
            Organization org = organizationDirectoryService.getOrganization(item.getOrganization());
            securityService.setOrganization(org);
            // to the distribution channels as well
            if (SeriesItem.Type.UpdateAcl.equals(seriesItem.getType())) {
                // Build a new XACML file for this mediapackage
                Attachment fileRepoCopy = authorizationService.setAcl(mp, AclScope.Series, seriesItem.getAcl()).getB();
                // Distribute the updated XACML file
                Job distributionJob = distributionService.distribute(CHANNEL_ID, mp, fileRepoCopy.getIdentifier());
                JobBarrier barrier = new JobBarrier(null, serviceRegistry, distributionJob);
                Result jobResult = barrier.waitForJobs();
                if (jobResult.getStatus().get(distributionJob).equals(FINISHED)) {
                    mp.remove(fileRepoCopy);
                    mp.add(getFromXml(serviceRegistry.getJob(distributionJob.getId()).getPayload()));
                } else {
                    logger.error("Unable to distribute XACML {}", fileRepoCopy.getIdentifier());
                    continue;
                }
            }
            // Update the series dublin core
            if (SeriesItem.Type.UpdateCatalog.equals(seriesItem.getType())) {
                DublinCoreCatalog seriesDublinCore = seriesItem.getMetadata();
                mp.setSeriesTitle(seriesDublinCore.getFirst(DublinCore.PROPERTY_TITLE));
                // Update the series dublin core
                Catalog[] seriesCatalogs = mp.getCatalogs(MediaPackageElements.SERIES);
                if (seriesCatalogs.length == 1) {
                    Catalog c = seriesCatalogs[0];
                    String filename = FilenameUtils.getName(c.getURI().toString());
                    URI uri = workspace.put(mp.getIdentifier().toString(), c.getIdentifier(), filename, dublinCoreService.serialize(seriesDublinCore));
                    c.setURI(uri);
                    // setting the URI to a new source so the checksum will most like be invalid
                    c.setChecksum(null);
                    // Distribute the updated series dc
                    Job distributionJob = distributionService.distribute(CHANNEL_ID, mp, c.getIdentifier());
                    JobBarrier barrier = new JobBarrier(null, serviceRegistry, distributionJob);
                    Result jobResult = barrier.waitForJobs();
                    if (jobResult.getStatus().get(distributionJob).equals(FINISHED)) {
                        mp.remove(c);
                        mp.add(getFromXml(serviceRegistry.getJob(distributionJob.getId()).getPayload()));
                    } else {
                        logger.error("Unable to distribute series catalog {}", c.getIdentifier());
                        continue;
                    }
                }
            }
            // Remove the series catalog and isPartOf from episode catalog
            if (SeriesItem.Type.Delete.equals(seriesItem.getType())) {
                mp.setSeries(null);
                mp.setSeriesTitle(null);
                boolean retractSeriesCatalog = retractSeriesCatalog(mp);
                boolean updateEpisodeCatalog = updateEpisodeCatalog(mp);
                if (!retractSeriesCatalog || !updateEpisodeCatalog)
                    continue;
            }
            // Update the search index with the modified mediapackage
            Job searchJob = searchService.add(mp);
            JobBarrier barrier = new JobBarrier(null, serviceRegistry, searchJob);
            barrier.waitForJobs();
        }
    } catch (SearchException e) {
        logger.warn("Unable to find mediapackages in search: ", e.getMessage());
    } catch (UnauthorizedException e) {
        logger.warn(e.getMessage());
    } catch (MediaPackageException e) {
        logger.warn(e.getMessage());
    } catch (ServiceRegistryException e) {
        logger.warn(e.getMessage());
    } catch (NotFoundException e) {
        logger.warn(e.getMessage());
    } catch (IOException e) {
        logger.warn(e.getMessage());
    } catch (DistributionException e) {
        logger.warn(e.getMessage());
    } finally {
        securityService.setOrganization(prevOrg);
        securityService.setUser(prevUser);
    }
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) SearchResultItem(org.opencastproject.search.api.SearchResultItem) SearchException(org.opencastproject.search.api.SearchException) NotFoundException(org.opencastproject.util.NotFoundException) SearchResult(org.opencastproject.search.api.SearchResult) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) JobBarrier(org.opencastproject.job.api.JobBarrier) URI(java.net.URI) Catalog(org.opencastproject.mediapackage.Catalog) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Result(org.opencastproject.job.api.JobBarrier.Result) SearchResult(org.opencastproject.search.api.SearchResult) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) DistributionException(org.opencastproject.distribution.api.DistributionException) Job(org.opencastproject.job.api.Job) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 5 with SearchException

use of org.opencastproject.search.api.SearchException in project opencast by opencast.

the class PublishEngageWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running engage publication workflow operation");
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance op = workflowInstance.getCurrentOperation();
    // Check which tags have been configured
    String downloadSourceTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_TAGS));
    String downloadTargetTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_TARGET_TAGS));
    String downloadSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_FLAVORS));
    String downloadTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(DOWNLOAD_TARGET_SUBFLAVOR));
    String streamingSourceTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_TAGS));
    String streamingTargetTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_TARGET_TAGS));
    String streamingSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_FLAVORS));
    String streamingTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(STREAMING_TARGET_SUBFLAVOR));
    String republishStrategy = StringUtils.trimToEmpty(op.getConfiguration(STRATEGY));
    boolean checkAvailability = option(op.getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
    String[] sourceDownloadTags = StringUtils.split(downloadSourceTags, ",");
    String[] targetDownloadTags = StringUtils.split(downloadTargetTags, ",");
    String[] sourceDownloadFlavors = StringUtils.split(downloadSourceFlavors, ",");
    String[] sourceStreamingTags = StringUtils.split(streamingSourceTags, ",");
    String[] targetStreamingTags = StringUtils.split(streamingTargetTags, ",");
    String[] sourceStreamingFlavors = StringUtils.split(streamingSourceFlavors, ",");
    if (sourceDownloadTags.length == 0 && sourceDownloadFlavors.length == 0 && sourceStreamingTags.length == 0 && sourceStreamingFlavors.length == 0) {
        logger.warn("No tags or flavors have been specified, so nothing will be published to the engage publication channel");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    // Parse the download target flavor
    MediaPackageElementFlavor downloadSubflavor = null;
    if (downloadTargetSubflavor != null) {
        try {
            downloadSubflavor = MediaPackageElementFlavor.parseFlavor(downloadTargetSubflavor);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException(e);
        }
    }
    // Parse the streaming target flavor
    MediaPackageElementFlavor streamingSubflavor = null;
    if (streamingTargetSubflavor != null) {
        try {
            streamingSubflavor = MediaPackageElementFlavor.parseFlavor(streamingTargetSubflavor);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException(e);
        }
    }
    // Configure the download element selector
    SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
    for (String flavor : sourceDownloadFlavors) {
        downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceDownloadTags) {
        downloadElementSelector.addTag(tag);
    }
    // Configure the streaming element selector
    SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
    for (String flavor : sourceStreamingFlavors) {
        streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceStreamingTags) {
        streamingElementSelector.addTag(tag);
    }
    // Select the appropriate elements for download and streaming
    Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
    Collection<MediaPackageElement> streamingElements = streamingElementSelector.select(mediaPackage, false);
    try {
        Set<String> downloadElementIds = new HashSet<String>();
        Set<String> streamingElementIds = new HashSet<String>();
        // Look for elements matching the tag
        for (MediaPackageElement elem : downloadElements) {
            downloadElementIds.add(elem.getIdentifier());
        }
        for (MediaPackageElement elem : streamingElements) {
            streamingElementIds.add(elem.getIdentifier());
        }
        // Also distribute the security configuration
        // -----
        // This was removed in the meantime by a fix for MH-8515, but could now be used again.
        // -----
        Attachment[] securityAttachments = mediaPackage.getAttachments(MediaPackageElements.XACML_POLICY);
        if (securityAttachments != null && securityAttachments.length > 0) {
            for (Attachment a : securityAttachments) {
                downloadElementIds.add(a.getIdentifier());
                streamingElementIds.add(a.getIdentifier());
            }
        }
        removePublicationElement(mediaPackage);
        switch(republishStrategy) {
            case ("merge"):
                // nothing to do here. other publication strategies can be added to this list later on
                break;
            default:
                retractFromEngage(mediaPackage);
        }
        List<Job> jobs = new ArrayList<Job>();
        // distribute Elements
        try {
            if (downloadElementIds.size() > 0) {
                Job job = downloadDistributionService.distribute(CHANNEL_ID, mediaPackage, downloadElementIds, checkAvailability);
                if (job != null) {
                    jobs.add(job);
                }
            }
            if (distributeStreaming) {
                for (String elementId : streamingElementIds) {
                    Job job = streamingDistributionService.distribute(CHANNEL_ID, mediaPackage, elementId);
                    if (job != null) {
                        jobs.add(job);
                    }
                }
            }
        } catch (DistributionException e) {
            throw new WorkflowOperationException(e);
        }
        if (jobs.size() < 1) {
            logger.info("No mediapackage element was found for distribution to engage");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        // Wait until all distribution jobs have returned
        if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess())
            throw new WorkflowOperationException("One of the distribution jobs did not complete successfully");
        logger.debug("Distribute of mediapackage {} completed", mediaPackage);
        String engageUrlString = null;
        try {
            MediaPackage mediaPackageForSearch = getMediaPackageForSearchIndex(mediaPackage, jobs, downloadSubflavor, targetDownloadTags, downloadElementIds, streamingSubflavor, streamingElementIds, targetStreamingTags);
            // MH-10216, check if only merging into existing mediapackage
            removePublicationElement(mediaPackage);
            switch(republishStrategy) {
                case ("merge"):
                    // merge() returns merged mediapackage or null mediaPackage is not published
                    mediaPackageForSearch = merge(mediaPackageForSearch);
                    if (mediaPackageForSearch == null) {
                        logger.info("Skipping republish for {} since it is not currently published", mediaPackage.getIdentifier().toString());
                        return createResult(mediaPackage, Action.SKIP);
                    }
                    break;
                default:
            }
            if (!isPublishable(mediaPackageForSearch))
                throw new WorkflowOperationException("Media package does not meet criteria for publication");
            logger.info("Publishing media package {} to search index", mediaPackageForSearch);
            URL engageBaseUrl = null;
            engageUrlString = StringUtils.trimToNull(workflowInstance.getOrganization().getProperties().get(ENGAGE_URL_PROPERTY));
            if (engageUrlString != null) {
                engageBaseUrl = new URL(engageUrlString);
            } else {
                engageBaseUrl = serverUrl;
                logger.info("Using 'server.url' as a fallback for the non-existing organization level key '{}' for the publication url", ENGAGE_URL_PROPERTY);
            }
            // create the publication URI (used by Admin UI for event details link)
            URI engageUri = this.createEngageUri(engageBaseUrl.toURI(), mediaPackage);
            // Create new distribution element
            Publication publicationElement = PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_ID, engageUri, MimeTypes.parseMimeType("text/html"));
            mediaPackage.add(publicationElement);
            // Adding media package to the search index
            Job publishJob = null;
            try {
                publishJob = searchService.add(mediaPackageForSearch);
                if (!waitForStatus(publishJob).isSuccess()) {
                    throw new WorkflowOperationException("Mediapackage " + mediaPackageForSearch.getIdentifier() + " could not be published");
                }
            } catch (SearchException e) {
                throw new WorkflowOperationException("Error publishing media package", e);
            } catch (MediaPackageException e) {
                throw new WorkflowOperationException("Error parsing media package", e);
            }
            logger.debug("Publishing of mediapackage {} completed", mediaPackage);
            return createResult(mediaPackage, Action.CONTINUE);
        } catch (MalformedURLException e) {
            logger.error("{} is malformed: {}", ENGAGE_URL_PROPERTY, engageUrlString);
            throw new WorkflowOperationException(e);
        } catch (Throwable t) {
            if (t instanceof WorkflowOperationException)
                throw (WorkflowOperationException) t;
            else
                throw new WorkflowOperationException(t);
        }
    } catch (Exception e) {
        if (e instanceof WorkflowOperationException) {
            throw (WorkflowOperationException) e;
        } else {
            throw new WorkflowOperationException(e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) SearchException(org.opencastproject.search.api.SearchException) Attachment(org.opencastproject.mediapackage.Attachment) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) URI(java.net.URI) URL(java.net.URL) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) Publication(org.opencastproject.mediapackage.Publication) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) SearchException(org.opencastproject.search.api.SearchException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) MalformedURLException(java.net.MalformedURLException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DistributionException(org.opencastproject.distribution.api.DistributionException)

Aggregations

SearchException (org.opencastproject.search.api.SearchException)11 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)9 HttpResponse (org.apache.http.HttpResponse)5 Job (org.opencastproject.job.api.Job)5 ArrayList (java.util.ArrayList)4 NotFoundException (org.opencastproject.util.NotFoundException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 DistributionException (org.opencastproject.distribution.api.DistributionException)3 MediaPackage (org.opencastproject.mediapackage.MediaPackage)3 URI (java.net.URI)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 Attachment (org.opencastproject.mediapackage.Attachment)2 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)2 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)2 SearchQuery (org.opencastproject.search.api.SearchQuery)2 SearchResult (org.opencastproject.search.api.SearchResult)2 SearchServiceDatabaseException (org.opencastproject.search.impl.persistence.SearchServiceDatabaseException)2