Search in sources :

Example 46 with SourceUnavailableException

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

the class DuplicateCommands method ingestSingly.

private List<Metacard> ingestSingly(CatalogFacade provider, List<Metacard> metacards) {
    if (metacards.isEmpty()) {
        return Collections.emptyList();
    }
    List<Metacard> createdMetacards = new ArrayList<>();
    LOGGER.debug("Preparing to ingest {} records one at time.", metacards.size());
    for (Metacard metacard : metacards) {
        CreateRequest createRequest = new CreateRequestImpl(Arrays.asList(metacard));
        CreateResponse createResponse;
        try {
            createResponse = provider.create(createRequest);
            createdMetacards.addAll(createResponse.getCreatedMetacards());
        } catch (IngestException | SourceUnavailableException e) {
            LOGGER.debug("Error during ingest:", e);
        } catch (Exception e) {
            LOGGER.debug("Unexpected Exception during ingest:", e);
        }
    }
    return createdMetacards;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Metacard(ddf.catalog.data.Metacard) CreateRequest(ddf.catalog.operation.CreateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) ArrayList(java.util.ArrayList) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException)

Example 47 with SourceUnavailableException

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

the class DuplicateCommands method ingestMetacards.

protected List<Metacard> ingestMetacards(CatalogFacade provider, List<Metacard> metacards) {
    if (metacards.isEmpty()) {
        return Collections.emptyList();
    }
    List<Metacard> createdMetacards = new ArrayList<>();
    LOGGER.debug("Preparing to ingest {} records", metacards.size());
    CreateRequest createRequest = new CreateRequestImpl(metacards);
    CreateResponse createResponse;
    try {
        createResponse = provider.create(createRequest);
        createdMetacards = createResponse.getCreatedMetacards();
    } catch (IngestException e) {
        printErrorMessage(String.format("Received error while ingesting: %s%n", e.getMessage()));
        LOGGER.debug("Error during ingest. Attempting to ingest batch individually.");
        return ingestSingly(provider, metacards);
    } catch (SourceUnavailableException e) {
        printErrorMessage(String.format("Received error while ingesting: %s%n", e.getMessage()));
        LOGGER.debug("Error during ingest:", e);
        return createdMetacards;
    } catch (Exception e) {
        printErrorMessage(String.format("Unexpected Exception received while ingesting: %s%n", e.getMessage()));
        LOGGER.debug("Unexpected Exception during ingest:", e);
        return createdMetacards;
    }
    ingestedCount.addAndGet(createdMetacards.size());
    failedCount.addAndGet(metacards.size() - createdMetacards.size());
    failedMetacards.addAll(subtract(metacards, createdMetacards));
    return createdMetacards;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Metacard(ddf.catalog.data.Metacard) CreateRequest(ddf.catalog.operation.CreateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) ArrayList(java.util.ArrayList) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException)

Example 48 with SourceUnavailableException

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

the class IngestCommand method processBatch.

