Skip to content

Commit

Permalink
use try with resources pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanetteclark committed Apr 8, 2024
1 parent e37ce1b commit 69fc0ad
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 111 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') || startsWith(github.ref_name, 'feature')
if: github.ref_name == 'develop' || startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref_name, 'bugfix')
needs: maven-build
runs-on: ubuntu-latest
permissions:
Expand Down
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>3.0.0</docker.tag>
<metadig-engine-version>3.0.0</metadig-engine-version>
<docker.tag>3.0.1-SNAPSHOT</docker.tag>
<metadig-engine-version>3.0.1-SNAPSHOT</metadig-engine-version>
</properties>

<modelVersion>4.0.0</modelVersion>

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

<repositories>
Expand Down
82 changes: 38 additions & 44 deletions src/main/java/edu/ucsb/nceas/mdq/rest/RunsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import edu.ucsb.nceas.mdqengine.exception.MetadigException;
import edu.ucsb.nceas.mdqengine.exception.MetadigStoreException;
import edu.ucsb.nceas.mdqengine.store.StoreFactory;
import edu.ucsb.nceas.mdqengine.store.DatabaseStore;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -26,79 +27,72 @@
@Path("runs")
public class RunsResource {

private Log log = LogFactory.getLog(this.getClass());
private Log log = LogFactory.getLog(this.getClass());

public RunsResource() {}

public RunsResource() {
}

/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
* Method handling HTTP GET requests. The returned object will be sent to the client as
* "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
// @GET
// @Produces(MediaType.APPLICATION_JSON)
// @GET
// @Produces(MediaType.APPLICATION_JSON)
public String listRuns() {
// persist = true causes a database based store to be created by the factory.
boolean persist = true;
MDQStore store = null;
try {
store = StoreFactory.getStore(persist);
Collection<String> runs = null;
try (DatabaseStore store = new DatabaseStore()) {
runs = store.listRuns();
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
throw (ise);
}

Collection<String> runs = store.listRuns();
store.shutdown();
return JsonMarshaller.toJson(runs);
}

@GET
@Path("/{suite}/{id : .+}") // Allow for '/' in the metadataId
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getRun(@PathParam("suite") String suiteId, @PathParam("id") String metadataId, @Context Request r) throws UnsupportedEncodingException, JAXBException {

boolean persist = true;
MDQStore store = null;
try {
store = StoreFactory.getStore(persist);
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
}
// Decode just the pid portion of the URL
try {
metadataId = java.net.URLDecoder.decode(metadataId, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// not going to happen - value came from JDK's own StandardCharsets
}
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getRun(@PathParam("suite") String suiteId, @PathParam("id") String metadataId,
@Context Request r) throws UnsupportedEncodingException, JAXBException {

Run run = null;
log.debug("Getting run for suiteId: " + suiteId + ", metadataId: " + metadataId);
try {
run = store.getRun(metadataId, suiteId);
} catch (MetadigStoreException e) {
try (DatabaseStore store = new DatabaseStore()) {

// Decode just the pid portion of the URL
try {
metadataId = java.net.URLDecoder.decode(metadataId, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// not going to happen - value came from JDK's own StandardCharsets
}

log.debug("Getting run for suiteId: " + suiteId + ", metadataId: " + metadataId);
try {
run = store.getRun(metadataId, suiteId);
} catch (MetadigStoreException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw (ise);
}
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
} finally {
store.shutdown();
throw (ise);
}

if(run != null) {
if (run != null) {
log.debug("Retrieved run with pid: " + run.getId());
} else {
log.info("Run not retrieved for suiteId: " + suiteId + ", metadataId: " + metadataId);
if(run == null) {
if (run == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
}

// Get the HTML request 'Accept' header specified media type and return that type
String resultString = null;
List<Variant> vs =
Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).build();
Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE)
.build();
Variant v = r.selectVariant(vs);
if (v == null) {
return Response.notAcceptable(vs).build();
Expand Down
105 changes: 42 additions & 63 deletions src/main/java/edu/ucsb/nceas/mdq/rest/SuitesResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import edu.ucsb.nceas.mdqengine.exception.MetadigException;
import edu.ucsb.nceas.mdqengine.store.StoreFactory;
import edu.ucsb.nceas.mdqengine.store.DatabaseStore;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -66,7 +67,6 @@ public String listSuites() {
}

Collection<String> suites = store.listSuites();
store.shutdown();
return JsonMarshaller.toJson(suites);
}

Expand All @@ -84,34 +84,27 @@ public String getSuite(@PathParam("id") String id)
throw (ise);
}
Suite suite = store.getSuite(id);
store.shutdown();
return XmlMarshaller.toXml(suite, true);
}

// @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;
MDQEngine engine = null;
try {
store = StoreFactory.getStore(persist);
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {

try (DatabaseStore store = new DatabaseStore()) {
Suite suite = null;
try {
suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class);
store.createSuite(suite);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw (ise);
}
Suite suite = null;
try {
suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class);
store.createSuite(suite);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
} finally {
store.shutdown();
}
return true;
}

Expand All @@ -121,26 +114,20 @@ public boolean createSuite(@FormDataParam("suite") InputStream xml) {
// 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;
MDQStore store = null;
MDQEngine engine = null;
try {
store = StoreFactory.getStore(persist);
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {

try (DatabaseStore store = new DatabaseStore()) {
Suite suite = null;
try {
suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class);
store.updateSuite(suite);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw (ise);
}
Suite suite = null;
try {
suite = (Suite) XmlMarshaller.fromXml(IOUtils.toString(xml, "UTF-8"), Suite.class);
store.updateSuite(suite);
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
} finally {
store.shutdown();
}
return true;
}

Expand All @@ -149,19 +136,14 @@ public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") I
// @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;
MDQEngine engine = null;
try {
store = StoreFactory.getStore(persist);
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {

try (DatabaseStore store = new DatabaseStore()) {
Suite suite = store.getSuite(id);
store.deleteSuite(suite);
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw (ise);
}
Suite suite = store.getSuite(id);
store.deleteSuite(suite);
store.shutdown();
return true;
}

Expand All @@ -179,8 +161,6 @@ public Response run(@PathParam("id") String id, // id is the metadig suite id
// ("high", "medium", "low")
@Context Request r) throws UnsupportedEncodingException, JAXBException {

boolean persist = true;
MDQStore store = null;
MDQEngine engine = null;

if (priority == null)
Expand Down Expand Up @@ -224,26 +204,25 @@ public Response run(@PathParam("id") String id, // id is the metadig suite id
// to the processing queue.
if (priority.equals("high")) {

try {
store = StoreFactory.getStore(persist);
try (DatabaseStore store = new DatabaseStore()) {
engine = new MDQEngine();

try {
log.info("Running suite " + id + " for pid "
+ sysMeta.getIdentifier().getValue());
Map<String, Object> params = new HashMap<String, Object>();
Suite suite = store.getSuite(id);
run = engine.runSuite(suite, input, params, sysMeta);
store.createRun(run);
Dispatcher.getDispatcher("python").close();
} catch (Exception e) {
log.error(e.getMessage(), e);
return Response.serverError().entity(e).build();
}
} catch (MetadigException | IOException | ConfigurationException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw (ise);
}
try {
log.info("Running suite " + id + " for pid " + sysMeta.getIdentifier().getValue());
Map<String, Object> params = new HashMap<String, Object>();
Suite suite = store.getSuite(id);
run = engine.runSuite(suite, input, params, sysMeta);
store.createRun(run);
Dispatcher.getDispatcher("python").close();
} catch (Exception e) {
log.error(e.getMessage(), e);
return Response.serverError().entity(e).build();
} finally {
store.shutdown();
}

// determine the format of plot to return
List<Variant> vs = Variant
Expand Down

0 comments on commit 69fc0ad

Please sign in to comment.