Skip to content

Commit

Permalink
Merge pull request #20 from NCEAS/feature-jep
Browse files Browse the repository at this point in the history
Use Jep instead of Jython for python scripting
  • Loading branch information
jeanetteclark authored Mar 6, 2024
2 parents 3255913 + f83de96 commit 1b55fed
Show file tree
Hide file tree
Showing 9 changed files with 1,780 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

docker-publish:
name: Docker Build and Publish
if: github.ref_name == 'develop' || startsWith(github.ref, 'refs/tags/v')
if: github.ref_name == 'develop' || startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref_name, 'feature')
needs: maven-build
runs-on: ubuntu-latest
permissions:
Expand Down
337 changes: 337 additions & 0 deletions .vscode/eclipse-java-google-style.xml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"java.configuration.updateBuildConfiguration": "interactive",
"java.compile.nullAnalysis.mode": "automatic",
"java.format.settings.url": ".vscode/eclipse-java-google-style.xml"
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- <docker.registry>docker.io</docker.registry> -->
<docker.repo>metadig</docker.repo>
<docker.tag>2.5.0</docker.tag>
<metadig-engine-version>2.5.0</metadig-engine-version>
<docker.tag>3.0.0-SNAPSHOT</docker.tag>
<metadig-engine-version>3.0.0-SNAPSHOT</metadig-engine-version>
</properties>

<modelVersion>4.0.0</modelVersion>

<groupId>edu.ucsb.nceas</groupId>
<artifactId>metadig-webapp</artifactId>
<packaging>war</packaging>
<version>2.5.0</version>
<version>3.0.0-SNAPSHOT</version>
<name>metadig-webapp</name>

<repositories>
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/edu/ucsb/nceas/mdq/MetadigContextListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import edu.ucsb.nceas.mdqengine.dispatch.Dispatcher;
import edu.ucsb.nceas.mdqengine.Controller;
import edu.ucsb.nceas.mdqengine.exception.MetadigException;

@WebListener
public class MetadigContextListener implements ServletContextListener {

public static Log log = LogFactory.getLog(MetadigContextListener.class);

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
log.debug("Metadig 'contextInitialized' called.");
try {
Dispatcher.setupJep();
} catch (MetadigException e) {
throw new RuntimeException("Error setting up Jep. Aborting startup.", e);
}

log.info("Metadig 'contextInitialized' called.");
}

