use of ddf.catalog.content.operation.impl.UpdateStorageRequestImpl in project ddf by codice.
the class FileSystemStorageProviderTest method testCreateWithQualifierAndOneInvalidItem.
@Test
public void testCreateWithQualifierAndOneInvalidItem() throws Exception {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return IOUtils.toInputStream("This data is my data, this data is your data.", StandardCharsets.UTF_8);
}
};
ContentItem contentItem = new ContentItemImpl(uuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
String invalidUuid = "wow-this-isn't-a-valid-uuid-right?!@#%";
ContentItem badContentItem = new ContentItemImpl(invalidUuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
CreateStorageRequest createRequest = new CreateStorageRequestImpl(Lists.newArrayList(contentItem, badContentItem), null);
CreateStorageResponse createResponse = provider.create(createRequest);
assertThat(createResponse.getCreatedContentItems().size(), is(1));
assertThat(createResponse.getCreatedContentItems().get(0).getId(), is(uuid));
ContentItem updateContentItem = new ContentItemImpl(invalidUuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
UpdateStorageRequest updateRequest = new UpdateStorageRequestImpl(Lists.newArrayList(updateContentItem), null);
UpdateStorageResponse updateResponse = provider.update(updateRequest);
assertThat(updateResponse.getUpdatedContentItems().size(), is(0));
}
use of ddf.catalog.content.operation.impl.UpdateStorageRequestImpl in project ddf by codice.
the class AbstractCatalogService method updateDocument.
private void updateDocument(Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard, String id, List<String> contentTypeList, String transformerParam, InputStream message) throws CatalogServiceException {
try {
MimeType mimeType = getMimeType(contentTypeList);
if (attachmentInfoAndMetacard == null) {
UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
catalogFramework.update(updateRequest);
} else {
UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, attachmentInfoAndMetacard.getKey().getStream(), attachmentInfoAndMetacard.getKey().getContentType(), attachmentInfoAndMetacard.getKey().getFilename(), 0, attachmentInfoAndMetacard.getValue())), null);
catalogFramework.update(streamUpdateRequest);
}
LOGGER.debug("Metacard {} updated.", LogSanitizer.sanitize(id));
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new InternalServerErrorException(exceptionMessage);
} catch (InternalIngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new InternalServerErrorException(exceptionMessage);
} catch (MetacardCreationException | IngestException e) {
String errorMessage = "Error cataloging updated metadata: ";
LOGGER.info(errorMessage, e);
throw new CatalogServiceException(errorMessage);
}
}
use of ddf.catalog.content.operation.impl.UpdateStorageRequestImpl in project ddf by codice.
the class RESTEndpoint method updateDocument.
/**
* REST Put. Updates the specified entry with the provided document.
*
* @param id
* @param message
* @return
*/
@PUT
@Path("/{id}")
@Consumes("multipart/*")
public Response updateDocument(@PathParam("id") String id, @Context HttpHeaders headers, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
LOGGER.trace("PUT");
Response response;
try {
if (id != null && message != null) {
MimeType mimeType = getMimeType(headers);
CreateInfo createInfo = null;
if (multipartBody != null) {
List<Attachment> contentParts = multipartBody.getAllAttachments();
if (contentParts != null && contentParts.size() > 0) {
createInfo = parseAttachments(contentParts, transformerParam);
} else {
LOGGER.debug("No file contents attachment found");
}
}
if (createInfo == null) {
UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
catalogFramework.update(updateRequest);
} else {
UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), 0, createInfo.getMetacard())), null);
catalogFramework.update(streamUpdateRequest);
}
LOGGER.debug("Metacard {} updated.", id);
response = Response.ok().build();
} else {
String errorResponseString = "Both ID and content are needed to perform UPDATE.";
LOGGER.info(errorResponseString);
throw new ServerErrorException(errorResponseString, Status.BAD_REQUEST);
}
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (InternalIngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (MetacardCreationException | IngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
}
return response;
}
use of ddf.catalog.content.operation.impl.UpdateStorageRequestImpl in project ddf by codice.
the class FileSystemStorageProviderTest method submitAndVerifySuccessfulUpdateStorageRequest.
private void submitAndVerifySuccessfulUpdateStorageRequest(ContentItem... requestContentItems) throws Exception {
final UpdateStorageRequest updateStorageRequest = new UpdateStorageRequestImpl(Arrays.asList(requestContentItems), null);
final UpdateStorageResponse updateStorageResponse = provider.update(updateStorageRequest);
final List<ContentItem> responseContentItems = updateStorageResponse.getUpdatedContentItems();
assertThat("Expect number of ContentItems in UpdateStorageResponse", responseContentItems, hasSize(requestContentItems.length));
for (final ContentItem responseContentItem : responseContentItems) {
// assert file exists
final URI uri = new URI(responseContentItem.getUri());
final List<String> parts = provider.getContentFilePathParts(uri.getSchemeSpecificPart(), uri.getFragment());
final String expectedFilePath = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_REPOSITORY + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_STORE + File.separator + FileSystemStorageProvider.DEFAULT_TMP + File.separator + updateStorageRequest.getId() + File.separator + parts.get(0) + File.separator + parts.get(1) + File.separator + parts.get(2) + (StringUtils.isNotBlank(responseContentItem.getQualifier()) ? File.separator + responseContentItem.getQualifier() : "") + File.separator + responseContentItem.getFilename();
assertThat("Expect file exists at " + expectedFilePath, Files.exists(Paths.get(expectedFilePath)));
// assert metacard attributes set
final ArgumentCaptor<Attribute> captor = ArgumentCaptor.forClass(Attribute.class);
final Metacard metacard = responseContentItem.getMetacard();
if (StringUtils.isBlank(responseContentItem.getQualifier())) {
verify(metacard, times(2)).setAttribute(captor.capture());
Attribute resourceUriAttribute = captor.getAllValues().get(0);
assertThat(resourceUriAttribute.getName(), is(Metacard.RESOURCE_URI));
assertThat(resourceUriAttribute.getValue(), is(uri.toString()));
Attribute resourceSizeAttribute = captor.getAllValues().get(1);
assertThat(resourceSizeAttribute.getName(), is(Metacard.RESOURCE_SIZE));
assertThat(resourceSizeAttribute.getValue(), is(responseContentItem.getSize()));
} else {
verify(metacard, never()).setAttribute(any());
}
}
provider.commit(updateStorageRequest);
for (ContentItem responseContentItem : responseContentItems) {
assertReadRequest(responseContentItem.getUri(), responseContentItem.getMimeType().toString());
}
}
use of ddf.catalog.content.operation.impl.UpdateStorageRequestImpl in project ddf by codice.
the class CatalogFrameworkImplTest method testUpdateItemWithQualifier.
/**
* Tests that the framework properly passes an update request to the local provider when the
* content item has a qualifier.
*/
@Test
public void testUpdateItemWithQualifier() throws Exception {
// store one item
MetacardImpl metacard = new MetacardImpl();
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);
CreateResponse response = framework.create(new CreateStorageRequestImpl(Collections.singletonList(contentItem), null));
Metacard createResponseMetacard = response.getCreatedMetacards().get(0);
// update with 2 more content items that have a qualifier and the same id and metacard as the
// already-created item
List<ContentItem> updateRequestContentItems = new ArrayList<>();
ByteSource q1ByteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("q1 data".getBytes());
}
};
ContentItem q1ContentItem = new ContentItemImpl(createResponseMetacard.getId(), "q1", q1ByteSource, "application/octet-stream", createResponseMetacard);
updateRequestContentItems.add(q1ContentItem);
ByteSource q2ByteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("q2 data".getBytes());
}
};
ContentItem q2ContentItem = new ContentItemImpl(createResponseMetacard.getId(), "q2", q2ByteSource, "application/octet-stream", createResponseMetacard);
updateRequestContentItems.add(q2ContentItem);
UpdateStorageRequest request = new UpdateStorageRequestImpl(updateRequestContentItems, null);
List<Result> mockFederationResults = Stream.of(createResponseMetacard).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 updateResponseMetacard = returnedCards.get(0).getNewMetacard();
assertThat(updateResponseMetacard.getId(), notNullValue());
assertThat(updateResponseMetacard.getResourceURI().toString(), is(contentItem.getUri()));
assertThat(updateResponseMetacard.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));
}
Aggregations