Search in sources :

Example 1 with ElementSetType

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;
}
Also used : Query(ddf.catalog.operation.Query) WebApplicationException(javax.ws.rs.WebApplicationException) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) JAXBException(javax.xml.bind.JAXBException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Result(ddf.catalog.data.Result)

Example 2 with ElementSetType

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"));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) QName(javax.xml.namespace.QName) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) MetacardMarshaller(org.codice.ddf.registry.schemabindings.helper.MetacardMarshaller) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Subject(ddf.security.Subject) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) CapabilitiesType(net.opengis.cat.csw.v_2_0_2.CapabilitiesType) ElementSetType(net.opengis.cat.csw.v_2_0_2.ElementSetType) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 3 with ElementSetType

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;
}
Also used : QName(javax.xml.namespace.QName) ElementSetType(net.opengis.cat.csw.v_2_0_2.ElementSetType) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ArrayList(java.util.ArrayList) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) ResultType(net.opengis.cat.csw.v_2_0_2.ResultType)

Example 4 with ElementSetType

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;
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) ElementSetType(net.opengis.cat.csw.v_2_0_2.ElementSetType)

Example 5 with ElementSetType

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;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SortByType(net.opengis.filter.v_1_1_0.SortByType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) StringReader(java.io.StringReader) ElementSetNameType(net.opengis.cat.csw.v_2_0_2.ElementSetNameType) SortPropertyType(net.opengis.filter.v_1_1_0.SortPropertyType) DistributedSearchType(net.opengis.cat.csw.v_2_0_2.DistributedSearchType) Unmarshaller(javax.xml.bind.Unmarshaller) PropertyNameType(net.opengis.filter.v_1_1_0.PropertyNameType) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) JAXBElement(javax.xml.bind.JAXBElement) LinkedList(java.util.LinkedList) JAXBException(javax.xml.bind.JAXBException) SortOrderType(net.opengis.filter.v_1_1_0.SortOrderType) FilterType(net.opengis.filter.v_1_1_0.FilterType) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

QName (javax.xml.namespace.QName)5 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)4 ElementSetType (net.opengis.cat.csw.v_2_0_2.ElementSetType)4 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)4 SourceResponseImpl (ddf.catalog.operation.impl.SourceResponseImpl)3 Result (ddf.catalog.data.Result)2 QueryRequest (ddf.catalog.operation.QueryRequest)2 Subject (ddf.security.Subject)2 ArrayList (java.util.ArrayList)2 JAXBException (javax.xml.bind.JAXBException)2 ElementSetNameType (net.opengis.cat.csw.v_2_0_2.ElementSetNameType)2 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)2 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)2 SortByType (net.opengis.filter.v_1_1_0.SortByType)2 MetacardMarshaller (org.codice.ddf.registry.schemabindings.helper.MetacardMarshaller)2 Csw (org.codice.ddf.spatial.ogc.csw.catalog.common.Csw)2 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)2 BundleContext (org.osgi.framework.BundleContext)2 Converter (com.thoughtworks.xstream.converters.Converter)1 Metacard (ddf.catalog.data.Metacard)1