Search in sources :

Example 66 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class SourceOperations method getFanoutSourceInfo.

/**
 * Retrieves the {@link SourceDescriptor} info for all {@link FederatedSource}s in the fanout
 * configuration, but the all of the source info, e.g., content types, for all of the available
 * {@link FederatedSource}s is packed into one {@link SourceDescriptor} for the fanout
 * configuration with the fanout's site name in it. This keeps the individual {@link
 * FederatedSource}s' source info hidden from the external client.
 */
private SourceInfoResponse getFanoutSourceInfo(SourceInfoRequest sourceInfoRequest) throws SourceUnavailableException {
    SourceInfoResponse response;
    SourceDescriptorImpl sourceDescriptor;
    try {
        // request
        if (sourceInfoRequest == null) {
            throw new IllegalArgumentException("SourceInfoRequest was null");
        }
        Set<SourceDescriptor> sourceDescriptors = new LinkedHashSet<>();
        Set<String> ids = sourceInfoRequest.getSourceIds();
        // specified
        if (ids != null) {
            Optional<String> notLocal = ids.stream().filter(s -> !s.equals(getId())).findFirst();
            if (notLocal.isPresent()) {
                SourceUnavailableException sourceUnavailableException = new SourceUnavailableException("Unknown source: " + notLocal.get());
                LOGGER.debug("Throwing SourceUnavailableException for unknown source: {}", notLocal.get(), sourceUnavailableException);
                throw sourceUnavailableException;
            }
        }
        // Fanout will only add one source descriptor with all the contents
        // Using a List here instead of a Set because we should not rely on how Sources are compared
        final List<Source> availableSources = frameworkProperties.getFederatedSources().stream().filter(this::isSourceAvailable).collect(Collectors.toList());
        final Set<ContentType> contentTypes = availableSources.stream().map(source -> contentTypesCache.getCachedValueForSource(source).orElseGet(() -> {
            LOGGER.debug("Unknown content types for source id={}", source.getId());
            return Collections.emptySet();
        })).flatMap(Collection::stream).collect(Collectors.toSet());
        if (isSourceAvailable(catalog)) {
            availableSources.add(catalog);
            final Optional<Set<ContentType>> catalogContentTypes = contentTypesCache.getCachedValueForSource(catalog);
            if (catalogContentTypes.isPresent()) {
                contentTypes.addAll(catalogContentTypes.get());
            } else {
                LOGGER.debug("Unknown content types for the localSource");
            }
        }
        List<Action> actions = getSourceActions(catalog);
        // only reveal this sourceDescriptor, not the federated sources
        sourceDescriptor = new SourceDescriptorImpl(this.getId(), contentTypes, actions);
        if (this.getVersion() != null) {
            sourceDescriptor.setVersion(this.getVersion());
        }
        sourceDescriptor.setAvailable(!availableSources.isEmpty());
        sourceDescriptors.add(sourceDescriptor);
        response = new SourceInfoResponseImpl(sourceInfoRequest, null, sourceDescriptors);
    } catch (RuntimeException re) {
        throw new SourceUnavailableException(GET_SOURCE_EXCEPTION_MSG, re);
    }
    return response;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceStatus(org.codice.ddf.catalog.sourcepoller.SourceStatus) LoggerFactory(org.slf4j.LoggerFactory) TreeSet(java.util.TreeSet) Source(ddf.catalog.source.Source) SourceDescriptor(ddf.catalog.source.SourceDescriptor) HashSet(java.util.HashSet) Action(ddf.action.Action) StorageProvider(ddf.catalog.content.StorageProvider) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) Describable(ddf.catalog.util.Describable) SourceDescriptorComparator(ddf.catalog.util.impl.SourceDescriptorComparator) LinkedHashSet(java.util.LinkedHashSet) DescribableImpl(ddf.catalog.util.impl.DescribableImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) ContentType(ddf.catalog.data.ContentType) SourcePoller(org.codice.ddf.catalog.sourcepoller.SourcePoller) Logger(org.slf4j.Logger) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) FederatedSource(ddf.catalog.source.FederatedSource) Collection(java.util.Collection) Set(java.util.Set) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) Collectors(java.util.stream.Collectors) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) List(java.util.List) CatalogProvider(ddf.catalog.source.CatalogProvider) Optional(java.util.Optional) ActionRegistry(ddf.action.ActionRegistry) Collections(java.util.Collections) Validate(org.apache.commons.lang.Validate) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) Action(ddf.action.Action) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ContentType(ddf.catalog.data.ContentType) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse)

