Search in sources :

Example 6 with SourceInfoRequestEnterprise

use of ddf.catalog.operation.impl.SourceInfoRequestEnterprise in project ddf by codice.

the class CatalogFrameworkImplTest method testGetAllSiteNames.

@Test
public void testGetAllSiteNames() {
    String frameworkName = "DDF";
    CatalogProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, new Date());
    List<FederatedSource> federatedSources = createDefaultFederatedSourceList(true);
    // Expected Set of Names
    Set<String> expectedNameSet = new HashSet<String>();
    for (FederatedSource curSite : federatedSources) {
        expectedNameSet.add(curSite.getId());
    }
    // Mock register the provider in the container
    // Mock the source poller
    SourcePoller mockPoller = mock(SourcePoller.class);
    when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
    FrameworkProperties frameworkProperties = new FrameworkProperties();
    frameworkProperties.setSourcePoller(mockPoller);
    Map<String, FederatedSource> sources = new HashMap<>();
    for (FederatedSource federatedSource : federatedSources) {
        sources.put(federatedSource.getId(), federatedSource);
    }
    frameworkProperties.setFederatedSources(sources);
    frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
    CatalogFrameworkImpl framework = createFramework(frameworkProperties);
    framework.setId(frameworkName);
    // Returned Set of Names
    // Returned Sites
    SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
    SourceInfoResponse response = null;
    try {
        response = framework.getSourceInfo(request);
    } catch (SourceUnavailableException e) {
        LOGGER.debug("SourceUnavilable", e);
        fail();
    }
    assert (response != null);
    Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
    // should contain ONLY the original federated sites
    assertEquals(expectedNameSet.size(), sourceDescriptors.size());
    Set<String> returnedSourceIds = new HashSet<String>();
    for (SourceDescriptor sd : sourceDescriptors) {
        returnedSourceIds.add(sd.getSourceId());
    }
    for (String id : returnedSourceIds) {
        LOGGER.debug("returned sourceId: {}", id);
    }
    assertTrue(expectedNameSet.equals(returnedSourceIds));
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) ContentType(ddf.catalog.data.ContentType) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) Date(java.util.Date) Source(ddf.catalog.source.Source) ByteSource(com.google.common.io.ByteSource) CachedSource(ddf.catalog.util.impl.CachedSource) FederatedSource(ddf.catalog.source.FederatedSource) SourcePoller(ddf.catalog.util.impl.SourcePoller) FederatedSource(ddf.catalog.source.FederatedSource) CatalogProvider(ddf.catalog.source.CatalogProvider) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with SourceInfoRequestEnterprise

use of ddf.catalog.operation.impl.SourceInfoRequestEnterprise in project ddf by codice.

the class CatalogFrameworkImplTest method testGetFederatedSources.

