/******************************************************************************* * Copyright 2019 Graz University of Technology * MOA ZS has been developed in a cooperation between EGIZ * and Graz University of Technology. * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ package at.gv.egiz.moazs.preprocess; import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.zustellung.app2mzs.xsd.ConfigType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.app2mzs.xsd.ObjectFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import java.util.Map; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; /** * @author Christof Rabensteiner * */ @Component public class DeliveryRequestAugmenter { private static final Logger log = LoggerFactory.getLogger(DeliveryRequestAugmenter.class); private static final ObjectFactory FACTORY = new ObjectFactory(); private final ConfigUtil util; private final Map configs; private final Marshaller mzsMarshaller; private static final String CONFIG_MISSING_ERROR_MSG = "Delivery request configuration is missing."; @Autowired public DeliveryRequestAugmenter(Map deliveryRequestConfigs, ConfigUtil util, Marshaller mzsMarshaller) { this.configs = deliveryRequestConfigs; this.util = util; this.mzsMarshaller = mzsMarshaller; } /** * Augments an incoming {@code request} by filling gaps with config- or default parameters. * * @param request * @throws at.gv.egiz.moazs.MoaZSException * @return augmented request and validated */ public DeliveryRequestType augment(DeliveryRequestType request) { var requestConfig = request.getConfig(); var fallbackProfileId = determineProfileIdFrom(requestConfig); var fallbackConfig = configs.get(fallbackProfileId); trace("Request config", requestConfig); trace("Fallback config", fallbackConfig); var augmentedConfig = (requestConfig != null && fallbackConfig != null) ? util.merge(requestConfig, fallbackConfig) : coalesce(requestConfig, fallbackConfig) .orElseThrow(()-> moaZSException(CONFIG_MISSING_ERROR_MSG)); trace("Augmented config", augmentedConfig); return deliveryRequestTypeBuilder(request) .withConfig(augmentedConfig) .build(); } private String determineProfileIdFrom(@Nullable ConfigType requestConfig) { return (requestConfig == null || requestConfig.getProfileID() == null || isProfileMissing(requestConfig.getProfileID())) ? "default" : requestConfig.getProfileID(); } private boolean isProfileMissing(String id) { return !configs.containsKey(id); } private void trace(String description, ConfigType config) { if (log.isTraceEnabled()) { log.trace("{} : {}", description, mzsMarshaller.marshallXml(FACTORY.createConfig(config))); } } }