forked from apache/incubator-kie-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AF-2637: Implement SVG backend Service for Kie Server (apache#1067)
* AF-2637: Implement SVG backend Service for Kie Server * Improvements * Checking optional before getting value * fixing method * Improvements * fixing typo
- Loading branch information
Showing
20 changed files
with
578 additions
and
72 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...xternal-backend/src/main/java/org/dashbuilder/external/impl/BackendComponentFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...kend/src/main/java/org/dashbuilder/external/impl/BackendComponentFunctionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...der-external-backend/src/main/java/org/dashbuilder/external/impl/BackendDateFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
82 changes: 44 additions & 38 deletions
82
dashbuilder/dashbuilder-backend/dashbuilder-kie-server-backend/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...-backend/src/main/java/org/dashbuilder/kieserver/backend/function/ProcessSVGFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.