Skip to content
Closed
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: 22 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<artifactId>cds-services-api</artifactId>
</dependency>

<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -93,13 +94,16 @@
<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 @@ -114,7 +118,22 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<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>
</plugin>

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

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

</project>
</project>
20 changes: 7 additions & 13 deletions sdm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
<properties>
<packageName>sdm</packageName>
<cds.install-cdsdk.version>7.6.0</cds.install-cdsdk.version>
<generation-package>com.sap.cds.sdm.generated</generation-package>
<generation-package>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 @@ -69,6 +68,12 @@
<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 @@ -87,17 +92,11 @@
<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 @@ -109,11 +108,6 @@
<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
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.sap.cds.sdm.service.handler;
package com.sap.cds.handler;

import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.Attachments;
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.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ServiceName(value = "*", type = AttachmentService.class)
public class SDMAttachmentsServiceHandler implements EventHandler {
public class SDMAttachmentsHandler implements EventHandler {
private static final Logger logger = LoggerFactory.getLogger(SDMAttachmentsHandler.class);

@On(event = AttachmentService.EVENT_CREATE_ATTACHMENT)
public void createAttachment(AttachmentCreateEventContext context) throws IOException {
var contentId = (String) context.getAttachmentIds().get(Attachments.ID);
context.setIsInternalStored(true);
context.setContentId(contentId);
context.setCompleted();
public void createAttachment(AttachmentCreateEventContext context) {
logger.info(
"Default Attachment Service handler called for creating attachment for entity name: {}",
context.getAttachmentEntity().getQualifiedName());
logger.info("Other details: {}", context.getAttachmentIds());
logger.info("Other details entity: {}", context.getAttachmentEntity());
}

@On(event = AttachmentService.EVENT_MARK_ATTACHMENT_AS_DELETED)
Expand Down
78 changes: 78 additions & 0 deletions sdm/src/main/java/com/sap/cds/handler/TokenGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.sap.cds.handler;

import static java.util.Objects.requireNonNull;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sap.cds.model.SDMCredentials;
import com.sap.cloud.environment.servicebinding.api.DefaultServiceBindingAccessor;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.codec.binary.Base64;

public class TokenGenerator {
private static Map<String, Object> uaaCredentials;
private static final ObjectMapper mapper = new ObjectMapper();

public static byte[] toBytes(String str) {
return requireNonNull(str).getBytes(StandardCharsets.UTF_8);
}

public static String toString(byte[] bytes) {
return new String(requireNonNull(bytes), StandardCharsets.UTF_8);
}

private static SDMCredentials getSDMCredentianls() {
List<ServiceBinding> allServiceBindings =
DefaultServiceBindingAccessor.getInstance().getServiceBindings();

// filter for a specific binding
ServiceBinding sdmBinding =
allServiceBindings.stream()
.filter(binding -> "sdm".equalsIgnoreCase(binding.getServiceName().orElse(null)))
.findFirst()
.get();
SDMCredentials sdmCredentials = new SDMCredentials();
uaaCredentials = sdmBinding.getCredentials();
sdmCredentials.setBaseTokenUrl(uaaCredentials.get("url").toString());
sdmCredentials.setUrl(sdmBinding.getCredentials().get("uri").toString());
sdmCredentials.setClientId(uaaCredentials.get("clientid").toString());
sdmCredentials.setClientSecret(uaaCredentials.get("clientsecret").toString());
return sdmCredentials;
}

public static String getAccessToken() throws IOException, ProtocolException {
SDMCredentials sdmCredentials = getSDMCredentianls();
String userCredentials = sdmCredentials.getClientId() + ":" + sdmCredentials.getClientSecret();
String authHeaderValue = "Basic " + Base64.encodeBase64String(toBytes(userCredentials));
String bodyParams = "grant_type=client_credentials";
byte[] postData = toBytes(bodyParams);
URL url = new URL(sdmCredentials.getBaseTokenUrl());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", authHeaderValue);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", "" + postData.length);
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
try (DataOutputStream os = new DataOutputStream(conn.getOutputStream())) {
os.write(postData);
}
String resp;
try (DataInputStream is = new DataInputStream(conn.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
resp = br.lines().collect(Collectors.joining("\n"));
}
conn.disconnect();
return mapper.readValue(resp, JsonNode.class).get("access_token").asText();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sap.cds.sdm.model;
package com.sap.cds.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
Expand Down
56 changes: 0 additions & 56 deletions sdm/src/main/java/com/sap/cds/sdm/caching/CacheConfig.java

This file was deleted.

13 changes: 0 additions & 13 deletions sdm/src/main/java/com/sap/cds/sdm/caching/CacheKey.java

This file was deleted.

Loading