Skip to content

Commit

Permalink
Add sender service
Browse files Browse the repository at this point in the history
  • Loading branch information
zechmeister committed Sep 23, 2024
1 parent 085a7c1 commit d820353
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation(libs.spring.boot.starter.actuator)
implementation(libs.spring.boot.starter.security)
implementation(libs.spring.boot.starter.web)
implementation(libs.fitko.fitconnect.sdk)

compileOnly(libs.lombok)

Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ spring-boot-starter-security = { module = "org.springframework.boot:spring-boot-
spring-boot-starter-web = { module = "org.springframework.boot:spring-boot-starter-web" }
spring-boot-starter-test = { module = "org.springframework.boot:spring-boot-starter-test" }
spring-security-test = { module = "org.springframework.security:spring-security-test" }
fitko-fitconnect-sdk = "dev.fitko.fitconnect.sdk:client:2.1.0"

[plugins]
dependency-license-report = "com.github.jk1.dependency-license-report:2.9"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.bund.digitalservice.a2j.config;

import dev.fitko.fitconnect.api.config.ApplicationConfig;
import dev.fitko.fitconnect.client.bootstrap.ApplicationConfigLoader;
import java.io.IOException;
import java.nio.file.Path;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

@Configuration
public class FitConnectConfig {
private final ResourceLoader resourceLoader;

public FitConnectConfig(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

@Bean
public ApplicationConfig applicationConfig() throws IOException {
return ApplicationConfigLoader.loadConfigFromPath(
Path.of(resourceLoader.getResource("classpath:fitConnectConfig.yaml").getURI()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ public class SecurityConfig {

@Bean
public SecurityFilterChain springSecurityWebFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.ignoringRequestMatchers("/api/sender/submit"))
.authorizeHttpRequests(requests -> requests
.requestMatchers("/.well-known/security.txt").permitAll()
.requestMatchers("/actuator/health").permitAll()
.requestMatchers("/api/sender/**").permitAll()
.anyRequest().denyAll()
).build();
return http.csrf(csrf -> csrf.ignoringRequestMatchers("/api/sender/submit"))
.authorizeHttpRequests(
requests ->
requests
.requestMatchers("/.well-known/security.txt")
.permitAll()
.requestMatchers("/actuator/health")
.permitAll()
.requestMatchers("/api/sender/**")
.permitAll()
.anyRequest()
.denyAll())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.bund.digitalservice.a2j.controller;

import de.bund.digitalservice.a2j.service.SenderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -9,10 +10,19 @@
@RestController
@RequestMapping("/api/sender")
public class SenderController {
public SenderController() {}
private final SenderService service;

public SenderController(SenderService service) {
this.service = service;
}

@PostMapping("/submit")
public ResponseEntity<String> submitMessage(@RequestBody String message) {
return ResponseEntity.ok("Received message: " + message);
try {
String response = service.sendMessage(message);
return ResponseEntity.ok("Submission successful, content: " + response);
} catch (Exception e) {
return ResponseEntity.internalServerError().body("Error: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.bund.digitalservice.a2j.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.fitko.fitconnect.api.config.ApplicationConfig;
import dev.fitko.fitconnect.api.domain.model.submission.SentSubmission;
import dev.fitko.fitconnect.api.domain.sender.SendableSubmission;
import dev.fitko.fitconnect.client.SenderClient;
import dev.fitko.fitconnect.client.bootstrap.ClientFactory;
import java.net.URI;
import java.util.UUID;
import org.springframework.stereotype.Service;

@Service
public class FitConnectSenderService implements SenderService {
private final SenderClient client;

public FitConnectSenderService(ApplicationConfig config) {
this.client = ClientFactory.createSenderClient(config);
}

@Override
public String sendMessage(String message) {

SendableSubmission submission =
SendableSubmission.Builder()
.setDestination(UUID.fromString("89126fd7-1069-46f1-9cdc-152037db95a9"))
.setServiceType("urn:de:fim:leika:leistung:99001001000000", "FIT Connect Demo")
.setJsonData(
buildJSON(message),
URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json"))
.build();

SentSubmission sentSubmission = client.send(submission);

System.out.println("helllooo" + sentSubmission.toString());

return "Tried to send " + message;
}

private String buildJSON(String message) {
ObjectMapper mapper = new ObjectMapper();

ObjectNode rootNode = mapper.createObjectNode();
rootNode.put("data", message);

try {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
} catch (JsonProcessingException e) {
return "{\"error\":\"Failed to build JSON\"}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.bund.digitalservice.a2j.service;

public interface SenderService {
String sendMessage(String message);
}
File renamed without changes.
5 changes: 5 additions & 0 deletions src/main/resources/fitConnectConfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
senderConfig:
clientId: "client"
clientSecret: "s3cr3t"

activeEnvironment: TEST

0 comments on commit d820353

Please sign in to comment.