use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class CatalogBundle method waitForCatalogProvider.
public CatalogProvider waitForCatalogProvider() throws InterruptedException {
LOGGER.info("Waiting for CatalogProvider to become available.");
serviceManager.printInactiveBundlesInfo();
CatalogProvider provider = null;
CatalogFramework framework = null;
long timeoutLimit = System.currentTimeMillis() + CATALOG_PROVIDER_TIMEOUT;
boolean available = false;
while (!available) {
if (provider == null) {
ServiceReference<CatalogFramework> frameworkRef = serviceManager.getServiceReference(CatalogFramework.class);
ServiceReference<CatalogProvider> providerRef = serviceManager.getServiceReference(CatalogProvider.class);
if (providerRef != null) {
provider = serviceManager.getService(providerRef);
}
if (frameworkRef != null) {
framework = serviceManager.getService(frameworkRef);
}
}
if (framework != null && provider != null) {
SourceInfoRequestLocal sourceInfoRequestEnterprise = new SourceInfoRequestLocal(true);
try {
SourceInfoResponse sources = framework.getSourceInfo(sourceInfoRequestEnterprise);
Set<SourceDescriptor> sourceInfo = sources.getSourceInfo();
for (SourceDescriptor sourceDescriptor : sourceInfo) {
if (sourceDescriptor.getSourceId().equals(provider.getId())) {
available = sourceDescriptor.isAvailable() && provider.isAvailable();
LOGGER.info("CatalogProvider.isAvailable = {}", available);
}
}
} catch (SourceUnavailableException e) {
available = false;
}
}
if (!available) {
if (System.currentTimeMillis() > timeoutLimit) {
LOGGER.info("CatalogProvider.isAvailable = false");
serviceManager.printInactiveBundles();
fail(String.format("Catalog provider timed out after %d minutes.", TimeUnit.MILLISECONDS.toMinutes(CATALOG_PROVIDER_TIMEOUT)));
}
Thread.sleep(1000);
}
}
LOGGER.info("CatalogProvider is available.");
return provider;
}
use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class ReplicateCommand method query.
@Override
protected SourceResponse query(CatalogFacade framework, Filter filter, int startIndex, long querySize) {
QueryImpl query = new QueryImpl(filter);
query.setRequestsTotalResultsCount(true);
query.setPageSize((int) querySize);
query.setSortBy(new SortByImpl(Metacard.EFFECTIVE, SortOrder.DESCENDING));
QueryRequest queryRequest = new QueryRequestImpl(query, Arrays.asList(sourceId));
query.setStartIndex(startIndex);
SourceResponse response;
try {
LOGGER.debug("Querying with startIndex: {}", startIndex);
response = framework.query(queryRequest);
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
printErrorMessage(String.format("Received error from %s: %s%n", sourceId, e.getMessage()));
return null;
}
if (response.getProcessingDetails() != null && !response.getProcessingDetails().isEmpty()) {
for (SourceProcessingDetails details : response.getProcessingDetails()) {
LOGGER.debug("Got Issues: {}", details.getWarnings());
}
return null;
}
final long totalHits = response.getHits();
if (totalHits == 0) {
console.println("No records were found to replicate.");
return null;
}
return response;
}
use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class OpenSearchEndpoint method executeQuery.
/**
* Executes the OpenSearchQuery and formulates the response
*
* @param format - of the results in the response
* @param query - the query to execute
* @param ui -the ui information to use to format the results
* @param properties
* @return the response on the query
*/
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
Response response = null;
String queryFormat = format;
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
LOGGER.debug("Attempting to execute query: {}", query.toString());
try {
Map<String, Serializable> arguments = new HashMap<String, Serializable>();
String organization = framework.getOrganization();
String url = ui.getRequestUri().toString();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("organization: {}", organization);
LOGGER.debug("url: {}", url);
}
arguments.put("organization", organization);
arguments.put("url", url);
// interval
if (subscriptionList != null && !subscriptionList.isEmpty()) {
String subscription = subscriptionList.get(0);
LOGGER.debug("Subscription: {}", subscription);
arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
if (intervalList != null && !intervalList.isEmpty()) {
arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
}
}
if (StringUtils.isEmpty(queryFormat)) {
queryFormat = DEFAULT_FORMAT;
}
if (query.getFilter() != null) {
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
QueryResponse queryResponse;
LOGGER.debug("Sending query");
queryResponse = framework.query(queryRequest);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
} else {
// No query was specified
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
// Create a dummy QueryResponse with zero results
QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<Result>(), 0);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
if (null != content) {
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
}
}
} catch (UnsupportedQueryException ce) {
LOGGER.info("Unsupported query", ce);
response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
} catch (CatalogTransformerException e) {
LOGGER.info("Error transforming response", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
} catch (FederationException e) {
LOGGER.info("Error executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
} catch (SourceUnavailableException e) {
LOGGER.info("Error executing query because the underlying source was unavailable.", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
} catch (RuntimeException e) {
// Account for any runtime exceptions and send back a server error
// this prevents full stacktraces returning to the client
// this allows for a graceful server error to be returned
LOGGER.info("RuntimeException on executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
}
return response;
}
use of ddf.catalog.source.SourceUnavailableException in project alliance by codice.
the class FindChildrenStreamEndPluginTest method testIrregularBatching.
/**
* It is possible (in theory) that the catalog could return irregular sized batches. We want to
* make sure this case is handled properly when mixed with intermittent exceptions. In this test,
* we are going to return a total of 10 metacards with exceptions intermixed.
*/
@Test
public void testIrregularBatching() throws UnsupportedQueryException, SourceUnavailableException, FederationException {
long expectedCount = 10;
Metacard metacard1 = mock(Metacard.class);
Metacard metacard2 = mock(Metacard.class);
Metacard metacard3 = mock(Metacard.class);
Metacard metacard4 = mock(Metacard.class);
Metacard metacard5 = mock(Metacard.class);
Metacard metacard6 = mock(Metacard.class);
Metacard metacard7 = mock(Metacard.class);
Metacard metacard8 = mock(Metacard.class);
Metacard metacard9 = mock(Metacard.class);
Metacard metacard10 = mock(Metacard.class);
Result result1 = mock(Result.class);
when(result1.getMetacard()).thenReturn(metacard1);
Result result2 = mock(Result.class);
when(result2.getMetacard()).thenReturn(metacard2);
Result result3 = mock(Result.class);
when(result3.getMetacard()).thenReturn(metacard3);
Result result4 = mock(Result.class);
when(result4.getMetacard()).thenReturn(metacard4);
Result result5 = mock(Result.class);
when(result5.getMetacard()).thenReturn(metacard5);
Result result6 = mock(Result.class);
when(result6.getMetacard()).thenReturn(metacard6);
Result result7 = mock(Result.class);
when(result7.getMetacard()).thenReturn(metacard7);
Result result8 = mock(Result.class);
when(result8.getMetacard()).thenReturn(metacard8);
Result result9 = mock(Result.class);
when(result9.getMetacard()).thenReturn(metacard9);
Result result10 = mock(Result.class);
when(result10.getMetacard()).thenReturn(metacard10);
QueryResponse queryResponse1 = mock(QueryResponse.class);
when(queryResponse1.getHits()).thenReturn(expectedCount);
when(queryResponse1.getResults()).thenReturn(Arrays.asList(result1, result2));
QueryResponse queryResponse2 = mock(QueryResponse.class);
when(queryResponse2.getHits()).thenReturn(expectedCount);
when(queryResponse2.getResults()).thenReturn(Arrays.asList(result3, result4, result5));
QueryResponse queryResponse3 = mock(QueryResponse.class);
when(queryResponse3.getHits()).thenReturn(expectedCount);
when(queryResponse3.getResults()).thenReturn(Arrays.asList(result6, result7, result8, result9, result10));
when(catalogFramework.query(any())).thenThrow(SourceUnavailableException.class).thenReturn(queryResponse1, queryResponse2).thenThrow(SourceUnavailableException.class).thenReturn(queryResponse3);
setIsParentDirty(true);
findChildrenStreamEndPlugin.streamEnded(context);
ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(handler, times(3)).handle(Matchers.eq(context), Matchers.eq(parentMetacard), argumentCaptor.capture());
verify(handler).end(Matchers.eq(context), Matchers.eq(parentMetacard));
long capturedMetacardCount = argumentCaptor.getAllValues().stream().mapToLong(List::size).sum();
assertThat(capturedMetacardCount, is(expectedCount));
}
use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.
the class SourceConfigurationAdminPlugin method getConfigurationData.
/**
* Returns a map of configuration data that should be appended to the configurationDataMap
* parameter. The configurationDataMap that is passed into this function is unmodifiable and is
* passed in to simply expose what information already exists.
*
* @param configurationPid service.pid for the ConfigurationAdmin configuration
* @param configurationDataMap map of what properties have already been added to the configuration
* in question
* @param bundleContext used to retrieve list of services
* @return Map defining additional properties to add to the configuration
*/
@Override
public Map<String, Object> getConfigurationData(String configurationPid, Map<String, Object> configurationDataMap, BundleContext bundleContext) {
LOGGER.debug("Obtaining configuration data for the following configuration PID: {}", configurationPid);
Map<String, Object> statusMap = new HashMap<String, Object>();
try {
List<ServiceReference<? extends Source>> refs = new ArrayList<ServiceReference<? extends Source>>();
refs.addAll(bundleContext.getServiceReferences(FederatedSource.class, null));
refs.addAll(bundleContext.getServiceReferences(CatalogProvider.class, null));
Set<SourceDescriptor> sources = null;
if (catalogFramework != null) {
sources = catalogFramework.getSourceInfo(new SourceInfoRequestEnterprise(true)).getSourceInfo();
}
boolean foundSources = CollectionUtils.isNotEmpty(sources);
for (ServiceReference<? extends Source> ref : refs) {
Source superService = bundleContext.getService(ref);
if (superService instanceof ConfiguredService) {
ConfiguredService cs = (ConfiguredService) superService;
LOGGER.debug("ConfiguredService configuration PID: {}", cs.getConfigurationPid());
boolean csConfigPidMatchesTargetPid = false;
if (StringUtils.isNotEmpty(cs.getConfigurationPid()) && cs.getConfigurationPid().equals(configurationPid)) {
csConfigPidMatchesTargetPid = true;
}
if (foundSources) {
// class name, then we can match them up this way.
if (csConfigPidMatchesTargetPid || cs.getClass().getCanonicalName().equals(configurationPid)) {
for (SourceDescriptor descriptor : sources) {
if (descriptor.getSourceId().equals(superService.getId())) {
statusMap.put("available", descriptor.isAvailable());
statusMap.put("sourceId", descriptor.getSourceId());
return statusMap;
}
}
}
} else if (csConfigPidMatchesTargetPid) {
// we don't want to call isAvailable because that can
// potentially block execution but if for some reason we
// have no catalog framework, just hit the source
// directly
statusMap.put("available", superService.isAvailable());
return statusMap;
}
}
}
} catch (org.osgi.framework.InvalidSyntaxException ise) {
// this should never happen because the filter is always null
LOGGER.debug("Error reading LDAP service filter", ise);
} catch (SourceUnavailableException sue) {
LOGGER.info("Unable to retrieve sources from Catalog Framework", sue);
}
return statusMap;
}
Aggregations