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

64 lines
2.4 KiB
Java

package eu.dnetlib.is.resource;
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.errors.InformationServiceException;
import eu.dnetlib.is.model.resource.SimpleResource;
@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 subtype,
@RequestParam(required = false, defaultValue = "") final String description,
@RequestParam final String content)
throws InformationServiceException {
return service.saveNewResource(name, type, subtype, description, content);
}
@DeleteMapping("/{resId}")
public void deleteResource(@PathVariable final String resId) {
service.deleteResource(resId);
}
@PostMapping("/{resId}/metadata")
public void saveMetadata(@PathVariable final String resId, @RequestBody final SimpleResource r) throws InformationServiceException {
service.saveMetadata(resId, r);
}
@PostMapping("/{resId}/content")
public void saveContent(@PathVariable final String resId, @RequestParam final String content) throws InformationServiceException {
service.saveContent(resId, content);
}
@PostMapping(value = "/{resId}/file", consumes = {
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE
})
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(resId, content);
} catch (final IOException e) {
throw new InformationServiceException("Error processing input file", e);
}
}
}