Search in sources :

Example 1 with AS2Exception

use of com.helger.as2lib.exception.AS2Exception in project as2-peppol-servlet by phax.

the class AS2ServletSBDModule method handle.

public void handle(@Nonnull final String sAction, @Nonnull final IMessage aMsg, @Nullable final Map<String, Object> aOptions) throws AS2Exception {
    try {
        // Set the signing algorithm, so that the MIC calculation is done
        // correctly
        aMsg.partnership().setSigningAlgorithm(m_eAS2Version.getCryptoAlgorithmSign());
        aMsg.partnership().setVerifyUseCertificateInBodyPart(ETriState.TRUE);
        // Interpret content as SBD
        final StandardBusinessDocument aSBD = new SBDMarshaller().read(aMsg.getData().getInputStream());
        if (aSBD == null)
            throw new IllegalArgumentException("Failed to interpret the passed document as a Standard Business Document!");
        if (AS2PeppolServletConfiguration.isReceiverCheckEnabled()) {
            final PeppolSBDHDocument aDD = new PeppolSBDHDocumentReader().extractData(aSBD);
            final String sLogPrefix = "[" + aDD.getInstanceIdentifier() + "] ";
            // Get the endpoint information required from the recipient
            final EndpointType aReceiverEndpoint = _getReceiverEndpoint(sLogPrefix, aDD.getReceiverAsIdentifier(), aDD.getDocumentTypeAsIdentifier(), aDD.getProcessAsIdentifier());
            if (aReceiverEndpoint == null) {
                throw new AS2Exception(sLogPrefix + "Failed to resolve endpoint for provided receiver/documentType/process - not handling document");
            }
            // Check if the message is for us
            _checkIfReceiverEndpointURLMatches(sLogPrefix, aReceiverEndpoint);
            // Get the recipient certificate from the SMP
            _checkIfEndpointCertificateMatches(sLogPrefix, aReceiverEndpoint);
        } else {
            LOGGER.info("Endpoint checks for the AS2 AP are disabled");
        }
        // Handle incoming document via SPI
        final HttpHeaderMap aHeaders = aMsg.headers().getClone();
        for (final IAS2IncomingSBDHandlerSPI aHandler : m_aHandlers) aHandler.handleIncomingSBD(aHeaders, aSBD);
    } catch (final Exception ex) {
        // Something went wrong
        throw WrappedAS2Exception.wrap(ex);
    }
}
Also used : SBDMarshaller(com.helger.sbdh.SBDMarshaller) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) PeppolSBDHDocument(com.helger.peppol.sbdh.PeppolSBDHDocument) StandardBusinessDocument(org.unece.cefact.namespaces.sbdh.StandardBusinessDocument) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) EndpointType(com.helger.smpclient.peppol.jaxb.EndpointType) PeppolSBDHDocumentReader(com.helger.peppol.sbdh.read.PeppolSBDHDocumentReader) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) CertificateException(java.security.cert.CertificateException)

Example 2 with AS2Exception

use of com.helger.as2lib.exception.AS2Exception in project as2-lib by phax.

the class PartnerMap method addPartner.

public void addPartner(@Nonnull final Partner aNewPartner) throws AS2Exception {
    ValueEnforcer.notNull(aNewPartner, "NewPartner");
    final String sName = aNewPartner.getName();
    if (m_aMap.containsKey(sName))
        throw new AS2Exception("Partner is defined more than once: '" + sName + "'");
    m_aMap.put(sName, aNewPartner);
}
Also used : AS2Exception(com.helger.as2lib.exception.AS2Exception)

Example 3 with AS2Exception

use of com.helger.as2lib.exception.AS2Exception in project as2-lib by phax.

the class XMLPartnershipFactory method load.

protected void load(@Nullable @WillClose final InputStream aIS) throws AS2Exception {
    final PartnerMap aNewPartners = new PartnerMap();
    final PartnershipMap aNewPartnerships = new PartnershipMap();
    if (aIS != null) {
        final IMicroDocument aDocument = MicroReader.readMicroXML(aIS);
        if (aDocument == null)
            throw new AS2Exception("Failed to read the XML partnership information");
        final IMicroElement aRoot = aDocument.getDocumentElement();
        for (final IMicroElement eRootNode : aRoot.getAllChildElements()) {
            final String sNodeName = eRootNode.getTagName();
            if (sNodeName.equals("partner")) {
                final Partner aNewPartner = loadPartner(eRootNode);
                aNewPartners.addPartner(aNewPartner);
            } else if (sNodeName.equals("partnership")) {
                final Partnership aNewPartnership = loadPartnership(eRootNode, aNewPartners);
                if (aNewPartnerships.getPartnershipByName(aNewPartnership.getName()) != null)
                    throw new AS2Exception("Partnership with name '" + aNewPartnership.getName() + "' is defined more than once");
                aNewPartnerships.addPartnership(aNewPartnership);
            } else {
                if (LOGGER.isWarnEnabled())
                    LOGGER.warn("Invalid element '" + sNodeName + "' in XML partnership file");
            }
        }
    }
    setPartners(aNewPartners);
    setPartnerships(aNewPartnerships);
}
Also used : Partnership(com.helger.as2lib.partner.Partnership) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) PartnershipMap(com.helger.as2lib.partner.PartnershipMap)

