Search in sources :

Example 1 with Subscription

use of ddf.catalog.event.Subscription in project ddf by codice.

the class CswSubscriptionEndpointTest method setUp.

@Before
public void setUp() throws Exception {
    systemKeystoreFile = temporaryFolder.newFile("serverKeystore.jks");
    FileOutputStream systemKeyOutStream = new FileOutputStream(systemKeystoreFile);
    InputStream systemKeyStream = CswSubscriptionEndpointTest.class.getResourceAsStream("/serverKeystore.jks");
    IOUtils.copy(systemKeyStream, systemKeyOutStream);
    systemTruststoreFile = temporaryFolder.newFile("serverTruststore.jks");
    FileOutputStream systemTrustOutStream = new FileOutputStream(systemTruststoreFile);
    InputStream systemTrustStream = CswSubscriptionEndpointTest.class.getResourceAsStream("/serverTruststore.jks");
    IOUtils.copy(systemTrustStream, systemTrustOutStream);
    System.setProperty(SecurityConstants.KEYSTORE_TYPE, "jks");
    System.setProperty(SecurityConstants.TRUSTSTORE_TYPE, "jks");
    System.setProperty("ddf.home", "");
    System.setProperty(SecurityConstants.KEYSTORE_PATH, systemKeystoreFile.getAbsolutePath());
    System.setProperty(SecurityConstants.TRUSTSTORE_PATH, systemTruststoreFile.getAbsolutePath());
    System.setProperty(SecurityConstants.KEYSTORE_PASSWORD, password);
    System.setProperty(SecurityConstants.TRUSTSTORE_PASSWORD, password);
    eventProcessor = mock(EventProcessor.class);
    mockInputManager = mock(TransformerManager.class);
    mockContext = mock(BundleContext.class);
    mockMimeTypeManager = mock(TransformerManager.class);
    mockSchemaManager = mock(TransformerManager.class);
    validator = mock(Validator.class);
    queryFactory = mock(CswQueryFactory.class);
    query = mock(QueryRequest.class);
    when(queryFactory.getQuery(any(GetRecordsType.class))).thenReturn(query);
    serviceRegistration = mock(ServiceRegistration.class);
    subscriptionReference = mock(ServiceReference.class);
    bundle = mock(Bundle.class);
    osgiFilter = mock(Filter.class);
    configAdminRef = mock(ServiceReference.class);
    configAdmin = mock(ConfigurationAdmin.class);
    config = mock(Configuration.class);
    SecureCxfClientFactory mockFactory = mock(SecureCxfClientFactory.class);
    clientBuilderFactory = mock(ClientBuilderFactory.class);
    ClientBuilder<WebClient> clientBuilder = new ClientBuilderImpl<WebClient>(mock(OAuthSecurity.class), mock(SamlSecurity.class), mock(SecurityLogger.class), mock(SecurityManager.class)) {

        @Override
        public SecureCxfClientFactory<WebClient> build() {
            return mockFactory;
        }
    };
    when(clientBuilderFactory.<WebClient>getClientBuilder()).thenReturn(clientBuilder);
    Configuration[] configArry = { config };
    defaultRequest = createDefaultGetRecordsRequest();
    subscription = new CswSubscription(mockMimeTypeManager, defaultRequest.get202RecordsType(), query, clientBuilderFactory, security);
    when(osgiFilter.toString()).thenReturn(FILTER_STR);
    doReturn(serviceRegistration).when(mockContext).registerService(eq(Subscription.class.getName()), any(Subscription.class), any(Dictionary.class));
    doReturn(configAdminRef).when(mockContext).getServiceReference(eq(ConfigurationAdmin.class.getName()));
    when(serviceRegistration.getReference()).thenReturn(subscriptionReference);
    doReturn(bundle).when(subscriptionReference).getBundle();
    when(subscriptionReference.getBundle()).thenReturn(bundle);
    when(bundle.getBundleId()).thenReturn(bundleId);
    when(mockContext.createFilter(anyString())).thenReturn(osgiFilter);
    when(mockContext.getService(eq(configAdminRef))).thenReturn(configAdmin);
    when(mockContext.getService(eq(subscriptionReference))).thenReturn(subscription);
    when(configAdmin.listConfigurations(eq(FILTER_STR))).thenReturn(configArry);
    when(configAdmin.createFactoryConfiguration(anyString(), isNull())).thenReturn(config);
    cswSubscriptionEndpoint = new CswSubscriptionEndpointStub(eventProcessor, mockMimeTypeManager, mockSchemaManager, mockInputManager, validator, queryFactory, mockContext, clientBuilderFactory, security);
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) SecurityManager(ddf.security.service.SecurityManager) SecureCxfClientFactory(org.codice.ddf.cxf.client.SecureCxfClientFactory) ClientBuilderFactory(org.codice.ddf.cxf.client.ClientBuilderFactory) Subscription(ddf.catalog.event.Subscription) CswSubscription(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.event.CswSubscription) ServiceRegistration(org.osgi.framework.ServiceRegistration) ClientBuilderImpl(org.codice.ddf.cxf.client.impl.ClientBuilderImpl) TransformerManager(org.codice.ddf.spatial.ogc.csw.catalog.common.transformer.TransformerManager) QueryRequest(ddf.catalog.operation.QueryRequest) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) OAuthSecurity(org.codice.ddf.cxf.oauth.OAuthSecurity) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) WebClient(org.apache.cxf.jaxrs.client.WebClient) ServiceReference(org.osgi.framework.ServiceReference) Filter(org.osgi.framework.Filter) FileOutputStream(java.io.FileOutputStream) EventProcessor(ddf.catalog.event.EventProcessor) SamlSecurity(org.codice.ddf.security.jaxrs.SamlSecurity) CswSubscription(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.event.CswSubscription) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext) SecurityLogger(ddf.security.audit.SecurityLogger) Before(org.junit.Before)

