use of net.opengis.cat.csw.v_2_0_2.GetRecordsType in project ddf by codice.
the class AbstractCswSource method query.
protected SourceResponse query(QueryRequest queryRequest, ElementSetType elementSetName, List<QName> elementNames, Csw csw) throws UnsupportedQueryException {
Query query = queryRequest.getQuery();
LOGGER.debug("{}: Received query:\n{}", cswSourceConfiguration.getId(), query);
GetRecordsType getRecordsType = createGetRecordsRequest(query, elementSetName, elementNames);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("{}: GetRecords request:\n {}", cswSourceConfiguration.getId(), getGetRecordsTypeAsXml(getRecordsType));
}
LOGGER.debug("{}: Sending query to: {}", cswSourceConfiguration.getId(), cswSourceConfiguration.getCswUrl());
List<Result> results;
Long totalHits;
try {
CswRecordCollection cswRecordCollection = csw.getRecords(getRecordsType);
if (cswRecordCollection == null) {
throw new UnsupportedQueryException("Invalid results returned from server");
}
this.availabilityTask.updateLastAvailableTimestamp(System.currentTimeMillis());
LOGGER.debug("{}: Received [{}] record(s) of the [{}] record(s) matched from {}.", cswSourceConfiguration.getId(), cswRecordCollection.getNumberOfRecordsReturned(), cswRecordCollection.getNumberOfRecordsMatched(), cswSourceConfiguration.getCswUrl());
results = createResults(cswRecordCollection);
totalHits = cswRecordCollection.getNumberOfRecordsMatched();
} catch (CswException cswe) {
LOGGER.info(CSW_SERVER_ERROR, cswe);
throw new UnsupportedQueryException(CSW_SERVER_ERROR, cswe);
} catch (WebApplicationException wae) {
String msg = handleWebApplicationException(wae);
throw new UnsupportedQueryException(msg, wae);
} catch (Exception ce) {
String msg = handleClientException(ce);
throw new UnsupportedQueryException(msg, ce);
}
LOGGER.debug("{}: Adding {} result(s) to the source response.", cswSourceConfiguration.getId(), results.size());
SourceResponseImpl sourceResponse = new SourceResponseImpl(queryRequest, results, totalHits);
addContentTypes(sourceResponse);
return sourceResponse;
}
use of net.opengis.cat.csw.v_2_0_2.GetRecordsType in project ddf by codice.
the class TestCswQueryResponseTransformer method testMarshalAcknowledgement.
@Test
public void testMarshalAcknowledgement() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
GetRecordsType query = new GetRecordsType();
query.setResultType(ResultType.VALIDATE);
query.setMaxRecords(BigInteger.valueOf(6));
query.setStartPosition(BigInteger.valueOf(4));
SourceResponse sourceResponse = createSourceResponse(query, 22);
Map<String, Serializable> args = new HashMap<>();
args.put(CswConstants.RESULT_TYPE_PARAMETER, ResultType.VALIDATE);
args.put(CswConstants.GET_RECORDS, query);
BinaryContent content = transformer.transform(sourceResponse, args);
String xml = new String(content.getByteArray());
JAXBElement<?> jaxb = (JAXBElement<?>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
assertThat(jaxb.getValue(), is(instanceOf(AcknowledgementType.class)));
AcknowledgementType response = (AcknowledgementType) jaxb.getValue();
assertThat(response.getEchoedRequest().getAny(), is(instanceOf(JAXBElement.class)));
JAXBElement<?> jaxB = (JAXBElement<?>) response.getEchoedRequest().getAny();
assertThat(jaxB.getValue(), is(instanceOf(GetRecordsType.class)));
}
use of net.opengis.cat.csw.v_2_0_2.GetRecordsType in project ddf by codice.
the class TestGetRecordsResponseConverter method testMarshalRecordCollectionHits.
@Ignore
public void testMarshalRecordCollectionHits() 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.FULL);
query.setElementSetName(set);
ObjectFactory objectFactory = new ObjectFactory();
getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query));
CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults);
collection.setElementSetType(ElementSetType.FULL);
collection.setResultType(ResultType.HITS);
String xml = xstream.toXML(collection);
// Verify the context arguments were set correctly
verify(mockProvider, never()).marshal(any(Object.class), any(HierarchicalStreamWriter.class), any(MarshallingContext.class));
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.FULL));
assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults));
assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(0));
assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA));
}
use of net.opengis.cat.csw.v_2_0_2.GetRecordsType 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.GetRecordsType 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;
}
Aggregations