11package it .cnr .irea .ediT .rest ;
22
33import it .cnr .irea .ediT .IdGenerator ;
4+ import it .cnr .irea .ediT .MetadataTemplateDocument ;
45import it .cnr .irea .ediT .XsltService ;
6+ import it .cnr .irea .ediT .exception .HostNotConfiguredException ;
57import it .cnr .irea .ediT .exception .Settings ;
8+ import it .cnr .irea .ediT .model .ErrorResponse ;
69import it .cnr .irea .ediT .model .Metadata ;
10+ import it .cnr .irea .ediT .model .PostMetadataResponse ;
11+ import it .cnr .irea .ediT .model .TemplateElement ;
12+ import it .cnr .irea .ediT .model .TemplateElementList ;
13+ import it .cnr .irea .ediT .model .TemplateItem ;
14+ import it .cnr .irea .ediT .model .XsltUrl ;
715import it .cnr .irea .ediT .service .BaseService ;
816
17+ import java .io .ByteArrayInputStream ;
18+ import java .io .IOException ;
19+ import java .io .UnsupportedEncodingException ;
20+ import java .util .Date ;
21+ import java .util .HashMap ;
922import java .util .logging .Logger ;
1023
1124import javax .annotation .PostConstruct ;
25+ import javax .persistence .EntityManager ;
1226import javax .servlet .http .HttpServletRequest ;
27+ import javax .xml .bind .JAXBContext ;
28+ import javax .xml .bind .JAXBException ;
29+ import javax .xml .bind .Unmarshaller ;
30+ import javax .xml .parsers .DocumentBuilder ;
31+ import javax .xml .parsers .DocumentBuilderFactory ;
32+ import javax .xml .parsers .ParserConfigurationException ;
33+ import javax .xml .xpath .XPath ;
34+ import javax .xml .xpath .XPathFactory ;
1335
1436import org .springframework .beans .factory .annotation .Autowired ;
37+ import org .springframework .http .HttpStatus ;
1538import org .springframework .http .MediaType ;
39+ import org .springframework .http .ResponseEntity ;
40+ import org .springframework .web .bind .annotation .ExceptionHandler ;
41+ import org .springframework .web .bind .annotation .PathVariable ;
42+ import org .springframework .web .bind .annotation .RequestBody ;
1643import org .springframework .web .bind .annotation .RequestHeader ;
1744import org .springframework .web .bind .annotation .RequestMapping ;
1845import org .springframework .web .bind .annotation .RequestMethod ;
1946import org .springframework .web .bind .annotation .ResponseBody ;
47+ import org .springframework .web .bind .annotation .ResponseStatus ;
2048import org .springframework .web .bind .annotation .RestController ;
49+ import org .w3c .dom .Document ;
50+ import org .xml .sax .SAXException ;
2151
2252@ RestController
2353public class RestBase extends CORSDecorator {
@@ -29,6 +59,7 @@ public class RestBase extends CORSDecorator {
2959 private IdGenerator generator ;
3060 @ Autowired
3161 private BaseService service ;
62+ @ Autowired EntityManager em ;
3263
3364 public RestBase () {
3465 Settings .load ();
@@ -40,35 +71,33 @@ public void postConstruct() {
4071 Metadata .service = service ;
4172 }
4273
74+ @ ExceptionHandler (HostNotConfiguredException .class )
75+ @ ResponseStatus (value = HttpStatus .PRECONDITION_FAILED )
76+ public @ ResponseBody ErrorResponse handleException (HostNotConfiguredException e ) {
77+ log .severe ("Host not configured" );
78+ return ErrorResponse .HOST_NOT_CONFIGURED ;
79+ }
80+
4381 @ RequestMapping (method = RequestMethod .GET , value = "rest/ediml/requestId" , produces = MediaType .APPLICATION_JSON_VALUE )
4482 @ ResponseBody
45- public Metadata getNewId (HttpServletRequest request , @ RequestHeader ("Host" ) String host ) {
83+ public Metadata getNewId (HttpServletRequest request , @ RequestHeader ("Host" ) String host ) throws HostNotConfiguredException {
84+ if ( service .getHostName () == null ) {
85+ throw new HostNotConfiguredException ();
86+ }
4687 log .info ("requestId " + host );
4788 log .info ("request: " + request .getRequestURL ());
4889 Metadata md = service .prepareMetadata ();
4990 log .info ("" + md .getId ());
5091 return md ;
5192 }
5293
53-
54- /*
55- @OPTIONS
56- @Path("rest/metadata")
57- @Consumes(value = {"application/xml", "text/xml"})
58- @Produces("application/xml")
59- public Response optionsPostMetadata(String xml) {
60- return build(Response.ok());
61- }
62-
63-
64- @POST
65- @Path("rest/metadata")
66- @Consumes(value = {"application/xml", "text/xml"})
67- @Produces("application/json")
68- public Response postMetadataNG(@Context HttpServletRequest req, @HeaderParam("X-Forwarded-For") String ip, String xml, @HeaderParam("api_key") String apiKey) {
94+ @ RequestMapping (method = RequestMethod .POST , value = "rest/metadata" , produces = MediaType .APPLICATION_JSON_VALUE , consumes = MediaType .APPLICATION_XML_VALUE )
95+ @ ResponseBody
96+ public ResponseEntity <PostMetadataResponse > postMetadataNG (HttpServletRequest req , @ RequestHeader (value = "X-Forwarded-For" , required = false ) String ip , @ RequestBody String xml , @ RequestHeader (value = "api_key" , required = false ) String apiKey ) {
97+ log .info ("xml: " + xml );
6998 PostMetadataResponse response = new PostMetadataResponse ();
7099 MetadataTemplateDocument document = null ;
71-
100+ // String ip = null;
72101 if ( ip == null ) {
73102 ip = req .getRemoteAddr ();
74103 }
@@ -116,14 +145,17 @@ public Response postMetadataNG(@Context HttpServletRequest req, @HeaderParam("X-
116145 } else {
117146 baseDocument = elementList .getBaseDocument ().getBytes ("utf-8" );
118147 }
119- document = new MetadataTemplateDocument(baseDocument);
148+ document = new MetadataTemplateDocument (baseDocument , service );
120149 document .addAll (elementList );
121150 // return Response.ok(xml).build();
122151 Document xmlDoc = document .getOutput ();
123152
153+
154+ service .saveMetadata (document , xml , elementList );
155+
124156 document .saveTo ("/tmp/last_md.xml" );
125157
126- response.setEdimlId(elementList.getFileId());
158+ response .setEdimlId (Integer . parseInt ( elementList .getFileId () ));
127159 response .setResponseCode (200 );
128160 response .setMessages (document .getMessages ());
129161
@@ -144,7 +176,7 @@ public Response postMetadataNG(@Context HttpServletRequest req, @HeaderParam("X-
144176 response .setGeneratedXml (xmlTemp );
145177
146178 System .out .println ("Морски" );
147- return build(Response.ok( response) );
179+ return new ResponseEntity < PostMetadataResponse >( response , HttpStatus . OK );
148180 }
149181 } catch (Exception e ) {
150182 e .printStackTrace ();
@@ -154,7 +186,75 @@ public Response postMetadataNG(@Context HttpServletRequest req, @HeaderParam("X-
154186 response .setMessages (document .getMessages ());
155187 }
156188 }
157- return build(Response.status(500).entity(response));
189+ return new ResponseEntity <PostMetadataResponse >(response , HttpStatus .INTERNAL_SERVER_ERROR );
190+ }
191+
192+ @ RequestMapping (method = RequestMethod .OPTIONS , value = "rest/metadata" , produces = MediaType .APPLICATION_JSON_VALUE , consumes = MediaType .APPLICATION_XML_VALUE )
193+ @ ResponseBody
194+ public ResponseEntity <PostMetadataResponse > optionsPostMetadata (String xml ) {
195+ return new ResponseEntity <PostMetadataResponse >(HttpStatus .OK );
158196 }
159- */
197+
198+
199+ @ RequestMapping (method = RequestMethod .GET , value = "/rest/ediml/{id}" , produces = MediaType .APPLICATION_XML_VALUE )
200+ @ ResponseBody
201+ public String getEDIMLXml (@ PathVariable int id ) {
202+ log .info ("getEDIMLXml " + id );
203+ Metadata md = service .getMetadata (id );
204+ return md .getInput ();
205+ }
206+
207+ @ RequestMapping (method = RequestMethod .GET , value = "/rest/ediml/{id}" , produces = MediaType .APPLICATION_JSON_VALUE )
208+ @ ResponseBody
209+ public TemplateElementList getEDIML (@ PathVariable int id ) {
210+ log .info ("getEDIML " + id );
211+ Metadata md = service .getMetadata (id );
212+ TemplateElementList elementList = null ;
213+ String xml = md .getInput ();
214+
215+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance ();
216+ DocumentBuilder dBuilder ;
217+
218+ try {
219+ dBuilder = dbFactory .newDocumentBuilder ();
220+ if ( xml != null ) {
221+ Document input = dBuilder .parse (new ByteArrayInputStream (xml .getBytes ("utf-8" )));
222+ JAXBContext jc = JAXBContext .newInstance (TemplateElementList .class );
223+ //Create unmarshaller
224+ Unmarshaller um = jc .createUnmarshaller ();
225+ //Unmarshal XML contents of the file myDoc.xml into your Java object instance.
226+ elementList = (TemplateElementList ) um .unmarshal (new ByteArrayInputStream (xml .getBytes ("utf-8" )));
227+ for ( TemplateElement element : elementList .getElements () ) {
228+ for ( TemplateItem item : element .getItems () ) {
229+ if ( item .getDataType () == null || item .getDataType ().trim ().equalsIgnoreCase ("" ) ) {
230+ log .info ("Item " + item .getId () + " has no datatype" );
231+ }
232+ }
233+ }
234+ } else {
235+ elementList = new TemplateElementList ();
236+ }
237+ if ( elementList != null && (elementList .getFileId () == null || elementList .getFileId ().trim ().equalsIgnoreCase ("" )) ) {
238+ elementList .setFileId ("" + md .getId ());
239+ elementList .setFileUri (md .getUri ());
240+ elementList .setStarterKitUri (md .getStarterKit ().getUri ());
241+ }
242+ } catch (ParserConfigurationException e ) {
243+ // TODO Auto-generated catch block
244+ e .printStackTrace ();
245+ } catch (UnsupportedEncodingException e ) {
246+ // TODO Auto-generated catch block
247+ e .printStackTrace ();
248+ } catch (SAXException e ) {
249+ // TODO Auto-generated catch block
250+ e .printStackTrace ();
251+ } catch (IOException e ) {
252+ // TODO Auto-generated catch block
253+ e .printStackTrace ();
254+ } catch (JAXBException e ) {
255+ // TODO Auto-generated catch block
256+ e .printStackTrace ();
257+ }
258+ return elementList ;
259+ }
160260}
0 commit comments