use of net.opengis.cat.csw.v_2_0_2.QueryType 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.QueryType in project ddf by codice.
the class CswQueryFactory method getQuery.
public QueryRequest getQuery(GetRecordsType request) throws CswException {
QueryType query = (QueryType) request.getAbstractQuery().getValue();
CswRecordMapperFilterVisitor filterVisitor = buildFilter(query.getConstraint());
QueryImpl frameworkQuery = new QueryImpl(filterVisitor.getVisitedFilter());
frameworkQuery.setSortBy(buildSort(query.getSortBy()));
if (ResultType.HITS.equals(request.getResultType()) || request.getMaxRecords().intValue() < 1) {
frameworkQuery.setStartIndex(1);
frameworkQuery.setPageSize(1);
} else {
frameworkQuery.setStartIndex(request.getStartPosition().intValue());
frameworkQuery.setPageSize(request.getMaxRecords().intValue());
}
QueryRequest queryRequest;
boolean isDistributed = request.getDistributedSearch() != null && (request.getDistributedSearch().getHopCount().longValue() > 1);
if (isDistributed && CollectionUtils.isEmpty(filterVisitor.getSourceIds())) {
queryRequest = new QueryRequestImpl(frameworkQuery, true);
} else if (isDistributed && !CollectionUtils.isEmpty(filterVisitor.getSourceIds())) {
queryRequest = new QueryRequestImpl(frameworkQuery, filterVisitor.getSourceIds());
} else {
queryRequest = new QueryRequestImpl(frameworkQuery, false);
}
return queryRequest;
}
use of net.opengis.cat.csw.v_2_0_2.QueryType in project ddf by codice.
the class CswSubscriptionEndpoint method createOrUpdateSubscription.
public Response createOrUpdateSubscription(GetRecordsType request, String requestId, boolean persist) throws CswException {
validator.validateOutputFormat(request.getOutputFormat(), mimeTypeTransformerManager);
validator.validateOutputSchema(request.getOutputSchema(), schemaTransformerManager);
if (request.getAbstractQuery() != null) {
if (!request.getAbstractQuery().getValue().getClass().equals(QueryType.class)) {
throw new CswException("Unknown QueryType: " + request.getAbstractQuery().getValue().getClass());
}
QueryType query = (QueryType) request.getAbstractQuery().getValue();
validator.validateTypes(query.getTypeNames(), CswConstants.VERSION_2_0_2);
validator.validateElementNames(query);
if (query.getConstraint() != null && query.getConstraint().isSetFilter() && query.getConstraint().isSetCqlText()) {
throw new CswException("A Csw Query can only have a Filter or CQL constraint");
}
}
if (requestId != null) {
request.setRequestId(requestId);
}
addOrUpdateSubscription(request, persist);
LOGGER.trace("Exiting getRecordsSubscription.");
return createAcknowledgment(request);
}
use of net.opengis.cat.csw.v_2_0_2.QueryType in project ddf by codice.
the class TestCswSource method testQueryWitNullSorting.
@Test
public void testQueryWitNullSorting() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
// Setup
final String searchPhrase = "*";
final int pageSize = 1;
final int numRecordsReturned = 1;
final long numRecordsMatched = 1;
setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
try {
configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
} catch (CswException e) {
fail("Could not configure Mock Remote CSW: " + e.getMessage());
}
QueryImpl query = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase));
query.setPageSize(pageSize);
query.setSortBy(null);
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
cswSource.setCswUrl(URL);
cswSource.setId(ID);
// Perform test
SourceResponse response = cswSource.query(new QueryRequestImpl(query));
// Verify
Assert.assertNotNull(response);
assertThat(response.getResults().size(), is(numRecordsReturned));
assertThat(response.getHits(), is(numRecordsMatched));
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();
QueryType cswQuery = (QueryType) getRecordsType.getAbstractQuery().getValue();
assertThat(cswQuery.getSortBy(), nullValue());
}
use of net.opengis.cat.csw.v_2_0_2.QueryType in project ddf by codice.
the class TestCswSource method testQueryWithSortByTemporal.
@Test
public void testQueryWithSortByTemporal() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
// Setup
final String searchPhrase = "*";
final int pageSize = 1;
final int numRecordsReturned = 1;
final long numRecordsMatched = 1;
setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
try {
configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
} catch (CswException e) {
fail("Could not configure Mock Remote CSW: " + e.getMessage());
}
QueryImpl query = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase));
query.setPageSize(pageSize);
SortBy sortBy = new SortByImpl(Result.TEMPORAL, SortOrder.DESCENDING);
query.setSortBy(sortBy);
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
cswSource.setCswUrl(URL);
cswSource.setId(ID);
// Perform test
SourceResponse response = cswSource.query(new QueryRequestImpl(query));
// Verify
Assert.assertNotNull(response);
assertThat(response.getResults().size(), is(numRecordsReturned));
assertThat(response.getHits(), is(numRecordsMatched));
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();
QueryType cswQuery = (QueryType) getRecordsType.getAbstractQuery().getValue();
assertThat(cswQuery.getSortBy().getSortProperty().size(), is(1));
assertThat(cswQuery.getSortBy().getSortProperty().get(0).getPropertyName().getContent().get(0).toString(), equalTo(Core.MODIFIED));
assertThat(cswQuery.getSortBy().getSortProperty().get(0).getSortOrder(), is(SortOrderType.DESC));
}
Aggregations