Search in sources :

Example 6 with ResourceRequestById

use of ddf.catalog.operation.impl.ResourceRequestById in project ddf by codice.

the class ResourceCacheImplTest method getSpecificResourceNotInCache.

@Test
public void getSpecificResourceNotInCache() {
    Optional<Resource> optionalResource = newResourceCache.get(notCachedMetacard, new ResourceRequestById(NOT_CACHED_METACARD_ID));
    assertFalse(optionalResource.isPresent());
}
Also used : ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) Test(org.junit.Test)

Example 7 with ResourceRequestById

use of ddf.catalog.operation.impl.ResourceRequestById in project ddf by codice.

the class MetacardApplication method revertContentandMetacard.

private void revertContentandMetacard(Metacard latestContent, Metacard versionMetacard, String id) throws SourceUnavailableException, IngestException, ResourceNotFoundException, IOException, ResourceNotSupportedException, FederationException, UnsupportedQueryException {
    LOGGER.trace("Reverting content and metacard for metacard [{}]. \nLatest content: [{}] \nVersion metacard: [{}]", id, latestContent.getId(), versionMetacard.getId());
    Map<String, Serializable> properties = new HashMap<>();
    properties.put("no-default-tags", true);
    ResourceResponse latestResource = catalogFramework.getLocalResource(new ResourceRequestById(latestContent.getId(), properties));
    ContentItemImpl contentItem = new ContentItemImpl(id, new ByteSourceWrapper(() -> latestResource.getResource().getInputStream()), latestResource.getResource().getMimeTypeValue(), latestResource.getResource().getName(), latestResource.getResource().getSize(), MetacardVersionImpl.toMetacard(versionMetacard, types));
    // Try to delete the "deleted metacard" marker first.
    boolean alreadyCreated = false;
    Action action = Action.fromKey((String) versionMetacard.getAttribute(MetacardVersion.ACTION).getValue());
    if (DELETE_ACTIONS.contains(action)) {
        alreadyCreated = true;
        catalogFramework.create(new CreateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>()));
    } else {
        // Currently we can't guarantee the metacard will exist yet because of the 1 second
        // soft commit in solr. this busy wait loop should be fixed when alternate solution
        // is found.
        tryUpdate(4, () -> {
            catalogFramework.update(new UpdateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>()));
            return true;
        });
    }
    LOGGER.trace("Successfully reverted metacard content for [{}]", id);
    revertMetacard(versionMetacard, id, alreadyCreated);
}
Also used : Serializable(java.io.Serializable) Action(ddf.catalog.core.versioning.MetacardVersion.Action) ResourceResponse(ddf.catalog.operation.ResourceResponse) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) HashMap(java.util.HashMap) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 8 with ResourceRequestById

use of ddf.catalog.operation.impl.ResourceRequestById in project ddf by codice.

the class ZipCompression method getResource.

