Search in sources :

Example 21 with AS2Exception

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

the class AS2XMLHelper method mapAttributeNodes.

/**
 * @param aNode
 *        Start node. May not be <code>null</code>.
 * @param sNodeName
 *        The element name to be queried relative to the start node.
 * @param sNodeKeyName
 *        The attribute name of the key.
 * @param sNodeValueName
 *        The attribute name of the value.
 * @return The non-<code>null</code> {@link Map}.
 * @throws AS2Exception
 *         In case a node is missing a key or value attribute.
 */
@Nonnull
@ReturnsMutableCopy
public static ICommonsOrderedMap<String, String> mapAttributeNodes(@Nonnull final IMicroElement aNode, @Nonnull final String sNodeName, @Nonnull final String sNodeKeyName, @Nonnull final String sNodeValueName) throws AS2Exception {
    ValueEnforcer.notNull(aNode, "Node");
    ValueEnforcer.notNull(sNodeName, "NodeName");
    ValueEnforcer.notNull(sNodeKeyName, "NodeKeyName");
    ValueEnforcer.notNull(sNodeValueName, "NodeValueName");
    final ICommonsOrderedMap<String, String> ret = new CommonsLinkedHashMap<>();
    int nIndex = 0;
    for (final IMicroElement eChild : aNode.getAllChildElements(sNodeName)) {
        final String sName = eChild.getAttributeValue(sNodeKeyName);
        if (sName == null)
            throw new AS2Exception(sNodeName + "[" + nIndex + "] does not have key attribute '" + sNodeKeyName + "'");
        final String sValue = eChild.getAttributeValue(sNodeValueName);
        if (sValue == null)
            throw new AS2Exception(sNodeName + "[" + nIndex + "] does not have value attribute '" + sNodeValueName + "'");
        ret.put(sName, sValue);
        ++nIndex;
    }
    return ret;
}
Also used : CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) AS2Exception(com.helger.as2lib.exception.AS2Exception) IMicroElement(com.helger.xml.microdom.IMicroElement) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 22 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 23 with AS2Exception

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

the class AbstractAS2MDNReceiveXServletHandler method onServletInit.

@Override
public void onServletInit(@Nonnull final ICommonsMap<String, String> aInitParams) throws ServletException {
    super.onServletInit(aInitParams);
    try {
        m_aReceiver = getSession().getMessageProcessor().getModuleOfClass(AS2ServletMDNReceiverModule.class);
        if (m_aReceiver == null)
            throw new ServletException("Failed to retrieve 'AS2ServletMDNReceiverModule' which is a mandatory module! Please ensure your configuration file contains at least the module '" + AS2ServletMDNReceiverModule.class.getName() + "'");
    } catch (final AS2Exception ex) {
        throw new ServletException("Failed to init AS2 configuration", ex);
    }
    LOGGER.info("Successfully initialized AS2 configuration");
}
Also used : ServletException(javax.servlet.ServletException) AS2Exception(com.helger.as2lib.exception.AS2Exception) AS2ServletMDNReceiverModule(com.helger.as2servlet.util.AS2ServletMDNReceiverModule)

Example 24 with AS2Exception

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

the class AS2ServletXMLSession method _load.

private void _load(@Nullable final InputStream aIS) throws AS2Exception {
    if (aIS != null) {
        final IMicroDocument aDoc = MicroReader.readMicroXML(aIS);
        final IMicroElement eRoot = aDoc.getDocumentElement();
        for (final IMicroElement eRootChild : eRoot.getAllChildElements()) {
            final String sNodeName = eRootChild.getTagName();
            if (sNodeName.equals(EL_CERTIFICATES))
                _loadCertificateFactory(eRootChild);
            else if (sNodeName.equals(EL_PROCESSOR))
                _loadMessageProcessor(eRootChild);
            else if (sNodeName.equals(EL_PARTNERSHIPS))
                _loadPartnershipFactory(eRootChild);
            else
                throw new AS2Exception("Undefined tag: " + sNodeName);
        }
    }
}
Also used : AS2Exception(com.helger.as2lib.exception.AS2Exception) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument)

Example 25 with AS2Exception

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

the class AS2ServerXMLSession method load.

protected void load(@Nonnull @WillClose final InputStream aIS) throws AS2Exception {
    final IMicroDocument aDoc = MicroReader.readMicroXML(aIS);
    final IMicroElement eRoot = aDoc.getDocumentElement();
    // Special global attributes
    final String sCryptoVerifyUseCertificateInBodyPart = eRoot.getAttributeValue(ATTR_CRYPTO_VERIFY_USE_CERTIFICATE_IN_BODY_PART);
    if (sCryptoVerifyUseCertificateInBodyPart != null)
        setCryptoVerifyUseCertificateInBodyPart(StringParser.parseBool(sCryptoVerifyUseCertificateInBodyPart, DEFAULT_CRYPTO_VERIFY_USE_CERTIFICATE_IN_BODY_PART));
    final String sCryptoSignIncludeCertificateInBodyPart = eRoot.getAttributeValue(ATTR_CRYPTO_SIGN_INCLUDE_CERTIFICATE_IN_BODY_PART);
    if (sCryptoSignIncludeCertificateInBodyPart != null)
        setCryptoSignIncludeCertificateInBodyPart(StringParser.parseBool(sCryptoSignIncludeCertificateInBodyPart, DEFAULT_CRYPTO_SIGN_INCLUDE_CERTIFICATE_IN_BODY_PART));
    for (final IMicroElement eRootChild : eRoot.getAllChildElements()) {
        final String sNodeName = eRootChild.getTagName();
        if (sNodeName.equals(EL_CERTIFICATES))
            loadCertificates(eRootChild);
        else if (sNodeName.equals(EL_PROCESSOR))
            loadMessageProcessor(eRootChild);
        else if (sNodeName.equals(EL_CMDPROCESSOR))
            loadCommandProcessors(eRootChild);
        else if (sNodeName.equals(EL_PARTNERSHIPS))
            loadPartnerships(eRootChild);
        else if (sNodeName.equals(EL_COMMANDS))
            loadCommands(eRootChild);
        else
            throw new AS2Exception("Undefined tag: " + sNodeName);
    }
}
Also used : AS2Exception(com.helger.as2lib.exception.AS2Exception) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument)

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