use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class TestGetRecordsResponseConverter method testMarshalRecordCollectionById.
@Ignore
public void testMarshalRecordCollectionById() throws UnsupportedEncodingException, JAXBException {
final int totalResults = 2;
XStream xstream = createXStream(CswConstants.GET_RECORD_BY_ID_RESPONSE);
CswRecordCollection collection = createCswRecordCollection(null, totalResults);
collection.setById(true);
ArgumentCaptor<MarshallingContext> captor = ArgumentCaptor.forClass(MarshallingContext.class);
String xml = xstream.toXML(collection);
// Verify the context arguments were set correctly
verify(mockProvider, times(totalResults)).marshal(any(Object.class), any(HierarchicalStreamWriter.class), captor.capture());
MarshallingContext context = captor.getValue();
assertThat(context, not(nullValue()));
assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
assertThat(context.get(CswConstants.ELEMENT_SET_TYPE), is(nullValue()));
assertThat(context.get(CswConstants.ELEMENT_NAMES), is(nullValue()));
JAXBElement<GetRecordByIdResponseType> jaxb = (JAXBElement<GetRecordByIdResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
GetRecordByIdResponseType response = jaxb.getValue();
// Assert the GetRecordsResponse elements and attributes
assertThat(response, not(nullValue()));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class TestGetRecordsResponseConverter method testMarshalRecordCollectionGetSummary.
@Ignore
public void testMarshalRecordCollectionGetSummary() throws UnsupportedEncodingException, JAXBException {
final int totalResults = 5;
XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE);
GetRecordsType getRecords = new GetRecordsType();
QueryType query = new QueryType();
ElementSetNameType set = new ElementSetNameType();
set.setValue(ElementSetType.SUMMARY);
query.setElementSetName(set);
ObjectFactory objectFactory = new ObjectFactory();
getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query));
CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults);
collection.setElementSetType(ElementSetType.SUMMARY);
ArgumentCaptor<MarshallingContext> captor = ArgumentCaptor.forClass(MarshallingContext.class);
String xml = xstream.toXML(collection);
// Verify the context arguments were set correctly
verify(mockProvider, times(totalResults)).marshal(any(Object.class), any(HierarchicalStreamWriter.class), captor.capture());
MarshallingContext context = captor.getValue();
assertThat(context, not(nullValue()));
assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
assertThat(context.get(CswConstants.ELEMENT_SET_TYPE), is(ElementSetType.SUMMARY));
JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
GetRecordsResponseType response = jaxb.getValue();
// Assert the GetRecordsResponse elements and attributes
assertThat(response, not(nullValue()));
SearchResultsType resultsType = response.getSearchResults();
assertThat(resultsType, not(nullValue()));
assertThat(resultsType.getElementSet(), is(ElementSetType.SUMMARY));
assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults));
assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(totalResults));
assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class AbstractCswSource method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> requestProperties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
if (canRetrieveResourceById()) {
// If no resource reader was found, retrieve the product through a GetRecordById request
Serializable serializableId = null;
if (requestProperties != null) {
serializableId = requestProperties.get(Core.ID);
}
if (serializableId == null) {
throw new ResourceNotFoundException("Unable to retrieve resource because no metacard ID was found.");
}
String metacardId = serializableId.toString();
LOGGER.debug("Retrieving resource for ID : {}", metacardId);
Csw csw = factory.getClientForSubject((Subject) requestProperties.get(SecurityConstants.SECURITY_SUBJECT));
GetRecordByIdRequest getRecordByIdRequest = new GetRecordByIdRequest();
getRecordByIdRequest.setService(CswConstants.CSW);
getRecordByIdRequest.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
getRecordByIdRequest.setOutputFormat(MediaType.APPLICATION_OCTET_STREAM);
getRecordByIdRequest.setId(metacardId);
String rangeValue = "";
long requestedBytesToSkip = 0;
if (requestProperties.containsKey(CswConstants.BYTES_TO_SKIP)) {
requestedBytesToSkip = (Long) requestProperties.get(CswConstants.BYTES_TO_SKIP);
rangeValue = String.format("%s%s-", CswConstants.BYTES_EQUAL, requestProperties.get(CswConstants.BYTES_TO_SKIP).toString());
LOGGER.debug("Range: {}", rangeValue);
}
CswRecordCollection recordCollection;
try {
recordCollection = csw.getRecordById(getRecordByIdRequest, rangeValue);
Resource resource = recordCollection.getResource();
if (resource != null) {
long responseBytesSkipped = 0L;
if (recordCollection.getResourceProperties().get(BYTES_SKIPPED) != null) {
responseBytesSkipped = (Long) recordCollection.getResourceProperties().get(BYTES_SKIPPED);
}
alignStream(resource.getInputStream(), requestedBytesToSkip, responseBytesSkipped);
return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(resource.getInputStream()), resource.getMimeTypeValue(), FilenameUtils.getName(resource.getName())));
}
} catch (CswException | IOException e) {
throw new ResourceNotFoundException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, metacardId), e);
}
}
LOGGER.debug("Retrieving resource at : {}", resourceUri);
return resourceReader.retrieveResource(resourceUri, requestProperties);
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithMimeType.
@Test
public void testWriteToWithMimeType() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
when(mockManager.getTransformerByMimeType(any(String.class))).thenReturn(mockTransformer);
when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
CswRecordCollection collection = createCswRecordCollection(6);
collection.setNumberOfRecordsMatched(22);
collection.setNumberOfRecordsReturned(6);
collection.setById(true);
collection.setResultType(ResultType.RESULTS);
collection.setMimeType(MediaType.APPLICATION_JSON);
QName example = new QName("example");
collection.setElementName(Arrays.asList(example));
collection.setElementSetType(ElementSetType.BRIEF);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.writeTo(collection, null, null, null, null, null, stream);
verify(mockManager, times(1)).getTransformerByMimeType(any(String.class));
// TODO - assert lookup by mime type
// TODO failure case
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithSchema.
@Test
public void testWriteToWithSchema() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
when(mockManager.getTransformerBySchema(anyString())).thenReturn(mockTransformer);
ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class);
when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
CswRecordCollection collection = createCswRecordCollection(6);
collection.setNumberOfRecordsMatched(22);
collection.setNumberOfRecordsReturned(6);
final String exampleSchema = "http://example.com/schema";
collection.setOutputSchema(exampleSchema);
collection.setById(true);
collection.setResultType(ResultType.HITS);
QName example = new QName("example");
collection.setElementName(Arrays.asList(example));
collection.setElementSetType(ElementSetType.BRIEF);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.writeTo(collection, null, null, null, null, null, stream);
verify(mockTransformer).transform(any(SourceResponse.class), captor.capture());
Map arguments = captor.getValue();
assertThat(arguments.get(CswConstants.WRITE_NAMESPACES), is(false));
assertThat(arguments.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(exampleSchema));
assertThat(arguments.get(CswConstants.RESULT_TYPE_PARAMETER), is(ResultType.HITS));
assertThat(arguments.get(CswConstants.IS_BY_ID_QUERY), is(true));
assertThat(arguments.get(CswConstants.ELEMENT_SET_TYPE), is(ElementSetType.BRIEF));
assertThat(((QName[]) arguments.get(CswConstants.ELEMENT_NAMES))[0], is(example));
}
Aggregations