use of ddf.catalog.content.operation.impl.CreateStorageRequestImpl in project ddf by codice.
the class FtpRequestHandler method getCreateStorageRequest.
private CreateStorageRequest getCreateStorageRequest(String fileName, TemporaryFileBackedOutputStream outputStream) throws IOException {
String fileExtension = FilenameUtils.getExtension(fileName);
String mimeType = getMimeType(fileExtension, outputStream);
ContentItem newItem = new ContentItemImpl(uuidGenerator.generateUuid(), outputStream.asByteSource(), mimeType, fileName, 0L, null);
return new CreateStorageRequestImpl(Collections.singletonList(newItem), null);
}
use of ddf.catalog.content.operation.impl.CreateStorageRequestImpl 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);
}
use of ddf.catalog.content.operation.impl.CreateStorageRequestImpl in project ddf by codice.
the class CatalogFrameworkImplTest method testUpdateStorage.
/**
* Tests that the framework properly passes an update request to the local provider.
*/
@Test
public void testUpdateStorage() throws Exception {
List<ContentItem> contentItems = new ArrayList<>();
MetacardImpl metacard = new MetacardImpl();
metacard.setId(null);
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("blah".getBytes());
}
};
ContentItemImpl contentItem = new ContentItemImpl(uuidGenerator.generateUuid(), byteSource, "application/octet-stream", "blah", 0L, metacard);
contentItems.add(contentItem);
CreateResponse response = framework.create(new CreateStorageRequestImpl(contentItems, null));
Metacard insertedCard = response.getCreatedMetacards().get(0);
List<ContentItem> updatedContentItems = new ArrayList<>();
updatedContentItems.add(new ContentItemImpl(insertedCard.getId(), byteSource, "application/octet-stream", insertedCard));
UpdateStorageRequest request = new UpdateStorageRequestImpl(updatedContentItems, null);
List<Result> mockFederationResults = Stream.of(insertedCard).map(m -> {
Result mockResult = mock(Result.class);
when(mockResult.getMetacard()).thenReturn(m);
return mockResult;
}).collect(Collectors.toList());
QueryResponseImpl queryResponse = new QueryResponseImpl(mock(QueryRequest.class), mockFederationResults, 1);
when(mockFederationStrategy.federate(anyList(), any())).thenReturn(queryResponse);
// send update to framework
List<Update> returnedCards = framework.update(request).getUpdatedMetacards();
assertThat(returnedCards, hasSize(1));
final Metacard newMetacard = returnedCards.get(0).getNewMetacard();
assertThat(newMetacard.getId(), notNullValue());
assertThat(newMetacard.getResourceURI().toString(), is(contentItem.getUri()));
assertThat(newMetacard.getResourceSize(), is(Long.toString(byteSource.size())));
assertThat(response.getCreatedMetacards(), hasSize(storageProvider.size()));
// make sure that the event was posted correctly
assertThat(eventAdmin.wasEventPosted(), is(true));
}
use of ddf.catalog.content.operation.impl.CreateStorageRequestImpl in project ddf by codice.
the class Historian method versionContentItems.
@Nullable
private CreateStorageResponse versionContentItems(Map<String, List<ContentItem>> items, Map<String, Metacard> versionedMetacards) throws SourceUnavailableException, IngestException {
List<ContentItem> contentItems = items.entrySet().stream().map(e -> getVersionedContentItems(e.getValue(), versionedMetacards)).flatMap(Collection::stream).collect(Collectors.toList());
if (contentItems.isEmpty()) {
LOGGER.debug("No content items to version");
return null;
}
CreateStorageResponse createStorageResponse = executeAsSystem(() -> storageProvider().create(new CreateStorageRequestImpl(contentItems, new HashMap<>())));
tryCommitStorage(createStorageResponse);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Successfully stored resources: {}", createStorageResponse.getCreatedContentItems());
}
return createStorageResponse;
}
use of ddf.catalog.content.operation.impl.CreateStorageRequestImpl in project ddf by codice.
the class CreateOperations method create.
public CreateResponse create(CreateStorageRequest streamCreateRequest, List<String> fanoutTagBlacklist) throws IngestException, SourceUnavailableException {
Map<String, Metacard> metacardMap = new HashMap<>();
List<ContentItem> contentItems = new ArrayList<>(streamCreateRequest.getContentItems().size());
HashMap<String, Map<String, Path>> tmpContentPaths = new HashMap<>();
CreateResponse createResponse;
CreateStorageRequest createStorageRequest = null;
CreateStorageResponse createStorageResponse;
CreateRequest createRequest = null;
Exception ingestError = null;
String fileNames = "";
streamCreateRequest = opsStorageSupport.prepareStorageRequest(streamCreateRequest, streamCreateRequest::getContentItems);
if (!streamCreateRequest.getContentItems().isEmpty()) {
List<String> fileList = streamCreateRequest.getContentItems().stream().map(ContentItem::getFilename).collect(Collectors.toList());
fileNames = String.join(", ", fileList);
}
INGEST_LOGGER.info("Started ingesting resources with titles: {}.", fileNames);
// Operation populates the metacardMap, contentItems, and tmpContentPaths
opsMetacardSupport.generateMetacardAndContentItems(streamCreateRequest.getContentItems(), metacardMap, contentItems, tmpContentPaths);
if (blockCreateMetacards(metacardMap.values(), fanoutTagBlacklist)) {
String message = "Fanout proxy does not support create operations with blacklisted metacard tag";
LOGGER.debug("{}. Tags blacklist: {}", message, fanoutTagBlacklist);
throw new IngestException(message);
}
streamCreateRequest.getProperties().put(CONTENT_PATHS, tmpContentPaths);
injectAttributes(metacardMap);
setDefaultValues(metacardMap);
streamCreateRequest = applyAttributeOverrides(streamCreateRequest, metacardMap);
try {
if (!contentItems.isEmpty()) {
createStorageRequest = new CreateStorageRequestImpl(contentItems, streamCreateRequest.getId(), streamCreateRequest.getProperties());
createStorageRequest = processPreCreateStoragePlugins(createStorageRequest);
try {
createStorageResponse = sourceOperations.getStorage().create(createStorageRequest);
createStorageResponse.getProperties().put(CONTENT_PATHS, tmpContentPaths);
} catch (StorageException e) {
INGEST_LOGGER.debug("Could not store content items: {}.", fileNames, e);
throw new IngestException("Could not store content items.", e);
}
createStorageResponse = processPostCreateStoragePlugins(createStorageResponse);
populateMetacardMap(metacardMap, createStorageResponse);
}
createRequest = new CreateRequestImpl(new ArrayList<>(metacardMap.values()), Optional.ofNullable(createStorageRequest).map(StorageRequest::getProperties).orElseGet(HashMap::new));
createResponse = doCreate(createRequest);
} catch (IngestException e) {
ingestError = e;
rollbackStorage(createStorageRequest);
throw e;
} catch (IOException | RuntimeException e) {
LOGGER.debug("Unhandled runtime exception or IOException during create", e);
ingestError = e;
rollbackStorage(createStorageRequest);
throw new IngestException("Unable to store products for request: " + fileNames, e);
} finally {
opsStorageSupport.commitAndCleanup(createStorageRequest, tmpContentPaths);
if (INGEST_LOGGER.isInfoEnabled()) {
if (createRequest != null && ingestError != null) {
INGEST_LOGGER.info("Error during ingesting resource: {}", fileNames, ingestError);
}
INGEST_LOGGER.info("Completed ingesting resource: {}.", fileNames);
}
}
createResponse = doPostIngest(createResponse);
return createResponse;
}
Aggregations