private boolean processBatch(CatalogFacade catalog, ArrayList<Metacard> metacards) throws SourceUnavailableException {
    CreateResponse createResponse = null;
    try {
        createResponse = createMetacards(catalog, metacards);
    } catch (IngestException e) {
        printErrorMessage("Error executing command: " + e.getMessage());
        if (INGEST_LOGGER.isWarnEnabled()) {
            INGEST_LOGGER.warn("Error ingesting metacard batch {}", buildIngestLog(metacards), e);
        }
    } catch (SourceUnavailableException e) {
        if (INGEST_LOGGER.isWarnEnabled()) {
            INGEST_LOGGER.warn("Error on process batch, local Provider not available. {}" + " metacards failed to ingest. {}", metacards.size(), buildIngestLog(metacards), e);
        }
    } finally {
        IntStream range = IntStream.range(0, metacards.size());
        range.forEach(i -> phaser.arriveAndDeregister());
        range.close();
    }
    if (createResponse != null) {
        ingestCount.getAndAdd(metacards.size());
    }
    return createResponse != null;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CreateResponse(ddf.catalog.operation.CreateResponse) IngestException(ddf.catalog.source.IngestException) IntStream(java.util.stream.IntStream)

Example 49 with SourceUnavailableException

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

the class RemoveCommand method executeRemoveFromStore.

private Object executeRemoveFromStore() throws CatalogCommandException {
    try {
        int batchCount = 0;
        int deletedCount = 0;
        if (CollectionUtils.isNotEmpty(ids) && !hasFilter()) {
            deletedCount = deletedIdsPassedAsArguments();
        }
        if (hasFilter()) {
            QueryRequestImpl queryRequest = new QueryRequestImpl(new QueryImpl(getFilter()), false);
            String[] idsToDelete = getNextQueryBatch(queryRequest);
            while (idsToDelete.length > 0) {
                if (CollectionUtils.isNotEmpty(ids)) {
                    idsToDelete = Arrays.asList(idsToDelete).stream().filter(id -> ids.contains(id)).toArray(String[]::new);
                }
                DeleteRequestImpl deleteRequest = new DeleteRequestImpl(idsToDelete);
                LOGGER.debug("Attempting to delete {} metacards from batch {}", idsToDelete.length, ++batchCount);
                DeleteResponse deleteResponse = catalogFramework.delete(deleteRequest);
                deletedCount += deleteResponse.getDeletedMetacards().size();
                idsToDelete = getNextQueryBatch(queryRequest);
            }
        }
        if (deletedCount > 0) {
            printSuccessMessage(deletedCount + " documents successfully deleted.");
            LOGGER.debug("{} documents removed using catalog:remove command", deletedCount);
        } else {
            printErrorMessage("No documents match provided IDs or filter");
            LOGGER.debug("No documents deleted using the catalog:remove command");
        }
    } catch (IngestException | SourceUnavailableException | ParseException | CQLException e) {
        throw new CatalogCommandException("Error executing catalog:remove", e);
    }
    return null;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) CatalogCommandException(org.codice.ddf.commands.util.CatalogCommandException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DeleteResponse(ddf.catalog.operation.DeleteResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) IngestException(ddf.catalog.source.IngestException) ParseException(java.text.ParseException) CQLException(org.geotools.filter.text.cql2.CQLException)

Example 50 with SourceUnavailableException

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

the class OpenSearchEndpoint method executeQuery.

/**
 * Executes the OpenSearchQuery and formulates the response
 *
 * @param format - of the results in the response
 * @param query - the query to execute
 * @param ui -the ui information to use to format the results
 * @return the response on the query
 */
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
    Response response = null;
    String queryFormat = format;
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
    List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
    LOGGER.trace("Attempting to execute query: {}", query);
    try {
        Map<String, Serializable> arguments = new HashMap<>();
        String organization = framework.getOrganization();
        String url = ui.getRequestUri().toString();
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("organization: {}", organization);
            LOGGER.trace("url: {}", url);
        }
        arguments.put("organization", organization);
        arguments.put("url", url);
        // interval
        if (CollectionUtils.isNotEmpty(subscriptionList)) {
            String subscription = subscriptionList.get(0);
            LOGGER.trace("Subscription: {}", subscription);
            arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
            List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
            if (CollectionUtils.isNotEmpty(intervalList)) {
                arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
            }
        }
        if (StringUtils.isEmpty(queryFormat)) {
            queryFormat = DEFAULT_FORMAT;
        }
        if (query.getFilter() != null) {
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
            QueryResponse queryResponse;
            LOGGER.trace("Sending query");
            queryResponse = framework.query(queryRequest);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
            response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
        } else {
            // No query was specified
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
            // Create a dummy QueryResponse with zero results
            QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<>(), 0);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
            if (null != content) {
                response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
            }
        }
    } catch (UnsupportedQueryException ce) {
        LOGGER.debug("Unsupported query", ce);
        response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
    } catch (CatalogTransformerException e) {
        LOGGER.debug("Error transforming response", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
    } catch (FederationException e) {
        LOGGER.debug("Error executing query", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
    } catch (SourceUnavailableException e) {
        LOGGER.debug("Error executing query because the underlying source was unavailable.", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
    } catch (RuntimeException e) {
        // Account for any runtime exceptions and send back a server error
        // this prevents full stacktraces returning to the client
        // this allows for a graceful server error to be returned
        LOGGER.debug("RuntimeException on executing query", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) FederationException(ddf.catalog.federation.FederationException) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

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