|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 B3Partners B.V. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +package org.tailormap.api.controller; |
| 7 | + |
| 8 | +import jakarta.validation.Valid; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.Serializable; |
| 11 | +import java.lang.invoke.MethodHandles; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.UUID; |
| 15 | +import org.geotools.api.data.Query; |
| 16 | +import org.geotools.api.data.SimpleFeatureSource; |
| 17 | +import org.geotools.api.filter.Filter; |
| 18 | +import org.geotools.api.filter.FilterFactory; |
| 19 | +import org.geotools.factory.CommonFactoryFinder; |
| 20 | +import org.geotools.util.factory.GeoTools; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.http.HttpStatus; |
| 24 | +import org.springframework.http.MediaType; |
| 25 | +import org.springframework.http.ResponseEntity; |
| 26 | +import org.springframework.transaction.annotation.Transactional; |
| 27 | +import org.springframework.validation.annotation.Validated; |
| 28 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 29 | +import org.springframework.web.bind.annotation.GetMapping; |
| 30 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 31 | +import org.springframework.web.bind.annotation.PathVariable; |
| 32 | +import org.springframework.web.bind.annotation.PutMapping; |
| 33 | +import org.springframework.web.bind.annotation.RequestPart; |
| 34 | +import org.springframework.web.server.ResponseStatusException; |
| 35 | +import org.tailormap.api.annotation.AppRestController; |
| 36 | +import org.tailormap.api.geotools.featuresources.AttachmentsHelper; |
| 37 | +import org.tailormap.api.geotools.featuresources.FeatureSourceFactoryHelper; |
| 38 | +import org.tailormap.api.persistence.Application; |
| 39 | +import org.tailormap.api.persistence.GeoService; |
| 40 | +import org.tailormap.api.persistence.TMFeatureType; |
| 41 | +import org.tailormap.api.persistence.json.AppTreeLayerNode; |
| 42 | +import org.tailormap.api.persistence.json.AttachmentAttributeType; |
| 43 | +import org.tailormap.api.persistence.json.GeoServiceLayer; |
| 44 | +import org.tailormap.api.util.EditUtil; |
| 45 | +import org.tailormap.api.viewer.model.AttachmentMetadata; |
| 46 | + |
| 47 | +@AppRestController |
| 48 | +@Validated |
| 49 | +public class AttachmentsController { |
| 50 | + |
| 51 | + private static final Logger logger = |
| 52 | + LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 53 | + |
| 54 | + private final EditUtil editUtil; |
| 55 | + private final FeatureSourceFactoryHelper featureSourceFactoryHelper; |
| 56 | + private final FilterFactory ff = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints()); |
| 57 | + |
| 58 | + public AttachmentsController(EditUtil editUtil, FeatureSourceFactoryHelper featureSourceFactoryHelper) { |
| 59 | + this.editUtil = editUtil; |
| 60 | + this.featureSourceFactoryHelper = featureSourceFactoryHelper; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Add an attachment to a feature |
| 65 | + * |
| 66 | + * @param appTreeLayerNode the application tree layer node |
| 67 | + * @param service the geo service |
| 68 | + * @param layer the geo service layer |
| 69 | + * @param application the application |
| 70 | + * @param featureId the feature id |
| 71 | + * @param attachment the attachment metadata |
| 72 | + * @param fileData the attachment file data |
| 73 | + * @return the response entity |
| 74 | + */ |
| 75 | + @PutMapping( |
| 76 | + path = { |
| 77 | + "${tailormap-api.base-path}/{viewerKind}/{viewerName}/layer/{appLayerId}/feature/{featureId}/attachment" |
| 78 | + }, |
| 79 | + consumes = MediaType.MULTIPART_FORM_DATA_VALUE, |
| 80 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 81 | + @Transactional |
| 82 | + public ResponseEntity<Serializable> addAttachment( |
| 83 | + @ModelAttribute AppTreeLayerNode appTreeLayerNode, |
| 84 | + @ModelAttribute GeoService service, |
| 85 | + @ModelAttribute GeoServiceLayer layer, |
| 86 | + @ModelAttribute Application application, |
| 87 | + @PathVariable String featureId, |
| 88 | + @RequestPart("attachmentMetadata") AttachmentMetadata attachment, |
| 89 | + @RequestPart("attachment") byte[] fileData) { |
| 90 | + |
| 91 | + editUtil.checkEditAuthorisation(); |
| 92 | + |
| 93 | + TMFeatureType tmFeatureType = editUtil.getEditableFeatureType(application, appTreeLayerNode, service, layer); |
| 94 | + |
| 95 | + Set<@Valid AttachmentAttributeType> attachmentAttrSet = |
| 96 | + tmFeatureType.getSettings().getAttachmentAttributes(); |
| 97 | + if (attachmentAttrSet == null || attachmentAttrSet.isEmpty()) { |
| 98 | + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Feature type does not support attachments"); |
| 99 | + } |
| 100 | + |
| 101 | + AttachmentAttributeType attachmentAttributeType = attachmentAttrSet.stream() |
| 102 | + .filter(attr -> (attr.getAttributeName().equals(attachment.getAttributeName()) |
| 103 | + && java.util.Arrays.stream(attr.getMimeType().split(",")) |
| 104 | + .map(String::trim) |
| 105 | + .anyMatch(mime -> mime.equals(attachment.getMimeType())) |
| 106 | + && (attr.getMaxAttachmentSize() == null || attr.getMaxAttachmentSize() >= fileData.length))) |
| 107 | + .findFirst() |
| 108 | + .orElseThrow(() -> new ResponseStatusException( |
| 109 | + HttpStatus.BAD_REQUEST, |
| 110 | + "Feature type does not support attachments for attribute " |
| 111 | + + attachment.getAttributeName() |
| 112 | + + " with mime type " |
| 113 | + + attachment.getMimeType() |
| 114 | + + " and size " |
| 115 | + + fileData.length)); |
| 116 | + logger.debug("Using attachment attribute {}", attachmentAttributeType); |
| 117 | + |
| 118 | + if (!checkFeatureExists(tmFeatureType, featureId)) { |
| 119 | + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Feature with id " + featureId + " does not exist"); |
| 120 | + } |
| 121 | + |
| 122 | + AttachmentMetadata response; |
| 123 | + try { |
| 124 | + response = AttachmentsHelper.insertAttachment(tmFeatureType, attachment, featureId, fileData); |
| 125 | + } catch (IOException | SQLException e) { |
| 126 | + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); |
| 127 | + } |
| 128 | + |
| 129 | + return new ResponseEntity<>(response, HttpStatus.CREATED); |
| 130 | + } |
| 131 | + |
| 132 | + @DeleteMapping(path = "${tailormap-api.base-path}/attachment/{attachmentId}") |
| 133 | + public ResponseEntity<Serializable> deleteAttachment(@PathVariable UUID attachmentId) { |
| 134 | + editUtil.checkEditAuthorisation(); |
| 135 | + |
| 136 | + logger.debug("TODO: Deleting attachment with id {}", attachmentId); |
| 137 | + throw new UnsupportedOperationException("Not implemented yet"); |
| 138 | + } |
| 139 | + |
| 140 | + @GetMapping( |
| 141 | + path = "${tailormap-api.base-path}/attachment/{attachmentId}", |
| 142 | + produces = {"application/octet-stream"}) |
| 143 | + // TODO determine return type: ResponseEntity<byte[]> or ResponseEntity<InputStreamResource>? |
| 144 | + public ResponseEntity<byte[]> getAttachment(@PathVariable UUID attachmentId) { |
| 145 | + logger.debug("TODO: Getting attachment with id {}", attachmentId); |
| 146 | + |
| 147 | + throw new UnsupportedOperationException("Not implemented yet"); |
| 148 | + } |
| 149 | + |
| 150 | + private boolean checkFeatureExists(TMFeatureType tmFeatureType, String featureId) { |
| 151 | + final Filter fidFilter = ff.id(ff.featureId(featureId)); |
| 152 | + SimpleFeatureSource fs = null; |
| 153 | + try { |
| 154 | + fs = featureSourceFactoryHelper.openGeoToolsFeatureSource(tmFeatureType); |
| 155 | + Query query = new Query(); |
| 156 | + query.setFilter(fidFilter); |
| 157 | + return fs.getCount(query) > 0; |
| 158 | + } catch (IOException e) { |
| 159 | + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); |
| 160 | + } finally { |
| 161 | + if (fs != null) { |
| 162 | + fs.getDataStore().dispose(); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments