Search in sources :

Example 1 with AsyncEnrichProcessor

use of io.openk9.search.enrich.api.AsyncEnrichProcessor in project openk9 by smclab.

the class PluginDriverBundleTrackerCustomizer method addingBundle.

@Override
public Void addingBundle(Bundle bundle, BundleEvent event) {
    if (bundle.getState() != Bundle.ACTIVE) {
        removedBundle(bundle, event, null);
        return null;
    }
    Dictionary<String, String> headers = bundle.getHeaders(null);
    String pluginDriverConfiguration = headers.get(Constants.PLUGIN_DRIVER_CONFIGURATION);
    if (pluginDriverConfiguration == null) {
        pluginDriverConfiguration = "plugin-driver-config.json";
    }
    URL entry = bundle.getEntry(pluginDriverConfiguration);
    if (entry == null) {
        return null;
    }
    PluginDriverConfig pluginDriverConfig;
    try (InputStream is = entry.openStream()) {
        byte[] bytes = is.readAllBytes();
        pluginDriverConfig = _jsonFactory.fromJson(bytes, PluginDriverConfig.class);
    } catch (IOException e) {
        _log.error(e.getMessage(), e);
        return null;
    }
    String pluginDriverName = pluginDriverConfig.getName();
    String driverServiceName = pluginDriverConfig.getDriverServiceName() != null ? pluginDriverConfig.getDriverServiceName() : pluginDriverName;
    boolean schedulerEnabled = pluginDriverConfig.isSchedulerEnabled();
    PluginDriverConfig.Type type = pluginDriverConfig.getType();
    PluginDriver pluginDriver;
    switch(type) {
        case HTTP:
            {
                Map<String, Object> options = pluginDriverConfig.getOptions();
                if (options == null) {
                    options = Map.of();
                }
                String path = (String) options.getOrDefault("path", "");
                String url = (String) options.getOrDefault("url", "");
                Map<String, Object> headersObject = (Map<String, Object>) options.getOrDefault("headers", Map.of());
                String method = (String) options.getOrDefault("method", "GET");
                List<String> jsonKeys = (List<String>) options.getOrDefault("jsonKeys", new String[0]);
                int methodN = _findHttpMethod(method);
                HttpClient httpClient = _httpClientFactory.getHttpClient(url);
                pluginDriver = new BasePluginDriver() {

                    @Override
                    protected Map<String, Object> headersObject() {
                        return headersObject;
                    }

                    @Override
                    protected String[] headers() {
                        return new String[0];
                    }

                    @Override
                    protected String path() {
                        return path;
                    }

                    @Override
                    protected int method() {
                        return methodN;
                    }

                    @Override
                    protected String[] jsonKeys() {
                        return jsonKeys.toArray(new String[0]);
                    }

                    @Override
                    protected JsonFactory getJsonFactory() {
                        return _jsonFactory;
                    }

                    @Override
                    protected HttpClient getHttpClient() {
                        return httpClient;
                    }

                    @Override
                    public String getName() {
                        return pluginDriverName;
                    }

                    @Override
                    public boolean schedulerEnabled() {
                        return schedulerEnabled;
                    }

                    @Override
                    public String getDriverServiceName() {
                        return driverServiceName;
                    }
                };
                break;
            }
        default:
            throw new IllegalStateException("type: " + type + " not supported");
    }
    List<AutoCloseables.AutoCloseableSafe> autoCloseableList = new ArrayList<>();
    ServiceRegistration<PluginDriver> pluginDriverServiceRegistration = bundle.getBundleContext().registerService(PluginDriver.class, pluginDriver, null);
    autoCloseableList.add(AutoCloseables.mergeAutoCloseableToSafe(pluginDriverServiceRegistration::unregister));
    for (DocumentTypeConfig documentType : pluginDriverConfig.getDocumentTypes()) {
        DocumentTypeFactory.DefaultDocumentTypeFactory documentTypeFactory = DocumentTypeFactory.DefaultDocumentTypeFactory.of(pluginDriver.getName(), documentType.isDefaultDocumentType(), DocumentType.builder().icon(documentType.getIcon()).name(documentType.getName()).searchKeywords(_searchKeywordConfigToSearchKeyword(documentType.getSearchKeywords())).sourceFields(_mappingsToSourceFields(documentType.getMappings())).build());
        autoCloseableList.add(_documentTypeFactoryRegistry.register(documentTypeFactory));
    }
    for (EnrichProcessorConfig enrichProcessor : pluginDriverConfig.getEnrichProcessors()) {
        String name = enrichProcessor.getName();
        if (name == null || name.isBlank()) {
            _log.warn("name must be specified");
            continue;
        }
        EnrichProcessorConfig.Type typeEP = enrichProcessor.getType();
        if (typeEP == null) {
            _log.warn("name enrichProcessor.type be specified");
            continue;
        }
        Map<String, Object> options = enrichProcessor.getOptions();
        if (options == null) {
            options = Map.of();
        }
        EnrichProcessor enrichProcessorService = null;
        switch(typeEP) {
            case SYNC:
                Map<String, Object> headersEP = (Map<String, Object>) options.getOrDefault("headers", Map.of());
                String pathEP = (String) options.getOrDefault("path", "");
                String urlEP = (String) options.getOrDefault("url", "");
                String methodStringEP = (String) options.getOrDefault("method", "GET");
                int methodEP = _findHttpMethod(methodStringEP);
                BaseNerEnrichProcessor baseNerEnrichProcessor = new BaseNerEnrichProcessor() {

                    @Override
                    public String name() {
                        return name;
                    }

                    @Override
                    protected Map<String, Object> getHeaders() {
                        return headersEP;
                    }

                    @Override
                    protected int getMethod() {
                        return methodEP;
                    }

                    @Override
                    protected String getPath() {
                        return pathEP;
                    }
                };
                baseNerEnrichProcessor.setHttpClient(_httpClientFactory.getHttpClient(urlEP));
                baseNerEnrichProcessor.setJsonFactory(_jsonFactory);
                baseNerEnrichProcessor.setEntityManagerClient(_entityManagerClient);
                enrichProcessorService = baseNerEnrichProcessor;
                break;
            case ASYNC:
                String destinationName = (String) options.get("destinationName");
                if (destinationName == null || destinationName.isBlank()) {
                    _log.warn("destinationName must be specified");
                    break;
                }
                enrichProcessorService = new AsyncEnrichProcessor() {

                    @Override
                    public String destinationName() {
                        return destinationName;
                    }

                    @Override
                    public String name() {
                        return name;
                    }
                };
                break;
            default:
                throw new IllegalStateException("type: " + typeEP + " not supported");
        }
        if (enrichProcessorService != null) {
            ServiceRegistration<EnrichProcessor> enrichProcessorServiceRegistration = bundle.getBundleContext().registerService(EnrichProcessor.class, enrichProcessorService, null);
            autoCloseableList.add(AutoCloseables.mergeAutoCloseableToSafe(enrichProcessorServiceRegistration::unregister));
        }
    }
    _registrationMap.put(bundle, AutoCloseables.reduceAutoCloseableSafe(autoCloseableList));
    return null;
}
Also used : ArrayList(java.util.ArrayList) EnrichProcessorConfig(io.openk9.plugin.driver.manager.api.config.EnrichProcessorConfig) AsyncEnrichProcessor(io.openk9.search.enrich.api.AsyncEnrichProcessor) URL(java.net.URL) PluginDriverConfig(io.openk9.plugin.driver.manager.api.config.PluginDriverConfig) ArrayList(java.util.ArrayList) List(java.util.List) InputStream(java.io.InputStream) BaseNerEnrichProcessor(io.openk9.search.enrich.api.BaseNerEnrichProcessor) DocumentTypeConfig(io.openk9.plugin.driver.manager.api.config.DocumentTypeConfig) IOException(java.io.IOException) AsyncEnrichProcessor(io.openk9.search.enrich.api.AsyncEnrichProcessor) BaseNerEnrichProcessor(io.openk9.search.enrich.api.BaseNerEnrichProcessor) EnrichProcessor(io.openk9.search.enrich.api.EnrichProcessor) HttpClient(io.openk9.http.client.HttpClient) DocumentTypeFactory(io.openk9.plugin.driver.manager.api.DocumentTypeFactory) BasePluginDriver(io.openk9.plugin.driver.manager.api.BasePluginDriver) BasePluginDriver(io.openk9.plugin.driver.manager.api.BasePluginDriver) PluginDriver(io.openk9.plugin.driver.manager.api.PluginDriver) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with AsyncEnrichProcessor

