dnet-applications/apps/dnet-is-application/src/main/java/eu/dnetlib/is/resources/ResourcesRestController.java

121 lines
4.6 KiB
Java

package eu.dnetlib.is.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
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;
import eu.dnetlib.is.resource.model.SimpleResource;
import eu.dnetlib.is.resource.repository.SimpleResourceRepository;
import eu.dnetlib.is.util.InformationServiceException;
import eu.dnetlib.is.util.OldProfilesImporter;
@RestController
@RequestMapping("/api/resources")
public class ResourcesRestController {
@Autowired
private OldProfilesImporter oldProfilesImporter;
@Autowired
private SimpleResourceRepository simpleResourceRepository;
private static final Log log = LogFactory.getLog(ResourcesRestController.class);
@GetMapping("/")
public List<SimpleResource> listResources(@RequestParam final String type) {
return simpleResourceRepository.findByType(type)
.stream()
.sorted((r1, r2) -> StringUtils.compareIgnoreCase(r1.getName(), r2.getName()))
.collect(Collectors.toList());
}
@DeleteMapping("/{id}")
public void deleteResource(@PathVariable final String id) {
log.info("Deleting resource: " + id);
simpleResourceRepository.deleteById(id);
}
@GetMapping("/{id}/metadata")
public SimpleResource getMetadata(@PathVariable final String id) throws InformationServiceException {
return simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
}
@GetMapping("/{id}/content")
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());
res.setContentType(sr.getContentType());
simpleResourceRepository.getContentById(id);
try {
IOUtils.write(simpleResourceRepository.getContentById(id), res.getOutputStream(), StandardCharsets.UTF_8.name());
} catch (final IOException e) {
throw new InformationServiceException("Error retrieving content", e);
}
}
@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);
}
}