private Resource getResource(Metacard metacard) {
    Resource resource = null;
    try {
        ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
        ResourceResponse resourceResponse = catalogFramework.getLocalResource(resourceRequest);
        resource = resourceResponse.getResource();
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        LOGGER.debug("Unable to retrieve content from metacard : {}", metacard.getId(), e);
    }
    return resource;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Resource(ddf.catalog.resource.Resource) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 9 with ResourceRequestById

use of ddf.catalog.operation.impl.ResourceRequestById in project ddf by codice.

the class SeedCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    if (resourceLimit <= 0) {
        printErrorMessage("A limit of " + resourceLimit + " was supplied. The limit must be greater than 0.");
        return null;
    }
    final long start = System.currentTimeMillis();
    int resourceDownloads = 0;
    int downloadErrors = 0;
    int pageCount = 0;
    while (resourceDownloads < resourceLimit) {
        final QueryImpl query = new QueryImpl(getFilter());
        query.setPageSize(resourceLimit);
        query.setStartIndex(pageCount * resourceLimit + 1);
        ++pageCount;
        final QueryRequestImpl queryRequest;
        if (isNotEmpty(sources)) {
            queryRequest = new QueryRequestImpl(query, sources);
        } else {
            queryRequest = new QueryRequestImpl(query, true);
        }
        queryRequest.setProperties(new HashMap<>(CACHE_UPDATE_PROPERTIES));
        final QueryResponse queryResponse = catalogFramework.query(queryRequest);
        if (queryResponse.getResults().isEmpty()) {
            break;
        }
        final List<Entry<? extends ResourceRequest, String>> resourceRequests = queryResponse.getResults().stream().map(Result::getMetacard).filter(isNonCachedResource).map(metacard -> new SimpleEntry<>(new ResourceRequestById(metacard.getId()), metacard.getSourceId())).collect(toList());
        for (Entry<? extends ResourceRequest, String> requestAndSourceId : resourceRequests) {
            final ResourceRequest request = requestAndSourceId.getKey();
            try {
                ResourceResponse response = catalogFramework.getResource(request, requestAndSourceId.getValue());
                ++resourceDownloads;
                EXECUTOR.execute(new ResourceCloseHandler(response));
            } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
                ++downloadErrors;
                LOGGER.debug("Could not download resource for metacard [id={}]", request.getAttributeValue(), e);
            }
            printProgressAndFlush(start, resourceLimit, resourceDownloads);
            if (resourceDownloads == resourceLimit) {
                break;
            }
        }
    }
    printProgressAndFlush(start, resourceDownloads, resourceDownloads);
    console.println();
    if (downloadErrors > 0) {
        printErrorMessage(downloadErrors + " resource download(s) had errors. Check the logs for details.");
    }
    printSuccessMessage("Done seeding. " + resourceDownloads + " resource download(s) started.");
    return null;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Command(org.apache.karaf.shell.api.action.Command) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) ResourceRequest(ddf.catalog.operation.ResourceRequest) SimpleEntry(java.util.AbstractMap.SimpleEntry) Result(ddf.catalog.data.Result) CollectionUtils.isNotEmpty(org.apache.commons.collections.CollectionUtils.isNotEmpty) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Serializable(java.io.Serializable) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) QueryResponse(ddf.catalog.operation.QueryResponse) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Attribute(ddf.catalog.data.Attribute) Entry(java.util.Map.Entry) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Option(org.apache.karaf.shell.api.action.Option) Collections(java.util.Collections) InputStream(java.io.InputStream) SimpleEntry(java.util.AbstractMap.SimpleEntry) IOException(java.io.IOException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SimpleEntry(java.util.AbstractMap.SimpleEntry) Entry(java.util.Map.Entry) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 10 with ResourceRequestById

use of ddf.catalog.operation.impl.ResourceRequestById in project ddf by codice.

the class OperationPluginTest method testPluginWithRole.

private void testPluginWithRole(String role) throws Exception {
    Map<String, Serializable> properties = new HashMap<>();
    properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    HashMap<String, Set<String>> perms = new HashMap<>();
    Set<String> roles = new HashSet<>();
    roles.add(role);
    perms.put("Roles", roles);
    properties.put(PolicyPlugin.OPERATION_SECURITY, perms);
    CreateRequestImpl request = new CreateRequestImpl(new ArrayList<>(), properties);
    QueryRequestImpl queryRequest = new QueryRequestImpl(mock(Query.class), properties);
    UpdateRequestImpl updateRequest = new UpdateRequestImpl(new ArrayList<>(), "", properties);
    DeleteRequestImpl deleteRequest = new DeleteRequestImpl(new String[] { "" }, properties);
    ResourceRequestById resourceRequestById = new ResourceRequestById("", properties);
    plugin.processPreCreate(request);
    plugin.processPreQuery(queryRequest);
    plugin.processPreUpdate(updateRequest, new HashMap<>());
    plugin.processPreDelete(deleteRequest);
    plugin.processPreResource(resourceRequestById);
}
Also used : Serializable(java.io.Serializable) HashSet(java.util.HashSet) Set(java.util.Set) Query(ddf.catalog.operation.Query) HashMap(java.util.HashMap) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) HashSet(java.util.HashSet)

Aggregations

ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)14 ResourceResponse (ddf.catalog.operation.ResourceResponse)7 ResourceRequest (ddf.catalog.operation.ResourceRequest)6 Resource (ddf.catalog.resource.Resource)6 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)6 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)6 IOException (java.io.IOException)6 Test (org.junit.Test)5 ReliableResource (ddf.catalog.resource.data.ReliableResource)4 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 Attribute (ddf.catalog.data.Attribute)2 Metacard (ddf.catalog.data.Metacard)2 Result (ddf.catalog.data.Result)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)2 PolicyResponse (ddf.catalog.plugin.PolicyResponse)2 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)2 MimeType (javax.activation.MimeType)2 CacheKey (ddf.catalog.cache.impl.CacheKey)1