separated ajax and api calls

This commit is contained in:
Michele Artini 2022-12-05 11:04:54 +01:00
parent 4335f420df
commit 67ec5bc368
20 changed files with 174 additions and 102 deletions

View File

@ -17,7 +17,7 @@ import eu.dnetlib.is.resource.model.ResourceType;
import eu.dnetlib.is.resource.repository.ResourceTypeRepository;
import eu.dnetlib.is.vocabulary.model.Vocabulary;
import eu.dnetlib.is.vocabulary.repository.VocabularyRepository;
import eu.dnetlib.is.wfs.WfHistoryRestController;
import eu.dnetlib.is.wfs.WfHistoryAjaxController;
@Controller
public class MainController {
@ -76,7 +76,7 @@ public class MainController {
public void wfHistory(final ModelMap map,
@RequestParam(required = false, defaultValue = "-1") final Long from,
@RequestParam(required = false, defaultValue = "-1") final Long to) {
map.put("maxNumberOfRecentWfs", WfHistoryRestController.MAX_NUMBER_OF_RECENT_WFS);
map.put("maxNumberOfRecentWfs", WfHistoryAjaxController.MAX_NUMBER_OF_RECENT_WFS);
map.put("fromDate", from);
map.put("toDate", to);
}

View File

@ -0,0 +1,21 @@
package eu.dnetlib.is.context;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.is.context.model.Context;
public class AbstractContextController extends AbstractDnetController {
@Autowired
protected ContextService contextService;
@GetMapping("/")
public List<Context> listContexts() {
return contextService.listContexts();
}
}

View File

@ -1,43 +1,24 @@
package eu.dnetlib.is.context;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.is.context.model.Category;
import eu.dnetlib.is.context.model.Context;
import eu.dnetlib.is.context.model.CtxChildInfo;
import eu.dnetlib.is.util.InformationServiceException;
@RestController
@RequestMapping("/api/contexts")
public class ContextRestController extends AbstractDnetController {
@Autowired
private ContextService contextService;
@GetMapping("/")
public List<Context> listContexts() {
return contextService.listContexts();
}
@RequestMapping("/ajax/contexts")
public class ContextAjaxController extends AbstractContextController {
@GetMapping("/{ctxId}")
public Context getContext(@PathVariable final String ctxId) throws InformationServiceException {
return contextService.getContext(ctxId);
}
@GetMapping("/{ctxId}/full")
public ObjectNode getContextFull(@PathVariable final String ctxId) throws InformationServiceException {
return contextService.getContextFull(ctxId);
}
@GetMapping("/{parent}/categories")
public Iterable<Category> listCategories(@PathVariable final String parent) {
return contextService.listCategories(parent);

View File

@ -0,0 +1,21 @@
package eu.dnetlib.is.context;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.dnetlib.is.util.InformationServiceException;
@RestController
@RequestMapping("/api/contexts")
public class ContextApiController extends AbstractContextController {
@GetMapping("/{ctxId}")
public ObjectNode getContextFull(@PathVariable final String ctxId) throws InformationServiceException {
return contextService.getContextFull(ctxId);
}
}

View File

@ -33,8 +33,8 @@ import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.common.controller.AbstractDnetController;
@RestController
@RequestMapping("/api/info")
public class InfoRestController extends AbstractDnetController {
@RequestMapping("/ajax/info")
public class InfoAjaxController extends AbstractDnetController {
@Autowired
private ConfigurableEnvironment configurableEnvironment;
@ -44,7 +44,7 @@ public class InfoRestController extends AbstractDnetController {
private final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
private static final Log log = LogFactory.getLog(InfoRestController.class);
private static final Log log = LogFactory.getLog(InfoAjaxController.class);
@GetMapping("/")
public List<InfoSection<?>> info() throws Exception {

View File

@ -0,0 +1,53 @@
package eu.dnetlib.is.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.is.resource.model.SimpleResource;
import eu.dnetlib.is.util.InformationServiceException;
import eu.dnetlib.is.util.XmlIndenter;
public class AbstractResourceController extends AbstractDnetController {
@Autowired
protected SimpleResourceService service;
@GetMapping("/")
public List<SimpleResource> listResources(@RequestParam final String type) {
return service.listResources(type);
}
@GetMapping("/{id}/metadata")
public SimpleResource getMetadata(@PathVariable final String id) throws InformationServiceException {
return service.getMetadata(id);
}
@GetMapping("/{id}/content")
public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException {
final String ctype = service.getContentType(id);
res.setCharacterEncoding(StandardCharsets.UTF_8.name());
res.setContentType(ctype);
final String content =
ctype.equals(MediaType.APPLICATION_XML_VALUE) ? XmlIndenter.indent(service.getContent(id)) : service.getContent(id);
try {
IOUtils.write(content, res.getOutputStream(), StandardCharsets.UTF_8.name());
} catch (final IOException e) {
throw new InformationServiceException("Error retrieving content", e);
}
}
}

View File

@ -2,16 +2,12 @@ package eu.dnetlib.is.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
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.RequestBody;
@ -19,22 +15,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.is.resource.model.SimpleResource;
import eu.dnetlib.is.util.InformationServiceException;
import eu.dnetlib.is.util.XmlIndenter;
@RestController
@RequestMapping("/api/resources")
public class ResourcesRestController extends AbstractDnetController {
@Autowired
private SimpleResourceService service;
@GetMapping("/")
public List<SimpleResource> listResources(@RequestParam final String type) {
return service.listResources(type);
}
@RequestMapping("/ajax/resources")
public class ResourceAjaxController extends AbstractResourceController {
@PostMapping("/")
public SimpleResource newResource(@RequestParam final String name,
@ -51,28 +37,6 @@ public class ResourcesRestController extends AbstractDnetController {
service.deleteResource(id);
}
@GetMapping("/{id}/metadata")
public SimpleResource getMetadata(@PathVariable final String id) throws InformationServiceException {
return service.getMetadata(id);
}
@GetMapping("/{id}/content")
public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException {
final String ctype = service.getContentType(id);
res.setCharacterEncoding(StandardCharsets.UTF_8.name());
res.setContentType(ctype);
final String content =
ctype.equals(MediaType.APPLICATION_XML_VALUE) ? XmlIndenter.indent(service.getContent(id)) : service.getContent(id);
try {
IOUtils.write(content, res.getOutputStream(), StandardCharsets.UTF_8.name());
} catch (final IOException e) {
throw new InformationServiceException("Error retrieving content", e);
}
}
@PostMapping("/{id}/metadata")
public void saveMetadata(@PathVariable final String id, @RequestBody final SimpleResource r) throws InformationServiceException {
service.saveMetadata(id, r);

View File

@ -0,0 +1,10 @@
package eu.dnetlib.is.resources;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/resources")
public class ResourceApiController extends AbstractResourceController {
}

View File

@ -0,0 +1,27 @@
package eu.dnetlib.is.vocabulary;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.is.vocabulary.model.Vocabulary;
import eu.dnetlib.is.vocabulary.model.VocabularyTerm;
public class AbstractVocabularyController extends AbstractDnetController {
@Autowired
protected VocabularyService vocabularyService;
@GetMapping("/")
public List<Vocabulary> listVocs() {
return vocabularyService.listVocs();
}
@GetMapping("/{vocabulary}/terms")
public Iterable<VocabularyTerm> listTerms(@PathVariable final String vocabulary) {
return vocabularyService.listTerms(vocabulary);
}
}

View File

@ -2,7 +2,6 @@ package eu.dnetlib.is.vocabulary;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -11,22 +10,13 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.is.util.InformationServiceException;
import eu.dnetlib.is.vocabulary.model.Vocabulary;
import eu.dnetlib.is.vocabulary.model.VocabularyTerm;
@RestController
@RequestMapping("/api/vocs")
public class VocabularyRestController extends AbstractDnetController {
@Autowired
private VocabularyService vocabularyService;
@GetMapping("/")
public List<Vocabulary> listVocs() {
return vocabularyService.listVocs();
}
@RequestMapping("/ajax/vocs")
public class VocabularyAjaxController extends AbstractVocabularyController {
@GetMapping("/{vocabulary}")
public Vocabulary getVoc(@PathVariable final String vocabulary) throws InformationServiceException {
@ -45,11 +35,6 @@ public class VocabularyRestController extends AbstractDnetController {
return vocabularyService.listVocs();
}
@GetMapping("/{vocabulary}/terms")
public Iterable<VocabularyTerm> listTerms(@PathVariable final String vocabulary) {
return vocabularyService.listTerms(vocabulary);
}
@PostMapping("/{vocabulary}/terms")
public Iterable<VocabularyTerm> saveTerm(@PathVariable final String vocabulary, @RequestBody final VocabularyTerm term) {
vocabularyService.saveTerms(vocabulary, term);

View File

@ -0,0 +1,10 @@
package eu.dnetlib.is.vocabulary;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/vocs")
public class VocabularyApiController extends AbstractVocabularyController {
}

View File

@ -16,8 +16,8 @@ import eu.dnetlib.is.wf.model.WfProcessExecution;
import eu.dnetlib.is.wf.repository.WfProcessExecutionRepository;
@RestController
@RequestMapping("/api/wfs")
public class WfHistoryRestController {
@RequestMapping("/ajax/wfs")
public class WfHistoryAjaxController {
@Autowired
private WfProcessExecutionRepository wfProcessExecutionRepository;

View File

@ -6,7 +6,7 @@ app.controller('contextController', function($scope, $http, $location) {
$scope.parameters = [];
$scope.reload = function() {
$scope.url = './api/contexts/' + encodeURIComponent($scope.ctxId) + '/categories';
$scope.url = './ajax/contexts/' + encodeURIComponent($scope.ctxId) + '/categories';
$http.get($scope.url + '?' + $.now()).then(function successCallback(res) {
$scope.categories = res.data;
@ -17,7 +17,7 @@ app.controller('contextController', function($scope, $http, $location) {
$scope.loadContextParameters = function() {
$scope.parameters = [];
$http.get('./api/contexts/' + encodeURIComponent($scope.ctxId) + '?' + $.now()).then(function successCallback(res) {
$http.get('./ajax/contexts/' + encodeURIComponent($scope.ctxId) + '?' + $.now()).then(function successCallback(res) {
$scope.parameters = res.data.parameters;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
@ -27,7 +27,7 @@ app.controller('contextController', function($scope, $http, $location) {
$scope.populateNode = function(level, node) {
$scope.url = './api/contexts/'
$scope.url = './ajax/contexts/'
+ encodeURIComponent(level)
+ '/'
+ encodeURIComponent(node.id)

View File

@ -6,7 +6,7 @@ app.controller('contextsController', function($scope, $http) {
$scope.mode = '';
$scope.reload = function() {
$http.get('./api/contexts/?' + $.now()).then(function successCallback(res) {
$http.get('./ajax/contexts/?' + $.now()).then(function successCallback(res) {
$scope.contexts = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
@ -43,7 +43,7 @@ app.controller('contextsController', function($scope, $http) {
}
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/contexts/?' + $.now(), ctx).then(function successCallback(res) {
$http.post('./ajax/contexts/?' + $.now(), ctx).then(function successCallback(res) {
$scope.contexts = res.data;
alert("Context saved");
}, function errorCallback(res) {
@ -53,7 +53,7 @@ app.controller('contextsController', function($scope, $http) {
$scope.deleteContext = function(id) {
if (confirm("Are you sure ?")) {
$http.delete('./api/contexts/' + encodeURIComponent(id) + '?' + $.now()).then(function successCallback(res) {
$http.delete('./ajax/contexts/' + encodeURIComponent(id) + '?' + $.now()).then(function successCallback(res) {
$scope.contexts = res.data;
alert("Context deleted");
}, function errorCallback(res) {

View File

@ -7,7 +7,7 @@ app.controller('resourcesController', function($scope, $http) {
$scope.type = typeId();
$scope.reload = function() {
$http.get('./api/resources/?type=' + $scope.type + '&' + $.now()).then(function successCallback(res) {
$http.get('./ajax/resources/?type=' + $scope.type + '&' + $.now()).then(function successCallback(res) {
$scope.resources = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
@ -29,7 +29,7 @@ app.controller('resourcesController', function($scope, $http) {
$scope.prepareEditContent = function(r) {
$scope.tmpRes = angular.copy(r);
$scope.tmpContent = "loading...";
$http.get('./api/resources/' + r.id + '/content?' + $.now()).then(function successCallback(res) {
$http.get('./ajax/resources/' + r.id + '/content?' + $.now()).then(function successCallback(res) {
if (res.data instanceof Object) {
$scope.tmpContent = JSON.stringify(res.data, null, "\t");
} else {
@ -42,7 +42,7 @@ app.controller('resourcesController', function($scope, $http) {
$scope.createNewResource = function(r) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
$http.post('./api/resources/?' + $.now(), $.param({
$http.post('./ajax/resources/?' + $.now(), $.param({
'name' : r.name,
'type' : $scope.type,
'description' : r.description,
@ -57,7 +57,7 @@ app.controller('resourcesController', function($scope, $http) {
$scope.saveMetadata = function(id, md) {
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/resources/' + id + '/metadata?' + $.now(), md).then(function successCallback(res) {
$http.post('./ajax/resources/' + id + '/metadata?' + $.now(), md).then(function successCallback(res) {
alert("Resource saved");
$scope.reload();
}, function errorCallback(res) {
@ -67,7 +67,7 @@ app.controller('resourcesController', function($scope, $http) {
$scope.saveContent = function(id, content) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
$http.post('./api/resources/' + id + '/content?' + $.now(), $.param({
$http.post('./ajax/resources/' + id + '/content?' + $.now(), $.param({
'content' : content
})).then(function successCallback(res) {
alert("Resource saved");
@ -78,7 +78,7 @@ app.controller('resourcesController', function($scope, $http) {
$scope.deleteResource = function(r) {
if (confirm("Are you sure ?")) {
$http.delete('./api/resources/' + encodeURIComponent(r.id) + '?' + $.now()).then(function successCallback(res) {
$http.delete('./ajax/resources/' + encodeURIComponent(r.id) + '?' + $.now()).then(function successCallback(res) {
alert("Resource deleted");
$scope.reload();
}, function errorCallback(res) {

View File

@ -6,7 +6,7 @@ app.controller('vocabulariesController', function($scope, $http) {
$scope.mode = '';
$scope.reload = function() {
$http.get('./api/vocs/?' + $.now()).then(function successCallback(res) {
$http.get('./ajax/vocs/?' + $.now()).then(function successCallback(res) {
$scope.vocabularies = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
@ -42,7 +42,7 @@ app.controller('vocabulariesController', function($scope, $http) {
}
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/vocs/?' + $.now(), voc).then(function successCallback(res) {
$http.post('./ajax/vocs/?' + $.now(), voc).then(function successCallback(res) {
$scope.vocabularies = res.data;
alert("Vocabulary saved");
}, function errorCallback(res) {
@ -52,7 +52,7 @@ app.controller('vocabulariesController', function($scope, $http) {
$scope.deleteVocabulary = function(id) {
if (confirm("Are you sure ?")) {
$http.delete('./api/vocs/' + encodeURIComponent(id) + '?' + $.now()).then(function successCallback(res) {
$http.delete('./ajax/vocs/' + encodeURIComponent(id) + '?' + $.now()).then(function successCallback(res) {
$scope.vocabularies = res.data;
alert("Vocabulary deleted");
}, function errorCallback(res) {

View File

@ -8,7 +8,7 @@ app.controller('vocabularyController', function($scope, $http, $location) {
$scope.mode = '';
$scope.currTerm = [];
$scope.baseUrl = './api/vocs/' + encodeURIComponent($scope.vocId) + '/terms';
$scope.baseUrl = './ajax/vocs/' + encodeURIComponent($scope.vocId) + '/terms';
$scope.reload = function() {
$http.get($scope.baseUrl + '?' + $.now()).then(function successCallback(res) {

View File

@ -14,7 +14,7 @@ app.controller('wfHistoryController', function($scope, $http) {
$scope.sortReverse = false;
$scope.reload = function() {
var url = './api/wfs/?' + $.now();
var url = './ajax/wfs/?' + $.now();
if ($scope.fromDate > 0) { url += "&from=" + $scope.fromDate; }
if ($scope.toDate > 0) { url += "&to=" + $scope.toDate; }

View File

@ -58,7 +58,7 @@
app.controller('infoController', function($scope, $http) {
$scope.info = [];
$http.get('./api/info/?' + $.now()).then(function successCallback(res) {
$http.get('./ajax/info/?' + $.now()).then(function successCallback(res) {
angular.forEach(res.data, function(section) {
if (section.name != 'Modules') {
angular.forEach(section.data, function(r) {

View File

@ -38,7 +38,7 @@
</p>
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editMetadataModal" ng-click="prepareEditMetadata(r)">edit metadata</button>
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editContentModal" ng-click="prepareEditContent(r)">edit content</button>
<a href="./api/resources/{{r.id}}/content" class="btn btn-sm btn-success" target="_blank">raw content</a>
<a href="./ajax/resources/{{r.id}}/content" class="btn btn-sm btn-success" target="_blank">raw content</a>
<button type="button" class="btn btn-sm btn-danger" ng-click="deleteResource(r)">delete</button>
</div>
</div>