diff options
Diffstat (limited to 'service/src/main/java')
5 files changed, 31 insertions, 55 deletions
diff --git a/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java b/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java index d767fd1..12fff3c 100644 --- a/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java +++ b/service/src/main/java/market/guess/service/catalog/LocalCatalogContext.java | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | package market.guess.service.catalog; | 1 | package market.guess.service.catalog; |
| 2 | 2 | ||
| 3 | import java.io.IOException; | ||
| 3 | import java.math.BigDecimal; | 4 | import java.math.BigDecimal; |
| 5 | import java.nio.file.Files; | ||
| 4 | import java.nio.file.Path; | 6 | import java.nio.file.Path; |
| 5 | import java.time.Instant; | 7 | import java.time.Instant; |
| 6 | import java.util.ArrayList; | 8 | import java.util.ArrayList; |
| @@ -41,7 +43,22 @@ public final class LocalCatalogContext implements CatalogContext { | |||
| 41 | 43 | ||
| 42 | @Override | 44 | @Override |
| 43 | public Result<LoadResult> loadEvents(Path path) { | 45 | public Result<LoadResult> loadEvents(Path path) { |
| 44 | return provider.load(path); | 46 | if (path == null) { |
| 47 | return Result.error("INVALID_PATH", "File path cannot be null."); | ||
| 48 | } | ||
| 49 | if (!Files.isRegularFile(path)) { | ||
| 50 | return Result.error("FILE_NOT_FOUND", "No such file: " + path); | ||
| 51 | } | ||
| 52 | if (path.getFileName() == null | ||
| 53 | || !path.getFileName().toString().toLowerCase().endsWith(".xml")) { | ||
| 54 | return Result.error("NOT_XML", "The file must have a .xml extension."); | ||
| 55 | } | ||
| 56 | |||
| 57 | try (var stream = Files.newInputStream(path)) { | ||
| 58 | return provider.load(stream, path.toString(), null); | ||
| 59 | } catch (IOException e) { | ||
| 60 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); | ||
| 61 | } | ||
| 45 | } | 62 | } |
| 46 | 63 | ||
| 47 | @Override | 64 | @Override |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java index 62406bb..78e9ef1 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/Loader.java | |||
| @@ -1,12 +1,9 @@ | |||
| 1 | package market.guess.service.catalog.infrastructure.provider; | 1 | package market.guess.service.catalog.infrastructure.provider; |
| 2 | 2 | ||
| 3 | import java.nio.file.Path; | 3 | import java.io.InputStream; |
| 4 | import market.guess.api.CatalogContext; | ||
| 5 | import market.guess.api.LoadResult; | 4 | import market.guess.api.LoadResult; |
| 6 | import market.guess.api.Result; | 5 | import market.guess.api.Result; |
| 7 | 6 | ||
| 8 | public interface Loader { | 7 | public interface Loader { |
| 9 | Result<LoadResult> seed(CatalogContext context); | 8 | Result<LoadResult> load(InputStream in, String source, String owner); |
| 10 | |||
| 11 | Result<LoadResult> load(Path path); | ||
| 12 | } | 9 | } |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java index 44e1c7f..4e93528 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v1/XMLLoaderV1.java | |||
| @@ -2,11 +2,9 @@ package market.guess.service.catalog.infrastructure.provider.v1; | |||
| 2 | 2 | ||
| 3 | import jakarta.xml.bind.JAXBContext; | 3 | import jakarta.xml.bind.JAXBContext; |
| 4 | import jakarta.xml.bind.JAXBException; | 4 | import jakarta.xml.bind.JAXBException; |
| 5 | import java.io.InputStream; | ||
| 5 | import java.math.BigDecimal; | 6 | import java.math.BigDecimal; |
| 6 | import java.nio.file.Files; | ||
| 7 | import java.nio.file.Path; | ||
| 8 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
| 9 | import market.guess.api.CatalogContext; | ||
| 10 | import market.guess.api.LoadResult; | 8 | import market.guess.api.LoadResult; |
| 11 | import market.guess.api.Result; | 9 | import market.guess.api.Result; |
| 12 | import market.guess.service.catalog.infrastructure.mapper.v1.EventMapperV1; | 10 | import market.guess.service.catalog.infrastructure.mapper.v1.EventMapperV1; |
| @@ -43,21 +41,8 @@ public final class XMLLoaderV1 implements Loader { | |||
| 43 | } | 41 | } |
| 44 | 42 | ||
| 45 | @Override | 43 | @Override |
| 46 | public Result<LoadResult> load(Path path) { | 44 | public Result<LoadResult> load(InputStream stream, String source, String owner) { |
| 47 | if (path == null) { | 45 | try { |
| 48 | return Result.error("INVALID_PATH", "File path cannot be null."); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (!Files.isRegularFile(path)) { | ||
| 52 | return Result.error("FILE_NOT_FOUND", "No such file: " + path); | ||
| 53 | } | ||
| 54 | |||
| 55 | if (path.getFileName() == null | ||
| 56 | || !path.getFileName().toString().toLowerCase().endsWith(".xml")) { | ||
| 57 | return Result.error("NOT_XML", "The file must have a .xml extension."); | ||
| 58 | } | ||
| 59 | |||
| 60 | try (var stream = Files.newInputStream(path)) { | ||
| 61 | var seed = (GuessMarket) context.createUnmarshaller().unmarshal(stream); | 46 | var seed = (GuessMarket) context.createUnmarshaller().unmarshal(stream); |
| 62 | 47 | ||
| 63 | var validation = validator.validate(seed); | 48 | var validation = validator.validate(seed); |
| @@ -80,7 +65,7 @@ public final class XMLLoaderV1 implements Loader { | |||
| 80 | 65 | ||
| 81 | userRepository.add(new User("Tester", new BigDecimal("500.00"))); | 66 | userRepository.add(new User("Tester", new BigDecimal("500.00"))); |
| 82 | 67 | ||
| 83 | return Result.ok(new LoadResult(path.toString(), eventRepository.getAll().size())); | 68 | return Result.ok(new LoadResult(source, eventRepository.getAll().size())); |
| 84 | } catch (JAXBException e) { | 69 | } catch (JAXBException e) { |
| 85 | var cause = | 70 | var cause = |
| 86 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); | 71 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); |
| @@ -91,9 +76,4 @@ public final class XMLLoaderV1 implements Loader { | |||
| 91 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); | 76 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); |
| 92 | } | 77 | } |
| 93 | } | 78 | } |
| 94 | |||
| 95 | @Override | ||
| 96 | public Result<LoadResult> seed(CatalogContext context) { | ||
| 97 | throw new UnsupportedOperationException("Unimplemented method 'seed'"); | ||
| 98 | } | ||
| 99 | } | 79 | } |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java index 7ca141f..1000e6e 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v2/XMLLoaderV2.java | |||
| @@ -2,13 +2,11 @@ package market.guess.service.catalog.infrastructure.provider.v2; | |||
| 2 | 2 | ||
| 3 | import jakarta.xml.bind.JAXBContext; | 3 | import jakarta.xml.bind.JAXBContext; |
| 4 | import jakarta.xml.bind.JAXBException; | 4 | import jakarta.xml.bind.JAXBException; |
| 5 | import java.io.InputStream; | ||
| 5 | import java.math.BigDecimal; | 6 | import java.math.BigDecimal; |
| 6 | import java.nio.file.Files; | ||
| 7 | import java.nio.file.Path; | ||
| 8 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
| 9 | import java.util.HashMap; | 8 | import java.util.HashMap; |
| 10 | import java.util.Map; | 9 | import java.util.Map; |
| 11 | import market.guess.api.CatalogContext; | ||
| 12 | import market.guess.api.LoadResult; | 10 | import market.guess.api.LoadResult; |
| 13 | import market.guess.api.Result; | 11 | import market.guess.api.Result; |
| 14 | import market.guess.service.catalog.infrastructure.mapper.v2.EventMapperV2; | 12 | import market.guess.service.catalog.infrastructure.mapper.v2.EventMapperV2; |
| @@ -45,21 +43,8 @@ public final class XMLLoaderV2 implements Loader { | |||
| 45 | } | 43 | } |
| 46 | 44 | ||
| 47 | @Override | 45 | @Override |
| 48 | public Result<LoadResult> load(Path path) { | 46 | public Result<LoadResult> load(InputStream stream, String source, String owner) { |
| 49 | if (path == null) { | 47 | try { |
| 50 | return Result.error("INVALID_PATH", "File path cannot be null."); | ||
| 51 | } | ||
| 52 | |||
| 53 | if (!Files.isRegularFile(path)) { | ||
| 54 | return Result.error("FILE_NOT_FOUND", "No such file: " + path); | ||
| 55 | } | ||
| 56 | |||
| 57 | if (path.getFileName() == null | ||
| 58 | || !path.getFileName().toString().toLowerCase().endsWith(".xml")) { | ||
| 59 | return Result.error("NOT_XML", "The file must have a .xml extension."); | ||
| 60 | } | ||
| 61 | |||
| 62 | try (var stream = Files.newInputStream(path)) { | ||
| 63 | var seed = (GuessMarket) contextV2.createUnmarshaller().unmarshal(stream); | 48 | var seed = (GuessMarket) contextV2.createUnmarshaller().unmarshal(stream); |
| 64 | 49 | ||
| 65 | var validation = validatorV2.validate(seed); | 50 | var validation = validatorV2.validate(seed); |
| @@ -100,7 +85,7 @@ public final class XMLLoaderV2 implements Loader { | |||
| 100 | eventRepository.add(e); | 85 | eventRepository.add(e); |
| 101 | } | 86 | } |
| 102 | 87 | ||
| 103 | return Result.ok(new LoadResult(path.toString(), eventRepository.getAll().size())); | 88 | return Result.ok(new LoadResult(source, eventRepository.getAll().size())); |
| 104 | } catch (JAXBException e) { | 89 | } catch (JAXBException e) { |
| 105 | var cause = | 90 | var cause = |
| 106 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); | 91 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); |
| @@ -111,9 +96,4 @@ public final class XMLLoaderV2 implements Loader { | |||
| 111 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); | 96 | return Result.error("UNREADABLE", "The file could not be read: " + e.getMessage()); |
| 112 | } | 97 | } |
| 113 | } | 98 | } |
| 114 | |||
| 115 | @Override | ||
| 116 | public Result<LoadResult> seed(CatalogContext context) { | ||
| 117 | throw new UnsupportedOperationException("Unimplemented method 'seed'"); | ||
| 118 | } | ||
| 119 | } | 99 | } |
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java index 59aa3ad..bf17f23 100644 --- a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java | |||
| @@ -9,11 +9,12 @@ import javax.xml.stream.XMLStreamException; | |||
| 9 | import market.guess.api.LoadResult; | 9 | import market.guess.api.LoadResult; |
| 10 | import market.guess.api.Result; | 10 | import market.guess.api.Result; |
| 11 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; | 11 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; |
| 12 | import market.guess.service.catalog.infrastructure.provider.Loader; | ||
| 12 | import market.guess.service.catalog.infrastructure.repository.EventRepository; | 13 | import market.guess.service.catalog.infrastructure.repository.EventRepository; |
| 13 | import market.guess.service.catalog.model.v3.GMEvent; | 14 | import market.guess.service.catalog.model.v3.GMEvent; |
| 14 | import market.guess.service.catalog.model.v3.GuessMarket; | 15 | import market.guess.service.catalog.model.v3.GuessMarket; |
| 15 | 16 | ||
| 16 | public final class XMLLoaderV3 { | 17 | public final class XMLLoaderV3 implements Loader { |
| 17 | private final JAXBContext contextV3; | 18 | private final JAXBContext contextV3; |
| 18 | private final XMLInputFactory xmlInput; | 19 | private final XMLInputFactory xmlInput; |
| 19 | private final EventRepository eventRepository; | 20 | private final EventRepository eventRepository; |
| @@ -35,6 +36,7 @@ public final class XMLLoaderV3 { | |||
| 35 | xmlInput.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); | 36 | xmlInput.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 39 | @Override | ||
| 38 | public synchronized Result<LoadResult> load(InputStream in, String source, String uploader) { | 40 | public synchronized Result<LoadResult> load(InputStream in, String source, String uploader) { |
| 39 | if (in == null) { | 41 | if (in == null) { |
| 40 | return Result.error("INVALID_INPUT", "No file content was provided."); | 42 | return Result.error("INVALID_INPUT", "No file content was provided."); |