Search in sources :

Example 11 with ResourceRequestById

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

the class XmlAttributeSecurityPolicyPluginTest method testProcessUnusedMethods.

@Test
public void testProcessUnusedMethods() throws StopProcessingException {
    PolicyResponse policyResponse = plugin.processPreQuery(new QueryImpl(Filter.INCLUDE), new HashMap<>());
    org.junit.Assert.assertThat(policyResponse.itemPolicy().entrySet().size(), Matchers.is(0));
    policyResponse = plugin.processPreResource(new ResourceRequestById(""));
    org.junit.Assert.assertThat(policyResponse.itemPolicy().entrySet().size(), Matchers.is(0));
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) PolicyResponse(ddf.catalog.plugin.PolicyResponse) Test(org.junit.Test)

Example 12 with ResourceRequestById

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

the class MetacardResourceSizePlugin method process.

@Override
public QueryResponse process(QueryResponse input) throws PluginExecutionException, StopProcessingException {
    List<Result> results = input.getResults();
    for (Result result : results) {
        Metacard metacard = result.getMetacard();
        if (metacard != null) {
            // Can only search cache based on Metacard - no way to generate ResourceRequest with
            // any properties for use in generating the CacheKey
            final ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
            CacheKey cacheKey;
            String key = null;
            ReliableResource cachedResource = null;
            try {
                cacheKey = new CacheKey(metacard, resourceRequest);
                ClassLoader tccl = Thread.currentThread().getContextClassLoader();
                key = cacheKey.generateKey();
                try {
                    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
                    cachedResource = (ReliableResource) cache.getValid(key, metacard);
                } finally {
                    Thread.currentThread().setContextClassLoader(tccl);
                }
            } catch (IllegalArgumentException e) {
                LOGGER.debug("Unable to retrieve cached resource for metacard id = {}", metacard.getId());
            }
            if (cachedResource != null) {
                long resourceSize = cachedResource.getSize();
                if (resourceSize > 0 && cachedResource.hasProduct()) {
                    LOGGER.debug("Setting resourceSize = {} for metacard ID = {}", resourceSize, metacard.getId());
                    Attribute resourceSizeAttribute = new AttributeImpl(Metacard.RESOURCE_SIZE, String.valueOf(resourceSize));
                    metacard.setAttribute(resourceSizeAttribute);
                } else {
                    LOGGER.debug("resourceSize <= 0 for metacard ID = {}", metacard.getId());
                }
            } else {
                LOGGER.debug("No cached resource for cache key = {}", key);
            }
        }
    }
    return input;
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ReliableResource(ddf.catalog.resource.data.ReliableResource) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 13 with ResourceRequestById

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

the class ResourceCacheImplTest method getSpecificResourceInCache.

@Test
public void getSpecificResourceInCache() {
    ReliableResource cachedResource = createCachedResource(cachedMetacard);
    resourceCache.put(cachedResource);
    Optional<Resource> optionalResource = newResourceCache.get(cachedMetacard, new ResourceRequestById(METACARD_ID));
    assertTrue(optionalResource.isPresent());
    assertTrue(assertReliableResourceEquals(cachedResource, optionalResource.get()));
}
Also used : ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ReliableResource(ddf.catalog.resource.data.ReliableResource) Test(org.junit.Test)

Example 14 with ResourceRequestById

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

the class ResourceMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
    if (!isValid(metacard)) {
        throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
    }
    if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
        arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
    }
    String id = metacard.getId();
    LOGGER.debug("executing resource request with id '{}'", id);
    final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
    ResourceResponse resourceResponse = null;
    String sourceName = metacard.getSourceId();
    if (StringUtils.isBlank(sourceName)) {
        sourceName = catalogFramework.getId();
    }
    String resourceUriAscii = "";
    if (metacard.getResourceURI() != null) {
        resourceUriAscii = metacard.getResourceURI().toASCIIString();
    }
    try {
        resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
    } catch (IOException e) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
    } catch (ResourceNotFoundException e) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
    } catch (ResourceNotSupportedException e) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
    }
    if (resourceResponse == null) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
    }
    Resource transformedContent = resourceResponse.getResource();
    MimeType mimeType = transformedContent.getMimeType();
    if (mimeType == null) {
        try {
            mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
            // There is no method to set the MIME type, so in order to set it to our default
            // one, we need to create a new object.
            transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
        } catch (MimeTypeParseException e) {
            throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
        }
    }
    LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
    LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
    return transformedContent;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ResourceRequest(ddf.catalog.operation.ResourceRequest) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) MimeType(javax.activation.MimeType)

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