This commit is contained in:
Michele Artini 2023-05-25 13:21:42 +02:00
parent df900f6806
commit 30889c47b3
8 changed files with 154 additions and 6 deletions

View File

@ -43,6 +43,12 @@
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -1,17 +1,28 @@
package eu.dnetlib.apps.oai2ftp;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.apps.oai2ftp.model.CollectionStatus;
import eu.dnetlib.apps.oai2ftp.service.Oai2FtpService;
@RestController
@RequestMapping("/api")
public class Oai2FtpController {
private static final Log log = LogFactory.getLog(Oai2FtpController.class);
@Autowired
private Oai2FtpService service;
@ -26,4 +37,35 @@ public class Oai2FtpController {
public CollectionStatus getExecutionStatus(@PathVariable final String id) {
return service.getStatus(id);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public @ResponseBody ErrorMessage handleException(final Exception e) {
log.error("Error processing http method", e);
return new ErrorMessage(e);
}
public class ErrorMessage {
private final String message;
private final String stacktrace;
public ErrorMessage(final Exception e) {
this(e.getMessage(), ExceptionUtils.getStackTrace(e));
}
public ErrorMessage(final String message, final String stacktrace) {
this.message = message;
this.stacktrace = stacktrace;
}
public String getMessage() {
return this.message;
}
public String getStacktrace() {
return this.stacktrace;
}
}
}

View File

@ -1,13 +1,79 @@
package eu.dnetlib.apps.oai2ftp;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.tags.Tag;
@SpringBootApplication
public class Oai2ftpApplication {
public static void main(String[] args) {
@Value("${swagger.public_url}")
private String swaggerPublicUrl;
@Value("${swagger.public_desc}")
private String swaggerPublicDesc;
@Value("${swagger.api_title}")
private String swaggerApiTitle;
@Value("${swagger.api_desc}")
private String swaggerApiDesc;
@Value("${swagger.api_version}")
private String swaggerApiVersion;
private static final License AGPL_3_LICENSE =
new License().name("GNU Affero General Public License v3.0 or later").url("https://www.gnu.org/licenses/agpl-3.0.txt");
public static void main(final String[] args) {
SpringApplication.run(Oai2ftpApplication.class, args);
}
@Bean
public OpenAPI newSwaggerDocket() {
final List<Server> servers = new ArrayList<>();
if (StringUtils.isNotBlank(swaggerPublicUrl)) {
final Server server = new Server();
server.setUrl(swaggerPublicUrl);
server.setDescription(swaggerPublicDesc);
servers.add(server);
}
return new OpenAPI()
.servers(servers)
.info(getSwaggerInfo())
.tags(swaggerTags());
}
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group(swaggerApiTitle)
.pathsToMatch("/api/**")
.build();
}
private Info getSwaggerInfo() {
return new Info()
.title(swaggerApiTitle)
.description(swaggerApiDesc)
.version(swaggerApiVersion)
.license(AGPL_3_LICENSE);
}
protected List<Tag> swaggerTags() {
return new ArrayList<>();
}
}

View File

@ -0,0 +1,15 @@
package eu.dnetlib.apps.oai2ftp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SwaggerController {
@GetMapping({
"/", "apidoc", "api-doc", "/doc", "/swagger"
})
public String apiDoc() {
return "redirect:swagger-ui/index.html";
}
}

View File

@ -36,7 +36,7 @@ public class CollectionJob {
}
public void oaiCollect() {
// TODO
}
public CollectionStatus getStatus() {

View File

@ -56,7 +56,7 @@ public class Oai2FtpService {
baseUrl,
format,
setSpec,
(id, body) -> FtpUtils.saveRecord(id, body),
(id, xml) -> FtpUtils.saveFile(ftp, ConvertUtils.oaiIdToFilename(id), xml),
(status) -> {
FtpUtils.ftpDisconnect(ftp);
collectionLogEntryRepository.save(ConvertUtils.statusToLog(status));

View File

@ -1,6 +1,8 @@
package eu.dnetlib.apps.oai2ftp.utils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -74,9 +76,20 @@ public class FtpUtils {
}
}
public static void saveRecord(final String id, final String body) {
// TODO Auto-generated method stub
public static void saveFile(final FTPClient ftp, final String filename, final String content) {
try (InputStream is = new ByteArrayInputStream(content.getBytes())) {
if (log.isDebugEnabled()) {
log.debug("Saving file " + filename);
log.debug(content);
}
if (!ftp.storeFile(filename, is)) {
log.error("Error saving file: " + ftp.getReplyCode() + " - " + ftp.getReplyString());
throw new RuntimeException("Error saving file: " + ftp.getReplyString());
}
} catch (final IOException e) {
log.error("Error saving info file");
throw new RuntimeException("Error saving info file", e);
}
}
}

View File

@ -1,3 +1,9 @@
swagger.public_url = http://localhost:8080
swagger.public_desc = OAI to FTP (DEV)
swagger.api_title = Oai2Ftp
swagger.api_desc = API Documentation
swagger.api_version = 0.0.1
spring.datasource.url=jdbc:h2:mem:
spring.datasource.username=
spring.datasource.password=