use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class GetRecordsMessageBodyReaderTest method testPartialContentNotSupportedHandling.
@Test
public void testPartialContentNotSupportedHandling() throws Exception {
CswSourceConfiguration config = new CswSourceConfiguration(encryptionService, permissions);
config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
String sampleData = "SampleData";
byte[] data = sampleData.getBytes();
CswRecordCollection cswRecords = null;
try (ByteArrayInputStream dataInputStream = new ByteArrayInputStream(data)) {
MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
httpHeaders.add(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, "TRUE");
httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=ResourceName"));
MediaType mediaType = new MediaType("text", "plain");
cswRecords = reader.readFrom(CswRecordCollection.class, null, null, mediaType, httpHeaders, dataInputStream);
}
Resource resource = cswRecords.getResource();
// assert that the CswRecordCollection property is not set if the server does not support
// Partial Content responses
assertThat(cswRecords.getResourceProperties().get(GetRecordsMessageBodyReader.BYTES_SKIPPED), nullValue());
// assert that the input stream has not been skipped at this stage. Since AbstractCswSource has
// the number
// of bytes that was attempted to be skipped, the stream must be aligned there instead.
assertThat(resource.getByteArray(), is(data));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswSourceTest method testAddingContentTypesOnQueries.
@Test
public void testAddingContentTypesOnQueries() throws CswException, UnsupportedQueryException, SecurityServiceException {
Csw mockCsw = createMockCsw();
List<String> expectedNames = new LinkedList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"));
ServiceRegistration<?> mockRegisteredMetacardType = (ServiceRegistration<?>) mock(ServiceRegistration.class);
LOGGER.info("mockRegisteredMetacardType: {}", mockRegisteredMetacardType);
doReturn(mockRegisteredMetacardType).when(mockContext).registerService(eq(MetacardType.class.getName()), any(MetacardType.class), any());
ServiceReference<?> mockServiceReference = (ServiceReference<?>) mock(ServiceReference.class);
doReturn(mockServiceReference).when(mockRegisteredMetacardType).getReference();
when(mockServiceReference.getProperty(eq(Metacard.CONTENT_TYPE))).thenReturn(expectedNames);
AbstractCswSource source = getCswSource(mockCsw, mockContext);
assertThat(source.getContentTypes(), hasSize(10));
Set<ContentType> expected = generateContentType(expectedNames);
assertThat(source.getContentTypes(), is(expected));
CswRecordCollection collection = generateCswCollection("/getRecordsResponse.xml");
when(mockCsw.getRecords(any(GetRecordsType.class))).thenReturn(collection);
QueryImpl propertyIsLikeQuery = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text("*"));
expectedNames.add("dataset");
expectedNames.add("dataset 2");
expectedNames.add("dataset 3");
expected = generateContentType(expectedNames);
source.query(getQueryRequestWithSubject(propertyIsLikeQuery));
assertThat(source.getContentTypes(), hasSize(13));
assertThat(source.getContentTypes(), is(expected));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswSourceTest method testCreateResults.
@Test
public void testCreateResults() throws SecurityServiceException {
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext, null, null, null, encryptionService, permissions);
CswRecordCollection recordCollection = new CswRecordCollection();
final int total = 2;
List<Metacard> metacards = new ArrayList<>(total);
for (int i = 0; i <= total; i++) {
String id = "ID_" + String.valueOf(i);
MetacardImpl metacard = new MetacardImpl();
metacard.setId(id);
metacard.setContentTypeName("myContentType");
metacard.setResourceURI(URI.create("http://example.com/resource"));
if (i == 1) {
metacard.setAttribute(Core.RESOURCE_DOWNLOAD_URL, "http://example.com/SECOND/RESOURCE");
}
metacards.add(metacard);
}
recordCollection.getCswRecords().addAll(metacards);
List<Result> results = cswSource.createResults(recordCollection);
assertThat(results, notNullValue());
assertThat(results.size(), is(recordCollection.getCswRecords().size()));
assertThat(results.get(0).getMetacard().getResourceURI(), is(recordCollection.getCswRecords().get(0).getResourceURI()));
assertThat(results.get(1).getMetacard().getResourceURI(), is(URI.create(recordCollection.getCswRecords().get(1).getAttribute(Core.RESOURCE_DOWNLOAD_URL).getValue().toString())));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class GetRecordsMessageBodyReader method readFrom.
@Override
public CswRecordCollection readFrom(Class<CswRecordCollection> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inStream) throws IOException, WebApplicationException {
CswRecordCollection cswRecords = null;
Map<String, Serializable> resourceProperties = new HashMap<>();
// Check if the server returned a Partial Content response (hopefully in response to a range
// header)
String contentRangeHeader = httpHeaders.getFirst(HttpHeaders.CONTENT_RANGE);
if (StringUtils.isNotBlank(contentRangeHeader)) {
contentRangeHeader = StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-");
long bytesSkipped = Long.parseLong(contentRangeHeader);
resourceProperties.put(BYTES_SKIPPED, Long.valueOf(bytesSkipped));
}
// If the following HTTP header exists and its value is true, the input stream will contain
// raw product data
String productRetrievalHeader = httpHeaders.getFirst(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER);
if (productRetrievalHeader != null && productRetrievalHeader.equalsIgnoreCase("TRUE")) {
String fileName = handleContentDispositionHeader(httpHeaders);
cswRecords = new CswRecordCollection();
cswRecords.setResource(new ResourceImpl(inStream, mediaType.toString(), fileName));
cswRecords.setResourceProperties(resourceProperties);
return cswRecords;
}
// Save original response for any exception message that might need to be
// created
String originalCswResponse = IOUtils.toString(inStream, StandardCharsets.UTF_8);
LOGGER.debug("Converting to CswRecordCollection: \n {}", LogSanitizer.sanitize(originalCswResponse));
cswRecords = unmarshalWithStaxReader(originalCswResponse);
return cswRecords;
}
Aggregations