use of io.openk9.search.enrich.api.AsyncEnrichProcessor in project openk9 by smclab.

the class EnrichProcessorServiceTracker method addingService.

@Override
public EnrichProcessor addingService(ServiceReference<EnrichProcessor> reference) {
    List<AutoCloseable> autoCloseableSafes = new ArrayList<>();
    EnrichProcessor service = _bundleContext.getService(reference);
    Binding binding = Binding.of(exchange, service.name(), service.name());
    ServiceRegistration<Binding> serviceRegistration = _bundleContext.registerService(Binding.class, binding, new Hashtable<>());
    autoCloseableSafes.add(serviceRegistration::unregister);
    if (service instanceof AsyncEnrichProcessor) {
        AsyncEnrichProcessor asyncEnrichProcessor = ((AsyncEnrichProcessor) service);
        Binding bindingDestinationName = Binding.of(exchange, asyncEnrichProcessor.destinationName(), asyncEnrichProcessor.destinationName());
        ServiceRegistration<Binding> destinationNameServiceRegistration = _bundleContext.registerService(Binding.class, bindingDestinationName, new Hashtable<>());
        autoCloseableSafes.add(destinationNameServiceRegistration::unregister);
    }
    EnrichProcessorExtension enrichProcessorExtension = new EnrichProcessorExtension(reference.getBundle(), service, prefetch);
    enrichProcessorExtension.start();
    autoCloseableSafes.add(enrichProcessorExtension::destroy);
    _map.put(service, AutoCloseables.mergeAutoCloseableToSafe(autoCloseableSafes));
    return service;
}
Also used : Binding(io.openk9.ingestion.api.Binding) AsyncEnrichProcessor(io.openk9.search.enrich.api.AsyncEnrichProcessor) EnrichProcessor(io.openk9.search.enrich.api.EnrichProcessor) SyncEnrichProcessor(io.openk9.search.enrich.api.SyncEnrichProcessor) EndEnrichProcessor(io.openk9.search.enrich.api.EndEnrichProcessor) ArrayList(java.util.ArrayList) AsyncEnrichProcessor(io.openk9.search.enrich.api.AsyncEnrichProcessor)

