Commit 05a76f9a authored by Nico Mack's avatar Nico Mack

Finalized Implementation of RESTfulWebServiceExecutor

parent 07ae1e26
......@@ -104,6 +104,12 @@
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<repositories>
......
package lu.list.itis.dkd.tui.cps.system.executor;
import lu.list.itis.dkd.tui.cps.system.Declaration;
import lu.list.itis.dkd.tui.cps.system.Import;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import lu.list.itis.dkd.tui.utility.StringUtils;
import com.google.common.base.Strings;
import org.glassfish.jersey.client.ClientResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
// ***************************************************************************
// * Class Definition *
......@@ -26,7 +30,10 @@ import javax.ws.rs.client.WebTarget;
public class RESTfulWebServiceExecutor extends Executor {
Client client;
String serviceUrl;
WebTarget service;
String response;
private Map<String, Object> parameters;
// ***************************************************************************
// * Constants
......@@ -34,8 +41,16 @@ public class RESTfulWebServiceExecutor extends Executor {
private static final Logger LOGGER = LoggerFactory.getLogger(RESTfulWebServiceExecutor.class.getSimpleName());
private static Pattern METHOD_PATTERN = Pattern.compile("^.*?([a-z0-9_]+)", Pattern.CASE_INSENSITIVE);
private static Pattern URL_PATTERN = Pattern.compile("^(.*?)//?$", Pattern.CASE_INSENSITIVE);
private static final String POST = "POST"; //$NON-NLS-1$
private static final String GET = "GET"; //$NON-NLS-1$
private static final String PUT = "PUT"; //$NON-NLS-1$
private static final String DELETE = "DELETE"; //$NON-NLS-1$
private static final String OPTIONS = "OPTIONS"; //$NON-NLS-1$
private static final String HEAD = "HEAD"; //$NON-NLS-1$
private static final String PARAMETER_TEMPLATE = "Parameter -> {} = {}"; //$NON-NLS-1$
private static Pattern SCRIPT_PATTERN = Pattern.compile("^(POST|GET|PUT|DELETE|OPTIONS|HEAD)\\s+(.*?)$", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -47,6 +62,7 @@ public class RESTfulWebServiceExecutor extends Executor {
super(properties);
client = ClientBuilder.newClient();
parameters = new ConcurrentHashMap<>();
}
// ---------------------------------------------------------------------------
......@@ -55,7 +71,15 @@ public class RESTfulWebServiceExecutor extends Executor {
// ***************************************************************************
// ---------------------------------------------------------------------------
private Entity<?> buildEntity() {
Object entity = null;
Set<String> identifiers = parameters.keySet();
if (identifiers.size() == 1) {
String identifier = identifiers.iterator().next();
entity = parameters.get(identifier);
}
return Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -63,19 +87,6 @@ public class RESTfulWebServiceExecutor extends Executor {
// ***************************************************************************
// ---------------------------------------------------------------------------
@Override
public void resolve(List<Import> imports) {
for (Import declaration : imports) {
Matcher method = METHOD_PATTERN.matcher(declaration.getFunction());
Matcher url = URL_PATTERN.matcher(declaration.getFrom());
if (url.matches() && method.matches()) {
String serviceUrl = StringUtils.build("{}/{}", url, method); //$NON-NLS-1$
LOGGER.info(serviceUrl);
service = client.target(serviceUrl);
}
}
}
// ---------------------------------------------------------------------------
......@@ -83,6 +94,10 @@ public class RESTfulWebServiceExecutor extends Executor {
public void set(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
String identifier = (Strings.isNullOrEmpty(declaration.getEquate())) ? variable.getName() : declaration.getEquate();
parameters.put(identifier, variable.getValue());
if (LOGGER.isInfoEnabled()) {
LOGGER.info(PARAMETER_TEMPLATE, variable.getName(), variable.getValue()); // $NON-NLS-1$
}
}
// ---------------------------------------------------------------------------
......@@ -91,16 +106,28 @@ public class RESTfulWebServiceExecutor extends Executor {
public boolean eval(String script) {
boolean success = true;
this.executionErrors.reset();
long elapsed = System.currentTimeMillis();
elapsed = System.currentTimeMillis() - elapsed;
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Eval took {} ms | {}", elapsed, script); //$NON-NLS-1$
}
if (this.executionErrors.size() > 0 && LOGGER.isErrorEnabled()) {
LOGGER.error("Eval returned an error: {}", this.executionErrors.toString()); //$NON-NLS-1$
success = false;
}
Matcher callMatcher = SCRIPT_PATTERN.matcher(script);
if (callMatcher.matches()) {
long elapsed = System.currentTimeMillis();
try {
service = client.target(callMatcher.group(2));
Builder request = service.request();
switch (callMatcher.group(1)) {
case POST:
request = request.accept(MediaType.APPLICATION_JSON_TYPE);
response = request.post(this.buildEntity(), String.class);
}
} catch (Exception exception) {
success = false;
ClientResponse status = client.target(serviceUrl).request().get(ClientResponse.class);
LOGGER.error("Eval returned an error code: {}", status.getStatus()); //$NON-NLS-1$
}
elapsed = System.currentTimeMillis() - elapsed;
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Eval took {} ms | {}", elapsed, script); //$NON-NLS-1$
}
}
return success;
}
......@@ -109,6 +136,13 @@ public class RESTfulWebServiceExecutor extends Executor {
@Override
public Variable<?> get(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
if (response != null) {
variable.setValueFromObject(response);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Result <- {} = {}", variable.getName(), response); //$NON-NLS-1$
}
}
return variable;
}
......@@ -117,6 +151,9 @@ public class RESTfulWebServiceExecutor extends Executor {
/** {@inheritDoc} */
@Override
public void resetContext() {
// TODO figure out how to reset context
parameters.clear();
response = null;
}
// ---------------------------------------------------------------------------
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment