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 8f9aa3d8b072f4a71c6f8a21c6d65716a19565b1..41a984e1a12132f3267bb4ad6c93bc9ed2023cef 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) {