use of io.openk9.plugin.driver.manager.api.PluginDriver in project openk9 by smclab.
the class SupportedDatasourcesEndpoint method _mapToResponse.
private SupportedDatasourcesResponse _mapToResponse(Tuple2<Datasource, PluginDriverDTO> t2) {
Datasource t1 = t2.getT1();
PluginDriverDTO pluginDriver = t2.getT2();
return SupportedDatasourcesResponse.of(t1.getName(), t1.getActive(), pluginDriver.getDocumentTypes().stream().map(documentTypeDTO -> SupportedDatasourcesDocumentType.of(documentTypeDTO.getName(), documentTypeDTO.getIcon())).collect(Collectors.toList()), SupportedDatasourcesDocumentType.of(pluginDriver.getDefaultDocumentType().getName(), pluginDriver.getDefaultDocumentType().getIcon()));
}
use of io.openk9.plugin.driver.manager.api.PluginDriver 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;
}
use of io.openk9.plugin.driver.manager.api.PluginDriver in project openk9 by smclab.
the class DocumentTypesEndpoint method _mapToResponse.
private Object _mapToResponse(List<Tuple2<Datasource, PluginDriverDTO>> list) {
Map<String, Collection<String>> response = new HashMap<>();
for (Tuple2<Datasource, PluginDriverDTO> t2 : list) {
PluginDriverDTO pluginDriver = t2.getT2();
for (DocumentTypeDTO documentType : pluginDriver.getDocumentTypes()) {
String name = documentType.getName();
List<SearchKeywordDTO> searchKeywords = documentType.getSearchKeywords();
List<String> keywords = searchKeywords.stream().map(SearchKeywordDTO::getKeyword).collect(Collectors.toList());
Collection<String> prevKeywords = response.get(name);
Set<String> combine = new HashSet<>(keywords);
if (prevKeywords != null) {
combine.addAll(prevKeywords);
}
response.put(name, combine);
}
}
return response;
}
use of io.openk9.plugin.driver.manager.api.PluginDriver in project openk9 by smclab.
the class PluginDriverEndPoints method _findPluginDriverByName.
private Publisher<Void> _findPluginDriverByName(HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
return Mono.defer(() -> {
String serviceDriverName = httpRequest.param("serviceDriverName");
PluginDriverDTO response = _pluginDriverDTOService.findPluginDriverDTOByName(serviceDriverName).orElseThrow(() -> new HttpException(404, "No Content. PluginDriver not found for serviceDriverName: " + serviceDriverName));
return Mono.from(_httpResponseWriter.write(httpResponse, response));
});
}
use of io.openk9.plugin.driver.manager.api.PluginDriver in project openk9 by smclab.
the class InsertIndexWriter method activate.
@Activate
void activate() {
_disposable = _receiverReactor.consumeAutoAck(_binding.getQueue()).flatMap(delivery -> {
ObjectNode enrichProcessorContext = _jsonFactory.fromJsonToJsonNode(delivery.getBody()).toObjectNode();
String routingKey = delivery.getEnvelope().getRoutingKey();
long tenantId = enrichProcessorContext.get("datasourceContext").get("tenant").get("tenantId").asLong();
String pluginDriverName = enrichProcessorContext.get("pluginDriver").get("name").asText();
String indexName = String.join("-", Long.toString(tenantId), pluginDriverName, "data");
if (routingKey.endsWith("entity")) {
return Mono.fromSupplier(() -> new IndexRequest(indexName).source(enrichProcessorContext.toString(), XContentType.JSON));
}
return _createDocWriterRequest(indexName, enrichProcessorContext);
}).onErrorContinue(this::_manageExceptions).doOnNext(_indexBus::sendRequest).subscribe();
}
Aggregations