Search in sources :

Example 1 with MetacardBackupException

use of org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException in project ddf by codice.

the class MetacardBackupPlugin method backupData.

private void backupData(BinaryContent content, String metacardId) throws PluginExecutionException {
    byte[] contentBytes = getContentBytes(content, metacardId);
    LOGGER.trace("Writing backup from {} to backup provider(s)", metacardId);
    for (MetacardBackupStorageProvider storageProvider : storageBackupPlugins) {
        if (metacardOutputProviderIds.contains(storageProvider.getId())) {
            try {
                storageProvider.store(metacardId, contentBytes);
            } catch (IOException | MetacardBackupException e) {
                LOGGER.debug("Unable to backup {} to backup provider: {}.", metacardId, storageProvider.getId(), e);
            }
        }
    }
}
Also used : MetacardBackupException(org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException) MetacardBackupStorageProvider(org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupStorageProvider) IOException(java.io.IOException)

Example 2 with MetacardBackupException

use of org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException in project ddf by codice.

the class MetacardBackupPluginTest method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    metacardTransformer = mock(MetacardTransformer.class);
    BinaryContent binaryContent = new BinaryContentImpl(new ByteArrayInputStream(XML_METADATA.getBytes(StandardCharsets.UTF_8)));
    when(metacardTransformer.transform(any(Metacard.class), anyMap())).thenReturn(binaryContent);
    doThrow(new MetacardBackupException("Not Implemented")).when(mockProvider).store(any(), any());
    doThrow(new MetacardBackupException("Not Implemented")).when(mockProvider).delete(any());
    doReturn(MOCK_ID).when(mockProvider).getId();
    metacardBackupPlugin = new MetacardBackupPlugin();
    metacardBackupPlugin.setMetacardTransformerId(METACARD_TRANSFORMER_ID);
    metacardBackupPlugin.setMetacardTransformer(metacardTransformer);
    createRequest = generateProcessRequest(ProcessCreateItem.class, true);
    updateRequest = generateProcessRequest(ProcessUpdateItem.class, true);
    deleteRequest = generateDeleteRequest();
    fileStorageProvider.setId(FILE_STORAGE_PROVIDER_ID);
    fileStorageProvider.setOutputDirectory(OUTPUT_DIRECTORY);
    metacardBackupPlugin.setMetacardOutputProviderIds(Collections.singletonList(FILE_STORAGE_PROVIDER_ID));
    metacardBackupPlugin.setStorageBackupPlugins(Arrays.asList(new MetacardBackupStorageProvider[] { fileStorageProvider }));
}
Also used : MetacardBackupException(org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException) Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessCreateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessCreateItem) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MetacardBackupStorageProvider(org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupStorageProvider) BinaryContent(ddf.catalog.data.BinaryContent) ProcessUpdateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessUpdateItem) Before(org.junit.Before)

Example 3 with MetacardBackupException

use of org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException in project ddf by codice.

the class MetacardBackupFileStorage method store.

@Override
public void store(String id, byte[] data) throws IOException, MetacardBackupException {
    if (StringUtils.isEmpty(outputDirectory)) {
        throw new MetacardBackupException("Unable to store data; no output directory specified.");
    }
    if (data == null) {
        throw new MetacardBackupException("No data to store");
    }
    Path metacardPath = getMetacardDirectory(id);
    if (metacardPath == null) {
        String message = String.format("Unable to create metacard path directory for %s", id);
        LOGGER.debug(message);
        throw new MetacardBackupException(message);
    }
    try {
        Path parent = metacardPath.getParent();
        if (parent != null) {
            Files.createDirectories(parent);
        }
        Files.createFile(metacardPath);
    } catch (IOException e) {
        LOGGER.trace("Unable to create empty backup file {}.  File may already exist.", metacardPath, e);
    }
    try (OutputStream outputStream = new FileOutputStream(metacardPath.toFile())) {
        IOUtils.write(data, outputStream);
    }
}
Also used : MetacardBackupException(org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Aggregations

MetacardBackupException (org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException)3 IOException (java.io.IOException)2 MetacardBackupStorageProvider (org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupStorageProvider)2 BinaryContent (ddf.catalog.data.BinaryContent)1 Metacard (ddf.catalog.data.Metacard)1 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)1 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 ProcessCreateItem (org.codice.ddf.catalog.async.data.api.internal.ProcessCreateItem)1 ProcessUpdateItem (org.codice.ddf.catalog.async.data.api.internal.ProcessUpdateItem)1 Before (org.junit.Before)1