package at.gv.egiz.moazs.msg; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.SOAPUtils; import at.gv.egiz.moazs.util.CXFMessageUtils; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.w3c.dom.Element; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; @Component public class StoreSOAPBodyBinaryInRepositoryInterceptor extends AbstractPhaseInterceptor { private static final Logger log = LoggerFactory.getLogger(StoreSOAPBodyBinaryInRepositoryInterceptor.class); private final CXFMessageUtils messageUtils; private final SOAPUtils soapUtils; private final DeliveryRepository repository; @Autowired public StoreSOAPBodyBinaryInRepositoryInterceptor(CXFMessageUtils extractor, SOAPUtils soapUtils, DeliveryRepository repository) { super(Phase.RECEIVE); this.messageUtils = extractor; this.soapUtils = soapUtils; this.repository = repository; } public void handleMessage(Message message) { try { byte[] content = messageUtils.copyContent(message); if(content.length > 0) { Element document = soapUtils.toDOM(content); byte[] status = soapUtils.unwrapSoapEnvelope(document); String appDeliveryId = soapUtils.getAppDeliveryIDFrom(document); repository.addSignedDeliveryRequestStatus(status, appDeliveryId); log.info("Store binary DeliveryRequestStatus with AppDeliveryId={}", appDeliveryId); } } catch (ParserConfigurationException | SAXException | IOException | NullPointerException e) { throw moaZSExceptionBuilder("Could not extract signed data from message.") .withCause(e) .build(); } } }