package at.gv.egiz.moazs; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ConfigType; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ConfigType.ConfigTypeBuilder; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType.DeliveryRequestTypeBuilder; import org.springframework.stereotype.Component; import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; @Component public class DeliveryPreprocessor { /** * Validates and augments an incoming {@code request} * * Validates a {@code request} to ensure the availability of all mandatory fields. Where possible, the method * augments the request with values taken from the app's configuration. * @param request * @return validated and augmented request */ public DeliveryRequestType preProcess(DeliveryRequestType request) { validateRequest(request); return new DeliveryRequestTypeBuilder(request) .withConfig(coalesce(request.getConfig(), initDefaultConfig()).get()) .build(); } private void validateRequest(DeliveryRequestType request) { if (request.getMetaData() == null) throw new IllegalArgumentException("Metadata is missing."); if (request.getMetaData().getAppDeliveryID() == null) throw new IllegalArgumentException("AppDeliveryID is missing."); } private ConfigType initDefaultConfig() { return new ConfigTypeBuilder() .withPerformQueryPersonRequest(false) .build(); } }