diff --git a/pom.xml b/pom.xml
index c26aae8..a5baa8a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -43,6 +43,12 @@
3.9.0
+
+ org.springdoc
+ springdoc-openapi-starter-webmvc-ui
+ 2.1.0
+
+
org.springframework.boot
spring-boot-starter-test
diff --git a/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2FtpController.java b/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2FtpController.java
index d9aff7a..a982189 100644
--- a/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2FtpController.java
+++ b/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2FtpController.java
@@ -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;
+ }
+
+ }
}
diff --git a/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2ftpApplication.java b/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2ftpApplication.java
index 75089ae..d94a240 100644
--- a/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2ftpApplication.java
+++ b/src/main/java/eu/dnetlib/apps/Oai2ftp/Oai2ftpApplication.java
@@ -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 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 swaggerTags() {
+ return new ArrayList<>();
+ }
+
}
diff --git a/src/main/java/eu/dnetlib/apps/Oai2ftp/SwaggerController.java b/src/main/java/eu/dnetlib/apps/Oai2ftp/SwaggerController.java
new file mode 100644
index 0000000..56cc150
--- /dev/null
+++ b/src/main/java/eu/dnetlib/apps/Oai2ftp/SwaggerController.java
@@ -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";
+ }
+}
diff --git a/src/main/java/eu/dnetlib/apps/Oai2ftp/service/CollectionJob.java b/src/main/java/eu/dnetlib/apps/Oai2ftp/service/CollectionJob.java
index 5dd623b..8a2b5fd 100644
--- a/src/main/java/eu/dnetlib/apps/Oai2ftp/service/CollectionJob.java
+++ b/src/main/java/eu/dnetlib/apps/Oai2ftp/service/CollectionJob.java
@@ -36,7 +36,7 @@ public class CollectionJob {
}
public void oaiCollect() {
-
+ // TODO
}
public CollectionStatus getStatus() {
diff --git a/src/main/java/eu/dnetlib/apps/Oai2ftp/service/Oai2FtpService.java b/src/main/java/eu/dnetlib/apps/Oai2ftp/service/Oai2FtpService.java
index 41445e4..6bf87cb 100644
--- a/src/main/java/eu/dnetlib/apps/Oai2ftp/service/Oai2FtpService.java
+++ b/src/main/java/eu/dnetlib/apps/Oai2ftp/service/Oai2FtpService.java
@@ -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));
diff --git a/src/main/java/eu/dnetlib/apps/Oai2ftp/utils/FtpUtils.java b/src/main/java/eu/dnetlib/apps/Oai2ftp/utils/FtpUtils.java
index f1ba303..829c23a 100644
--- a/src/main/java/eu/dnetlib/apps/Oai2ftp/utils/FtpUtils.java
+++ b/src/main/java/eu/dnetlib/apps/Oai2ftp/utils/FtpUtils.java
@@ -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);
+ }
}
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index ce33a3b..580609e 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -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=