Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
<artifactId>cds-services-api</artifactId>
</dependency>

<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -94,16 +93,13 @@
<version>5.10.2</version>
<scope>test</scope>
</dependency>

<!-- AssertJ -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
<scope>test</scope>
</dependency>

<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand All @@ -118,22 +114,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/SDMAttachmentsHandlerTest.*</include>
</includes>
<properties>
<property>
<name>junit.platform.output.capture.stdout</name>
<value>false</value>
</property>
<property>
<name>junit.platform.output.capture.stderr</name>
<value>false</value>
</property>
</properties>
</configuration>
<version>3.2.5</version>
</plugin>

<plugin>
Expand Down Expand Up @@ -187,7 +168,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
Expand Down Expand Up @@ -223,4 +203,4 @@
</repository>
</distributionManagement>

</project>
</project>
20 changes: 13 additions & 7 deletions sdm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
<properties>
<packageName>sdm</packageName>
<cds.install-cdsdk.version>7.6.0</cds.install-cdsdk.version>
<generation-package>sdm.generated</generation-package>
<generation-package>com.sap.cds.sdm.generated</generation-package>
<test-generation-folder>src/test/gen</test-generation-folder>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<attachments_version>1.0.2</attachments_version>
<lombok.version>1.18.30</lombok.version>
<jacoco.version>0.8.7</jacoco.version>
<commons-codec.version>1.13</commons-codec.version>
<ehcache-version>3.1.3</ehcache-version>
</properties>

<dependencies>
Expand Down Expand Up @@ -68,12 +69,6 @@
<artifactId>cds-services-impl</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.spotbugs</groupId>-->
<!-- <artifactId>spotbugs-annotations</artifactId>-->
<!-- <version>4.8.5</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand All @@ -92,11 +87,17 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
<dependency>
<groupId>com.sap.cds</groupId>
<artifactId>cds-feature-attachments</artifactId>
<version>${attachments_version}</version>
</dependency>
<dependency> <groupId>com.sap.cds</groupId> <artifactId>cds-starter-cloudfoundry</artifactId> </dependency>
<!-- https://mvnrepository.com/artifact/com.sap.cloud.environment.servicebinding/java-sap-vcap-services -->
<dependency>
<groupId>com.sap.cloud.environment.servicebinding</groupId>
Expand All @@ -108,6 +109,11 @@
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache-version}</version>
</dependency>
</dependencies>

<build>
Expand Down
78 changes: 0 additions & 78 deletions sdm/src/main/java/com/sap/cds/handler/TokenGenerator.java

This file was deleted.

56 changes: 56 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.sap.cds.sdm.caching;

import java.util.concurrent.TimeUnit;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.*;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CacheConfig {

private static CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
private static Cache<CacheKey, String> userTokenCache;
private static Cache<String, String> clientCredentialsTokenCache;
private static final int HEAP_SIZE = 1000;
private static final int USER_TOKEN_EXPIRY = 660;
private static final int ACCESS_TOKEN_EXPIRY = 660;
private static final Logger logger = LoggerFactory.getLogger(CacheConfig.class);

private CacheConfig() {
throw new IllegalStateException("CacheConfig class");
}

public static void initializeCache() {
// Expiring the cache after 11 hours
logger.info("Cache for user token and access token initialized");
cacheManager.init();

userTokenCache =
cacheManager.createCache(
"userToken",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
CacheKey.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
.withExpiry(
Expirations.timeToLiveExpiration(
new Duration(USER_TOKEN_EXPIRY, TimeUnit.MINUTES))));
clientCredentialsTokenCache =
cacheManager.createCache(
"clientCredentialsToken",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class, String.class, ResourcePoolsBuilder.heap(HEAP_SIZE))
.withExpiry(
Expirations.timeToLiveExpiration(
new Duration(ACCESS_TOKEN_EXPIRY, TimeUnit.MINUTES))));
}

public static Cache<CacheKey, String> getUserTokenCache() {
return userTokenCache;
}

public static Cache<String, String> getClientCredentialsTokenCache() {
return clientCredentialsTokenCache;
}
}
13 changes: 13 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/caching/CacheKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sap.cds.sdm.caching;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CacheKey {
private String email;
private String expiration;
}
101 changes: 101 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/configuration/Registration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.sap.cds.sdm.configuration;

