Search in sources :

Example 61 with AttributeImpl

use of ddf.catalog.data.impl.AttributeImpl in project ddf by codice.

the class RESTEndpoint method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, String id, InputStream message, String transformerId) throws MetacardCreationException {
    Metacard generatedMetacard = null;
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.trace("Entering generateMetacard.");
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            if (null != message) {
                IOUtils.copy(message, fileBackedOutputStream);
            } else {
                throw new MetacardCreationException("Could not copy bytes of content message.  Message was NULL.");
            }
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        Iterator<InputTransformer> it = listOfCandidates.iterator();
        if (StringUtils.isNotEmpty(transformerId)) {
            BundleContext bundleContext = getBundleContext();
            Collection<ServiceReference<InputTransformer>> serviceReferences = bundleContext.getServiceReferences(InputTransformer.class, "(id=" + transformerId + ")");
            it = serviceReferences.stream().map(bundleContext::getService).iterator();
        }
        while (it.hasNext()) {
            InputTransformer transformer = it.next();
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (CatalogTransformerException | IOException e) {
                List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
                stackTraceList.add(String.format("Transformer [%s] could not create metacard.", transformer));
                stackTraceList.addAll(stackTraces);
                LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeType, StringUtils.join(stackTraceList, "\n")));
        }
        if (id != null) {
            generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
        } else {
            LOGGER.debug("Metacard had a null id");
        }
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    } catch (InvalidSyntaxException e) {
        throw new MetacardCreationException("Could not determine transformer", e);
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 62 with AttributeImpl

use of ddf.catalog.data.impl.AttributeImpl in project ddf by codice.

the class FederationAdminServiceImplTest method testGetRegistryObjectsWithEmptyMetadata.

@Test(expected = FederationAdminException.class)
public void testGetRegistryObjectsWithEmptyMetadata() throws Exception {
    Metacard metacard = getTestMetacard();
    metacard.setAttribute(new AttributeImpl(Metacard.METADATA, ""));
    QueryRequest request = getTestQueryRequest();
    QueryResponse response = getPopulatedTestQueryResponse(request, metacard);
    when(catalogFramework.query(any(QueryRequest.class))).thenReturn(response);
    federationAdminServiceImpl.getRegistryObjects();
    verify(catalogFramework).query(any(QueryRequest.class));
    verify(parser, never()).unmarshal(any(ParserConfigurator.class), eq(JAXBElement.class), any(InputStream.class));
}
Also used : ParserConfigurator(org.codice.ddf.parser.ParserConfigurator) Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) QueryResponse(ddf.catalog.operation.QueryResponse) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 63 with AttributeImpl

use of ddf.catalog.data.impl.AttributeImpl in project ddf by codice.

the class FederationAdminServiceImplTest method testGetRegistryMetacards.

@Test
public void testGetRegistryMetacards() throws Exception {
    Metacard findThisMetacard = testMetacard;
    findThisMetacard.setAttribute(new AttributeImpl(RegistryObjectMetacardType.REGISTRY_IDENTITY_NODE, true));
    QueryRequest request = getTestQueryRequest();
    QueryResponse response = getPopulatedTestQueryResponse(request, findThisMetacard, getTestMetacard());
    when(security.getSystemSubject()).thenReturn(subject);
    when(catalogFramework.query(any(QueryRequest.class))).thenReturn(response);
    List<Metacard> metacards = federationAdminServiceImpl.getRegistryMetacards();
    assertThat(metacards, hasSize(2));
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Test(org.junit.Test)

Example 64 with AttributeImpl

use of ddf.catalog.data.impl.AttributeImpl in project ddf by codice.

the class FederationAdminServiceImplTest method getPopulatedRemoteTestRegistryMetacard.

private Metacard getPopulatedRemoteTestRegistryMetacard() {
    Metacard mcard = getPopulatedTestRegistryMetacard();
    mcard.setAttribute(new AttributeImpl(RegistryObjectMetacardType.REMOTE_REGISTRY_ID, "RemoteRegId"));
    return mcard;
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeImpl(ddf.catalog.data.impl.AttributeImpl)

Example 65 with AttributeImpl

use of ddf.catalog.data.impl.AttributeImpl in project ddf by codice.

the class FederationAdminServiceImplTest method testAddRegistryEntryWithInvalidMetacardNoRegistryTag.

@Test(expected = FederationAdminException.class)
public void testAddRegistryEntryWithInvalidMetacardNoRegistryTag() throws Exception {
    String destination = TEST_DESTINATION;
    Metacard metacard = getTestMetacard();
    metacard.setAttribute(new AttributeImpl(Metacard.TAGS, Collections.singletonList(null)));
    Set<String> destinations = new HashSet<>();
    destinations.add(destination);
    federationAdminServiceImpl.addRegistryEntry(metacard, destinations);
    verify(catalogFramework, never()).create(any(CreateRequest.class));
}
Also used : Metacard(ddf.catalog.data.Metacard) CreateRequest(ddf.catalog.operation.CreateRequest) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AttributeImpl (ddf.catalog.data.impl.AttributeImpl)181 Metacard (ddf.catalog.data.Metacard)109 Test (org.junit.Test)75 ArrayList (java.util.ArrayList)56 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)49 Serializable (java.io.Serializable)30 Date (java.util.Date)30 Attribute (ddf.catalog.data.Attribute)29 List (java.util.List)23 HashMap (java.util.HashMap)20 IOException (java.io.IOException)18 InputStream (java.io.InputStream)17 Result (ddf.catalog.data.Result)15 HashSet (java.util.HashSet)15 PolicyResponse (ddf.catalog.plugin.PolicyResponse)14 ResultImpl (ddf.catalog.data.impl.ResultImpl)11 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)11 Set (java.util.Set)11 ContentItem (ddf.catalog.content.data.ContentItem)10 QueryResponse (ddf.catalog.operation.QueryResponse)10