54 lines
1.8 KiB
Java
54 lines
1.8 KiB
Java
package eu.dnetlib.is.resources;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.List;
|
|
|
|
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.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
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;
|
|
|
|
public class AbstractResourceController extends AbstractDnetController {
|
|
|
|
@Autowired
|
|
protected SimpleResourceService service;
|
|
|
|
@GetMapping("/")
|
|
public List<SimpleResource> listResources(@RequestParam final String type) {
|
|
return service.listResources(type);
|
|
}
|
|
|
|
@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);
|
|
}
|
|
}
|
|
|
|
}
|