@Override
Expand All @@ -26,7 +35,7 @@ public void contextDestroyed(ServletContextEvent servletContextEvent) {
try {
log.debug("Shutting down controller...");
controller.shutdown();
log.info("Controller shutdonw successfully.");
log.info("Controller shut down successfully.");
} catch (IOException | TimeoutException e) {
log.error("Error shutting down metadig controller.");
e.printStackTrace();
Expand Down
171 changes: 85 additions & 86 deletions src/main/java/edu/ucsb/nceas/mdq/rest/ChecksResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;
import javax.xml.bind.JAXBException;

import com.hp.hpl.jena.shared.ConfigException;
import edu.ucsb.nceas.mdqengine.exception.MetadigException;
import edu.ucsb.nceas.mdqengine.exception.MetadigStoreException;
import net.sf.saxon.functions.ConstantFunction;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
Expand All @@ -44,31 +41,32 @@
import edu.ucsb.nceas.mdqengine.store.StoreFactory;
import edu.ucsb.nceas.mdqengine.serialize.JsonMarshaller;
import edu.ucsb.nceas.mdqengine.serialize.XmlMarshaller;
import edu.ucsb.nceas.mdqengine.dispatch.Dispatcher;

/**
* Root resource (exposed at "checks" path)
*/
@Path("checks")
public class ChecksResource {
private Log log = LogFactory.getLog(this.getClass());
private MDQStore store = null;
private MDQEngine engine = null;
public ChecksResource() throws MetadigStoreException {
boolean persist = false;
this.store = StoreFactory.getStore(persist);
private Log log = LogFactory.getLog(this.getClass());
private MDQStore store = null;
private MDQEngine engine = null;
public ChecksResource() throws MetadigStoreException {
boolean persist = false;
this.store = StoreFactory.getStore(persist);

try {
this.engine = new MDQEngine();
this.engine.setStore(this.store);
} catch (MetadigException | IOException | ConfigurationException e) {
log.error(e.getMessage(), e);
}
}
try {
this.engine = new MDQEngine();
this.engine.setStore(this.store);
} catch (MetadigException | IOException | ConfigurationException e) {
log.error(e.getMessage(), e);
}
}
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
Expand All @@ -78,53 +76,56 @@ public ChecksResource() throws MetadigStoreException {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String listChecks() {
Collection<String> checks = store.listChecks();
Collection<String> checks = store.listChecks();
return JsonMarshaller.toJson(checks);
}

@GET
@Path("/{id}")
@Produces(MediaType.TEXT_XML)
public String getCheck(@PathParam("id") String id) throws UnsupportedEncodingException, JAXBException {
Check check = store.getCheck(id);
Check check = store.getCheck(id);
return (String) XmlMarshaller.toXml(check, true);
}

// @POST
// @Consumes(MediaType.MULTIPART_FORM_DATA)
// not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
public boolean createCheck(@FormDataParam("check") InputStream xml) {
Check check = null;
try {
check = (Check) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Check.class);
store.createCheck(check);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
Check check = null;
try {
check = (Check) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Check.class);
store.createCheck(check);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
return true;
}

// @PUT
// @Path("/{id}")
// @Consumes(MediaType.MULTIPART_FORM_DATA)
// not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
public boolean updateCheck(@PathParam("id") String id, @FormDataParam("check") InputStream xml) throws JAXBException, IOException {
Check check = null;
try {
check = (Check) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Check.class);
store.updateCheck(check);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
Check check = null;
try {
check = (Check) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Check.class);
store.updateCheck(check);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
return true;
}

// @DELETE
// @Path("/{id}")
// @Produces(MediaType.TEXT_PLAIN)
// not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
public boolean updateCheck(@PathParam("id") String id) {
Check check = store.getCheck(id);
store.deleteCheck(check);
Check check = store.getCheck(id);
store.deleteCheck(check);
return true;
}

Expand All @@ -133,51 +134,49 @@ public boolean updateCheck(@PathParam("id") String id) {
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response run(
@PathParam("id") String id,
@FormDataParam("document") InputStream input,
@FormDataParam("systemMetadata") InputStream sysMetaStream,
@Context Request r) throws UnsupportedEncodingException, JAXBException {

Run run = null;
// include SM if it was provided
SystemMetadata sysMeta = null;
if (sysMetaStream != null) {
try {
sysMeta = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysMetaStream);
} catch (InstantiationException | IllegalAccessException
| IOException | MarshallingException e) {
log.warn("Could not unmarshall SystemMetadata from stream", e);
}
}
try {
Map<String, Object> params = new HashMap<String, Object>();
// params.putAll(formParams);
// params.remove("id");
// params.remove("document");
Check check = store.getCheck(id);
run = engine.runCheck(check, input, params, sysMeta);
store.createRun(run);
} catch (Exception e) {
log.error(e.getMessage(), e);
return Response.serverError().entity(e).build();
}

// determine the format of plot to return
@PathParam("id") String id,
@FormDataParam("document") InputStream input,
@FormDataParam("systemMetadata") InputStream sysMetaStream,
@Context Request r) throws UnsupportedEncodingException, JAXBException {

Run run = null;
// include SM if it was provided
SystemMetadata sysMeta = null;
if (sysMetaStream != null) {
try {
sysMeta = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysMetaStream);
} catch (InstantiationException | IllegalAccessException
| IOException | MarshallingException e) {
log.warn("Could not unmarshall SystemMetadata from stream", e);
}
}
try {
Map<String, Object> params = new HashMap<String, Object>();
Check check = store.getCheck(id);
run = engine.runCheck(check, input, params, sysMeta);
store.createRun(run);
Dispatcher.getDispatcher("python").close();
} catch (Exception e) {
log.error(e.getMessage(), e);
return Response.serverError().entity(e).build();
}

// determine the format of plot to return
String resultString = null;
List<Variant> vs =
Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).build();
Variant v = r.selectVariant(vs);
if (v == null) {
return Response.notAcceptable(vs).build();
} else {
MediaType mt = v.getMediaType();
if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
resultString = XmlMarshaller.toXml(run, true);
} else {
resultString = JsonMarshaller.toJson(run);
}
}
return Response.ok(resultString).build();
List<Variant> vs =
Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).build();
Variant v = r.selectVariant(vs);
if (v == null) {
return Response.notAcceptable(vs).build();
} else {
MediaType mt = v.getMediaType();
if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
resultString = XmlMarshaller.toXml(run, true);
} else {
resultString = JsonMarshaller.toJson(run);
}
}
return Response.ok(resultString).build();
}
}
Loading

0 comments on commit 1b55fed

Please sign in to comment.