Example 2 with Subscription

use of ddf.catalog.event.Subscription in project ddf by codice.

the class ListCommand method printSubscription.

private void printSubscription(Map.Entry<String, ServiceReference<Subscription>> entry) {
    PrintStream console = System.out;
    Subscription subscription = bundleContext.getService(entry.getValue());
    String rowColor = "";
    if (subscription != null && subscription.getDeliveryMethod() instanceof Pingable && !((Pingable) subscription.getDeliveryMethod()).ping()) {
        rowColor = RED_CONSOLE_COLOR;
    }
    console.println(String.format("%s%s\t %s%s", rowColor, entry.getKey(), entry.getValue().getProperty("event-endpoint"), DEFAULT_CONSOLE_COLOR));
}
Also used : PrintStream(java.io.PrintStream) Pingable(ddf.catalog.operation.Pingable) Subscription(ddf.catalog.event.Subscription)

Example 3 with Subscription

use of ddf.catalog.event.Subscription in project ddf by codice.

the class DummyPreSubscriptionPlugin method process.

public Subscription process(Subscription input) throws PluginExecutionException {
    String methodName = "process";
    LOGGER.trace(ENTERING, methodName);
    Subscription newSubscription = input;
    if (input != null) {
        FilterDelegate<Filter> delegate = new CopyFilterDelegate(filterBuilder);
        try {
            // Make a defensive copy of the original filter (just in case anyone else expects
            // it to remain unmodified)
            Filter copiedFilter = filterAdapter.adapt(input, delegate);
            // Define the extra query clause(s) to add to the copied filter
            Filter extraFilter = filterBuilder.attribute(Metacard.ANY_TEXT).like().text("CAN");
            // AND the extra query clause(s) to the copied filter
            Filter modifiedFilter = filterBuilder.allOf(copiedFilter, extraFilter);
            // Create a new subscription with the modified filter
            newSubscription = new SubscriptionImpl(modifiedFilter, input.getDeliveryMethod(), input.getSourceIds(), input.isEnterprise());
        } catch (UnsupportedQueryException e) {
            throw new PluginExecutionException(e);
        }
    }
    LOGGER.trace(EXITING, methodName);
    return newSubscription;
}
Also used : Filter(org.opengis.filter.Filter) CopyFilterDelegate(ddf.catalog.filter.delegate.CopyFilterDelegate) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) Subscription(ddf.catalog.event.Subscription) SubscriptionImpl(ddf.catalog.event.impl.SubscriptionImpl) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException)

Example 4 with Subscription

use of ddf.catalog.event.Subscription in project ddf by codice.

the class ListCommandTest method testListNoArgsSourceAndEnterpriseSubscriptionFound.

/**
 * Test subscriptions:list command with source and enterprise subscriptions. Make sure enterprise
 * and source id info is printed.
 *
 * @throws Exception
 */