import com.sap.cds.feature.attachments.handler.applicationservice.helper.ThreadLocalDataStorage;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.modifyevents.CreateAttachmentEvent;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.modifyevents.DefaultModifyAttachmentEventFactory;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.modifyevents.DoNothingAttachmentEvent;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.modifyevents.MarkAsDeletedAttachmentEvent;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.modifyevents.ModifyAttachmentEvent;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.modifyevents.ModifyAttachmentEventFactory;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.modifyevents.UpdateAttachmentEvent;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.transaction.CreationChangeSetListener;
import com.sap.cds.feature.attachments.handler.applicationservice.processor.transaction.ListenerProvider;
import com.sap.cds.feature.attachments.service.AttachmentService;
import com.sap.cds.feature.attachments.utilities.LoggingMarker;
import com.sap.cds.sdm.caching.CacheConfig;
import com.sap.cds.sdm.handler.applicationservice.SDMCreateEventHandler;
import com.sap.cds.sdm.service.SDMAttachmentsService;
import com.sap.cds.sdm.service.handler.SDMAttachmentsServiceHandler;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.outbox.OutboxService;
import com.sap.cds.services.runtime.CdsRuntimeConfiguration;
import com.sap.cds.services.runtime.CdsRuntimeConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;

/**
* The class {@link Registration} is a configuration class that registers the services and event
* handlers for the attachments feature.
*/
public class Registration implements CdsRuntimeConfiguration {

private static final Logger logger = LoggerFactory.getLogger(Registration.class);
private static final Marker marker = LoggingMarker.ATTACHMENT_SERVICE_REGISTRATION.getMarker();

@Override
public void services(CdsRuntimeConfigurer configurer) {
configurer.service(buildAttachmentService());
}

@Override
public void eventHandlers(CdsRuntimeConfigurer configurer) {
logger.info(marker, "Registering event handler for attachment service");
CacheConfig.initializeCache();
var attachmentService =
configurer
.getCdsRuntime()
.getServiceCatalog()
.getService(AttachmentService.class, AttachmentService.DEFAULT_NAME);
var outbox =
configurer
.getCdsRuntime()
.getServiceCatalog()
.getService(
OutboxService.class,
OutboxService.PERSISTENT_UNORDERED_NAME); // need to check if required
var outboxedAttachmentService = outbox.outboxed(attachmentService);

configurer.eventHandler(new SDMAttachmentsServiceHandler());

var deleteContentEvent = new MarkAsDeletedAttachmentEvent(outboxedAttachmentService);
var eventFactory =
buildAttachmentEventFactory(
attachmentService, deleteContentEvent, outboxedAttachmentService);
ThreadLocalDataStorage storage = new ThreadLocalDataStorage();

configurer.eventHandler(buildCreateHandler(eventFactory, storage));
}

private AttachmentService buildAttachmentService() {
logger.info(marker, "Registering SDM attachment service");

return new SDMAttachmentsService();
}

protected DefaultModifyAttachmentEventFactory buildAttachmentEventFactory(
AttachmentService attachmentService,
ModifyAttachmentEvent deleteContentEvent,
AttachmentService outboxedAttachmentService) {
var creationChangeSetListener = createCreationFailedListener(outboxedAttachmentService);
var createAttachmentEvent =
new CreateAttachmentEvent(attachmentService, creationChangeSetListener);
var updateAttachmentEvent =
new UpdateAttachmentEvent(createAttachmentEvent, deleteContentEvent);

var doNothingAttachmentEvent = new DoNothingAttachmentEvent();
return new DefaultModifyAttachmentEventFactory(
createAttachmentEvent, updateAttachmentEvent, deleteContentEvent, doNothingAttachmentEvent);
}

private ListenerProvider createCreationFailedListener(
AttachmentService outboxedAttachmentService) {
return (contentId, cdsRuntime) ->
new CreationChangeSetListener(contentId, cdsRuntime, outboxedAttachmentService);
}

protected EventHandler buildCreateHandler(
ModifyAttachmentEventFactory factory, ThreadLocalDataStorage storage) {
return new SDMCreateEventHandler(factory, storage);
}
}
10 changes: 10 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sap.cds.sdm.constants;

public class SDMConstants {
private SDMConstants() {
throw new IllegalStateException("Constants class");
}

public static final String REPOSITORY_ID = System.getenv("REPOSITORY_ID");
public static final String TENANT_ID = "X-zid";
}
Loading