Example 4 with AS2Exception

use of com.helger.as2lib.exception.AS2Exception in project as2-lib by phax.

the class XMLPartnershipFactory method refreshPartnershipFactory.

@OverridingMethodsMustInvokeSuper
public void refreshPartnershipFactory() throws AS2Exception {
    try {
        final File aFile = new File(getFilename());
        load(FileHelper.getInputStream(aFile));
    } catch (final Exception ex) {
        throw WrappedAS2Exception.wrap(ex);
    }
}
Also used : File(java.io.File) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IOException(java.io.IOException) AS2InvalidParameterException(com.helger.as2lib.params.AS2InvalidParameterException) OverridingMethodsMustInvokeSuper(javax.annotation.OverridingMethodsMustInvokeSuper)

Example 5 with AS2Exception

use of com.helger.as2lib.exception.AS2Exception in project as2-lib by phax.

the class XMLPartnershipFactory method storePartnership.

/**
 * Store the current status of the partnerships to a file.
 *
 * @throws AS2Exception
 *         In case of an error
 */
public void storePartnership() throws AS2Exception {
    final String sFilename = getFilename();
    if (!isDisableBackup()) {
        final File aBackupFile = _getUniqueBackupFile(sFilename);
        if (LOGGER.isWarnEnabled())
            LOGGER.info("backing up " + sFilename + " to " + aBackupFile.getName());
        final File aSourceFile = new File(sFilename);
        try {
            AS2IOHelper.moveFile(aSourceFile, aBackupFile, true, true);
        } catch (final IOException ex) {
            new AS2Exception("Failed to move file " + aSourceFile + " to " + aBackupFile, ex).terminate();
        }
    }
    final IMicroDocument aDoc = new MicroDocument();
    final IMicroElement eRoot = aDoc.appendElement("partnerships");
    for (final IPartner aPartner : getAllPartners()) {
        final IMicroElement ePartner = eRoot.appendElement("partner");
        for (final Map.Entry<String, String> aAttr : aPartner) ePartner.setAttribute(aAttr.getKey(), aAttr.getValue());
    }
    for (final Partnership aPartnership : getAllPartnerships()) {
        final IMicroElement ePartnership = eRoot.appendElement("partnership");
        ePartnership.setAttribute(ATTR_PARTNERSHIP_NAME, aPartnership.getName());
        final IMicroElement eSender = ePartnership.appendElement("sender");
        for (final Map.Entry<String, String> aAttr : aPartnership.getAllSenderIDs().entrySet()) eSender.setAttribute(aAttr.getKey(), aAttr.getValue());
        final IMicroElement eReceiver = ePartnership.appendElement("receiver");
        for (final Map.Entry<String, String> aAttr : aPartnership.getAllReceiverIDs().entrySet()) eReceiver.setAttribute(aAttr.getKey(), aAttr.getValue());
        for (final Map.Entry<String, String> aAttr : aPartnership.getAllAttributes().entrySet()) ePartnership.appendElement("attribute").setAttribute("name", aAttr.getKey()).setAttribute("value", aAttr.getValue());
    }
    if (MicroWriter.writeToFile(aDoc, new File(sFilename)).isFailure())
        throw new AS2Exception("Failed to write to file " + sFilename);
}
Also used : IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) Partnership(com.helger.as2lib.partner.Partnership) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IMicroElement(com.helger.xml.microdom.IMicroElement) IOException(java.io.IOException) IMicroDocument(com.helger.xml.microdom.IMicroDocument) File(java.io.File) StringMap(com.helger.commons.collection.attr.StringMap) Map(java.util.Map) PartnershipMap(com.helger.as2lib.partner.PartnershipMap) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) IStringMap(com.helger.commons.collection.attr.IStringMap)

Aggregations

AS2Exception (com.helger.as2lib.exception.AS2Exception)42 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)23 IOException (java.io.IOException)16 IMicroElement (com.helger.xml.microdom.IMicroElement)8 Nonnull (javax.annotation.Nonnull)8 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)7 AS2NoModuleException (com.helger.as2lib.processor.AS2NoModuleException)7 File (java.io.File)7 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 AS2ComponentNotFoundException (com.helger.as2lib.session.AS2ComponentNotFoundException)6 X509Certificate (java.security.cert.X509Certificate)6 MessagingException (javax.mail.MessagingException)6 ICertificateFactory (com.helger.as2lib.cert.ICertificateFactory)5 IMicroDocument (com.helger.xml.microdom.IMicroDocument)5 InputStream (java.io.InputStream)5 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)5 AS2InvalidParameterException (com.helger.as2lib.params.AS2InvalidParameterException)4 Partnership (com.helger.as2lib.partner.Partnership)4 AS2ProcessorException (com.helger.as2lib.processor.AS2ProcessorException)4 ETriState (com.helger.commons.state.ETriState)4