2023-11-20 11:10:00 +01:00
|
|
|
package eu.dnetlib.organizations.controller;
|
|
|
|
|
2023-12-14 11:10:43 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2023-11-20 11:10:00 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2023-12-14 11:10:43 +01:00
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.MediaType;
|
2023-11-20 11:10:00 +01:00
|
|
|
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;
|
|
|
|
|
2023-12-14 11:10:43 +01:00
|
|
|
import eu.dnetlib.organizations.utils.DatabaseUtils;
|
2023-11-20 11:10:00 +01:00
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/public-api")
|
|
|
|
public class PublicApiController {
|
|
|
|
|
|
|
|
@Autowired
|
2023-12-14 11:10:43 +01:00
|
|
|
private DatabaseUtils dbUtils;
|
2023-11-20 11:10:00 +01:00
|
|
|
|
2023-12-14 11:10:43 +01:00
|
|
|
@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 {
|
2023-11-20 11:10:00 +01:00
|
|
|
|
2023-12-14 11:10:43 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2023-11-20 11:10:00 +01:00
|
|
|
|
|
|
|
}
|
2023-12-14 11:10:43 +01:00
|
|
|
|
2023-11-20 11:10:00 +01:00
|
|
|
}
|