Search in sources :

Example 1 with OpenAS2Exception

use of com.helger.as2lib.exception.OpenAS2Exception 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 OpenAS2Exception {
    try {
        // 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 sMessageID = aDD.getInstanceIdentifier();
            // Get the endpoint information required from the recipient
            final EndpointType aReceiverEndpoint = _getReceiverEndpoint(aDD.getReceiverAsIdentifier(), aDD.getDocumentTypeAsIdentifier(), aDD.getProcessAsIdentifier(), sMessageID);
            if (aReceiverEndpoint == null) {
                throw new OpenAS2Exception(sMessageID + " Failed to resolve endpoint for provided receiver/documentType/process - not handling document");
            }
            // Check if the message is for us
            _checkIfReceiverEndpointURLMatches(aReceiverEndpoint, sMessageID);
            // Get the recipient certificate from the SMP
            _checkIfEndpointCertificateMatches(aReceiverEndpoint, sMessageID);
        } else {
            s_aLogger.info("Endpoint checks for the AS2 AP are disabled");
        }
        // Handle incoming document via SPI
        for (final IAS2IncomingSBDHandlerSPI aHandler : m_aHandlers) aHandler.handleIncomingSBD(aSBD);
    } catch (final Exception ex) {
        // Something went wrong
        throw WrappedOpenAS2Exception.wrap(ex);
    }
}
Also used : SBDMarshaller(com.helger.sbdh.SBDMarshaller) PeppolSBDHDocument(com.helger.peppol.sbdh.PeppolSBDHDocument) WrappedOpenAS2Exception(com.helger.as2lib.exception.WrappedOpenAS2Exception) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) StandardBusinessDocument(org.unece.cefact.namespaces.sbdh.StandardBusinessDocument) EndpointType(com.helger.peppol.smp.EndpointType) PeppolSBDHDocumentReader(com.helger.peppol.sbdh.read.PeppolSBDHDocumentReader) WrappedOpenAS2Exception(com.helger.as2lib.exception.WrappedOpenAS2Exception) CertificateException(java.security.cert.CertificateException) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception)

Example 2 with OpenAS2Exception

use of com.helger.as2lib.exception.OpenAS2Exception in project as2-server by phax.

the class MainOpenAS2Server method start.

public void start(@Nullable final String sConfigFilePath) {
    AS2ServerXMLSession aXMLSession = null;
    try {
        s_aLogger.info(CAS2Info.NAME_VERSION + " - starting Server...");
        // create the OpenAS2 Session object
        // this is used by all other objects to access global configs and
        // functionality
        s_aLogger.info("Loading configuration...");
        if (StringHelper.hasText(sConfigFilePath)) {
            // Load config file
            aXMLSession = new AS2ServerXMLSession(sConfigFilePath);
        } else {
            s_aLogger.info("Usage:");
            s_aLogger.info("java " + getClass().getName() + " <configuration file>");
            throw new Exception("Missing configuration file name on the commandline. You may specify src/main/resources/config/config.xml");
        }
        // start the active processor modules
        s_aLogger.info("Starting Active Modules...");
        aXMLSession.getMessageProcessor().startActiveModules();
        final ICommandRegistry aCommandRegistry = aXMLSession.getCommandRegistry();
        final CommandManager aCommandMgr = aXMLSession.getCommandManager();
        final List<AbstractCommandProcessor> aCommandProcessors = aCommandMgr.getProcessors();
        for (final AbstractCommandProcessor cmd : aCommandProcessors) {
            s_aLogger.info("Loading Command Processor " + cmd.getClass().getName() + "");
            cmd.init();
            cmd.addCommands(aCommandRegistry);
            new Thread(cmd, ClassHelper.getClassLocalName(cmd)).start();
        }
        // enter the command processing loop
        s_aLogger.info("OpenAS2 Started");
        // Start waiting for termination
        breakOut: while (true) {
            for (final AbstractCommandProcessor cmd : aCommandProcessors) {
                if (cmd.isTerminated())
                    break breakOut;
            }
            // Wait outside loop in case no command processor is present
            Thread.sleep(100);
        }
        s_aLogger.info("- OpenAS2 Stopped -");
    } catch (final Throwable t) {
        t.printStackTrace();
    } finally {
        if (aXMLSession != null) {
            try {
                aXMLSession.getMessageProcessor().stopActiveModules();
            } catch (final OpenAS2Exception same) {
                same.terminate();
            }
        }
        s_aLogger.info("OpenAS2 has shut down");
    }
}
Also used : CommandManager(com.helger.as2.cmd.CommandManager) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) AS2ServerXMLSession(com.helger.as2.app.session.AS2ServerXMLSession) ICommandRegistry(com.helger.as2.cmd.ICommandRegistry) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) AbstractCommandProcessor(com.helger.as2.cmdprocessor.AbstractCommandProcessor)

