Skip to content

Commit

Permalink
Merge pull request #24 from NCEAS/bugfix-postgres-connections
Browse files Browse the repository at this point in the history
Ensure postgres connections are closed correctly
  • Loading branch information
jeanetteclark committed May 3, 2024
2 parents e37ce1b + 30cd7bf commit 8122103
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 376 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
84 changes: 43 additions & 41 deletions src/main/java/edu/ucsb/nceas/mdq/rest/ChecksResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
*/
@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);
Expand All @@ -66,10 +66,10 @@ public ChecksResource() throws MetadigStoreException {
log.error(e.getMessage(), e);
}
}

/**
* 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.
*/
Expand All @@ -79,18 +79,19 @@ public String 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 {
public String getCheck(@PathParam("id") String id)
throws UnsupportedEncodingException, JAXBException {
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

// @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 {
Expand All @@ -99,54 +100,54 @@ public boolean createCheck(@FormDataParam("check") InputStream xml) {
} 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 {

// @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;
}
}
return true;
}
// @DELETE
// @Path("/{id}")
// @Produces(MediaType.TEXT_PLAIN)
// not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21

// @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);
return true;
}

@POST
@Path("/{id}/run")
@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 {

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) {
sysMeta =
TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysMetaStream);
} catch (InstantiationException | IllegalAccessException | IOException
| MarshallingException e) {
log.warn("Could not unmarshall SystemMetadata from stream", e);
}
}
Expand All @@ -159,12 +160,13 @@ public Response run(
} 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();
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();
Expand All @@ -176,7 +178,7 @@ public Response run(
resultString = JsonMarshaller.toJson(run);
}
}

return Response.ok(resultString).build();
}
}
84 changes: 38 additions & 46 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,70 @@
@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) {
return Response.status(Response.Status.NOT_FOUND).build();
}
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

0 comments on commit 8122103

Please sign in to comment.