-
Notifications
You must be signed in to change notification settings - Fork 4
Initial version of FS attachment feature #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 26 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
2a651a0
Initial version of FS attachment feature
mofterdinger 51091c1
move root folder configuration
mofterdinger 1e02ddd
moved delete content to a deleted folder.
mofterdinger df83cc2
added readme
mofterdinger de41e14
readme
mofterdinger 76ae732
enhance Attachments with size
mofterdinger 136667d
make size readonly
mofterdinger 99957c2
added title to size field
mofterdinger a7401e2
added new application service handler
mofterdinger f729aa8
Merge branch 'main' into fs_plugin
mofterdinger 50a64b2
Merge branch 'main' into fs_plugin
mofterdinger c080fb4
Merge branch 'main' into fs_plugin
mofterdinger 0bb45de
Merge branch 'main' into fs_plugin
mofterdinger f9ba32d
Merge branch 'main' into fs_plugin
mofterdinger 9b01d58
removed size from model
mofterdinger 3904084
added handler for new draft event
mofterdinger bddc22c
added tenant to storage path
mofterdinger 7320aa5
Merge branch 'main' into fs_plugin
mofterdinger f4bf3f0
Merge branch 'main' into fs_plugin
mofterdinger 4f3fed3
fixed compile issue
mofterdinger ae6f16a
Merge branch 'main' into fs_plugin
mofterdinger e8e474e
Merge branch 'main' into fs_plugin
mofterdinger 0cbb0d7
Merge branch 'main' into fs_plugin
mofterdinger b00910a
Merge branch 'main' into fs_plugin
mofterdinger e72900c
moved to examples folder
mofterdinger d90ef1e
moved README
mofterdinger 031b1d2
exclude examples folder from sonarqube scan
mofterdinger b942ea3
pom cleanup
mofterdinger 5e08f88
removed handler for application service events
mofterdinger 343fa39
added test cases
mofterdinger 0aaa3d8
Merge branch 'main' into fs_plugin
mofterdinger 1fd3dba
update tests
mofterdinger cd72638
Merge branch 'fs_plugin' of github.com:cap-java/cds-feature-attachmen…
mofterdinger 3a42228
Merge branch 'main' into fs_plugin
mofterdinger 95cc294
Merge branch 'main' into fs_plugin
mofterdinger 646fcc2
Merge branch 'main' into fs_plugin
mofterdinger c867609
Merge branch 'main' into fs_plugin
mofterdinger ad94437
determine foreign-key elements in attachment entity.
mofterdinger cb6550c
added new event handler
mofterdinger 5d5e5a6
improved stability
mofterdinger 85c14ad
respect all foreign keys
mofterdinger 4a76a4c
remove unused code
mofterdinger c2bae49
renamed class
mofterdinger 9c24883
Bump logback-classic to 1.5.18
mofterdinger 1d4c252
Bump awaitility to 4.3.0
mofterdinger e86468c
workaround for wrong mime type provided by SAP UI5.
mofterdinger ba3381a
Merge branch 'main' into fs_plugin
mofterdinger 623aea6
Merge branch 'main' into fs_plugin
mofterdinger 3b2de34
Merge branch 'main' into fs_plugin
mofterdinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ### General Info | ||
|
|
||
| This artifact demonstrates how to implement a custom storage for the cds-feature-attachments. This is just a sample implementation using a filessystem as content storage. It's not supposed for productive usage. | ||
|
|
||
| ### Implementation details | ||
|
|
||
| This artifact provides a custom handlers for events from the [AttachmentService](../cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/service/AttachmentService.java). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-feature-attachments-root</artifactId> | ||
| <version>${revision}</version> | ||
| <relativePath>../..</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>cds-feature-attachments-fs</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>CDS Feature for Attachments - Filesystem</name> | ||
| <url>https://cap.cloud.sap/docs/plugins/#attachments</url> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-feature-attachments</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-services-api</artifactId> | ||
| </dependency> | ||
|
|
||
| <!-- TESTS --> | ||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-services-impl</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>ch.qos.logback</groupId> | ||
| <artifactId>logback-classic</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.awaitility</groupId> | ||
| <artifactId>awaitility</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
|
|
||
| <plugin> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-maven-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <id>cds.resolve</id> | ||
| <goals> | ||
| <goal>resolve</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
|
|
||
| <plugin> | ||
| <groupId>com.github.spotbugs</groupId> | ||
| <artifactId>spotbugs-maven-plugin</artifactId> | ||
| <configuration> | ||
| <skip>true</skip> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
32 changes: 32 additions & 0 deletions
32
...ments-fs/src/main/java/com/sap/cds/feature/attachments/fs/configuration/Registration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.sap.cds.feature.attachments.fs.configuration; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.apache.commons.io.FileUtils; | ||
|
|
||
| import com.sap.cds.feature.attachments.fs.handler.FSApplicationServiceHandler; | ||
| import com.sap.cds.feature.attachments.fs.handler.FSAttachmentsServiceHandler; | ||
| import com.sap.cds.services.runtime.CdsRuntimeConfiguration; | ||
| import com.sap.cds.services.runtime.CdsRuntimeConfigurer; | ||
|
|
||
| /** | ||
| * The class registers the event handlers for the attachments feature based on filesystem. | ||
| */ | ||
| public class Registration implements CdsRuntimeConfiguration { | ||
|
|
||
| @Override | ||
| public void eventHandlers(CdsRuntimeConfigurer configurer) { | ||
| configurer.eventHandler(new FSApplicationServiceHandler()); | ||
|
|
||
| Path tmpPath = FileUtils.getTempDirectory().toPath(); | ||
| Path rootFolder = tmpPath.resolve("com.sap.cds.cds-feature-attachments-fs"); | ||
|
|
||
| try { | ||
| configurer.eventHandler(new FSAttachmentsServiceHandler(rootFolder)); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException("Error while creating the FSAttachmentsServiceHandler", e); | ||
| } | ||
| } | ||
|
|
||
| } | ||
41 changes: 41 additions & 0 deletions
41
...src/main/java/com/sap/cds/feature/attachments/fs/handler/FSApplicationServiceHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.sap.cds.feature.attachments.fs.handler; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.sap.cds.CdsData; | ||
| import com.sap.cds.services.cds.ApplicationService; | ||
| import com.sap.cds.services.cds.CdsCreateEventContext; | ||
| import com.sap.cds.services.cds.CdsUpdateEventContext; | ||
| import com.sap.cds.services.draft.DraftCreateEventContext; | ||
| import com.sap.cds.services.handler.EventHandler; | ||
| import com.sap.cds.services.handler.annotations.Before; | ||
| import com.sap.cds.services.handler.annotations.HandlerOrder; | ||
| import com.sap.cds.services.handler.annotations.ServiceName; | ||
| import com.sap.cds.services.utils.OrderConstants; | ||
|
|
||
| @ServiceName(value = "*", type = ApplicationService.class) | ||
| public class FSApplicationServiceHandler implements EventHandler { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(FSApplicationServiceHandler.class); | ||
|
|
||
| @Before | ||
| public void processBeforeNewDraft(DraftCreateEventContext context, List<CdsData> data) { | ||
| logger.info("Creating new draft for entity {}", context.getTarget()); | ||
| } | ||
|
|
||
| @Before | ||
| @HandlerOrder(OrderConstants.Before.CHECK_CAPABILITIES + 1) | ||
| public void processCreateAttachments(CdsCreateEventContext context, List<CdsData> data) { | ||
| // TODO: add real implementation | ||
| } | ||
|
|
||
| @Before | ||
| @HandlerOrder(OrderConstants.Before.CHECK_CAPABILITIES + 1) | ||
| public void processUpdateAttachments(CdsUpdateEventContext context, List<CdsData> data) { | ||
| // TODO: add real implementation | ||
| } | ||
|
|
||
| } |
111 changes: 111 additions & 0 deletions
111
...src/main/java/com/sap/cds/feature/attachments/fs/handler/FSAttachmentsServiceHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /************************************************************************** | ||
| * (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. * | ||
| **************************************************************************/ | ||
| package com.sap.cds.feature.attachments.fs.handler; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.apache.commons.io.FileUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.Attachments; | ||
| import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.MediaData; | ||
| import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.StatusCode; | ||
| import com.sap.cds.feature.attachments.service.AttachmentService; | ||
| import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentCreateEventContext; | ||
| import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentMarkAsDeletedEventContext; | ||
| import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext; | ||
| import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentRestoreEventContext; | ||
| import com.sap.cds.services.EventContext; | ||
| import com.sap.cds.services.handler.EventHandler; | ||
| import com.sap.cds.services.handler.annotations.On; | ||
| import com.sap.cds.services.handler.annotations.ServiceName; | ||
|
|
||
| /** | ||
| * This class is an event handler that is called when an attachment is created, marked as deleted, restored or read. | ||
| */ | ||
| @ServiceName(value = "*", type = AttachmentService.class) | ||
| public class FSAttachmentsServiceHandler implements EventHandler { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(FSAttachmentsServiceHandler.class); | ||
|
|
||
| private final Path rootFolder; | ||
|
|
||
| public FSAttachmentsServiceHandler(Path rootFolder) throws IOException { | ||
| this.rootFolder = rootFolder; | ||
| if (!Files.exists(this.rootFolder)) { | ||
| Files.createDirectories(this.rootFolder); | ||
| } | ||
| } | ||
|
|
||
| @On | ||
| public void createAttachment(AttachmentCreateEventContext context) throws IOException { | ||
| logger.info("FS Attachment Service handler called for creating attachment for entity name: {}", | ||
| context.getAttachmentEntity().getQualifiedName()); | ||
| String contentId = (String) context.getAttachmentIds().get(Attachments.ID); | ||
|
|
||
| MediaData data = context.getData(); | ||
| data.setStatus(StatusCode.CLEAN); | ||
|
|
||
| try (InputStream input = data.getContent()) { | ||
| Path contentPath = getContentPath(context, contentId); | ||
| Files.createDirectories(contentPath.getParent()); | ||
| Files.copy(input, contentPath); | ||
|
|
||
| context.setIsInternalStored(false); | ||
| context.setContentId(contentId); | ||
| context.setCompleted(); | ||
| } | ||
| } | ||
|
|
||
| @On | ||
| public void markAttachmentAsDeleted(AttachmentMarkAsDeletedEventContext context) throws IOException { | ||
| logger.info("Marking attachment as deleted with document id: {}", context.getContentId()); | ||
|
|
||
| Path contenPath = getContentPath(context, context.getContentId()); | ||
| Path parent = contenPath.getParent(); | ||
| Path destPath = getDeletedFolder(context).resolve(parent.getFileName()); | ||
|
|
||
| FileUtils.moveDirectory(parent.toFile(), destPath.toFile()); | ||
| context.setCompleted(); | ||
| } | ||
|
|
||
| @On | ||
| public void restoreAttachment(AttachmentRestoreEventContext context) { | ||
| logger.info("FS Attachment Service handler called for restoring attachment for timestamp: {}", | ||
| context.getRestoreTimestamp()); | ||
|
|
||
| // nothing to do as data are stored in the database and handled by the database | ||
| context.setCompleted(); | ||
| } | ||
|
|
||
| @On | ||
| public void readAttachment(AttachmentReadEventContext context) throws IOException { | ||
| logger.info("FS Attachment Service handler called for reading attachment with document id: {}", | ||
| context.getContentId()); | ||
| InputStream fileInputStream = Files.newInputStream(getContentPath(context, context.getContentId())); | ||
| context.getData().setContent(fileInputStream); | ||
| context.setCompleted(); | ||
| } | ||
|
|
||
| private Path getContentPath(EventContext context, String contentId) { | ||
| return this.rootFolder.resolve("%s/%s/content.bin".formatted(getTenant(context), contentId)); | ||
| } | ||
|
|
||
| private Path getDeletedFolder(EventContext context) { | ||
| return this.rootFolder.resolve("%s/deleted".formatted(getTenant(context))); | ||
| } | ||
|
|
||
| private static String getTenant(EventContext context) { | ||
| String tenant = context.getUserInfo().getTenant(); | ||
| if (tenant == null) { | ||
| tenant = "default"; | ||
| } | ||
| return tenant; | ||
| } | ||
|
|
||
| } |
1 change: 1 addition & 0 deletions
1
...src/main/resources/META-INF/services/com.sap.cds.services.runtime.CdsRuntimeConfiguration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| com.sap.cds.feature.attachments.fs.configuration.Registration |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Local information disclosure in a temporary directory