Skip to content

Commit

Permalink
AF-2637: Implement SVG backend Service for Kie Server (apache#1067)
Browse files Browse the repository at this point in the history
* AF-2637: Implement SVG backend Service for Kie Server

* Improvements

* Checking optional before getting value

* fixing method

* Improvements

* fixing typo
  • Loading branch information
jesuino authored Nov 3, 2020
1 parent c6cb4ba commit f2e1d07
Show file tree
Hide file tree
Showing 20 changed files with 578 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dashbuilder.external.impl;

import java.util.Map;

/**
* Server side component functions contract.
*
* @param <T>
* The function return type.
*/
public interface BackendComponentFunction<T> {

default String getName() {
return this.getClass().getSimpleName();
}

/**
*
* The function execution. Must return an object that can be used in browser windows communication.
* @param params
* Params set by user when configuring the component.
* @return
* The result
*
*/
T exec(Map<String, Object> params);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dashbuilder.external.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.dashbuilder.external.service.BackendComponentFunctionService;
import org.jboss.errai.bus.server.annotations.Service;

@Service
@ApplicationScoped
public class BackendComponentFunctionServiceImpl implements BackendComponentFunctionService {

Map<String, BackendComponentFunction<?>> functions;

@Inject
Instance<BackendComponentFunction<?>> functionsInstances;

@PostConstruct
void loadFunctions() {
functions = new HashMap<>();
functionsInstances.forEach(instance -> functions.put(instance.getName(), instance));
}

@Override
public List<String> listFunctions() {
return new ArrayList<>(functions.keySet());
}

@Override
public Object callFunction(String name, Map<String, Object> params) {
BackendComponentFunction<?> function = functions.get(name);
return function.exec(params);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dashbuilder.external.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.enterprise.context.Dependent;

/**
* Component backend function that returns the date.
*
*/
@Dependent
public class BackendDateFunction implements BackendComponentFunction<String> {

private static final String FORMAT_PARAM = "format";

@Override
public String exec(Map<String, Object> params) {
Object pattern = params.get(FORMAT_PARAM);
if (pattern != null) {
return new SimpleDateFormat(pattern.toString()).format(new Date());
}
return new Date().toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Copyright 2014 Red Hat, Inc. and/or its affiliates. ~ ~ Licensed under
the Apache License, Version 2.0 (the "License"); ~ you may not use this file
except in compliance with the License. ~ You may obtain a copy of the License
at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by
applicable law or agreed to in writing, software ~ distributed under the
License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. ~ See the License for the specific
language governing permissions and ~ limitations under the License. -->
the Apache License, Version 2.0 (the "License"); ~ you may not use this file
except in compliance with the License. ~ You may obtain a copy of the License
at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by
applicable law or agreed to in writing, software ~ distributed under the
License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. ~ See the License for the specific
language governing permissions and ~ limitations under the License. -->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dashbuilder</groupId>
<artifactId>dashbuilder-backend</artifactId>
<version>7.46.0-SNAPSHOT</version>
</parent>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dashbuilder</groupId>
<artifactId>dashbuilder-backend</artifactId>
<version>7.46.0-SNAPSHOT</version>
</parent>

<artifactId>dashbuilder-kie-server-backend</artifactId>
<packaging>jar</packaging>
<name>Dashbuilder Kie Server Backend</name>
<artifactId>dashbuilder-kie-server-backend</artifactId>
<packaging>jar</packaging>
<name>Dashbuilder Kie Server Backend</name>

<dependencies>
<dependency>
<groupId>org.dashbuilder</groupId>
<artifactId>dashbuilder-kie-server-api</artifactId>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.errai</groupId>
<artifactId>errai-config</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.dashbuilder</groupId>
<artifactId>dashbuilder-kie-server-api</artifactId>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.errai</groupId>
<artifactId>errai-config</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.dashbuilder</groupId>
<artifactId>dashbuilder-external-backend</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
Expand All @@ -45,6 +46,7 @@ public class KieServerConnectionInfoProviderImpl implements KieServerConnectionI
"You should provide user/password or token authentication";

private static final String SERVER_TEMPLATE_SEPARATOR = ",";
static final String DEFAULT_SERVER_TEMPLATE_PROPERTY = "dashbuilder.kieserver.defaultServerTemplate";
static final String SERVER_TEMPLATE_LIST_PROPERTY = "dashbuilder.kieserver.serverTemplates";
static final String SERVER_TEMPLATE_PROP_PREFFIX = "dashbuilder.kieserver.serverTemplate";
static final String DATASET_PROP_PREFFIX = "dashbuilder.kieserver.dataset";
Expand Down Expand Up @@ -130,6 +132,15 @@ public Optional<String> remoteDatasetProperty(String datasetUUID,
return filteredProperty(property);
}

@Override
public Optional<KieServerConnectionInfo> getDefault() {
String defaultTemplate = System.getProperty(DEFAULT_SERVER_TEMPLATE_PROPERTY);
if (defaultTemplate != null) {
return this.get(null, defaultTemplate);
}
return findFirstServerTemplateConf().flatMap(template -> get(null, template));
}

private Optional<String> filteredProperty(String property) {
return Optional.ofNullable(System.getProperty(property)).filter(v -> !v.trim().isEmpty());
}
Expand All @@ -155,4 +166,23 @@ private Optional<KieServerConnectionInfo> get(String confType,
return Optional.of(new KieServerConnectionInfo(url, user, password, token, replaceQuery));
}

Optional<String> findFirstServerTemplateConf() {
return Collections.list(System.getProperties().keys())
.stream()
.map(Object::toString)
.filter(key -> key.startsWith(SERVER_TEMPLATE_PROP_PREFFIX))
.map(this::retrieveTemplateId)
.filter(Objects::nonNull)
.findFirst();

}

String retrieveTemplateId(String property) {
String[] parts = property.replaceAll(SERVER_TEMPLATE_PROP_PREFFIX, "")
.split("\\.");
if (parts.length > 1) {
return parts[1];
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dashbuilder.kieserver.backend.function;

import java.util.Map;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;

import org.dashbuilder.external.impl.BackendComponentFunction;
import org.dashbuilder.kieserver.KieServerConnectionInfo;
import org.dashbuilder.kieserver.KieServerConnectionInfoProvider;
import org.dashbuilder.kieserver.backend.rest.KieServerQueryClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Dependent
public class ProcessSVGFunction implements BackendComponentFunction<String> {

private static final Logger LOGGER = LoggerFactory.getLogger(ProcessSVGFunction.class);

private static final String ERROR_REQUESTING_SVG = "Error requesting SVG from Kie Server: %s";

private static final String NOT_FOUND_MSG = "Process SVG not found for container %s and process %s";

private static final String CONTAINERID_PARAM = "containerId";
private static final String PROCESSID_PARAM = "processId";
private static final String SERVER_TEMPLATE_PARAM = "serverTemplate";

@Inject
KieServerQueryClient queryClient;

@Inject
KieServerConnectionInfoProvider connectionInfoProvider;

@Override
public String exec(Map<String, Object> params) {
String containerId = getRequiredParam(CONTAINERID_PARAM, params);
String processId = getRequiredParam(PROCESSID_PARAM, params);
Object serverTemplate = params.get(SERVER_TEMPLATE_PARAM);
KieServerConnectionInfo connectionInfo;
if (serverTemplate != null && !serverTemplate.toString().trim().isEmpty()) {
connectionInfo = connectionInfoProvider.get(null, serverTemplate.toString())
.orElseThrow(() -> new RuntimeException("Configuration for server template not found " + serverTemplate));
} else {
connectionInfo = connectionInfoProvider.getDefault()
.orElseThrow(() -> new RuntimeException("Not able to find credentials to retrieve processes SVG"));
}
try {
return queryClient.processSVG(connectionInfo, containerId, processId);
} catch (WebApplicationException e) {
if (e.getResponse().getStatus() == 404) {
notFoundSVGError(containerId, processId);
}
errorRetrievingSVG(e);
} catch (Exception e) {
errorRetrievingSVG(e);
}
return null;
}

private void errorRetrievingSVG(Exception e) {
String message = String.format(ERROR_REQUESTING_SVG, e.getMessage());
LOGGER.warn(message);
LOGGER.debug(message, e);
throw new RuntimeException(message, e);
}

private void notFoundSVGError(String containerId, String processId) {
String notFoundMessage = String.format(NOT_FOUND_MSG, containerId, processId);
LOGGER.warn(notFoundMessage);
throw new RuntimeException(notFoundMessage);
}

private String getRequiredParam(String param, Map<String, Object> params) {
Object value = params.get(param);
if (value == null || value.toString().trim().isEmpty()) {
throw new RuntimeException("Param '" + param + "' is required.");
}
return value.toString();
}

}
Loading

0 comments on commit f2e1d07

Please sign in to comment.