/******************************************************************************* * Copyright 2020 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.scheme; import at.gv.egiz.moazs.MoaZSException; import at.gv.zustellung.app2mzs.xsd.Mzs2AppPortType; import at.gv.zustellung.msg.xsd.DeliveryAnswerType; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import at.gv.zustellung.msg.xsd.ErrorInfoType; import at.gv.zustellung.msg.xsd.ObjectFactory; import org.springframework.lang.Nullable; import javax.xml.bind.JAXBElement; import java.util.Optional; import java.util.concurrent.CompletableFuture; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Error.errorBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; import static at.gv.zustellung.msg.xsd.ErrorInfoType.errorInfoTypeBuilder; /** * @author Christof Rabensteiner * */ public class RequestStatusResponse extends MsgResponse { private final DeliveryRequestStatusType status; private final DeliveryAnswerType answer; private static final String ID_SUFFIX = ".RS"; private static final ObjectFactory factory = new ObjectFactory(); public RequestStatusResponse(DeliveryRequestStatusType status) { this.status = status; this.answer = getAnswer(status); super.id = createResponseId(answer.getAppDeliveryID(), ID_SUFFIX); } /** * Convenience method to access DeliveryAnswerType's base fields of Success, PartialSuccess or Error. * @param status * @return Success, PartialSuccess, or Error casted to DeliveryAnswerType */ public static DeliveryAnswerType getAnswer(DeliveryRequestStatusType status) { return coalesce(status.getSuccess(), status.getPartialSuccess(), status.getError()).get(); } public static String getResponseID(String appDeliveryID) { return appDeliveryID + ID_SUFFIX; } @Override public DeliveryRequestStatusType getResponse() { return status; } @Override public JAXBElement getResponseAsJAXBElement() { return factory.createDeliveryRequestStatus(status); } @Override public String getAppDeliveryID() { return answer.getAppDeliveryID(); } @Override public String getZSDeliveryID() { return answer.getZSDeliveryID(); } @Override public DeliveryAnswerType getAnswer() { return this.answer; } @Override public MsgResponse generateError(String text, String code) { var auxException = moaZSException(text, code); return generateError(auxException, answer); } /** * Creates A MsgResponse with a DeliveryRequestStatus of type error and merges fields from * {@code exception} and {@code answer} * @param exception * @param answer * @return */ public static MsgResponse generateError(MoaZSException exception, @Nullable DeliveryAnswerType answer) { ErrorInfoType info = errorInfoTypeBuilder() .withText(exception.getMessage()) .withCode(exception.getCode()) .build(); var errorBuilder = errorBuilder() .withPreAdviceNoteSent(exception.getPreAdviceNoteSent()) .withErrorInfo(info); if (answer != null) { errorBuilder .withAppDeliveryID(answer.getAppDeliveryID()) .withDeliverySystem(answer.getDeliverySystem()) .withZSDeliveryID(answer.getZSDeliveryID()) .withGZ(answer.getGZ()); } var errorStatus = deliveryRequestStatusTypeBuilder() .withError(errorBuilder.build()) .build(); return new RequestStatusResponse(errorStatus); } @Override public CompletableFuture sendToAppClient(Msg2MzsConverter converter, Optional signedStatus, Mzs2AppPortType client) { var msgStatus = converter.convert(status, signedStatus); client.forwardStatus(msgStatus); return CompletableFuture.completedFuture(null); } }