Example 3 with OpenAS2Exception

use of com.helger.as2lib.exception.OpenAS2Exception in project as2-server by phax.

the class AS2ServerXMLSession method load.

protected void load(@Nonnull @WillClose final InputStream aIS) throws OpenAS2Exception {
    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 OpenAS2Exception("Undefined tag: " + sNodeName);
    }
}
Also used : OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument)

Example 4 with OpenAS2Exception

use of com.helger.as2lib.exception.OpenAS2Exception in project as2-server by phax.

the class SocketCommandProcessor method processCommand.

@Override
public void processCommand() throws OpenAS2Exception {
    try (final SSLSocket socket = (SSLSocket) m_aSSLServerSocket.accept()) {
        socket.setSoTimeout(2000);
        m_aReader = new NonBlockingBufferedReader(new InputStreamReader(socket.getInputStream()));
        m_aWriter = new NonBlockingBufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        final String line = m_aReader.readLine();
        m_aParser.parse(line);
        if (!m_aParser.getUserid().equals(m_sUserID)) {
            m_aWriter.write("Bad userid/password");
            throw new OpenAS2Exception("Bad userid");
        }
        if (!m_aParser.getPassword().equals(m_sPassword)) {
            m_aWriter.write("Bad userid/password");
            throw new OpenAS2Exception("Bad password");
        }
        final String str = m_aParser.getCommandText();
        if (str != null && str.length() > 0) {
            final CommandTokenizer cmdTkn = new CommandTokenizer(str);
            if (cmdTkn.hasMoreTokens()) {
                final String sCommandName = cmdTkn.nextToken().toLowerCase();
                if (sCommandName.equals(StreamCommandProcessor.EXIT_COMMAND)) {
                    terminate();
                } else {
                    final ICommonsList<String> params = new CommonsArrayList<>();
                    while (cmdTkn.hasMoreTokens()) {
                        params.add(cmdTkn.nextToken());
                    }
                    final ICommand aCommand = getCommand(sCommandName);
                    if (aCommand != null) {
                        final CommandResult aResult = aCommand.execute(params.toArray());
                        if (aResult.getType().isSuccess()) {
                            m_aWriter.write(aResult.getAsXMLString());
                        } else {
                            m_aWriter.write("\r\n" + StreamCommandProcessor.COMMAND_ERROR + "\r\n");
                            m_aWriter.write(aResult.getResultAsString());
                        }
                    } else {
                        m_aWriter.write(StreamCommandProcessor.COMMAND_NOT_FOUND + "> " + sCommandName + "\r\n");
                        m_aWriter.write("List of commands:" + "\r\n");
                        for (final String sCurCmd : getAllCommands().keySet()) m_aWriter.write(sCurCmd + "\r\n");
                    }
                }
            }
        }
        m_aWriter.flush();
    } catch (final Exception ex) {
        throw WrappedOpenAS2Exception.wrap(ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SSLSocket(javax.net.ssl.SSLSocket) NonBlockingBufferedWriter(com.helger.commons.io.stream.NonBlockingBufferedWriter) IOException(java.io.IOException) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) WrappedOpenAS2Exception(com.helger.as2lib.exception.WrappedOpenAS2Exception) CommandResult(com.helger.as2.cmd.CommandResult) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) WrappedOpenAS2Exception(com.helger.as2lib.exception.WrappedOpenAS2Exception) CommandTokenizer(com.helger.as2.util.CommandTokenizer) ICommand(com.helger.as2.cmd.ICommand) OutputStreamWriter(java.io.OutputStreamWriter) NonBlockingBufferedReader(com.helger.commons.io.stream.NonBlockingBufferedReader) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

Example 5 with OpenAS2Exception

use of com.helger.as2lib.exception.OpenAS2Exception in project as2-server by phax.

the class SocketCommandProcessor method initDynamicComponent.

@Override
public void initDynamicComponent(@Nonnull final IAS2Session aSession, @Nullable final IStringMap aParams) throws OpenAS2Exception {
    final StringMap aParameters = aParams == null ? new StringMap() : new StringMap(aParams);
    final String sPort = aParameters.getAsString(ATTR_PORTID);
    try {
        final int nPort = Integer.parseInt(sPort);
        final SSLServerSocketFactory aSSLServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        m_aSSLServerSocket = (SSLServerSocket) aSSLServerSocketFactory.createServerSocket(nPort);
        final String[] aEnabledCipherSuites = getEnabledAnonymousCipherSuites(m_aSSLServerSocket.getEnabledCipherSuites(), m_aSSLServerSocket.getSupportedCipherSuites());
        m_aSSLServerSocket.setEnabledCipherSuites(aEnabledCipherSuites);
    } catch (final IOException e) {
        throw new OpenAS2Exception(e);
    } catch (final NumberFormatException e) {
        throw new OpenAS2Exception("Error converting portid parameter '" + sPort + "': " + e);
    }
    m_sUserID = aParameters.getAsString(ATTR_USERID);
    if (StringHelper.hasNoText(m_sUserID))
        throw new OpenAS2Exception("missing 'userid' parameter");
    m_sPassword = aParameters.getAsString(ATTR_PASSWORD);
    if (StringHelper.hasNoText(m_sPassword))
        throw new OpenAS2Exception("missing 'password' parameter");
    try {
        m_aParser = new SocketCommandParser();
    } catch (final Exception e) {
        throw new OpenAS2Exception(e);
    }
}
Also used : StringMap(com.helger.commons.collection.attr.StringMap) IStringMap(com.helger.commons.collection.attr.IStringMap) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) WrappedOpenAS2Exception(com.helger.as2lib.exception.WrappedOpenAS2Exception) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) IOException(java.io.IOException) IOException(java.io.IOException) OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) WrappedOpenAS2Exception(com.helger.as2lib.exception.WrappedOpenAS2Exception)

