This commit is contained in:
Michele Artini 2022-12-05 12:23:02 +01:00
parent 67ec5bc368
commit 781254121d
7 changed files with 88 additions and 33 deletions

View File

@ -1,16 +1,16 @@
package eu.dnetlib.is;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SwaggerController {
@GetMapping({
"/doc", "/swagger"
@RequestMapping(value = {
"/docs", "swagger-ui.html", "swagger-ui/"
})
public String apiDoc() {
return "redirect:swagger-ui/";
return "redirect:swagger-ui/index.html";
}
}

View File

@ -11,7 +11,6 @@ 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;
@ -23,25 +22,25 @@ public class AbstractResourceController extends AbstractDnetController {
@Autowired
protected SimpleResourceService service;
@GetMapping("/")
public List<SimpleResource> listResources(@RequestParam final String type) {
return service.listResources(type);
@GetMapping("/{typeId}")
public List<SimpleResource> listResources(@PathVariable final String typeId) {
return service.listResources(typeId);
}
@GetMapping("/{id}/metadata")
public SimpleResource getMetadata(@PathVariable final String id) throws InformationServiceException {
return service.getMetadata(id);
@GetMapping("/{resId}/metadata")
public SimpleResource getMetadata(@PathVariable final String resId) throws InformationServiceException {
return service.getMetadata(resId);
}
@GetMapping("/{id}/content")
public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException {
final String ctype = service.getContentType(id);
@GetMapping("/{resId}/content")
public void getContent(@PathVariable final String resId, final HttpServletResponse res) throws InformationServiceException {
final String ctype = service.getContentType(resId);
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);
ctype.equals(MediaType.APPLICATION_XML_VALUE) ? XmlIndenter.indent(service.getContent(resId)) : service.getContent(resId);
try {
IOUtils.write(content, res.getOutputStream(), StandardCharsets.UTF_8.name());

View File

@ -32,28 +32,28 @@ public class ResourceAjaxController extends AbstractResourceController {
return service.saveNewResource(name, type, description, content);
}
@DeleteMapping("/{id}")
public void deleteResource(@PathVariable final String id) {
service.deleteResource(id);
@DeleteMapping("/{resId}")
public void deleteResource(@PathVariable final String resId) {
service.deleteResource(resId);
}
@PostMapping("/{id}/metadata")
public void saveMetadata(@PathVariable final String id, @RequestBody final SimpleResource r) throws InformationServiceException {
service.saveMetadata(id, r);
@PostMapping("/{resId}/metadata")
public void saveMetadata(@PathVariable final String resId, @RequestBody final SimpleResource r) throws InformationServiceException {
service.saveMetadata(resId, r);
}
@PostMapping("/{id}/content")
public void saveContent(@PathVariable final String id, @RequestParam final String content) throws InformationServiceException {
service.saveContent(id, content);
@PostMapping("/{resId}/content")
public void saveContent(@PathVariable final String resId, @RequestParam final String content) throws InformationServiceException {
service.saveContent(resId, content);
}
@PostMapping(value = "/{id}/file", consumes = {
@PostMapping(value = "/{resId}/file", 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 {
public void uploadContent(@PathVariable final String resId, final HttpServletRequest request) throws InformationServiceException {
try {
final String content = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
service.saveContent(id, content);
service.saveContent(resId, content);
} catch (final IOException e) {
throw new InformationServiceException("Error processing input file", e);
}

View File

@ -1,10 +1,65 @@
package eu.dnetlib.is.resources;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.is.resource.model.ResourceType;
import eu.dnetlib.is.resource.repository.ResourceTypeRepository;
@RestController
@RequestMapping("/api/resources")
public class ResourceApiController extends AbstractResourceController {
@Autowired
private ResourceTypeRepository resourceTypeRepository;
@GetMapping("/")
private List<SimpleResourceType> types() {
return StreamSupport.stream(resourceTypeRepository.findAll().spliterator(), false)
.filter(rt -> rt.isSimple())
.map(rt -> new SimpleResourceType(rt))
.collect(Collectors.toList());
}
class SimpleResourceType {
private final String typeId;
private final String typeName;
private final String contentType;
private final long count;
public SimpleResourceType(final ResourceType rt) {
this.typeId = rt.getId();
this.typeName = rt.getName();
this.contentType = rt.getContentType();
this.count = rt.getCount();
}
public String getTypeId() {
return typeId;
}
public String getTypeName() {
return typeName;
}
public String getContentType() {
return contentType;
}
public long getCount() {
return count;
}
}
}

View File

@ -7,7 +7,7 @@ app.controller('resourcesController', function($scope, $http) {
$scope.type = typeId();
$scope.reload = function() {
$http.get('./ajax/resources/?type=' + $scope.type + '&' + $.now()).then(function successCallback(res) {
$http.get('./ajax/resources/' + encodeURIComponent($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('./ajax/resources/' + r.id + '/content?' + $.now()).then(function successCallback(res) {
$http.get('./ajax/resources/' + encodeURIComponent(r.id) + '/content?' + $.now()).then(function successCallback(res) {
if (res.data instanceof Object) {
$scope.tmpContent = JSON.stringify(res.data, null, "\t");
} else {
@ -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('./ajax/resources/' + id + '/metadata?' + $.now(), md).then(function successCallback(res) {
$http.post('./ajax/resources/' + encodeURIComponent(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('./ajax/resources/' + id + '/content?' + $.now(), $.param({
$http.post('./ajax/resources/' + encodeURIComponent(id) + '/content?' + $.now(), $.param({
'content' : content
})).then(function successCallback(res) {
alert("Resource saved");

View File

@ -59,7 +59,8 @@
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="javascript:void(0)" data-toggle="dropdown">Info</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="./info">Common Info</a>
<a class="dropdown-item" href="./info">Container Info</a>
<a class="dropdown-item" href="./docs" target="_blank">API documentation</a>
</div>
</li>
</ul>

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="./ajax/resources/{{r.id}}/content" class="btn btn-sm btn-success" target="_blank">raw content</a>
<a href="./api/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>