dnet-applications/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/PublicApiController.java

54 lines
1.6 KiB
Java

package eu.dnetlib.organizations.controller;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.organizations.utils.DatabaseUtils;
@RestController
@RequestMapping("/public-api")
public class PublicApiController {
@Autowired
private DatabaseUtils dbUtils;
@GetMapping("/logs/{year}/{month}/{country}")
public void findJournalByCountry(@PathVariable final int year,
@PathVariable final int month,
@PathVariable final String country,
final HttpServletResponse res) throws IOException {
if (month < 1 || month > 12) {
res.sendError(HttpStatus.BAD_REQUEST.value(), "Invalid month");
}
if (year < 2020 || year > 2100) {
res.sendError(HttpStatus.BAD_REQUEST.value(), "Invalid year");
}
res.setContentType(MediaType.TEXT_PLAIN.getType());
final ServletOutputStream out = res.getOutputStream();
dbUtils.obtainLogEntries(year, month, country).forEach(s -> {
try {
IOUtils.write(s, out, StandardCharsets.UTF_8);
} catch (final IOException e) {
throw new RuntimeException(e);
}
});
}
}