use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.
the class FileSystemStorageProviderTest method assertReadRequest.
private void assertReadRequest(String uriString, String mimeType, boolean extension) throws StorageException, IOException, URISyntaxException {
final URI uri = new URI(uriString);
ReadStorageRequest readRequest = new ReadStorageRequestImpl(uri, Collections.emptyMap());
ReadStorageResponse readResponse = provider.read(readRequest);
ContentItem item = readResponse.getContentItem();
LOGGER.debug("Item retrieved: {}", item);
assertThat(item, notNullValue());
assertThat(item.getId(), is(uri.getSchemeSpecificPart()));
if (uri.getFragment() != null) {
assertThat(item.getQualifier(), is(uri.getFragment()));
}
if (mimeType.equals(NITF_MIME_TYPE)) {
assertThat(item.getMimeTypeRawData(), is(NITF_MIME_TYPE));
}
List<String> parts = provider.getContentFilePathParts(uri.getSchemeSpecificPart(), uri.getFragment());
String expectedFilePath = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_REPOSITORY + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_STORE + File.separator + parts.get(0) + File.separator + parts.get(1) + File.separator + parts.get(2) + (StringUtils.isNotBlank(item.getQualifier()) ? File.separator + item.getQualifier() : "") + File.separator + item.getFilename();
if (extension) {
expectedFilePath = expectedFilePath + "." + FileSystemStorageProvider.REF_EXT;
}
assertThat(Files.exists(Paths.get(expectedFilePath)), is(true));
assertTrue(item.getSize() > 0);
}
use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.
the class ContentResourceReader method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> arguments) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
LOGGER.trace("ENTERING: retrieveResource");
ResourceResponse response = null;
if (resourceUri == null) {
throw new ResourceNotFoundException("Unable to find resource - resource URI was null");
}
if (resourceUri.getScheme().equals(ContentItem.CONTENT_SCHEME)) {
LOGGER.debug("Resource URI is content scheme");
String contentId = resourceUri.getSchemeSpecificPart();
if (contentId != null && !contentId.isEmpty()) {
if (arguments != null && arguments.get(ContentItem.QUALIFIER_KEYWORD) instanceof String && StringUtils.isNotBlank((String) arguments.get(ContentItem.QUALIFIER_KEYWORD))) {
try {
resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), (String) arguments.get(ContentItem.QUALIFIER_KEYWORD));
} catch (URISyntaxException e) {
throw new ResourceNotFoundException("Unable to create with qualifier", e);
}
}
ReadStorageRequest readRequest = new ReadStorageRequestImpl(resourceUri, arguments);
try {
ReadStorageResponse readResponse = storage.read(readRequest);
ContentItem contentItem = readResponse.getContentItem();
String fileName = contentItem.getFilename();
LOGGER.debug("resource name: {}", fileName);
InputStream is = contentItem.getInputStream();
response = new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), contentItem.getMimeType(), fileName));
} catch (StorageException e) {
throw new ResourceNotFoundException(e);
}
}
}
LOGGER.trace("EXITING: retrieveResource");
return response;
}
use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.
the class Historian method version.
/**
* Versions updated {@link Metacard}s and {@link ContentItem}s.
*
* @param streamUpdateRequest Needed to pass {@link
* ddf.catalog.core.versioning.MetacardVersion#SKIP_VERSIONING} flag into downstream update
* @param updateStorageResponse Versions this response's updated items
* @return the update response originally passed in
* @throws UnsupportedQueryException
* @throws SourceUnavailableException
* @throws IngestException
*/
public UpdateStorageResponse version(UpdateStorageRequest streamUpdateRequest, UpdateStorageResponse updateStorageResponse, UpdateResponse updateResponse) throws UnsupportedQueryException, SourceUnavailableException, IngestException {
if (doSkip(updateStorageResponse)) {
return updateStorageResponse;
}
setSkipFlag(streamUpdateRequest);
setSkipFlag(updateStorageResponse);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Versioning updated metacards and content: {}", updateStorageResponse);
}
List<Metacard> updatedMetacards = updateStorageResponse.getUpdatedContentItems().stream().filter(ci -> StringUtils.isBlank(ci.getQualifier())).map(ContentItem::getMetacard).filter(Objects::nonNull).filter(isNotVersionNorDeleted).collect(Collectors.toList());
if (updatedMetacards.isEmpty()) {
LOGGER.trace("No updated metacards applicable to versioning");
securityLogger.audit("Skipping versioning updated metacards with ids: {}", updateStorageResponse.getUpdatedContentItems().stream().map(ContentItem::getMetacard).filter(Objects::nonNull).map(Metacard::getId).collect(TO_A_STRING));
return updateStorageResponse;
}
Map<String, Metacard> originalMetacards = updateResponse.getUpdatedMetacards().stream().map(Update::getOldMetacard).collect(Collectors.toMap(Metacard::getId, Function.identity(), Historian::firstInWinsMerge));
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Found current data for the following metacards: {}", getList(originalMetacards));
}
Collection<ReadStorageRequest> ids = getReadStorageRequests(updatedMetacards);
Map<String, List<ContentItem>> content = getContent(ids);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Found resources for the following metacards: {}", getList(content));
}
Function<String, Action> getAction = id -> content.containsKey(id) ? Action.VERSIONED_CONTENT : Action.VERSIONED;
Map<String, Metacard> versionMetacards = getVersionMetacards(originalMetacards.values(), getAction, (Subject) updateResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
CreateStorageResponse createStorageResponse = versionContentItems(content, versionMetacards);
if (createStorageResponse == null) {
String message = "Could not version content items for: " + getList(originalMetacards);
securityLogger.audit(message);
LOGGER.debug(message);
return updateStorageResponse;
}
setResourceUriForContent(/*mutable*/
versionMetacards, createStorageResponse);
CreateResponse createResponse = storeVersionMetacards(versionMetacards);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Successfully created metacard versions under ids: {}", createResponse.getCreatedMetacards().stream().map(Metacard::getId).collect(TO_A_STRING));
}
return updateStorageResponse;
}
use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.
the class FileSystemStorageProviderTest method assertReadRequest.
private void assertReadRequest(String uriString, String mimeType) throws StorageException, IOException, URISyntaxException {
final URI uri = new URI(uriString);
ReadStorageRequest readRequest = new ReadStorageRequestImpl(uri, Collections.emptyMap());
ReadStorageResponse readResponse = provider.read(readRequest);
ContentItem item = readResponse.getContentItem();
LOGGER.debug("Item retrieved: {}", item);
assertThat(item, notNullValue());
assertThat(item.getId(), is(uri.getSchemeSpecificPart()));
if (uri.getFragment() != null) {
assertThat(item.getQualifier(), is(uri.getFragment()));
}
if (mimeType.equals(NITF_MIME_TYPE)) {
assertThat(item.getMimeTypeRawData(), is(NITF_MIME_TYPE));
}
List<String> parts = provider.getContentFilePathParts(uri.getSchemeSpecificPart(), uri.getFragment());
String expectedFilePath = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_REPOSITORY + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_STORE + File.separator + parts.get(0) + File.separator + parts.get(1) + File.separator + parts.get(2) + (StringUtils.isNotBlank(item.getQualifier()) ? File.separator + item.getQualifier() : "") + File.separator + item.getFilename();
assertThat(Files.exists(Paths.get(expectedFilePath)), is(true));
assertTrue(item.getSize() > 0);
}
use of ddf.catalog.content.operation.ReadStorageRequest in project ddf by codice.
the class FileSystemStorageProviderTest method testRollback.
@Test(expected = StorageException.class)
public void testRollback() throws Exception {
String id = UUID.randomUUID().toString().replaceAll("-", "");
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return IOUtils.toInputStream(TEST_INPUT_CONTENTS, StandardCharsets.UTF_8);
}
};
Metacard metacard = mock(Metacard.class);
when(metacard.getId()).thenReturn(id);
ContentItem contentItem = new ContentItemImpl(id, byteSource, NITF_MIME_TYPE, TEST_INPUT_FILENAME, TEST_INPUT_CONTENTS.getBytes().length, metacard);
CreateStorageRequest createRequest = new CreateStorageRequestImpl(Collections.singletonList(contentItem), null);
CreateStorageResponse createStorageResponse = provider.create(createRequest);
provider.rollback(createRequest);
ReadStorageRequest readStorageRequest = new ReadStorageRequestImpl(new URI("content:" + id), null);
ReadStorageResponse read = provider.read(readStorageRequest);
}
Aggregations