Skip to content

Commit

Permalink
use a local store to get suite resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanetteclark committed Jan 12, 2024
1 parent bae0048 commit c7d0085
Showing 1 changed file with 97 additions and 88 deletions.
185 changes: 97 additions & 88 deletions src/main/java/edu/ucsb/nceas/mdq/rest/SuitesResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,58 +41,55 @@
*/
@Path("suites")
public class SuitesResource {
private Log log = LogFactory.getLog(this.getClass());

private Log log = LogFactory.getLog(this.getClass());
private static Controller metadigCtrl = null;
public SuitesResource() {}

public SuitesResource() {}

/**
* 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)
public String listSuites() {
boolean persist = true;
boolean persist = false;
MDQStore store = null;
MDQEngine engine = null;
try {
store = StoreFactory.getStore(persist);
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
throw (ise);
}

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

@GET
@Path("/{id}")
@Produces(MediaType.TEXT_XML)
public String getSuite(@PathParam("id") String id) throws UnsupportedEncodingException, JAXBException {
boolean persist = true;
public String getSuite(@PathParam("id") String id)
throws UnsupportedEncodingException, JAXBException {
boolean persist = false;
MDQStore store = null;
MDQEngine engine = null;
try {
store = StoreFactory.getStore(persist);
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {
} catch (MetadigException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
throw (ise);
}
Suite suite = store.getSuite(id);
Suite suite = store.getSuite(id);
store.shutdown();
return XmlMarshaller.toXml(suite, true);
}
// @POST
// @Consumes(MediaType.MULTIPART_FORM_DATA)

// @POST
// @Consumes(MediaType.MULTIPART_FORM_DATA)
public boolean createSuite(@FormDataParam("suite") InputStream xml) {
boolean persist = true;
MDQStore store = null;
Expand All @@ -102,25 +99,26 @@ public boolean createSuite(@FormDataParam("suite") InputStream xml) {
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
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();
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;
}

// @PUT
// @Path("/{id}")
// @Consumes(MediaType.MULTIPART_FORM_DATA)
public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") InputStream xml) throws JAXBException, IOException {

// @PUT
// @Path("/{id}")
// @Consumes(MediaType.MULTIPART_FORM_DATA)
public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") InputStream xml)
throws JAXBException, IOException {
boolean persist = true;
MDQStore store = null;
MDQEngine engine = null;
Expand All @@ -129,24 +127,24 @@ public boolean updateSuite(@PathParam("id") String id, @FormDataParam("suite") I
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
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();
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;
}
// @DELETE
// @Path("/{id}")
// @Produces(MediaType.TEXT_PLAIN)

// @DELETE
// @Path("/{id}")
// @Produces(MediaType.TEXT_PLAIN)
public boolean deleteSuite(@PathParam("id") String id) {
boolean persist = true;
MDQStore store = null;
Expand All @@ -156,33 +154,37 @@ public boolean deleteSuite(@PathParam("id") String id) {
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
throw (ise);
}
Suite suite = store.getSuite(id);
store.deleteSuite(suite);
store.shutdown();
Suite suite = store.getSuite(id);
store.deleteSuite(suite);
store.shutdown();
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, // id is the metadig suite id
@FormDataParam("document") InputStream input, // the input metadata document
@FormDataParam("systemMetadata") InputStream sysMetaStream, // the system metadata for the input metadata document
@FormDataParam("priority") String priority, // the priority to enqueue the metadig engine request with ("high", "medium", "low")
@Context Request r) throws UnsupportedEncodingException, JAXBException {
public Response run(@PathParam("id") String id, // id is the metadig suite id
@FormDataParam("document") InputStream input, // the input metadata document
@FormDataParam("systemMetadata") InputStream sysMetaStream, // the system metadata for
// the input metadata
// document
@FormDataParam("priority") String priority, // the priority to enqueue the metadig
// engine request with
// ("high", "medium", "low")
@Context Request r) throws UnsupportedEncodingException, JAXBException {

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

if(priority == null) priority = "low";
Run run = null;
if (priority == null)
priority = "low";
Run run = null;
String resultString = null;
// Copy the sysmeta input stream because we need to read it twice
// Copy the sysmeta input stream because we need to read it twice
ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] streamData = null;
Expand All @@ -202,27 +204,29 @@ public Response run(

ByteArrayInputStream sysmetaStream = new ByteArrayInputStream(streamData);

SystemMetadata sysMeta = null;
// Read sysmeta input stream to get values to log and pass to the controller
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);
}
}
SystemMetadata sysMeta = null;
// Read sysmeta input stream to get values to log and pass to the controller
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);
}
}

// If the request is identifying itself as 'high', then process it now, otherwise send it
// If the request is identifying itself as 'high', then process it now,
// otherwise send it
// to the processing queue.
if(priority.equals("high")) {
if (priority.equals("high")) {

try {
store = StoreFactory.getStore(persist);
engine = new MDQEngine();
} catch (MetadigException | IOException | ConfigurationException e) {
InternalServerErrorException ise = new InternalServerErrorException(e.getMessage());
throw(ise);
throw (ise);
}
try {
log.info("Running suite " + id + " for pid " + sysMeta.getIdentifier().getValue());
Expand All @@ -239,8 +243,9 @@ public Response run(
}

// determine the format of plot to return
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 @@ -254,7 +259,7 @@ public Response run(
}
} else {
try {
if(metadigCtrl == null) {
if (metadigCtrl == null) {
metadigCtrl = Controller.getInstance();
// Start the controller if it has not already been started.
if (!metadigCtrl.getIsStarted()) {
Expand All @@ -266,7 +271,8 @@ public Response run(
return Response.serverError().entity(e).build();
}

// Check if the metadig-engine controller has been started. If not, return a message.
// Check if the metadig-engine controller has been started. If not, return a
// message.
// TODO: return a properly formatted XML error message
if (!metadigCtrl.getIsStarted()) {
return Response.serverError().build();
Expand All @@ -278,8 +284,11 @@ public Response run(
DateTime requestDateTime = new DateTime();
NodeReference dataSource = sysMeta.getOriginMemberNode();
String metadataPid = sysMeta.getIdentifier().getValue();
log.info("Queue generation request of quality document for: " + dataSource.getValue() + ", PID: " + metadataPid + ", " + id + ", " + requestDateTime.toString());
metadigCtrl.processQualityRequest(dataSource.getValue(), metadataPid, input, id, "", requestDateTime, sysmetaStream2);
log.info("Queue generation request of quality document for: "
+ dataSource.getValue() + ", PID: " + metadataPid + ", " + id + ", "
+ requestDateTime.toString());
metadigCtrl.processQualityRequest(dataSource.getValue(), metadataPid, input, id, "",
requestDateTime, sysmetaStream2);
} catch (Exception e) {
log.error(e.getMessage(), e);
return Response.serverError().entity(e).build();
Expand All @@ -288,5 +297,5 @@ public Response run(

return Response.ok().build();
}

}

0 comments on commit c7d0085

Please sign in to comment.