use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException in project ddf by codice.
the class WfsSource method handleWebApplicationException.
private String handleWebApplicationException(WebApplicationException wae) {
Response response = wae.getResponse();
WfsException wfsException = new WfsResponseExceptionMapper().fromResponse(response);
return "Error received from WFS Server " + getId() + "\n" + wfsException.getMessage();
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException in project ddf by codice.
the class TestWfsSource method getWfsSource.
public WfsSource getWfsSource(final String schema, final FilterCapabilities filterCapabilities, final String srsName, final int numFeatures, final boolean throwExceptionOnDescribeFeatureType, boolean prefix, int numReturned) throws WfsException, SecurityServiceException {
mockFactory = mock(SecureCxfClientFactory.class);
when(mockFactory.getClient()).thenReturn(mockWfs);
// GetCapabilities Response
when(mockWfs.getCapabilities(any(GetCapabilitiesRequest.class))).thenReturn(mockCapabilites);
when(mockFeatureCollection.getMembers()).thenAnswer(new Answer<List<Metacard>>() {
@Override
public List<Metacard> answer(InvocationOnMock invocation) {
// Create as many metacards as there are features
List<Metacard> metacards = new ArrayList<Metacard>(numFeatures);
for (int i = 0; i < numFeatures; i++) {
MetacardImpl mc = new MetacardImpl();
mc.setId("ID_" + String.valueOf(i + 1));
metacards.add(mc);
}
return metacards;
}
});
if (numReturned != NULL_NUM_RETURNED) {
when(mockFeatureCollection.getNumberReturned()).thenReturn(BigInteger.valueOf(numReturned));
} else {
when(mockFeatureCollection.getNumberReturned()).thenReturn(null);
}
when(mockWfs.getFeature(any(GetFeatureType.class))).thenReturn(mockFeatureCollection);
mockCapabilites.setFilterCapabilities(filterCapabilities);
when(mockAvailabilityTask.isAvailable()).thenReturn(true);
mockCapabilites.setFeatureTypeList(new FeatureTypeListType());
for (int ii = 0; ii < numFeatures; ii++) {
FeatureTypeType feature = new FeatureTypeType();
QName qName;
if (prefix) {
qName = new QName("http://example.com", SAMPLE_FEATURE_NAME + ii, "Prefix" + ii);
} else {
qName = new QName("http://example.com", SAMPLE_FEATURE_NAME + ii);
}
feature.setName(qName);
feature.setDefaultCRS(GeospatialUtil.EPSG_4326_URN);
mockCapabilites.getFeatureTypeList().getFeatureType().add(feature);
}
XmlSchema xmlSchema = null;
if (StringUtils.isNotBlank(schema)) {
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
WfsUriResolver wfsUriResolver = new WfsUriResolver();
wfsUriResolver.setGmlNamespace(Wfs20Constants.GML_3_2_NAMESPACE);
wfsUriResolver.setWfsNamespace(Wfs20Constants.WFS_2_0_NAMESPACE);
schemaCollection.setSchemaResolver(wfsUriResolver);
xmlSchema = schemaCollection.read(new StreamSource(new ByteArrayInputStream(schema.getBytes())));
}
if (throwExceptionOnDescribeFeatureType) {
when(mockWfs.describeFeatureType(any(DescribeFeatureTypeRequest.class))).thenThrow(new WfsException(""));
} else {
when(mockWfs.describeFeatureType(any(DescribeFeatureTypeRequest.class))).thenReturn(xmlSchema);
}
WfsSource wfsSource = new WfsSource(new GeotoolsFilterAdapterImpl(), mockContext, mockAvailabilityTask, mockFactory, encryptionService);
wfsSource.setFeatureCollectionReader(mockReader);
return wfsSource;
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException in project ddf by codice.
the class TestWfsResponseExceptionMapper method testCswExceptionWithNullResponse.
@Test
public void testCswExceptionWithNullResponse() {
WfsException cswException = new WfsResponseExceptionMapper().fromResponse(null);
assertThat(cswException.getMessage(), equalTo("Error handling response, response is null"));
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException in project ddf by codice.
the class WfsSource method getCapabilities.
private WFSCapabilitiesType getCapabilities() throws SecurityServiceException {
WFSCapabilitiesType capabilities = null;
Wfs wfs = factory.getClient();
try {
capabilities = wfs.getCapabilities(new GetCapabilitiesRequest());
} catch (WfsException wfse) {
LOGGER.info(WFS_ERROR_MESSAGE + " Received HTTP code '{}' from server for source with id='{}'", wfse.getHttpStatus(), getId());
LOGGER.debug(WFS_ERROR_MESSAGE, wfse);
} catch (WebApplicationException wae) {
LOGGER.debug(handleWebApplicationException(wae), wae);
} catch (Exception e) {
handleClientException(e);
}
return capabilities;
}
use of org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException in project ddf by codice.
the class WfsSource method query.
@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
Wfs wfs = factory.getClient();
Query query = request.getQuery();
LOGGER.debug("WFS Source {}: Received query: \n{}", getId(), query);
if (query.getStartIndex() < 1) {
throw new UnsupportedQueryException("Start Index is one-based and must be an integer greater than 0; should not be [" + query.getStartIndex() + "]");
}
SourceResponseImpl simpleResponse = null;
// WFS v1.0 specification does not support response indicating total
// number
// of features satisfying query constraints.
// Hence, we save off the original
// page size from the query request and create a copy of the query,
// changing
// the page size by a multiplier and the current page number of results
// so that
// more features are returned as the user pages through the results,
// getting
// a better sense of how many total features exist that satisfy the
// query.
int origPageSize = query.getPageSize();
if (origPageSize <= 0 || origPageSize > WFS_MAX_FEATURES_RETURNED) {
origPageSize = WFS_MAX_FEATURES_RETURNED;
}
QueryImpl modifiedQuery = new QueryImpl(query);
// Determine current page number of results being requested.
// Example: startIndex = 21 and origPageSize=10, then requesting to go
// to page number 3.
// Note: Integer division will truncate remainders so 4 / 2 will return 0 and not .5. Also,
// pages are numbered 1 - N so we add 1 to the result
int pageNumber = query.getStartIndex() / origPageSize + 1;
// Modified page size is based on current page number and a constant
// multiplier,
// but limited to a max value to prevent time consuming queries just to
// get an
// approximation of total number of features.
// So as page number increases the pageSize increases.
// Example:
// pageNumber=2, modifiedPageSize=60
// pageNumber=3, modifiedPageSize=90
int modifiedPageSize = Math.min(pageNumber * origPageSize * WFS_QUERY_PAGE_SIZE_MULTIPLIER, WFS_MAX_FEATURES_RETURNED);
LOGGER.debug("WFS Source {}: modified page size = {}", getId(), modifiedPageSize);
modifiedQuery.setPageSize(modifiedPageSize);
GetFeatureType getFeature = buildGetFeatureRequest(modifiedQuery);
try {
LOGGER.debug("WFS Source {}: Sending query ...", getId());
WfsFeatureCollection featureCollection = wfs.getFeature(getFeature);
if (featureCollection == null) {
throw new UnsupportedQueryException("Invalid results returned from server");
}
availabilityTask.updateLastAvailableTimestamp(System.currentTimeMillis());
LOGGER.debug("WFS Source {}: Received featureCollection with {} metacards.", getId(), featureCollection.getFeatureMembers().size());
// Only return the number of results originally asked for in the
// query, or the entire list of results if it is smaller than the
// original page size.
int numberOfResultsToReturn = Math.min(origPageSize, featureCollection.getFeatureMembers().size());
List<Result> results = new ArrayList<Result>(numberOfResultsToReturn);
int stopIndex = Math.min((origPageSize * pageNumber) + query.getStartIndex(), featureCollection.getFeatureMembers().size() + 1);
LOGGER.debug("WFS Source {}: startIndex = {}, stopIndex = {}, origPageSize = {}, pageNumber = {}", getId(), query.getStartIndex(), stopIndex, origPageSize, pageNumber);
for (int i = query.getStartIndex(); i < stopIndex; i++) {
Metacard mc = featureCollection.getFeatureMembers().get(i - 1);
mc = transform(mc, DEFAULT_WFS_TRANSFORMER_ID);
Result result = new ResultImpl(mc);
results.add(result);
debugResult(result);
}
Long totalHits = (long) featureCollection.getFeatureMembers().size();
simpleResponse = new SourceResponseImpl(request, results, totalHits);
} catch (WfsException wfse) {
LOGGER.debug(WFS_ERROR_MESSAGE, wfse);
throw new UnsupportedQueryException("Error received from WFS Server", wfse);
} catch (Exception ce) {
String msg = handleClientException(ce);
throw new UnsupportedQueryException(msg, ce);
}
return simpleResponse;
}
Aggregations