@Test
public void testGetFederatedSources() {
    SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
    SourceInfoResponse response = null;
    try {
        response = framework.getSourceInfo(request);
    } catch (SourceUnavailableException e) {
        fail();
    }
    Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
    for (SourceDescriptor descriptor : sourceDescriptors) {
        LOGGER.debug("Descriptor id: {}", descriptor.getSourceId());
    }
    // The "+1" is to account for the CatalogFramework source descriptor.
    // Even if no local catalog provider is configured, the catalog framework's
    // site info is included in the SourceDescriptos list.
    assertEquals(federatedSources.size() + 1, sourceDescriptors.size());
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Test(org.junit.Test)

Example 8 with SourceInfoRequestEnterprise

use of ddf.catalog.operation.impl.SourceInfoRequestEnterprise in project ddf by codice.

the class RESTEndpoint method getDocument.

/**
     * REST Get. Retrieves information regarding sources available.
     *
     * @param uriInfo
     * @param httpRequest
     * @return
     */
@GET
@Path(SOURCES_PATH)
public Response getDocument(@Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
    BinaryContent content;
    ResponseBuilder responseBuilder;
    String sourcesString = null;
    JSONArray resultsList = new JSONArray();
    SourceInfoResponse sources;
    try {
        SourceInfoRequestEnterprise sourceInfoRequestEnterprise = new SourceInfoRequestEnterprise(true);
        sources = catalogFramework.getSourceInfo(sourceInfoRequestEnterprise);
        for (SourceDescriptor source : sources.getSourceInfo()) {
            JSONObject sourceObj = new JSONObject();
            sourceObj.put("id", source.getSourceId());
            sourceObj.put("version", source.getVersion() != null ? source.getVersion() : "");
            sourceObj.put("available", Boolean.valueOf(source.isAvailable()));
            JSONArray contentTypesObj = new JSONArray();
            if (source.getContentTypes() != null) {
                for (ContentType contentType : source.getContentTypes()) {
                    if (contentType != null && contentType.getName() != null) {
                        JSONObject contentTypeObj = new JSONObject();
                        contentTypeObj.put("name", contentType.getName());
                        contentTypeObj.put("version", contentType.getVersion() != null ? contentType.getVersion() : "");
                        contentTypesObj.add(contentTypeObj);
                    }
                }
            }
            sourceObj.put("contentTypes", contentTypesObj);
            resultsList.add(sourceObj);
        }
    } catch (SourceUnavailableException e) {
        LOGGER.info("Unable to retrieve Sources. {}", e.getMessage());
        LOGGER.debug("Unable to retrieve Sources", e);
    }
    sourcesString = JSONValue.toJSONString(resultsList);
    content = new BinaryContentImpl(new ByteArrayInputStream(sourcesString.getBytes(StandardCharsets.UTF_8)), jsonMimeType);
    responseBuilder = Response.ok(content.getInputStream(), content.getMimeTypeValue());
    // Add the Accept-ranges header to let the client know that we accept ranges in bytes
    responseBuilder.header(HEADER_ACCEPT_RANGES, BYTES);
    return responseBuilder.build();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) JSONObject(net.minidev.json.JSONObject) ContentType(ddf.catalog.data.ContentType) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONArray(net.minidev.json.JSONArray) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with SourceInfoRequestEnterprise

use of ddf.catalog.operation.impl.SourceInfoRequestEnterprise in project ddf by codice.

the class SearchService method getSourceIds.

private Set<String> getSourceIds(String sources) {
    Set<String> sourceIds;
    if (StringUtils.equalsIgnoreCase(sources, LOCAL_SOURCE)) {
        LOGGER.debug("Received local query");
        sourceIds = new HashSet<String>(Arrays.asList(searchController.getFramework().getId()));
    } else if (!(StringUtils.isEmpty(sources))) {
        LOGGER.debug("Received source names from client: {}", sources);
        sourceIds = new HashSet<String>(Arrays.asList(StringUtils.stripAll(sources.split(","))));
    } else {
        LOGGER.debug("Received enterprise query");
        SourceInfoResponse sourceInfo = null;
        try {
            sourceInfo = searchController.getFramework().getSourceInfo(new SourceInfoRequestEnterprise(true));
        } catch (SourceUnavailableException e) {
            LOGGER.debug("Exception while getting source status. Defaulting to all sources. " + "This could include unavailable sources.", e);
        }
        if (sourceInfo != null) {
            sourceIds = new HashSet<String>();
            for (SourceDescriptor source : sourceInfo.getSourceInfo()) {
                if (source.isAvailable()) {
                    sourceIds.add(source.getSourceId());
                }
            }
        } else {
            sourceIds = searchController.getFramework().getSourceIds();
        }
    }
    return sourceIds;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) HashSet(java.util.HashSet)

Example 10 with SourceInfoRequestEnterprise

use of ddf.catalog.operation.impl.SourceInfoRequestEnterprise 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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfiguredService(ddf.catalog.service.ConfiguredService) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) FederatedSource(ddf.catalog.source.FederatedSource) Source(ddf.catalog.source.Source) ServiceReference(org.osgi.framework.ServiceReference) FederatedSource(ddf.catalog.source.FederatedSource) CatalogProvider(ddf.catalog.source.CatalogProvider)

Aggregations

SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)10 SourceDescriptor (ddf.catalog.source.SourceDescriptor)9 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)9 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)8 FederatedSource (ddf.catalog.source.FederatedSource)6 SourceInfoRequest (ddf.catalog.operation.SourceInfoRequest)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 CatalogProvider (ddf.catalog.source.CatalogProvider)4 Source (ddf.catalog.source.Source)4 SourcePoller (ddf.catalog.util.impl.SourcePoller)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ByteSource (com.google.common.io.ByteSource)2 ContentType (ddf.catalog.data.ContentType)2 CachedSource (ddf.catalog.util.impl.CachedSource)2 SourcePollerRunner (ddf.catalog.util.impl.SourcePollerRunner)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 GET (javax.ws.rs.GET)2