Search in sources :

Example 21 with CswException

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.

the class TestCswEndpoint method testDescribeRecordValidOutputFormat.

@Test
public void testDescribeRecordValidOutputFormat() {
    DescribeRecordRequest drr = createDefaultDescribeRecordRequest();
    drr.setTypeName(VALID_PREFIX_LOCAL_TYPE);
    drr.setOutputFormat(CswConstants.OUTPUT_FORMAT_XML);
    DescribeRecordResponseType drrt = null;
    try {
        drrt = csw.describeRecord(drr);
    } catch (CswException e) {
        fail("DescribeRecord failed with message '" + e.getMessage() + "'");
    }
    assertThat(drrt, notNullValue());
    assertThat(drrt.getSchemaComponent(), notNullValue());
    List<SchemaComponentType> schemaComponents = drrt.getSchemaComponent();
    assertThat(schemaComponents.size(), is(1));
}
Also used : SchemaComponentType(net.opengis.cat.csw.v_2_0_2.SchemaComponentType) DescribeRecordRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.DescribeRecordRequest) DescribeRecordResponseType(net.opengis.cat.csw.v_2_0_2.DescribeRecordResponseType) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) Test(org.junit.Test)

Example 22 with CswException

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.

the class CswResponseExceptionMapper method convertToCswException.

private CswException convertToCswException(ExceptionReport report) {
    CswException cswException = null;
    List<ExceptionType> list = new ArrayList<ExceptionType>(report.getException());
    for (ExceptionType exceptionType : list) {
        String exceptionCode = exceptionType.getExceptionCode();
        String locator = exceptionType.getLocator();
        List<String> exceptionText = exceptionType.getExceptionText();
        StringBuilder exceptionMsg = new StringBuilder();
        // Exception code is required per CSW schema, but check it anyway
        if (StringUtils.isNotBlank(exceptionCode)) {
            exceptionMsg.append("exceptionCode = " + exceptionCode + "\n");
        } else {
            exceptionMsg.append("exceptionCode = UNSPECIFIED");
        }
        // Locator and exception text(s) are both optional
        if (StringUtils.isNotBlank(locator)) {
            exceptionMsg.append("locator = " + locator + "\n");
        }
        if (!CollectionUtils.isEmpty(exceptionText)) {
            for (String text : exceptionText) {
                exceptionMsg.append(text);
            }
        }
        cswException = new CswException(exceptionMsg.toString());
    }
    if (null == cswException) {
        cswException = new CswException("Empty Exception Report (version = " + report.getVersion() + ")");
    }
    return cswException;
}
Also used : ExceptionType(net.opengis.ows.v_1_0_0.ExceptionType) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ArrayList(java.util.ArrayList)

Example 23 with CswException

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.

the class CswResponseExceptionMapper method fromResponse.

@Override
public CswException fromResponse(Response response) {
    CswException cswException = null;
    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                if (is.markSupported()) {
                    is.reset();
                }
                msg = IOUtils.toString(is);
            } catch (IOException e) {
                cswException = new CswException("Error received from remote Csw server" + (msg != null ? ": " + msg : ""));
                LOGGER.info("Unable to parse exception report: {}", e.getMessage());
                LOGGER.debug("Unable to parse exception report: {}", e);
            }
            if (msg != null) {
                try {
                    JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<ExceptionReport>();
                    Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class).createUnmarshaller();
                    XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
                    xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                    xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
                    xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
                    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(msg));
                    ExceptionReport report = (ExceptionReport) um.unmarshal(xmlStreamReader);
                    cswException = convertToCswException(report);
                } catch (JAXBException | XMLStreamException e) {
                    cswException = new CswException("Error received from remote Csw server: " + msg, e);
                    LOGGER.info("Error parsing the exception report: {}", e.getMessage());
                    LOGGER.debug("Error parsing the exception report", e);
                }
            }
        } else {
            cswException = new CswException("Error reading response, entity type not understood: " + response.getEntity().getClass().getName());
        }
        cswException.setHttpStatus(response.getStatus());
    } else {
        cswException = new CswException("Error handling response, response is null");
    }
    return cswException;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) JAXBException(javax.xml.bind.JAXBException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) IOException(java.io.IOException) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 24 with CswException

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException 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());
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Test(org.junit.Test)

Example 25 with CswException

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException 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));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SortBy(org.opengis.filter.sort.SortBy) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Test(org.junit.Test)

Aggregations

CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)84 Test (org.junit.Test)55 CapabilitiesType (net.opengis.cat.csw.v_2_0_2.CapabilitiesType)19 GetCapabilitiesType (net.opengis.cat.csw.v_2_0_2.GetCapabilitiesType)19 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)17 QueryImpl (ddf.catalog.operation.impl.QueryImpl)13 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)13 DescribeRecordResponseType (net.opengis.cat.csw.v_2_0_2.DescribeRecordResponseType)13 Matchers.anyString (org.mockito.Matchers.anyString)13 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)11 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)10 ArrayList (java.util.ArrayList)10 SchemaComponentType (net.opengis.cat.csw.v_2_0_2.SchemaComponentType)10 GetCapabilitiesRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.GetCapabilitiesRequest)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 IOException (java.io.IOException)9 DescribeRecordRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.DescribeRecordRequest)9 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)7 Filter (org.opengis.filter.Filter)7 JAXBException (javax.xml.bind.JAXBException)6