use of ddf.catalog.operation.impl.SourceInfoResponseImpl in project ddf by codice.
the class SourceOperations method getSourceInfo.
public SourceInfoResponse getSourceInfo(SourceInfoRequest sourceInfoRequest, boolean fanoutEnabled) throws SourceUnavailableException {
SourceInfoResponse response;
Set<SourceDescriptor> sourceDescriptors;
if (fanoutEnabled) {
return getFanoutSourceInfo(sourceInfoRequest);
}
boolean addCatalogProviderDescriptor = false;
try {
validateSourceInfoRequest(sourceInfoRequest);
// Obtain the source information based on the sourceIds in the
// request
sourceDescriptors = new LinkedHashSet<>();
Set<String> requestedSourceIds = sourceInfoRequest.getSourceIds();
// If it is an enterprise request than add all source information for the enterprise
if (sourceInfoRequest.isEnterprise()) {
sourceDescriptors = getFederatedSourceDescriptors(frameworkProperties.getFederatedSources().values(), true);
// If Ids are specified check if they are known sources
} else if (requestedSourceIds != null) {
LOGGER.debug("getSourceRequest contains requested source ids");
Set<FederatedSource> discoveredSources = new HashSet<>();
boolean containsId = false;
for (String requestedSourceId : requestedSourceIds) {
if (frameworkProperties.getFederatedSources().containsKey(requestedSourceId)) {
containsId = true;
LOGGER.debug("Found federated source: {}", requestedSourceId);
discoveredSources.add(frameworkProperties.getFederatedSources().get(requestedSourceId));
}
if (!containsId) {
LOGGER.debug("Unable to find source: {}", requestedSourceId);
// Check for the local catalog provider, DDF sourceId represents this
if (requestedSourceId.equals(getId())) {
LOGGER.debug("adding CatalogSourceDescriptor since it was in sourceId list as: {}", requestedSourceId);
addCatalogProviderDescriptor = true;
}
}
containsId = false;
}
sourceDescriptors = getFederatedSourceDescriptors(discoveredSources, addCatalogProviderDescriptor);
} else {
// only add the local catalogProviderdescriptor
addCatalogSourceDescriptor(sourceDescriptors);
}
response = new SourceInfoResponseImpl(sourceInfoRequest, null, sourceDescriptors);
} catch (RuntimeException re) {
LOGGER.debug("Exception during runtime while performing getSourceInfo", re);
throw new SourceUnavailableException("Exception during runtime while performing getSourceInfo");
}
return response;
}
use of ddf.catalog.operation.impl.SourceInfoResponseImpl in project ddf by codice.
the class SourceOperations method getFanoutSourceInfo.
/**
* Retrieves the {@link SourceDescriptor} info for all {@link FederatedSource}s in the fanout
* configuration, but the all of the source info, e.g., content types, for all of the available
* {@link FederatedSource}s is packed into one {@link SourceDescriptor} for the
* fanout configuration with the fanout's site name in it. This keeps the individual
* {@link FederatedSource}s' source info hidden from the external client.
*/
private SourceInfoResponse getFanoutSourceInfo(SourceInfoRequest sourceInfoRequest) throws SourceUnavailableException {
SourceInfoResponse response;
SourceDescriptorImpl sourceDescriptor;
try {
// request
if (sourceInfoRequest == null) {
throw new IllegalArgumentException("SourceInfoRequest was null");
}
Set<SourceDescriptor> sourceDescriptors = new LinkedHashSet<>();
Set<String> ids = sourceInfoRequest.getSourceIds();
// specified
if (ids != null) {
Optional<String> notLocal = ids.stream().filter(s -> !s.equals(getId())).findFirst();
if (notLocal.isPresent()) {
SourceUnavailableException sourceUnavailableException = new SourceUnavailableException("Unknown source: " + notLocal.get());
LOGGER.debug("Throwing SourceUnavailableException for unknown source: {}", notLocal.get(), sourceUnavailableException);
throw sourceUnavailableException;
}
}
// Fanout will only add one source descriptor with all the contents
Set<Source> availableSources = frameworkProperties.getFederatedSources().values().stream().map(source -> frameworkProperties.getSourcePoller().getCachedSource(source)).filter(source -> source != null && source.isAvailable()).collect(Collectors.toSet());
Set<ContentType> contentTypes = availableSources.stream().map(source -> frameworkProperties.getSourcePoller().getCachedSource(source)).filter(source -> source.getContentTypes() != null).map(Source::getContentTypes).flatMap(Collection::stream).collect(Collectors.toSet());
if (catalog != null) {
Source localSource = frameworkProperties.getSourcePoller().getCachedSource(catalog);
if (localSource != null && localSource.isAvailable()) {
availableSources.add(localSource);
contentTypes.addAll(localSource.getContentTypes());
}
}
// only reveal this sourceDescriptor, not the federated sources
sourceDescriptor = new SourceDescriptorImpl(this.getId(), contentTypes);
if (this.getVersion() != null) {
sourceDescriptor.setVersion(this.getVersion());
}
sourceDescriptor.setAvailable(availableSources.size() > 0);
sourceDescriptors.add(sourceDescriptor);
response = new SourceInfoResponseImpl(sourceInfoRequest, null, sourceDescriptors);
} catch (RuntimeException re) {
throw new SourceUnavailableException("Exception during runtime while performing getSourceInfo", re);
}
return response;
}
use of ddf.catalog.operation.impl.SourceInfoResponseImpl in project ddf by codice.
the class TestRestEndpoint method testGetDocumentSourcesSuccess.
/**
* Tests getting source information
*
* @throws Exception
*/
@Test
public void testGetDocumentSourcesSuccess() throws Exception {
final String localSourceId = "local";
final String fed1SourceId = "fed1";
final String fed2SourceId = "fed2";
final String version = "4.0";
final String jsonMimeTypeString = "application/json";
Set<ContentType> contentTypes = new HashSet<ContentType>();
contentTypes.add(new ContentTypeImpl("ct1", "v1"));
contentTypes.add(new ContentTypeImpl("ct2", "v2"));
contentTypes.add(new ContentTypeImpl("ct3", null));
JSONArray contentTypesInJSON = new JSONArray();
for (ContentType ct : contentTypes) {
JSONObject ob = new JSONObject();
ob.put("name", ct.getName());
ob.put("version", ct.getVersion() != null ? ct.getVersion() : "");
contentTypesInJSON.add(ob);
}
Set<SourceDescriptor> sourceDescriptors = new HashSet<SourceDescriptor>();
SourceDescriptorImpl localDescriptor = new SourceDescriptorImpl(localSourceId, contentTypes);
localDescriptor.setVersion(version);
localDescriptor.setAvailable(true);
SourceDescriptorImpl fed1Descriptor = new SourceDescriptorImpl(fed1SourceId, contentTypes);
fed1Descriptor.setVersion(version);
fed1Descriptor.setAvailable(true);
SourceDescriptorImpl fed2Descriptor = new SourceDescriptorImpl(fed2SourceId, null);
fed2Descriptor.setAvailable(true);
sourceDescriptors.add(localDescriptor);
sourceDescriptors.add(fed1Descriptor);
sourceDescriptors.add(fed2Descriptor);
SourceInfoResponse sourceInfoResponse = new SourceInfoResponseImpl(null, null, sourceDescriptors);
CatalogFramework framework = mock(CatalogFramework.class);
when(framework.getSourceInfo(isA(SourceInfoRequestEnterprise.class))).thenReturn(sourceInfoResponse);
RESTEndpoint restEndpoint = new RESTEndpoint(framework);
Response response = restEndpoint.getDocument(null, null);
assertEquals(OK, response.getStatus());
assertEquals(jsonMimeTypeString, response.getMetadata().get("Content-Type").get(0));
String responseMessage = IOUtils.toString((ByteArrayInputStream) response.getEntity());
JSONArray srcList = (JSONArray) new JSONParser().parse(responseMessage);
assertEquals(3, srcList.size());
for (Object o : srcList) {
JSONObject src = (JSONObject) o;
assertEquals(true, src.get("available"));
String id = (String) src.get("id");
if (id.equals(localSourceId)) {
assertThat((Iterable<Object>) src.get("contentTypes"), hasItems(contentTypesInJSON.toArray()));
assertEquals(contentTypes.size(), ((JSONArray) src.get("contentTypes")).size());
assertEquals(version, src.get("version"));
} else if (id.equals(fed1SourceId)) {
assertThat((Iterable<Object>) src.get("contentTypes"), hasItems(contentTypesInJSON.toArray()));
assertEquals(contentTypes.size(), ((JSONArray) src.get("contentTypes")).size());
assertEquals(version, src.get("version"));
} else if (id.equals(fed2SourceId)) {
assertEquals(0, ((JSONArray) src.get("contentTypes")).size());
assertEquals("", src.get("version"));
} else {
fail("Invalid ID returned");
}
}
}
use of ddf.catalog.operation.impl.SourceInfoResponseImpl in project ddf by codice.
the class SourceInfoResponseImplTest method testSourceInfoResponse.
@Test
public void testSourceInfoResponse() {
SourceDescriptor[] expectedDescriptorArr = new SourceDescriptor[] { firstSource, nextSource, lastSource };
SourceInfoResponse response = new SourceInfoResponseImpl(new SourceInfoRequestLocal(false), null, sourceDescriptors);
Set<SourceDescriptor> sources = response.getSourceInfo();
assertArrayEquals(expectedDescriptorArr, sources.toArray(new SourceDescriptor[sources.size()]));
}
use of ddf.catalog.operation.impl.SourceInfoResponseImpl in project ddf by codice.
the class SourceInfoResponseImplTest method testSourceInfoResponseNullSourceId.
@Test
public void testSourceInfoResponseNullSourceId() {
SourceDescriptor desc = new SourceDescriptorImpl(null, null);
sourceDescriptors.add(desc);
SourceDescriptor[] expectedDescriptorArr = new SourceDescriptor[] { firstSource, nextSource, lastSource, desc };
SourceInfoResponse response = new SourceInfoResponseImpl(new SourceInfoRequestLocal(true), null, sourceDescriptors);
Set<SourceDescriptor> sources = response.getSourceInfo();
assertArrayEquals(expectedDescriptorArr, sources.toArray(new SourceDescriptor[sources.size()]));
}
Aggregations