Example 67 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class SourceOperations method getSourceInfo.

public SourceInfoResponse getSourceInfo(SourceInfoRequest sourceInfoRequest, boolean fanoutEnabled) throws SourceUnavailableException {
    SourceInfoResponse response;
    Set<SourceDescriptor> sourceDescriptors;
    if (fanoutEnabled) {
        return getFanoutSourceInfo(sourceInfoRequest);
    }
    boolean addCatalogProviderDescriptor = false;
    try {
        validateSourceInfoRequest(sourceInfoRequest);
        // Obtain the source information based on the sourceIds in the
        // request
        sourceDescriptors = new LinkedHashSet<>();
        Set<String> requestedSourceIds = sourceInfoRequest.getSourceIds();
        // If it is an enterprise request than add all source information for the enterprise
        if (sourceInfoRequest.isEnterprise()) {
            sourceDescriptors = getFederatedSourceDescriptors(frameworkProperties.getFederatedSources(), true);
        // If Ids are specified check if they are known sources
        } else if (requestedSourceIds != null) {
            LOGGER.debug("getSourceRequest contains requested source ids");
            Set<FederatedSource> discoveredSources = new HashSet<>();
            for (String requestedSourceId : requestedSourceIds) {
                // Check if the requestedSourceId can be found in the known federatedSources
                final List<FederatedSource> sources = frameworkProperties.getFederatedSources().stream().filter(e -> e.getId().equals(requestedSourceId)).collect(Collectors.toList());
                if (!sources.isEmpty()) {
                    String logMsg = (sources.size() == 1) ? "Found federated source: {}" : "Multiple FederatedSources found for id: {}";
                    LOGGER.debug(logMsg, requestedSourceId);
                    discoveredSources.add(sources.get(0));
                } else {
                    LOGGER.debug("Unable to find source: {}", requestedSourceId);
                    // Check for the local catalog provider, DDF sourceId represents this
                    if (requestedSourceId.equals(getId())) {
                        LOGGER.debug("adding CatalogSourceDescriptor since it was in sourceId list as: {}", requestedSourceId);
                        addCatalogProviderDescriptor = true;
                    }
                }
            }
            sourceDescriptors = getFederatedSourceDescriptors(discoveredSources, addCatalogProviderDescriptor);
        } else {
            // only add the local catalogProviderDescriptor
            addCatalogSourceDescriptor(sourceDescriptors);
        }
        response = new SourceInfoResponseImpl(sourceInfoRequest, null, sourceDescriptors);
    } catch (RuntimeException re) {
        LOGGER.debug(GET_SOURCE_EXCEPTION_MSG, re);
        throw new SourceUnavailableException(GET_SOURCE_EXCEPTION_MSG);
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) List(java.util.List) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse)

Example 68 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class GazetteerQueryCatalog method getSuggestedNames.

