Search in sources :

Example 1 with SourceInfoResponseImpl

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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse)

Example 2 with SourceInfoResponseImpl

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) LoggerFactory(org.slf4j.LoggerFactory) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Source(ddf.catalog.source.Source) SourceDescriptor(ddf.catalog.source.SourceDescriptor) StorageProvider(ddf.catalog.content.StorageProvider) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceDescriptorComparator(ddf.catalog.util.impl.SourceDescriptorComparator) LinkedHashSet(java.util.LinkedHashSet) DescribableImpl(ddf.catalog.util.impl.DescribableImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) ContentType(ddf.catalog.data.ContentType) Logger(org.slf4j.Logger) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) Collection(java.util.Collection) FederatedSource(ddf.catalog.source.FederatedSource) Set(java.util.Set) Collectors(java.util.stream.Collectors) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) List(java.util.List) CatalogProvider(ddf.catalog.source.CatalogProvider) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) Optional(java.util.Optional) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) ContentType(ddf.catalog.data.ContentType) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) Source(ddf.catalog.source.Source) FederatedSource(ddf.catalog.source.FederatedSource) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse)

Example 3 with SourceInfoResponseImpl

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");
        }
    }
}
Also used : ContentTypeImpl(ddf.catalog.data.impl.ContentTypeImpl) SourceDescriptor(ddf.catalog.source.SourceDescriptor) ContentType(ddf.catalog.data.ContentType) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) JSONArray(net.minidev.json.JSONArray) Matchers.anyString(org.mockito.Matchers.anyString) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) JSONObject(net.minidev.json.JSONObject) CatalogFramework(ddf.catalog.CatalogFramework) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with SourceInfoResponseImpl

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()]));
}
Also used : SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceInfoRequestLocal(ddf.catalog.operation.impl.SourceInfoRequestLocal) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) Test(org.junit.Test)

Example 5 with SourceInfoResponseImpl

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()]));
}
Also used : SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) SourceInfoRequestLocal(ddf.catalog.operation.impl.SourceInfoRequestLocal) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) Test(org.junit.Test)

Aggregations

SourceInfoResponseImpl (ddf.catalog.operation.impl.SourceInfoResponseImpl)5 SourceDescriptor (ddf.catalog.source.SourceDescriptor)5 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)3 SourceDescriptorImpl (ddf.catalog.source.impl.SourceDescriptorImpl)3 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 ContentType (ddf.catalog.data.ContentType)2 SourceInfoRequestLocal (ddf.catalog.operation.impl.SourceInfoRequestLocal)2 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 CatalogFramework (ddf.catalog.CatalogFramework)1 StorageProvider (ddf.catalog.content.StorageProvider)1 ContentTypeImpl (ddf.catalog.data.impl.ContentTypeImpl)1 FrameworkProperties (ddf.catalog.impl.FrameworkProperties)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 SourceInfoRequest (ddf.catalog.operation.SourceInfoRequest)1 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)1 CatalogProvider (ddf.catalog.source.CatalogProvider)1