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));
}
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;
}
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;
}
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());
}
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));
}
Aggregations