/******************************************************************************* * 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.MoaZSException; import at.gv.zustellung.app2mzs.xsd.*; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.egiz.moazs.scheme.NameSpace.*; import static java.lang.String.format; /** * @author Christof Rabensteiner * */ @Component public class MzsDeliveryRequestValidator { /** * Checks if request is valid and can be processed. * @param request * @throws MoaZSException if the request is invalid. */ public void isRequestValid(DeliveryRequestType request) { isTnvzComplete(request); isConfigProfileComplete(request.getConfig()); } /** * Checks if the mandatory fields that are needed to send a tnvz:QueryPersonRequest are present. * @param request * @throws MoaZSException if a field is missing. */ public void isTnvzComplete(DeliveryRequestType request) { if (request.getConfig().isPerformQueryPersonRequest()) { if (request.getMetaData().getOrigin() == null) throw mzse(MZS_DELIVERY_REQUEST, MZS_DELIVERY_REQUEST + "/MetaData/Origin is missing."); if (request.getMetaData().getDeliveryQuality() == null) throw mzse(MZS_DELIVERY_REQUEST, MZS_DELIVERY_REQUEST + "/MetaData/DeliveryQuality missing."); if (request.getSender().getCorporateBody() == null) throw mzse(MZS_DELIVERY_REQUEST, MZS_DELIVERY_REQUEST + "/Sender/CorporateBody is missing."); if (request.getSender().getCorporateBody().getIdentification() == null || request.getSender().getCorporateBody().getIdentification().size() < 1) { throw mzse(MZS_DELIVERY_REQUEST, MZS_DELIVERY_REQUEST + "/Sender/CorporateBody/Identification is missing."); } } } /** * Check if all mandatory fields of configuration are present. * * @param profile * @throws MoaZSException if a field is missing. */ public void isConfigProfileComplete(@Nullable ConfigType profile) { if (profile == null) throw mzse(MZS_DELIVERY_REQUEST + "/Config"); if (profile.isPerformQueryPersonRequest() == null) throw mzse(MZS_DELIVERY_REQUEST + "/Config", "PerformQueryPersonRequest is missing."); isTNVZClientConfigured(profile.getTNVZClient(), profile.isPerformQueryPersonRequest()); areSinksConfigured(profile.getMsgResponseSinks()); try { isClientConfigured(profile.getMSGClient()); } catch (MoaZSException ex) { throw mzse(MZS_MSGCLIENT, ex.getMessage()); } } private void isTNVZClientConfigured(@Nullable ClientType tnvzClient, Boolean isPerformQueryPersonRequest) { if (!isPerformQueryPersonRequest) return; var isConfigured = tnvzClient != null && tnvzClient.getURL() != null && tnvzClient.getReceiveTimeout() != null && tnvzClient.getConnectionTimeout() != null; if (!isConfigured) { if(tnvzClient == null) throw mzse(MZS_TNVZCLIENT); var reasons = new StringBuilder("The following elements in " + MZS_TNVZCLIENT + " are missing: "); if(tnvzClient.getURL() == null) reasons.append("URL;"); if(tnvzClient.getReceiveTimeout() == null) reasons.append("ReceiveTimeout;"); if(tnvzClient.getConnectionTimeout() == null) reasons.append("ConnectionTimeout;"); throw mzse(MZS_TNVZCLIENT, reasons.toString()); } try { isSSLConfigured(tnvzClient); } catch (MoaZSException ex) { throw mzse(MZS_TNVZCLIENT, ex.getMessage()); } } private void isClientConfigured(@Nullable ClientType clientParams) { var isConfigured = clientParams != null && clientParams.getURL() != null && clientParams.getReceiveTimeout() != null && clientParams.getConnectionTimeout() != null; if (!isConfigured) throw mzse("Client"); isSSLConfigured(clientParams); } private void isSSLConfigured(ClientType clientParams) { if (!clientParams.getURL().startsWith("https")) return; var isConfigured = (clientParams.getSSL() != null && clientParams.getSSL().isTrustAll() != null && clientParams.getSSL().isLaxHostNameVerification() != null); if (!isConfigured) throw mzse("SSL"); try { isKeyStoreConfigured(clientParams.getSSL().getKeyStore()); isTrustStoreConfigured(clientParams.getSSL().getTrustStore()); } catch (MoaZSException ex) { throw mzse("SSL", ex.getMessage()); } } private void isKeyStoreConfigured(@Nullable KeyStoreType keyStore) { if (keyStore == null) return; var isConfigured = keyStore.getPassword() != null && keyStore.getFileType() != null && keyStore.getFileName() != null; if (!isConfigured) throw mzse("KeyStore"); } private void isTrustStoreConfigured(@Nullable KeyStoreType trustStore) { if (trustStore == null) return; var isConfigured = trustStore.getPassword() != null && "JKS".equals(trustStore.getFileType()) && trustStore.getFileName() != null; if (!isConfigured) throw mzse("TrustStore"); } public void areSinksConfigured(@Nullable MsgResponseSinksType sinks) { var isConfigured = (sinks != null && sinks.isLogResponse() != null); if (!isConfigured) throw mzse("MsgResponseSinks"); isSaveResponseToFileConfigured(sinks.getSaveResponseToFile()); isForwardResponseToServiceConfigured(sinks.getForwardResponseToService()); } private void isSaveResponseToFileConfigured(@Nullable SaveResponseToFileType fileSink) { var isConfigured = fileSink != null && (!fileSink.isActive() || fileSink.getPath() != null); if (!isConfigured) throw mzse("SaveResponseToFile"); } private void isForwardResponseToServiceConfigured(@Nullable ForwardResponseToServiceType forwardSink) { if (forwardSink == null) throw mzse("ForwardResponseToService"); if (forwardSink.isActive()) { try { isClientConfigured(forwardSink.getAppClient()); } catch (MoaZSException e) { throw mzse("ForwardResponseToService", e.getMessage()); } } } private MoaZSException mzse(String missing) { return moaZSException(format("%s is not configured.", missing)); } private MoaZSException mzse(String missing, String reason) { return moaZSException(format("%s is not configured. Reason: %s", missing, reason)); } }