Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Jep instead of Jython for python scripting #20

Merged
merged 22 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MetaDIG webapp
# MetaDIG webapp
Web service for interacting with the [MetaDIG Engine](https://github.com/NCEAS/metadig-engine) API.

This project builds a 'metadig-webapp.war' file that can be deployed in the Tomcat webapps directory. Depending on the Tomcat configuration, the war may be unpacked automatically each time a new version is copied in place.
Expand Down Expand Up @@ -58,4 +58,4 @@ Authorization for Docker Hub can be setup in several ways. One method is to crea
</settings>
```

The appropriate username and password is available from the NCEAS secure repo.
The appropriate username and password is available from the NCEAS secure repo.
4 changes: 2 additions & 2 deletions src/main/java/edu/ucsb/nceas/mdq/MetadigContextListener.java
jeanetteclark marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void contextInitialized(ServletContextEvent servletContextEvent) {
try {
Dispatcher.setupJep();
} catch (MetadigException e) {
log.error("Error setting up Jep. Python checks may not work.", e);
throw new RuntimeException("Error setting up Jep. Aborting startup.", e);
}

log.info("Metadig 'contextInitialized' called.");
Expand All @@ -35,7 +35,7 @@ public void contextDestroyed(ServletContextEvent servletContextEvent) {
try {
log.debug("Shutting down controller...");
controller.shutdown();
log.info("Controller shutdonw successfully.");
log.info("Controller shutdown successfully.");
jeanetteclark marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException | TimeoutException e) {
log.error("Error shutting down metadig controller.");
e.printStackTrace();
Expand Down
174 changes: 87 additions & 87 deletions src/main/java/edu/ucsb/nceas/mdq/rest/ChecksResource.java
jeanetteclark marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -51,25 +48,25 @@
*/
@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 @@ -79,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 @@ -134,52 +134,52 @@ 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);
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
@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);
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();
}
}
3 changes: 3 additions & 0 deletions src/main/java/edu/ucsb/nceas/mdq/rest/SuitesResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public String getSuite(@PathParam("id") String id)

// @POST
// @Consumes(MediaType.MULTIPART_FORM_DATA)
// not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
public boolean createSuite(@FormDataParam("suite") InputStream xml) {
boolean persist = true;
MDQStore store = null;
Expand Down Expand Up @@ -117,6 +118,7 @@ public boolean createSuite(@FormDataParam("suite") InputStream xml) {
// @PUT
// @Path("/{id}")
// @Consumes(MediaType.MULTIPART_FORM_DATA)
// not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") InputStream xml)
throws JAXBException, IOException {
boolean persist = true;
Expand Down Expand Up @@ -145,6 +147,7 @@ public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") I
// @DELETE
// @Path("/{id}")
// @Produces(MediaType.TEXT_PLAIN)
// not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
public boolean deleteSuite(@PathParam("id") String id) {
boolean persist = true;
MDQStore store = null;
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/test-api.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash

# API endpoint URL with placeholder for {id}
url="http://localhost:8080/metadig-webapp-3.0.0-SNAPSHOT/checks/resource.keywords.controlled-2.0.0/run/"
# url="http://localhost:8080/metadig-webapp-3.0.0-SNAPSHOT/suites/FAIR-suite-0.4.0/run/"
# API endpoint URL for testing a check or suite
check_url="http://localhost:8080/metadig-webapp-3.0.0/checks/resource.keywords.controlled-2.0.0/run/"
suite_url="http://localhost:8080/metadig-webapp-3.0.0/suites/FAIR-suite-0.4.0/run/"
# Headers
headers=(
"Content-Type: multipart/mixed"
)

# Make the API request using curl
curl -X POST "$url" \
curl -X POST "$suite_url" \
--header 'Content-Type: multipart/form-data; boundary=---------BOUNDARY' \
--data-binary @testfile.txt

Loading