Search in sources :

Example 1 with GetRecordsType

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;
}
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 GetRecordsType

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)));
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) JAXBElement(javax.xml.bind.JAXBElement) BinaryContent(ddf.catalog.data.BinaryContent) AcknowledgementType(net.opengis.cat.csw.v_2_0_2.AcknowledgementType) Test(org.junit.Test)

Example 3 with GetRecordsType

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));
}
Also used : HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) SearchResultsType(net.opengis.cat.csw.v_2_0_2.SearchResultsType) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) JAXBElement(javax.xml.bind.JAXBElement) ObjectFactory(net.opengis.cat.csw.v_2_0_2.ObjectFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ElementSetNameType(net.opengis.cat.csw.v_2_0_2.ElementSetNameType) GetRecordsResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Ignore(jdk.nashorn.internal.ir.annotations.Ignore)

Example 4 with GetRecordsType

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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Marshaller(javax.xml.bind.Marshaller) QueryRequest(ddf.catalog.operation.QueryRequest) JAXBException(javax.xml.bind.JAXBException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) FederationException(ddf.catalog.federation.FederationException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) MimeTypeParseException(javax.activation.MimeTypeParseException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) FederationException(ddf.catalog.federation.FederationException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ParseException(com.vividsolutions.jts.io.ParseException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringWriter(java.io.StringWriter) ObjectFactory(net.opengis.cat.csw.v_2_0_2.ObjectFactory) QueryResponse(ddf.catalog.operation.QueryResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 5 with GetRecordsType

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;
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequest(ddf.catalog.operation.QueryRequest) CswRecordMapperFilterVisitor(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.mappings.CswRecordMapperFilterVisitor) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType)

Aggregations

GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)57 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)39 Test (org.junit.Test)32 JAXBElement (javax.xml.bind.JAXBElement)30 Matchers.anyString (org.mockito.Matchers.anyString)22 QueryImpl (ddf.catalog.operation.impl.QueryImpl)21 QName (javax.xml.namespace.QName)21 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)19 ArrayList (java.util.ArrayList)16 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)14 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)14 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)13 SourceResponse (ddf.catalog.operation.SourceResponse)11 ElementSetNameType (net.opengis.cat.csw.v_2_0_2.ElementSetNameType)10 ObjectFactory (net.opengis.cat.csw.v_2_0_2.ObjectFactory)10 QueryRequest (ddf.catalog.operation.QueryRequest)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 XStream (com.thoughtworks.xstream.XStream)7 JAXBException (javax.xml.bind.JAXBException)6 GetRecordsResponseType (net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType)6