1818import javax .ws .rs .Produces ;
1919import javax .ws .rs .core .Context ;
2020import javax .ws .rs .core .MediaType ;
21- import javax .ws .rs .core .MultivaluedMap ;
2221import javax .ws .rs .core .Request ;
2322import javax .ws .rs .core .Response ;
2423import javax .ws .rs .core .Variant ;
2524import javax .xml .bind .JAXBException ;
2625
27- import com .hp .hpl .jena .shared .ConfigException ;
2826import edu .ucsb .nceas .mdqengine .exception .MetadigException ;
2927import edu .ucsb .nceas .mdqengine .exception .MetadigStoreException ;
30- import net .sf .saxon .functions .ConstantFunction ;
3128import org .apache .commons .configuration2 .ex .ConfigurationException ;
3229import org .apache .commons .io .IOUtils ;
3330import org .apache .commons .logging .Log ;
4441import edu .ucsb .nceas .mdqengine .store .StoreFactory ;
4542import edu .ucsb .nceas .mdqengine .serialize .JsonMarshaller ;
4643import edu .ucsb .nceas .mdqengine .serialize .XmlMarshaller ;
44+ import edu .ucsb .nceas .mdqengine .dispatch .Dispatcher ;
4745
4846/**
4947 * Root resource (exposed at "checks" path)
5048 */
5149@ Path ("checks" )
5250public class ChecksResource {
53-
54- private Log log = LogFactory .getLog (this .getClass ());
55-
56- private MDQStore store = null ;
57-
58- private MDQEngine engine = null ;
59-
60- public ChecksResource () throws MetadigStoreException {
61- boolean persist = false ;
62- this .store = StoreFactory .getStore (persist );
51+
52+ private Log log = LogFactory .getLog (this .getClass ());
53+
54+ private MDQStore store = null ;
55+
56+ private MDQEngine engine = null ;
57+
58+ public ChecksResource () throws MetadigStoreException {
59+ boolean persist = false ;
60+ this .store = StoreFactory .getStore (persist );
6361
64- try {
65- this .engine = new MDQEngine ();
66- this .engine .setStore (this .store );
67- } catch (MetadigException | IOException | ConfigurationException e ) {
68- log .error (e .getMessage (), e );
69- }
70- }
71-
62+ try {
63+ this .engine = new MDQEngine ();
64+ this .engine .setStore (this .store );
65+ } catch (MetadigException | IOException | ConfigurationException e ) {
66+ log .error (e .getMessage (), e );
67+ }
68+ }
69+
7270 /**
7371 * Method handling HTTP GET requests. The returned object will be sent
7472 * to the client as "text/plain" media type.
@@ -78,53 +76,56 @@ public ChecksResource() throws MetadigStoreException {
7876 @ GET
7977 @ Produces (MediaType .APPLICATION_JSON )
8078 public String listChecks () {
81- Collection <String > checks = store .listChecks ();
79+ Collection <String > checks = store .listChecks ();
8280 return JsonMarshaller .toJson (checks );
8381 }
8482
8583 @ GET
8684 @ Path ("/{id}" )
8785 @ Produces (MediaType .TEXT_XML )
8886 public String getCheck (@ PathParam ("id" ) String id ) throws UnsupportedEncodingException , JAXBException {
89- Check check = store .getCheck (id );
87+ Check check = store .getCheck (id );
9088 return (String ) XmlMarshaller .toXml (check , true );
9189 }
9290
9391// @POST
9492// @Consumes(MediaType.MULTIPART_FORM_DATA)
93+ // not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
9594 public boolean createCheck (@ FormDataParam ("check" ) InputStream xml ) {
96- Check check = null ;
97- try {
98- check = (Check ) XmlMarshaller .fromXml (IOUtils .toString (xml , "UTF-8" ), Check .class );
99- store .createCheck (check );
100- } catch (Exception e ) {
101- log .error (e .getMessage (), e );
102- return false ;
103- }
95+ Check check = null ;
96+ try {
97+ check = (Check ) XmlMarshaller .fromXml (IOUtils .toString (xml , "UTF-8" ), Check .class );
98+ store .createCheck (check );
99+ } catch (Exception e ) {
100+ log .error (e .getMessage (), e );
101+ return false ;
102+ }
104103 return true ;
105104 }
106105
107106// @PUT
108107// @Path("/{id}")
109108// @Consumes(MediaType.MULTIPART_FORM_DATA)
109+ // not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
110110 public boolean updateCheck (@ PathParam ("id" ) String id , @ FormDataParam ("check" ) InputStream xml ) throws JAXBException , IOException {
111- Check check = null ;
112- try {
113- check = (Check ) XmlMarshaller .fromXml (IOUtils .toString (xml , "UTF-8" ), Check .class );
114- store .updateCheck (check );
115- } catch (Exception e ) {
116- log .error (e .getMessage (), e );
117- return false ;
118- }
111+ Check check = null ;
112+ try {
113+ check = (Check ) XmlMarshaller .fromXml (IOUtils .toString (xml , "UTF-8" ), Check .class );
114+ store .updateCheck (check );
115+ } catch (Exception e ) {
116+ log .error (e .getMessage (), e );
117+ return false ;
118+ }
119119 return true ;
120120 }
121121
122122// @DELETE
123123// @Path("/{id}")
124124// @Produces(MediaType.TEXT_PLAIN)
125+ // not enabled for security reasons, see: https://github.com/NCEAS/metadig-webapp/issues/21
125126 public boolean updateCheck (@ PathParam ("id" ) String id ) {
126- Check check = store .getCheck (id );
127- store .deleteCheck (check );
127+ Check check = store .getCheck (id );
128+ store .deleteCheck (check );
128129 return true ;
129130 }
130131
@@ -133,51 +134,49 @@ public boolean updateCheck(@PathParam("id") String id) {
133134 @ Consumes (MediaType .MULTIPART_FORM_DATA )
134135 @ Produces ({MediaType .APPLICATION_JSON , MediaType .APPLICATION_XML })
135136 public Response run (
136- @ PathParam ("id" ) String id ,
137- @ FormDataParam ("document" ) InputStream input ,
138- @ FormDataParam ("systemMetadata" ) InputStream sysMetaStream ,
139- @ Context Request r ) throws UnsupportedEncodingException , JAXBException {
140-
141- Run run = null ;
142- // include SM if it was provided
143- SystemMetadata sysMeta = null ;
144- if (sysMetaStream != null ) {
145- try {
146- sysMeta = TypeMarshaller .unmarshalTypeFromStream (SystemMetadata .class , sysMetaStream );
147- } catch (InstantiationException | IllegalAccessException
148- | IOException | MarshallingException e ) {
149- log .warn ("Could not unmarshall SystemMetadata from stream" , e );
150- }
151- }
152- try {
153- Map <String , Object > params = new HashMap <String , Object >();
154- // params.putAll(formParams);
155- // params.remove("id");
156- // params.remove("document");
157- Check check = store .getCheck (id );
158- run = engine .runCheck (check , input , params , sysMeta );
159- store .createRun (run );
160- } catch (Exception e ) {
161- log .error (e .getMessage (), e );
162- return Response .serverError ().entity (e ).build ();
163- }
164-
165- // determine the format of plot to return
137+ @ PathParam ("id" ) String id ,
138+ @ FormDataParam ("document" ) InputStream input ,
139+ @ FormDataParam ("systemMetadata" ) InputStream sysMetaStream ,
140+ @ Context Request r ) throws UnsupportedEncodingException , JAXBException {
141+
142+ Run run = null ;
143+ // include SM if it was provided
144+ SystemMetadata sysMeta = null ;
145+ if (sysMetaStream != null ) {
146+ try {
147+ sysMeta = TypeMarshaller .unmarshalTypeFromStream (SystemMetadata .class , sysMetaStream );
148+ } catch (InstantiationException | IllegalAccessException
149+ | IOException | MarshallingException e ) {
150+ log .warn ("Could not unmarshall SystemMetadata from stream" , e );
151+ }
152+ }
153+ try {
154+ Map <String , Object > params = new HashMap <String , Object >();
155+ Check check = store .getCheck (id );
156+ run = engine .runCheck (check , input , params , sysMeta );
157+ store .createRun (run );
158+ Dispatcher .getDispatcher ("python" ).close ();
159+ } catch (Exception e ) {
160+ log .error (e .getMessage (), e );
161+ return Response .serverError ().entity (e ).build ();
162+ }
163+
164+ // determine the format of plot to return
166165 String resultString = null ;
167- List <Variant > vs =
168- Variant .mediaTypes (MediaType .APPLICATION_JSON_TYPE , MediaType .APPLICATION_XML_TYPE ).build ();
169- Variant v = r .selectVariant (vs );
170- if (v == null ) {
171- return Response .notAcceptable (vs ).build ();
172- } else {
173- MediaType mt = v .getMediaType ();
174- if (mt .equals (MediaType .APPLICATION_XML_TYPE )) {
175- resultString = XmlMarshaller .toXml (run , true );
176- } else {
177- resultString = JsonMarshaller .toJson (run );
178- }
179- }
180-
181- return Response .ok (resultString ).build ();
166+ List <Variant > vs =
167+ Variant .mediaTypes (MediaType .APPLICATION_JSON_TYPE , MediaType .APPLICATION_XML_TYPE ).build ();
168+ Variant v = r .selectVariant (vs );
169+ if (v == null ) {
170+ return Response .notAcceptable (vs ).build ();
171+ } else {
172+ MediaType mt = v .getMediaType ();
173+ if (mt .equals (MediaType .APPLICATION_XML_TYPE )) {
174+ resultString = XmlMarshaller .toXml (run , true );
175+ } else {
176+ resultString = JsonMarshaller .toJson (run );
177+ }
178+ }
179+
180+ return Response .ok (resultString ).build ();
182181 }
183182}
0 commit comments