From a49a9c9c0113edf6d7b1f05fa6c6ecdb1e4be19e Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Wed, 23 Nov 2022 14:45:56 +0100 Subject: [PATCH] upload file api --- apps/dnet-is-application/pom.xml | 2 +- .../is/resources/ResourcesRestController.java | 50 ++++++++++++++++--- .../resources/static/js/simpleResources.js | 14 +++--- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/apps/dnet-is-application/pom.xml b/apps/dnet-is-application/pom.xml index 95b95510..2ff3ed49 100644 --- a/apps/dnet-is-application/pom.xml +++ b/apps/dnet-is-application/pom.xml @@ -24,7 +24,7 @@ org.springframework.boot spring-boot-starter-thymeleaf - + jaxen jaxen diff --git a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java index 11410bcf..51de8f75 100644 --- a/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java +++ b/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java @@ -13,10 +13,13 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -47,18 +50,18 @@ public class ResourcesRestController { } @DeleteMapping("/{id}") - public void deleteResources(@PathVariable final String id) { + public void deleteResource(@PathVariable final String id) { log.info("Deleting resource: " + id); simpleResourceRepository.deleteById(id); } @GetMapping("/{id}/metadata") - public SimpleResource getMetadataResource(@PathVariable final String id) throws InformationServiceException { + public SimpleResource getMetadata(@PathVariable final String id) throws InformationServiceException { return simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found")); } @GetMapping("/{id}/content") - public void getContentResource(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException { + public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException { final SimpleResource sr = simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found")); res.setCharacterEncoding(StandardCharsets.UTF_8.name()); @@ -73,12 +76,45 @@ public class ResourcesRestController { } } - @PostMapping(value = "/operation/import", consumes = "text/plain") + @PostMapping("/{id}/metadata") + public void saveMetadata(@RequestBody final SimpleResource r) throws InformationServiceException { + if (simpleResourceRepository.existsById(r.getId())) { + simpleResourceRepository.save(r); + } else { + throw new InformationServiceException("Resource not found"); + } + } + + @PostMapping("/{id}/content") + public void saveContent(@PathVariable final String id, @RequestParam final String content) throws InformationServiceException { + if (simpleResourceRepository.existsById(id)) { + simpleResourceRepository.setContentById(id, content); + } else { + throw new InformationServiceException("Resource not found"); + } + } + + @PutMapping(value = "/{id}/content", consumes = { + MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE + }) + public void uploadContent(@PathVariable final String id, final HttpServletRequest request) throws InformationServiceException { + if (simpleResourceRepository.existsById(id)) { + try { + final String content = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); + simpleResourceRepository.setContentById(id, content); + } catch (final Exception e) { + throw new InformationServiceException("Error processing file", e); + } + } else { + throw new InformationServiceException("Resource not found"); + } + } + + @PostMapping(value = "/operation/import", consumes = { + MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE + }) public SimpleResource importFromOldProfile(final HttpServletRequest request) throws Exception { - final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); - return oldProfilesImporter.importSimpleResource(xml); - } } diff --git a/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js b/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js index 1ccf1789..0127f76e 100644 --- a/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js +++ b/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js @@ -43,12 +43,12 @@ app.controller('resourcesController', function($scope, $http) { $scope.tmpRes = angular.copy(res); } - $scope.saveMetadata = function(res) { + $scope.saveMetadata = function(md) { if ($scope.mode == 'new') { var found = false; angular.forEach($scope.resources, function(r) { - if (res.id == r.id) { found = true; }; + if (md.id == r.id) { found = true; }; }); if (found) { @@ -58,17 +58,19 @@ app.controller('resourcesController', function($scope, $http) { } $http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8"; - $http.post('./api/resources/?' + $.now(), res).then(function successCallback(res) { - $scope.resources = res.data; + $http.post('./api/resources/?' + $.now(), md).then(function successCallback(res) { alert("Resource saved"); + $scope.reload(); }, function errorCallback(res) { alert('ERROR: ' + res.data.message); }); }; $scope.saveContent = function(id, content) { - $http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8"; - $http.post('./api/resources/' + id + '/content?' + $.now(), res).then(function successCallback(res) { + $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"; + $http.post('./api/resources/' + id + '/content?' + $.now(), $.param({ + 'content' : content + })).then(function successCallback(res) { alert("Resource saved"); }, function errorCallback(res) { alert('ERROR: ' + res.data.message);