Aggregations

OpenAS2Exception (com.helger.as2lib.exception.OpenAS2Exception)9 WrappedOpenAS2Exception (com.helger.as2lib.exception.WrappedOpenAS2Exception)5 IMicroElement (com.helger.xml.microdom.IMicroElement)3 IMicroDocument (com.helger.xml.microdom.IMicroDocument)2 IOException (java.io.IOException)2 CertificateException (java.security.cert.CertificateException)2 AS2ServerXMLSession (com.helger.as2.app.session.AS2ServerXMLSession)1 CommandManager (com.helger.as2.cmd.CommandManager)1 CommandResult (com.helger.as2.cmd.CommandResult)1 ICommand (com.helger.as2.cmd.ICommand)1 ICommandRegistry (com.helger.as2.cmd.ICommandRegistry)1 AbstractCommandProcessor (com.helger.as2.cmdprocessor.AbstractCommandProcessor)1 CommandTokenizer (com.helger.as2.util.CommandTokenizer)1 IStringMap (com.helger.commons.collection.attr.IStringMap)1 StringMap (com.helger.commons.collection.attr.StringMap)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 NonBlockingBufferedReader (com.helger.commons.io.stream.NonBlockingBufferedReader)1 NonBlockingBufferedWriter (com.helger.commons.io.stream.NonBlockingBufferedWriter)1 PeppolSBDHDocument (com.helger.peppol.sbdh.PeppolSBDHDocument)1 PeppolSBDHDocumentReader (com.helger.peppol.sbdh.read.PeppolSBDHDocumentReader)1