Aggregations

AsyncEnrichProcessor (io.openk9.search.enrich.api.AsyncEnrichProcessor)2 EnrichProcessor (io.openk9.search.enrich.api.EnrichProcessor)2 ArrayList (java.util.ArrayList)2 HttpClient (io.openk9.http.client.HttpClient)1 Binding (io.openk9.ingestion.api.Binding)1 BasePluginDriver (io.openk9.plugin.driver.manager.api.BasePluginDriver)1 DocumentTypeFactory (io.openk9.plugin.driver.manager.api.DocumentTypeFactory)1 PluginDriver (io.openk9.plugin.driver.manager.api.PluginDriver)1 DocumentTypeConfig (io.openk9.plugin.driver.manager.api.config.DocumentTypeConfig)1 EnrichProcessorConfig (io.openk9.plugin.driver.manager.api.config.EnrichProcessorConfig)1 PluginDriverConfig (io.openk9.plugin.driver.manager.api.config.PluginDriverConfig)1 BaseNerEnrichProcessor (io.openk9.search.enrich.api.BaseNerEnrichProcessor)1 EndEnrichProcessor (io.openk9.search.enrich.api.EndEnrichProcessor)1 SyncEnrichProcessor (io.openk9.search.enrich.api.SyncEnrichProcessor)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1