From aec75ff196beaca13e0b5785021f656e68aae71c Mon Sep 17 00:00:00 2001
From: Jan Zickermann <jan.zickermann@dataport.de>
Date: Fri, 25 Apr 2025 11:35:20 +0200
Subject: [PATCH] OZG-7978 servicekonto: Avoid try catch block

---
 .../semantik/common/ServiceKontoFactory.java  | 29 ++++++++++++-------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java b/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java
index 8f9aa3d8..41a984e1 100644
--- a/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java
+++ b/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java
@@ -23,11 +23,13 @@
  */
 package de.ozgcloud.eingang.semantik.common;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.stream.Stream;
 
 import org.apache.commons.collections.MapUtils;
 import org.springframework.stereotype.Component;
@@ -102,21 +104,28 @@ public class ServiceKontoFactory {
 	}
 
 	int getPostfachAddressType(Map<String, Object> restResponseName) {
-		try {
-			return getMailboxType(restResponseName);
-		} catch (RuntimeException e) {
-			LOG.error("Error while getting mailbox type from rest response name", e);
-			return POSTFACH_ADDRESS_DEFAULT;
-		}
+		return getMailboxType(restResponseName)
+				.orElseGet(() -> {
+					LOG.warn("Mailbox type not found in rest_response_name! Using default value '{}'", POSTFACH_ADDRESS_DEFAULT);
+					return POSTFACH_ADDRESS_DEFAULT;
+				});
 	}
 
-	private Integer getMailboxType(Map<String, Object> restResponseName) {
-		return (Integer) getMemberScope(restResponseName).get(REST_RESPONSE_NAME_MEMBER_SCOPE_MAILBOX_TYPE);
+	private Optional<Integer> getMailboxType(Map<String, Object> restResponseName) {
+		return getMemberScope(restResponseName)
+				.map(scope -> scope.get(REST_RESPONSE_NAME_MEMBER_SCOPE_MAILBOX_TYPE))
+				.filter(Integer.class::isInstance)
+				.map(Integer.class::cast);
 	}
 
 	@SuppressWarnings("unchecked")
-	private Map<String, Object> getMemberScope(Map<String, Object> restResponseName) {
-		return ((List<Map<String, Object>>) restResponseName.get(REST_RESPONSE_NAME_MEMBER_SCOPE)).get(0);
+	private Optional<Map<String, Object>> getMemberScope(Map<String, Object> restResponseName) {
+		return Optional.ofNullable(restResponseName.get(REST_RESPONSE_NAME_MEMBER_SCOPE))
+				.filter(List.class::isInstance)
+				.map(List.class::cast)
+				.map(Collection::stream)
+				.flatMap(Stream::findFirst)
+				.filter(Map.class::isInstance);
 	}
 
 	public Optional<ServiceKonto> createBayernIdServiceKonto(Map<String, Object> formDataHeaders) {
-- 
GitLab