@Override
public List<Suggestion> getSuggestedNames(String queryString, int maxResults) throws GeoEntryQueryException {
    Map<String, Serializable> suggestProps = new HashMap<>();
    suggestProps.put(SUGGESTION_QUERY_KEY, queryString);
    suggestProps.put(SUGGESTION_CONTEXT_KEY, GAZETTEER_METACARD_TAG);
    suggestProps.put(SUGGESTION_DICT_KEY, SUGGEST_PLACE_KEY);
    Query suggestionQuery = new QueryImpl(filterBuilder.attribute(Core.TITLE).text(queryString));
    QueryRequest suggestionRequest = new QueryRequestImpl(suggestionQuery, suggestProps);
    try {
        QueryResponse suggestionResponse = catalogFramework.query(suggestionRequest);
        if (suggestionResponse.getPropertyValue(SUGGESTION_RESULT_KEY) instanceof List) {
            List<Map.Entry<String, String>> suggestions = (List<Map.Entry<String, String>>) suggestionResponse.getPropertyValue(SUGGESTION_RESULT_KEY);
            return suggestions.stream().map(suggestion -> new SuggestionImpl(suggestion.getKey(), suggestion.getValue())).limit(maxResults).collect(Collectors.toList());
        }
    } catch (SourceUnavailableException | FederationException | UnsupportedQueryException e) {
        throw new GeoEntryQueryException("Failed to execute suggestion query", e);
    }
    return Collections.emptyList();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) SuggestionImpl(org.codice.ddf.spatial.geocoding.context.impl.SuggestionImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 69 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class CatalogFeatureQueryable method query.

@Override
public List<SimpleFeature> query(String queryString, String featureCode, int maxResults) throws FeatureQueryException {
    Validate.notNull(queryString, "queryString can't be null");
    if (maxResults < 0) {
        throw new IllegalArgumentException("maxResults can't be negative");
    }
    Query query = catalogHelper.getQueryForName(queryString);
    QueryRequest queryRequest = new QueryRequestImpl(query);
    SourceResponse response;
    try {
        response = catalogFramework.query(queryRequest);
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        throw new FeatureQueryException("Failed to query catalog", e);
    }
    return response.getResults().stream().map(Result::getMetacard).map(this::getFeatureForMetacard).filter(Objects::nonNull).limit(maxResults).collect(Collectors.toList());
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) Objects(java.util.Objects) FederationException(ddf.catalog.federation.FederationException) FeatureQueryException(org.codice.ddf.spatial.geocoding.FeatureQueryException) Result(ddf.catalog.data.Result)

Example 70 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class GeoNamesCatalogIndexer method executeCreateMetacardRequest.

private void executeCreateMetacardRequest(List<Metacard> metacards) {
    int totalMetacards = metacards.size();
    for (int i = 0; i < totalMetacards; i += BATCH_SIZE) {
        int lastIndex = i + BATCH_SIZE;
        if (lastIndex > metacards.size()) {
            lastIndex = metacards.size();
        }
        List<Metacard> sublist = metacards.subList(i, lastIndex);
        Map<String, Serializable> properties = new HashMap<>();
        CreateRequest createRequest = new CreateRequestImpl(sublist, properties);
        try {
            CreateResponse createResponse = catalogFramework.create(createRequest);
            List<Metacard> createdMetacards = createResponse.getCreatedMetacards();
            LOGGER.trace("Created {} metacards.", createdMetacards == null ? 0 : createdMetacards.size());
        } catch (IngestException | SourceUnavailableException e) {
            LOGGER.debug("Unable to create Metacards", e);
        }
    }
    LOGGER.trace("Created {} metacards.", totalMetacards);
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) HashMap(java.util.HashMap) CreateRequest(ddf.catalog.operation.CreateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Metacard(ddf.catalog.data.Metacard) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException)

Aggregations

SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)87 FederationException (ddf.catalog.federation.FederationException)39 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)38 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)33 Metacard (ddf.catalog.data.Metacard)30 IngestException (ddf.catalog.source.IngestException)30 QueryResponse (ddf.catalog.operation.QueryResponse)29 QueryImpl (ddf.catalog.operation.impl.QueryImpl)28 ArrayList (java.util.ArrayList)26 Test (org.junit.Test)26 QueryRequest (ddf.catalog.operation.QueryRequest)24 CatalogFramework (ddf.catalog.CatalogFramework)22 HashMap (java.util.HashMap)19 Result (ddf.catalog.data.Result)18 Filter (org.opengis.filter.Filter)18 CreateResponse (ddf.catalog.operation.CreateResponse)17 List (java.util.List)16 Map (java.util.Map)16 ContentType (ddf.catalog.data.ContentType)14 IOException (java.io.IOException)14