use of net.opengis.cat.csw.v_2_0_2.ElementSetType 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.ElementSetType in project ddf by codice.
the class TestRegistryStore method testInit.
@Test
public void testInit() throws Exception {
RegistryStoreImpl registryStore = spy(new RegistryStoreImpl(context, cswSourceConfiguration, provider, factory, encryptionService) {
@Override
protected void validateOperation() {
}
@Override
public boolean isAvailable() {
return availability;
}
@Override
protected SourceResponse query(QueryRequest queryRequest, ElementSetType elementSetName, List<QName> elementNames, Csw csw) throws UnsupportedQueryException {
if (queryResults == null) {
throw new UnsupportedQueryException("Test - Bad Query");
}
return new SourceResponseImpl(queryRequest, queryResults);
}
@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
return new SourceResponseImpl(request, Collections.singletonList(new Result() {
@Override
public Metacard getMetacard() {
MetacardImpl metacard = new MetacardImpl();
metacard.setAttribute(RegistryObjectMetacardType.REGISTRY_ID, "registryId");
metacard.setAttribute(Metacard.TITLE, "title");
return metacard;
}
@Override
public Double getRelevanceScore() {
return null;
}
@Override
public Double getDistanceInMeters() {
return null;
}
}));
}
@Override
protected CapabilitiesType getCapabilities() {
return mock(CapabilitiesType.class);
}
@Override
public void configureCswSource() {
}
;
@Override
protected Subject getSystemSubject() {
return subject;
}
@Override
BundleContext getBundleContext() {
return context;
}
});
registryStore.setFilterBuilder(filterBuilder);
registryStore.setFilterAdapter(filterAdapter);
registryStore.setConfigAdmin(configAdmin);
registryStore.setMetacardMarshaller(new MetacardMarshaller(parser));
registryStore.setSchemaTransformerManager(transformer);
registryStore.setAutoPush(true);
registryStore.setRegistryUrl("http://test.url:0101/example");
properties = new Hashtable<>();
properties.put(RegistryStoreImpl.ID, "registryId");
registryStore.setMetacardMarshaller(marshaller);
Csw csw = mock(Csw.class);
when(factory.getClientForSubject(any())).thenReturn(csw);
cswSourceConfiguration.setCswUrl("https://localhost");
cswSourceConfiguration.setPollIntervalMinutes(1);
queryResults.add(new ResultImpl(getDefaultMetacard()));
registryStore.init();
assertThat(registryStore.getRegistryId(), is("registryId"));
}
use of net.opengis.cat.csw.v_2_0_2.ElementSetType 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.ElementSetType in project ddf by codice.
the class CswMarshallHelper method getArguments.
static Map<String, Object> getArguments(MarshallingContext context) {
Map<String, Object> args = new HashMap<>();
Object writeNamespaceObj = context.get(CswConstants.WRITE_NAMESPACES);
Boolean doWriteNamespaces = false;
if (writeNamespaceObj instanceof Boolean) {
doWriteNamespaces = (Boolean) writeNamespaceObj;
args.put(CswConstants.WRITE_NAMESPACES, doWriteNamespaces);
} else {
args.put(CswConstants.WRITE_NAMESPACES, doWriteNamespaces);
}
Object elementSetObj = context.get(CswConstants.ELEMENT_SET_TYPE);
Object elementNamesObj = context.get(CswConstants.ELEMENT_NAMES);
String rootNodeName = CswConstants.CSW_RECORD;
if (elementSetObj instanceof ElementSetType) {
List<QName> elementsToWrite;
ElementSetType elementSetType = (ElementSetType) elementSetObj;
switch(elementSetType) {
case BRIEF:
elementsToWrite = CswConstants.BRIEF_CSW_RECORD_FIELDS;
rootNodeName = CswConstants.CSW_BRIEF_RECORD;
break;
case SUMMARY:
elementsToWrite = CswConstants.SUMMARY_CSW_RECORD_FIELDS;
rootNodeName = CswConstants.CSW_SUMMARY_RECORD;
break;
case FULL:
default:
elementsToWrite = CswConstants.FULL_CSW_RECORD_FIELDS;
break;
}
args.put(CswConstants.ELEMENT_NAMES, elementsToWrite);
args.put(CswConstants.ROOT_NODE_NAME, rootNodeName);
} else if (elementNamesObj instanceof List<?>) {
args.put(CswConstants.ELEMENT_NAMES, elementNamesObj);
args.put(CswConstants.ROOT_NODE_NAME, rootNodeName);
} else {
args.put(CswConstants.ROOT_NODE_NAME, rootNodeName);
args.put(CswConstants.ELEMENT_NAMES, CswConstants.FULL_CSW_RECORD_FIELDS);
}
return args;
}
use of net.opengis.cat.csw.v_2_0_2.ElementSetType in project ddf by codice.
the class GetRecordsRequest method get202RecordsType.
/**
* Convert the KVP values into a GetRecordsType, validates format of fields and enumeration
* constraints required to meet the schema requirements of the GetRecordsType. No further
* validation is done at this point
*
* @return GetRecordsType representation of this key-value representation
* @throws CswException
* An exception when some field cannot be converted to the equivalent GetRecordsType
* value
*/
public GetRecordsType get202RecordsType() throws CswException {
GetRecordsType getRecords = new GetRecordsType();
getRecords.setOutputSchema(getOutputSchema());
getRecords.setRequestId(getRequestId());
if (getMaxRecords() != null) {
getRecords.setMaxRecords(getMaxRecords());
}
if (getStartPosition() != null) {
getRecords.setStartPosition(getStartPosition());
}
if (getOutputFormat() != null) {
getRecords.setOutputFormat(getOutputFormat());
}
if (getResponseHandler() != null) {
getRecords.setResponseHandler(Arrays.asList(getResponseHandler()));
}
if (getResultType() != null) {
try {
getRecords.setResultType(ResultType.fromValue(getResultType()));
} catch (IllegalArgumentException iae) {
LOGGER.debug("Failed to find \"{}\" as a valid ResultType", getResultType(), iae);
throw new CswException("A CSW getRecords request ResultType must be \"hits\", \"results\", or \"validate\"");
}
}
if (getDistributedSearch() != null && getDistributedSearch()) {
DistributedSearchType disSearch = new DistributedSearchType();
disSearch.setHopCount(getHopCount());
getRecords.setDistributedSearch(disSearch);
}
QueryType query = new QueryType();
Map<String, String> namespaces = parseNamespaces(getNamespace());
List<QName> typeNames = typeStringToQNames(getTypeNames(), namespaces);
query.setTypeNames(typeNames);
if (getElementName() != null && getElementSetName() != null) {
LOGGER.debug("CSW getRecords request received with mutually exclusive ElementName and SetElementName set");
throw new CswException("A CSW getRecords request can only have an \"ElementName\" or an \"ElementSetName\"");
}
if (getElementName() != null) {
query.setElementName(typeStringToQNames(getElementName(), namespaces));
}
if (getElementSetName() != null) {
try {
ElementSetNameType eleSetName = new ElementSetNameType();
eleSetName.setTypeNames(typeNames);
eleSetName.setValue(ElementSetType.fromValue(getElementSetName()));
query.setElementSetName(eleSetName);
} catch (IllegalArgumentException iae) {
LOGGER.debug("Failed to find \"{}\" as a valid elementSetType, Exception {}", getElementSetName(), iae);
throw new CswException("A CSW getRecords request ElementSetType must be \"brief\", \"summary\", or \"full\"");
}
}
if (getSortBy() != null) {
SortByType sort = new SortByType();
List<SortPropertyType> sortProps = new LinkedList<SortPropertyType>();
String[] sortOptions = getSortBy().split(",");
for (String sortOption : sortOptions) {
if (sortOption.lastIndexOf(':') < 1) {
throw new CswException("Invalid Sort Order format: " + getSortBy());
}
SortPropertyType sortProperty = new SortPropertyType();
PropertyNameType propertyName = new PropertyNameType();
String propName = StringUtils.substringBeforeLast(sortOption, ":");
String direction = StringUtils.substringAfterLast(sortOption, ":");
propertyName.setContent(Arrays.asList((Object) propName));
SortOrderType sortOrder;
if (direction.equals("A")) {
sortOrder = SortOrderType.ASC;
} else if (direction.equals("D")) {
sortOrder = SortOrderType.DESC;
} else {
throw new CswException("Invalid Sort Order format: " + getSortBy());
}
sortProperty.setPropertyName(propertyName);
sortProperty.setSortOrder(sortOrder);
sortProps.add(sortProperty);
}
sort.setSortProperty(sortProps);
query.setElementName(typeStringToQNames(getElementName(), namespaces));
query.setSortBy(sort);
}
if (getConstraint() != null) {
QueryConstraintType queryConstraint = new QueryConstraintType();
if (getConstraintLanguage().equalsIgnoreCase(CswConstants.CONSTRAINT_LANGUAGE_CQL)) {
queryConstraint.setCqlText(getConstraint());
} else if (getConstraintLanguage().equalsIgnoreCase(CswConstants.CONSTRAINT_LANGUAGE_FILTER)) {
try {
XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(constraint));
Unmarshaller unmarshaller = JAX_BCONTEXT.createUnmarshaller();
@SuppressWarnings("unchecked") JAXBElement<FilterType> jaxbFilter = (JAXBElement<FilterType>) unmarshaller.unmarshal(xmlStreamReader);
queryConstraint.setFilter(jaxbFilter.getValue());
} catch (JAXBException e) {
LOGGER.debug("JAXBException parsing OGC Filter:", e);
throw new CswException("JAXBException parsing OGC Filter:" + getConstraint());
} catch (Exception e) {
LOGGER.debug("Unable to parse OGC Filter:", e);
throw new CswException("Unable to parse OGC Filter:" + getConstraint());
}
} else {
throw new CswException("Invalid Constraint Language defined: " + getConstraintLanguage());
}
query.setConstraint(queryConstraint);
}
JAXBElement<QueryType> jaxbQuery = new JAXBElement<QueryType>(new QName(CswConstants.CSW_OUTPUT_SCHEMA), QueryType.class, query);
getRecords.setAbstractQuery(jaxbQuery);
return getRecords;
}
Aggregations