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

63 lines
2.2 KiB
Java

package eu.dnetlib.is.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
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.is.resource.model.SimpleResource;
import eu.dnetlib.is.util.InformationServiceException;
@RestController
@RequestMapping("/ajax/resources")
public class ResourceAjaxController extends AbstractResourceController {
@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);
}
@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);
}
}
}