99 lines
3.5 KiB
Java
99 lines
3.5 KiB
Java
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;
|
|
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);
|
|
}
|
|
|
|
@PostMapping("/")
|
|
public SimpleResource newResource(@RequestParam final String name,
|
|
@RequestParam final String type,
|
|
@RequestParam(required = false, defaultValue = "") final String description,
|
|
@RequestParam final String content)
|
|
throws InformationServiceException {
|
|
|
|
return service.saveNewResource(name, type, description, content);
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public void deleteResource(@PathVariable final String id) {
|
|
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);
|
|
}
|
|
|
|
@PostMapping("/{id}/content")
|
|
public void saveContent(@PathVariable final String id, @RequestParam final String content) throws InformationServiceException {
|
|
service.saveContent(id, content);
|
|
}
|
|
|
|
@PostMapping(value = "/{id}/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 {
|
|
try {
|
|
final String content = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
|
|
service.saveContent(id, content);
|
|
} catch (final IOException e) {
|
|
throw new InformationServiceException("Error processing input file", e);
|
|
}
|
|
}
|
|
|
|
}
|