@Test
public void testListNoArgsSourceAndEnterpriseSubscriptionFound() throws Exception {
    ListCommand listCommand = new ListCommand();
    BundleContext bundleContext = mock(BundleContext.class);
    listCommand.setBundleContext(bundleContext);
    Subscription sourceSubscription = mock(Subscription.class);
    when(sourceSubscription.getSourceIds()).thenReturn(Collections.singleton("source.id"));
    when(sourceSubscription.isEnterprise()).thenReturn(false);
    ServiceReference<Subscription> sourceSubscriptionReference = mock(ServiceReference.class);
    when(sourceSubscriptionReference.getPropertyKeys()).thenReturn(new String[] { SUBSCRIPTION_ID_PROPERTY_KEY });
    when(sourceSubscriptionReference.getProperty("subscription-id")).thenReturn(MY_SUBSCRIPTION_ID);
    when(bundleContext.getService(sourceSubscriptionReference)).thenReturn(sourceSubscription);
    Subscription enterpriseSubscription = mock(Subscription.class);
    when(enterpriseSubscription.getSourceIds()).thenReturn(null);
    when(enterpriseSubscription.isEnterprise()).thenReturn(true);
    ServiceReference enterpriseSubscriptionReference = mock(ServiceReference.class);
    when(enterpriseSubscriptionReference.getPropertyKeys()).thenReturn(new String[] { SUBSCRIPTION_ID_PROPERTY_KEY });
    when(enterpriseSubscriptionReference.getProperty(SUBSCRIPTION_ID_PROPERTY_KEY)).thenReturn(YOUR_SUBSCRIPTION_ID);
    when(bundleContext.getService(enterpriseSubscriptionReference)).thenReturn(enterpriseSubscription);
    ServiceReference[] refs = new ServiceReference[] { sourceSubscriptionReference, enterpriseSubscriptionReference };
    when(bundleContext.getServiceReferences(eq(SubscriptionsCommand.SERVICE_PID), isNull())).thenReturn(refs);
    PrintStream realSystemOut = System.out;
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    System.setOut(new PrintStream(buffer));
    // when
    listCommand.execute();
    /* cleanup */
    System.setOut(realSystemOut);
    // then
    List<String> linesWithText = getConsoleOutputText(buffer);
    assertThat(linesWithText.size(), is(4));
    assertThat(linesWithText, hasItems(containsString("Total subscriptions found: 2"), allOf(containsString(MY_SUBSCRIPTION_ID), containsString("false"), containsString("source.id")), allOf(containsString(YOUR_SUBSCRIPTION_ID), containsString("true"))));
    buffer.close();
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Subscription(ddf.catalog.event.Subscription) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with Subscription

use of ddf.catalog.event.Subscription in project ddf by codice.

the class CswSubscriptionEndpoint method addOrUpdateSubscription.

public synchronized String addOrUpdateSubscription(GetRecordsType request, boolean persistSubscription) throws CswException {
    String methodName = "createSubscription";
    LOGGER.trace("ENTERING: {}    (persistSubscription = {})", methodName, persistSubscription);
    if (request.getResponseHandler() == null || request.getResponseHandler().isEmpty() || StringUtils.isEmpty(request.getResponseHandler().get(0))) {
        throw new CswException("Unable to create subscription because deliveryMethodUrl is null or empty");
    }
    String deliveryMethodUrl = request.getResponseHandler().get(0);
    String subscriptionUuid = getSubscriptionUuid(request.getRequestId());
    LOGGER.debug("subscriptionUuid = {}", subscriptionUuid);
    request.setRequestId(subscriptionUuid);
    // to registry
    if (registeredSubscriptions.containsKey(subscriptionUuid)) {
        LOGGER.debug("Delete existing subscription {} for re-creation", subscriptionUuid);
        deleteCswSubscription(subscriptionUuid);
    }
    CswSubscription sub = createSubscription(request);
    Dictionary<String, String> props = new DictionaryMap<>();
    props.put("subscription-id", subscriptionUuid);
    props.put("event-endpoint", request.getResponseHandler().get(0));
    LOGGER.debug("Registering Subscription");
    ServiceRegistration serviceRegistration = getBundleContext().registerService(Subscription.class.getName(), sub, props);
    if (serviceRegistration != null) {
        LOGGER.debug("Subscription registered with bundle ID = {} ", serviceRegistration.getReference().getBundle().getBundleId());
        registeredSubscriptions.put(subscriptionUuid, serviceRegistration);
        // client originally provided.
        if (persistSubscription) {
            persistSubscription(sub, deliveryMethodUrl, subscriptionUuid);
        }
    } else {
        LOGGER.debug("Subscription registration failed");
    }
    LOGGER.trace(EXITING_STR, methodName);
    return subscriptionUuid;
}
Also used : CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) CswSubscription(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.event.CswSubscription) Subscription(ddf.catalog.event.Subscription) CswSubscription(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.event.CswSubscription) DictionaryMap(org.codice.ddf.configuration.DictionaryMap) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

Subscription (ddf.catalog.event.Subscription)5 PrintStream (java.io.PrintStream)2 CswSubscription (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.event.CswSubscription)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceReference (org.osgi.framework.ServiceReference)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 EventProcessor (ddf.catalog.event.EventProcessor)1 SubscriptionImpl (ddf.catalog.event.impl.SubscriptionImpl)1 CopyFilterDelegate (ddf.catalog.filter.delegate.CopyFilterDelegate)1 Pingable (ddf.catalog.operation.Pingable)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 PluginExecutionException (ddf.catalog.plugin.PluginExecutionException)1 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)1 SecurityLogger (ddf.security.audit.SecurityLogger)1 SecurityManager (ddf.security.service.SecurityManager)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Dictionary (java.util.Dictionary)1 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)1