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

64 lines
2.4 KiB
Java
Raw Normal View History

2023-02-06 15:31:21 +01:00
package eu.dnetlib.is.resource;
2022-11-16 14:35:24 +01:00
2022-11-23 11:17:50 +01:00
import java.io.IOException;
2022-11-17 11:19:07 +01:00
import java.nio.charset.StandardCharsets;
2022-11-16 14:35:24 +01:00
2022-11-17 11:19:07 +01:00
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
2022-11-23 14:45:56 +01:00
import org.springframework.http.MediaType;
2022-11-17 15:21:40 +01:00
import org.springframework.web.bind.annotation.DeleteMapping;
2022-11-17 11:19:07 +01:00
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
2022-11-23 14:45:56 +01:00
import org.springframework.web.bind.annotation.RequestBody;
2022-11-17 09:53:09 +01:00
import org.springframework.web.bind.annotation.RequestMapping;
2022-11-23 11:17:50 +01:00
import org.springframework.web.bind.annotation.RequestParam;
2022-11-17 09:53:09 +01:00
import org.springframework.web.bind.annotation.RestController;
2023-02-06 15:53:07 +01:00
import eu.dnetlib.errors.InformationServiceException;
2023-02-06 15:31:21 +01:00
import eu.dnetlib.is.model.resource.SimpleResource;
2022-11-17 09:53:09 +01:00
@RestController
2022-12-05 11:04:54 +01:00
@RequestMapping("/ajax/resources")
public class ResourceAjaxController extends AbstractResourceController {
2022-11-17 11:19:07 +01:00
2022-11-23 15:32:36 +01:00
@PostMapping("/")
public SimpleResource newResource(@RequestParam final String name,
@RequestParam final String type,
2023-03-03 13:53:21 +01:00
@RequestParam(required = false, defaultValue = "") final String subtype,
2022-11-23 15:32:36 +01:00
@RequestParam(required = false, defaultValue = "") final String description,
@RequestParam final String content)
throws InformationServiceException {
2023-03-03 13:53:21 +01:00
return service.saveNewResource(name, type, subtype, description, content);
2022-11-23 15:32:36 +01:00
}
2022-12-05 12:23:02 +01:00
@DeleteMapping("/{resId}")
public void deleteResource(@PathVariable final String resId) {
service.deleteResource(resId);
2022-11-23 11:17:50 +01:00
}
2022-12-05 12:23:02 +01:00
@PostMapping("/{resId}/metadata")
public void saveMetadata(@PathVariable final String resId, @RequestBody final SimpleResource r) throws InformationServiceException {
service.saveMetadata(resId, r);
2022-11-23 14:45:56 +01:00
}
2022-11-17 11:19:07 +01:00
2022-12-05 12:23:02 +01:00
@PostMapping("/{resId}/content")
public void saveContent(@PathVariable final String resId, @RequestParam final String content) throws InformationServiceException {
service.saveContent(resId, content);
2022-11-23 14:45:56 +01:00
}
2022-11-17 11:19:07 +01:00
2022-12-05 12:23:02 +01:00
@PostMapping(value = "/{resId}/file", consumes = {
2022-11-23 14:45:56 +01:00
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE
})
2022-12-05 12:23:02 +01:00
public void uploadContent(@PathVariable final String resId, final HttpServletRequest request) throws InformationServiceException {
2022-11-24 11:45:10 +01:00
try {
final String content = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
2022-12-05 12:23:02 +01:00
service.saveContent(resId, content);
2022-11-24 11:45:10 +01:00
} catch (final IOException e) {
throw new InformationServiceException("Error processing input file", e);
2022-11-23 14:45:56 +01:00
}
}
2022-11-17 11:19:07 +01:00
2022-11-16 14:35:24 +01:00
}