|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * |
| 5 | + * * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by |
| 6 | + * * * the European Commission - subsequent versions of the EUPL (the "Licence"); |
| 7 | + * * * You may not use this work except in compliance with the Licence. |
| 8 | + * * * You may obtain a copy of the Licence at: |
| 9 | + * * * |
| 10 | + * * * https://joinup.ec.europa.eu/software/page/eupl |
| 11 | + * * * |
| 12 | + * * * Unless required by applicable law or agreed to in writing, software |
| 13 | + * * * distributed under the Licence is distributed on an "AS IS" basis, |
| 14 | + * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * * * See the Licence for the specific language governing permissions and |
| 16 | + * * * limitations under the Licence. |
| 17 | + * * |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +package org.entur.gbfs.validator.api.handler; |
| 22 | + |
| 23 | +import org.entur.gbfs.validation.GbfsValidator; |
| 24 | +import org.entur.gbfs.validation.GbfsValidatorFactory; |
| 25 | +import org.entur.gbfs.validation.model.FileValidationError; |
| 26 | +import org.entur.gbfs.validation.model.FileValidationResult; |
| 27 | +import org.entur.gbfs.validation.model.ValidationResult; |
| 28 | +import org.entur.gbfs.validator.api.gen.ValidateOption1ApiDelegate; |
| 29 | +import org.entur.gbfs.validator.api.model.FileError; |
| 30 | +import org.entur.gbfs.validator.api.model.FileLangOption1; |
| 31 | +import org.entur.gbfs.validator.api.model.FileOption1; |
| 32 | +import org.entur.gbfs.validator.api.model.ValidateOption1PostRequest; |
| 33 | +import org.entur.gbfs.validator.api.model.ValidationResultOption1; |
| 34 | +import org.entur.gbfs.validator.api.model.ValidationResultOption1Summary; |
| 35 | +import org.entur.gbfs.validator.api.model.ValidationResultOption1SummaryFilesInner; |
| 36 | +import org.entur.gbfs.validator.loader.Loader; |
| 37 | +import org.springframework.http.ResponseEntity; |
| 38 | +import org.springframework.stereotype.Service; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.InputStream; |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | +import java.util.Map; |
| 45 | + |
| 46 | +@Service |
| 47 | +public class ValidateApiDelegateHandler implements ValidateOption1ApiDelegate { |
| 48 | + |
| 49 | + @Override |
| 50 | + public ResponseEntity<ValidationResultOption1> validateOption1Post(ValidateOption1PostRequest validateOption1PostRequest) { |
| 51 | + Loader loader = new Loader(); |
| 52 | + Map<String, InputStream> fileMap = null; |
| 53 | + try { |
| 54 | + fileMap = loader.load(validateOption1PostRequest.getFeedUrl()); |
| 55 | + } catch (IOException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + GbfsValidator validator = GbfsValidatorFactory.getGbfsJsonValidator(); |
| 59 | + return ResponseEntity.ok( |
| 60 | + mapValidationResult(validator.validate(fileMap)) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + private ValidationResultOption1 mapValidationResult(ValidationResult validationResult) { |
| 65 | + ValidationResultOption1Summary validationResultOption1Summary = new ValidationResultOption1Summary(); |
| 66 | + validationResultOption1Summary.setValidatorVersion("2.0.30-SNAPSHOT"); // TODO inject this value |
| 67 | + validationResultOption1Summary.setGbfsVersion(validationResult.summary().version()); |
| 68 | + validationResultOption1Summary.setFiles(mapFiles(validationResult.files())); |
| 69 | + ValidationResultOption1 validationResultOption1 = new ValidationResultOption1(); |
| 70 | + validationResultOption1.setSummary(validationResultOption1Summary); |
| 71 | + return validationResultOption1; |
| 72 | + } |
| 73 | + |
| 74 | + private List<ValidationResultOption1SummaryFilesInner> mapFiles(Map<String, FileValidationResult> files) { |
| 75 | + List<ValidationResultOption1SummaryFilesInner> summaryFiles = new ArrayList<>(); |
| 76 | + files.entrySet().stream().forEach(entry -> { |
| 77 | + String fileName = entry.getKey(); |
| 78 | + FileValidationResult fileValidationResult = entry.getValue(); |
| 79 | + |
| 80 | + FileLangOption1 filesInner = new FileLangOption1(); |
| 81 | + filesInner.setName(fileName); |
| 82 | + filesInner.setExists(fileValidationResult.exists()); |
| 83 | + filesInner.setRequired(fileValidationResult.required()); |
| 84 | + //filesInner.setRecommended(); // TODO not available |
| 85 | + filesInner.setSchema(fileValidationResult.schema()); |
| 86 | + filesInner.setVersion(fileValidationResult.version()); |
| 87 | + |
| 88 | + FileOption1 file = new FileOption1(); |
| 89 | + file.exists(fileValidationResult.exists()); |
| 90 | + file.errors( |
| 91 | + mapFileErrors(fileValidationResult.errors()) |
| 92 | + ); |
| 93 | + file.fileContent(fileValidationResult.fileContents()); |
| 94 | + |
| 95 | + |
| 96 | + filesInner.setFiles( |
| 97 | + List.of(file) |
| 98 | + ); |
| 99 | + summaryFiles.add(filesInner); |
| 100 | + }); |
| 101 | + return summaryFiles; |
| 102 | + } |
| 103 | + |
| 104 | + private List<FileError> mapFileErrors(List<FileValidationError> errors) { |
| 105 | + return errors.stream().map(error -> { |
| 106 | + var mapped = new FileError(); |
| 107 | + mapped.setMessage(error.message()); |
| 108 | + mapped.setInstancePath(error.violationPath()); |
| 109 | + mapped.setSchemaPath(error.schemaPath()); |
| 110 | + //mapped.setParams(error.); // TODO no source? |
| 111 | + //mapped.setKeyword(error.); // TODO no source? |
| 112 | + return mapped; |
| 113 | + }).toList(); |
| 114 | + } |
| 115 | +} |
0 commit comments