use of net.opengis.cat.csw.v_2_0_2.ResultType in project ddf by codice.
the class CswEndpoint method queryCsw.
private CswRecordCollection queryCsw(GetRecordsType request) throws CswException {
if (LOGGER.isDebugEnabled()) {
try {
Writer writer = new StringWriter();
try {
Marshaller marshaller = CswQueryFactory.getJaxBContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
JAXBElement<GetRecordsType> jaxbElement = new ObjectFactory().createGetRecords(request);
marshaller.marshal(jaxbElement, writer);
} catch (JAXBException e) {
LOGGER.debug("Unable to marshall {} to XML. Exception {}", GetRecordsType.class, e);
}
LOGGER.debug(writer.toString());
} catch (Exception e) {
LOGGER.debug("Unable to create debug message for getRecordsType: {}", e);
}
}
QueryType query = (QueryType) request.getAbstractQuery().getValue();
CswRecordCollection response = new CswRecordCollection();
response.setRequest(request);
response.setOutputSchema(request.getOutputSchema());
response.setMimeType(request.getOutputFormat());
response.setElementName(query.getElementName());
response.setElementSetType((query.getElementSetName() != null) ? query.getElementSetName().getValue() : null);
response.setResultType((ResultType) ObjectUtils.defaultIfNull(request.getResultType(), ResultType.HITS));
if (ResultType.HITS.equals(request.getResultType()) || ResultType.RESULTS.equals(request.getResultType())) {
QueryRequest queryRequest = queryFactory.getQuery(request);
try {
queryRequest = queryFactory.updateQueryRequestTags(queryRequest, request.getOutputSchema());
LOGGER.debug("Attempting to execute query: {}", queryRequest);
QueryResponse queryResponse = framework.query(queryRequest);
response.setSourceResponse(queryResponse);
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.debug("Unable to query", e);
throw new CswException(e);
}
}
return response;
}
use of net.opengis.cat.csw.v_2_0_2.ResultType in project ddf by codice.
the class TestCswSource method testAbsoluteTemporalSearchPropertyIsBetweenQuery.
@Test
public void testAbsoluteTemporalSearchPropertyIsBetweenQuery() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
// Setup
String expectedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" + "<GetRecords resultType=\"results\" outputFormat=\"application/xml\" " + " outputSchema=\"http://www.opengis.net/cat/csw/2.0.2\" startPosition=\"1\" " + " maxRecords=\"10\" service=\"CSW\" version=\"2.0.2\" " + " xmlns=\"http://www.opengis.net/cat/csw/2.0.2\" " + " xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" " + " xmlns:ogc=\"http://www.opengis.net/ogc\">\n " + " <Query typeNames=\"csw:Record\">\r\n" + " <ElementSetName>full</ElementSetName>\r\n" + " <Constraint version=\"1.1.0\">\r\n" + " <ogc:Filter>\r\n" + " <ogc:PropertyIsBetween>\r\n" + " <ogc:PropertyName>effective</ogc:PropertyName>\r\n" + " <ogc:LowerBoundary>\r\n" + " <ogc:Literal>START_DATE_TIME</ogc:Literal>\r\n" + " </ogc:LowerBoundary>\r\n" + " <ogc:UpperBoundary>\r\n" + " <ogc:Literal>END_DATE_TIME</ogc:Literal>\r\n" + " </ogc:UpperBoundary>\r\n" + " </ogc:PropertyIsBetween>\r\n" + " </ogc:Filter>\r\n" + " </Constraint>\r\n" + " </Query>\r\n" + "</GetRecords>";
final int pageSize = 10;
final int numRecordsReturned = 1;
final long numRecordsMatched = 10;
setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
try {
configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
} catch (CswException e) {
fail("Could not configure Mock Remote CSW: " + e.getMessage());
}
// Create start and end date times that are before current time
DateTime startDate = new DateTime(2013, 5, 1, 0, 0, 0, 0);
DateTime endDate = new DateTime(2013, 12, 31, 0, 0, 0, 0);
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
// Load the expected start and end date time into the excepted result
// XML
expectedXml = expectedXml.replace("START_DATE_TIME", fmt.print(startDate));
expectedXml = expectedXml.replace("END_DATE_TIME", fmt.print(endDate));
// Single absolute time range to search across
Filter temporalFilter = builder.attribute(Metacard.EFFECTIVE).is().during().dates(startDate.toDate(), endDate.toDate());
QueryImpl temporalQuery = new QueryImpl(temporalFilter);
temporalQuery.setPageSize(pageSize);
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
cswSource.setCswUrl(URL);
cswSource.setId(ID);
// Perform test
cswSource.query(new QueryRequestImpl(temporalQuery));
// Verify
ArgumentCaptor<GetRecordsType> captor = ArgumentCaptor.forClass(GetRecordsType.class);
try {
verify(mockCsw, atLeastOnce()).getRecords(captor.capture());
} catch (CswException e) {
fail("Could not verify Mock CSW record count " + e.getMessage());
}
GetRecordsType getRecordsType = captor.getValue();
String xml = getGetRecordsTypeAsXml(getRecordsType);
Diff xmlDiff = new Diff(expectedXml, xml);
if (!xmlDiff.similar()) {
LOGGER.error("Unexpected XML request sent");
LOGGER.error("Expected: {}", expectedXml);
LOGGER.error("Actual: {}", xml);
}
assertXMLEqual(expectedXml, xml);
}
use of net.opengis.cat.csw.v_2_0_2.ResultType in project ddf by codice.
the class TestCswSource method testAbsoluteTemporalSearchTwoRanges.
@Test
public void testAbsoluteTemporalSearchTwoRanges() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
// Setup
String expectedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" + "<GetRecords resultType=\"results\" outputFormat=\"application/xml\"\r\n" + " outputSchema=\"http://www.opengis.net/cat/csw/2.0.2\" startPosition=\"1\"\r\n" + " maxRecords=\"10\" service=\"CSW\" version=\"2.0.2\"" + " xmlns=\"http://www.opengis.net/cat/csw/2.0.2\"" + " xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\"" + " xmlns:ogc=\"http://www.opengis.net/ogc\">\r\n" + " <Query typeNames=\"csw:Record\">\r\n" + " <ElementSetName>full</ElementSetName>\r\n" + " <Constraint version=\"1.1.0\">\r\n" + " <ogc:Filter>\r\n" + " <ogc:Or>\r\n" + " <ogc:PropertyIsBetween>\r\n" + " <ogc:PropertyName>effective</ogc:PropertyName>\r\n" + " <ogc:LowerBoundary>\r\n" + " <ogc:Literal>START1_DATE_TIME</ogc:Literal>\r\n" + " </ogc:LowerBoundary>\r\n" + " <ogc:UpperBoundary>\r\n" + " <ogc:Literal>END1_DATE_TIME</ogc:Literal>\r\n" + " </ogc:UpperBoundary>\r\n" + " </ogc:PropertyIsBetween>\r\n" + " <ogc:PropertyIsBetween>\r\n" + " <ogc:PropertyName>effective</ogc:PropertyName>\r\n" + " <ogc:LowerBoundary>\r\n" + " <ogc:Literal>START2_DATE_TIME</ogc:Literal>\r\n" + " </ogc:LowerBoundary>\r\n" + " <ogc:UpperBoundary>\r\n" + " <ogc:Literal>END2_DATE_TIME</ogc:Literal>\r\n" + " </ogc:UpperBoundary>\r\n" + " </ogc:PropertyIsBetween>\r\n" + " </ogc:Or>\r\n" + " </ogc:Filter>\r\n" + " </Constraint>\r\n" + " </Query>\r\n" + "</GetRecords>\r\n";
final int pageSize = 10;
final int numRecordsReturned = 1;
final long numRecordsMatched = 10;
setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
try {
configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
} catch (CswException e) {
fail("Could not configure Mock Remote CSW: " + e.getMessage());
}
DateTime startDate = new DateTime(2012, 5, 1, 0, 0, 0, 0);
DateTime endDate = new DateTime(2012, 12, 31, 0, 0, 0, 0);
DateTime startDate2 = new DateTime(2013, 5, 1, 0, 0, 0, 0);
DateTime endDate2 = new DateTime(2013, 12, 31, 0, 0, 0, 0);
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
// Load the expected start and end date time into the excepted result
// XML
expectedXml = expectedXml.replace("START1_DATE_TIME", fmt.print(startDate));
expectedXml = expectedXml.replace("END1_DATE_TIME", fmt.print(endDate));
expectedXml = expectedXml.replace("START2_DATE_TIME", fmt.print(startDate2));
expectedXml = expectedXml.replace("END2_DATE_TIME", fmt.print(endDate2));
// Single absolute time range to search across
FilterFactory filterFactory = new FilterFactoryImpl();
Filter temporalFilter1 = builder.attribute(Metacard.EFFECTIVE).is().during().dates(startDate.toDate(), endDate.toDate());
Filter temporalFilter2 = builder.attribute(Metacard.EFFECTIVE).is().during().dates(startDate2.toDate(), endDate2.toDate());
Filter temporalFilter = filterFactory.or(temporalFilter1, temporalFilter2);
QueryImpl temporalQuery = new QueryImpl(temporalFilter);
temporalQuery.setPageSize(pageSize);
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
cswSource.setCswUrl(URL);
cswSource.setId(ID);
// Perform test
cswSource.query(new QueryRequestImpl(temporalQuery));
// Verify
ArgumentCaptor<GetRecordsType> captor = ArgumentCaptor.forClass(GetRecordsType.class);
try {
verify(mockCsw, atLeastOnce()).getRecords(captor.capture());
} catch (CswException e) {
fail("Could not verify Mock CSW record count " + e.getMessage());
}
GetRecordsType getRecordsType = captor.getValue();
String xml = getGetRecordsTypeAsXml(getRecordsType);
Diff xmlDiff = new Diff(expectedXml, xml);
if (!xmlDiff.similar()) {
LOGGER.error("Unexpected XML request sent");
LOGGER.error("Expected: {}", expectedXml);
LOGGER.error("Actual: {}", xml);
}
assertXMLEqual(expectedXml, xml);
}
use of net.opengis.cat.csw.v_2_0_2.ResultType in project ddf by codice.
the class CswQueryResponseTransformer method buildCollection.
private CswRecordCollection buildCollection(SourceResponse sourceResponse, Map<String, Serializable> arguments) {
CswRecordCollection recordCollection = new CswRecordCollection();
recordCollection.setNumberOfRecordsMatched(sourceResponse.getHits());
recordCollection.setNumberOfRecordsReturned(sourceResponse.getResults().size());
recordCollection.setStartPosition(sourceResponse.getRequest().getQuery().getStartIndex());
Object elementSetTypeArg = arguments.get(CswConstants.ELEMENT_SET_TYPE);
if (elementSetTypeArg instanceof ElementSetType) {
ElementSetType elementSetType = (ElementSetType) elementSetTypeArg;
recordCollection.setElementSetType(elementSetType);
}
Object elementNamesArg = arguments.get(CswConstants.ELEMENT_NAMES);
if (elementNamesArg instanceof QName[]) {
QName[] qnames = (QName[]) elementNamesArg;
if (qnames.length > 0) {
List<QName> elementNames = new ArrayList();
for (QName entry : qnames) {
elementNames.add(entry);
}
recordCollection.setElementName(elementNames);
}
}
Object isByIdQuery = arguments.get(CswConstants.IS_BY_ID_QUERY);
if (isByIdQuery != null) {
recordCollection.setById((Boolean) isByIdQuery);
}
Object arg = arguments.get((CswConstants.GET_RECORDS));
if (arg != null && arg instanceof GetRecordsType) {
recordCollection.setRequest((GetRecordsType) arg);
}
Object resultType = arguments.get(CswConstants.RESULT_TYPE_PARAMETER);
if (resultType instanceof ResultType) {
recordCollection.setResultType((ResultType) resultType);
}
Object outputSchema = arguments.get(CswConstants.OUTPUT_SCHEMA_PARAMETER);
if (outputSchema instanceof String) {
recordCollection.setOutputSchema((String) outputSchema);
} else {
recordCollection.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
}
Object doWriteNamespaces = arguments.get(CswConstants.WRITE_NAMESPACES);
if (doWriteNamespaces instanceof Boolean) {
recordCollection.setDoWriteNamespaces((Boolean) doWriteNamespaces);
}
return recordCollection;
}
use of net.opengis.cat.csw.v_2_0_2.ResultType in project ddf by codice.
the class TestCswSourceBase method configureMockCsw.
protected void configureMockCsw(int numRecordsReturned, long numRecordsMatched, String cswVersion) throws CswException {
ServiceIdentification mockServiceIdentification = mock(ServiceIdentification.class);
when(mockServiceIdentification.getAbstract()).thenReturn("myDescription");
CapabilitiesType mockCapabilities = mock(CapabilitiesType.class);
when(mockCapabilities.getVersion()).thenReturn(cswVersion);
when(mockCapabilities.getServiceIdentification()).thenReturn(mockServiceIdentification);
when(mockCapabilities.getServiceIdentification()).thenReturn(mockServiceIdentification);
when(mockCsw.getCapabilities(any(GetCapabilitiesRequest.class))).thenReturn(mockCapabilities);
FilterCapabilities mockFilterCapabilities = mock(FilterCapabilities.class);
when(mockCapabilities.getFilterCapabilities()).thenReturn(mockFilterCapabilities);
List<ComparisonOperatorType> comparisonOpsList = new ArrayList<>();
comparisonOpsList.add(ComparisonOperatorType.EQUAL_TO);
comparisonOpsList.add(ComparisonOperatorType.LIKE);
comparisonOpsList.add(ComparisonOperatorType.NOT_EQUAL_TO);
comparisonOpsList.add(ComparisonOperatorType.GREATER_THAN);
comparisonOpsList.add(ComparisonOperatorType.GREATER_THAN_EQUAL_TO);
comparisonOpsList.add(ComparisonOperatorType.LESS_THAN);
comparisonOpsList.add(ComparisonOperatorType.LESS_THAN_EQUAL_TO);
comparisonOpsList.add(ComparisonOperatorType.BETWEEN);
comparisonOpsList.add(ComparisonOperatorType.NULL_CHECK);
ComparisonOperatorsType comparisonOps = new ComparisonOperatorsType();
comparisonOps.setComparisonOperator(comparisonOpsList);
ScalarCapabilitiesType mockScalarCapabilities = mock(ScalarCapabilitiesType.class);
when(mockScalarCapabilities.getLogicalOperators()).thenReturn(mock(LogicalOperators.class));
mockScalarCapabilities.setComparisonOperators(comparisonOps);
when(mockScalarCapabilities.getComparisonOperators()).thenReturn(comparisonOps);
when(mockFilterCapabilities.getScalarCapabilities()).thenReturn(mockScalarCapabilities);
List<DomainType> getRecordsParameters = new ArrayList<>();
DomainType typeName = new DomainType();
typeName.setName(CswConstants.TYPE_NAME_PARAMETER);
typeName.setValue(Collections.singletonList("csw:Record"));
getRecordsParameters.add(typeName);
DomainType typeNames = new DomainType();
typeNames.setName(CswConstants.TYPE_NAMES_PARAMETER);
getRecordsParameters.add(typeNames);
DomainType getRecordsOutputSchema = new DomainType();
getRecordsOutputSchema.setName(CswConstants.OUTPUT_SCHEMA_PARAMETER);
getRecordsOutputSchema.getValue().add(CswConstants.CSW_OUTPUT_SCHEMA);
getRecordsParameters.add(getRecordsOutputSchema);
DomainType constraintLang = new DomainType();
constraintLang.setName(CswConstants.CONSTRAINT_LANGUAGE_PARAMETER);
constraintLang.setValue(Collections.singletonList(CswConstants.CONSTRAINT_LANGUAGE_FILTER));
getRecordsParameters.add(constraintLang);
DomainType outputFormat = new DomainType();
outputFormat.setName(CswConstants.OUTPUT_FORMAT_PARAMETER);
getRecordsParameters.add(outputFormat);
DomainType resultType = new DomainType();
resultType.setName(CswConstants.RESULT_TYPE_PARAMETER);
getRecordsParameters.add(resultType);
DomainType elementSetName = new DomainType();
elementSetName.setName(CswConstants.ELEMENT_SET_NAME_PARAMETER);
getRecordsParameters.add(elementSetName);
List<DomainType> getRecordByIdParameters = new ArrayList<>();
DomainType getRecordByIdOutputSchema = new DomainType();
getRecordByIdOutputSchema.setName(CswConstants.OUTPUT_SCHEMA_PARAMETER);
List<String> outputSchemas = new ArrayList<>();
outputSchemas.add(CswConstants.CSW_OUTPUT_SCHEMA);
getRecordByIdOutputSchema.setValue(outputSchemas);
getRecordByIdParameters.add(getRecordByIdOutputSchema);
Operation getRecords = new Operation();
getRecords.setName(CswConstants.GET_RECORDS);
getRecords.setParameter(getRecordsParameters);
Operation getRecordById = new Operation();
getRecordById.setName(CswConstants.GET_RECORD_BY_ID);
getRecordById.setParameter(getRecordByIdParameters);
List<Operation> operations = new ArrayList<>(2);
operations.add(getRecords);
operations.add(getRecordById);
OperationsMetadata mockOperationsMetadata = mock(OperationsMetadata.class);
mockOperationsMetadata.setOperation(operations);
when(mockCapabilities.getOperationsMetadata()).thenReturn(mockOperationsMetadata);
when(mockOperationsMetadata.getOperation()).thenReturn(operations);
if (numRecordsReturned > 0) {
List<Metacard> metacards = new ArrayList<>(numRecordsReturned);
for (int i = 1; i <= numRecordsReturned; i++) {
String id = "ID_" + String.valueOf(i);
MetacardImpl metacard = new MetacardImpl();
metacard.setId(id);
metacard.setContentTypeName("myContentType");
metacards.add(metacard);
}
SearchResultsType searchResults = mock(SearchResultsType.class);
when(searchResults.getNumberOfRecordsMatched()).thenReturn(BigInteger.valueOf(numRecordsMatched));
when(searchResults.getNumberOfRecordsReturned()).thenReturn(BigInteger.valueOf(numRecordsReturned));
CswRecordCollection mockCswRecordCollection = mock(CswRecordCollection.class);
when(mockCswRecordCollection.getCswRecords()).thenReturn(metacards);
when(mockCswRecordCollection.getNumberOfRecordsMatched()).thenReturn(numRecordsMatched);
when(mockCswRecordCollection.getNumberOfRecordsReturned()).thenReturn((long) numRecordsReturned);
when(mockCsw.getRecords(any(GetRecordsType.class))).thenReturn(mockCswRecordCollection);
}
}
Aggregations