Commit 07ae1e26 authored by Nico Mack's avatar Nico Mack

Implementation of WebServiceExecutor

parent 3dc5a63c
......@@ -15,6 +15,7 @@ EQUATION_ELEMENT=equation
MODE_ATTRIBUTE=mode
INVOKE_ELEMENT=invoke
INDEX_ATTRIBUTE=index
FORMAT_ATTRIBUTE=format
FIELD_ATTRIBUTE=field
INVOKE_ELEMENT=invoke
EPSILON_ATTRIBUTE=epsilon
......
......@@ -93,6 +93,17 @@
<version>0.9.5.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.26</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
<repositories>
......
......@@ -34,6 +34,7 @@ import com.jgoodies.common.base.Preconditions;
public class Declaration implements Cloneable {
protected String field;
protected String equate;
protected String format;
protected int index;
protected Variable<?> variable;
......@@ -72,6 +73,7 @@ public class Declaration implements Cloneable {
this.field = other.field;
this.equate = other.equate;
this.index = other.index;
this.format = other.format;
}
// ---------------------------------------------------------------------------
......@@ -80,14 +82,14 @@ public class Declaration implements Cloneable {
// ***************************************************************************
// ---------------------------------------------------------------------------
public void setEquate(String equate) {
this.equate = equate;
public String getIdentifier() {
return (this.variable != null) ? this.variable.getName() : null;
}
// ---------------------------------------------------------------------------
public String getIdentifier() {
return (this.variable != null) ? this.variable.getName() : null;
public void setEquate(String equate) {
this.equate = equate;
}
// ---------------------------------------------------------------------------
......@@ -102,6 +104,18 @@ public class Declaration implements Cloneable {
return equate;
}
// ---------------------------------------------------------------------------
public void setFormat(String format) {
this.format = format;
}
// ---------------------------------------------------------------------------
public String getFormat() {
return this.format;
}
// ---------------------------------------------------------------------------
/**
* Simple getter method for field.
......
......@@ -398,6 +398,8 @@ public class EquationSystemBuilder {
}
String equate = attributes.get(EquationSystemBundle.EQUATE_ATTRIBUTE);
declaration.setEquate(equate);
String format = attributes.get(EquationSystemBundle.FORMAT_ATTRIBUTE);
declaration.setFormat(format);
return declaration;
}
......
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Properties;
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.WebTarget;
// ***************************************************************************
// * Class Definition *
// ***************************************************************************
public class RESTfulWebServiceExecutor extends Executor {
Client client;
WebTarget service;
// ***************************************************************************
// * Constants
// ***************************************************************************
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);
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
public RESTfulWebServiceExecutor(Properties properties) {
super(properties);
client = ClientBuilder.newClient();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitive(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
@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);
}
}
}
// ---------------------------------------------------------------------------
@Override
public void set(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
String identifier = (Strings.isNullOrEmpty(declaration.getEquate())) ? variable.getName() : declaration.getEquate();
}
// ---------------------------------------------------------------------------
@Override
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;
}
return success;
}
// ---------------------------------------------------------------------------
@Override
public Variable<?> get(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
return variable;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
@Override
public void resetContext() {
// TODO figure out how to reset context
}
}
......@@ -46,6 +46,7 @@ public class EquationSystemBundle extends NLS {
public static String MODE_ATTRIBUTE;
public static String EQUATIONS_ELEMENT;
public static String INDEX_ATTRIBUTE;
public static String FORMAT_ATTRIBUTE;
public static String FIELD_ATTRIBUTE;
public static String INVOKE_ELEMENT;
public static String EXECUTOR_ATTRIBUTE;
......
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