Search in sources :

Example 6 with AS2Exception

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

the class AS2DispositionException method wrap.

@Nonnull
public static AS2DispositionException wrap(@Nonnull final Exception ex, @Nonnull final Supplier<DispositionType> aDispositionTypeSupplier, @Nonnull final Supplier<String> aTextSupplier) {
    ValueEnforcer.notNull(ex, "Exception");
    ValueEnforcer.notNull(aDispositionTypeSupplier, "DispositionTypeSupplier");
    ValueEnforcer.notNull(aTextSupplier, "TextSupplier");
    if (ex instanceof AS2DispositionException)
        return (AS2DispositionException) ex;
    if (ex instanceof AS2ProcessorException) {
        // Special handling for #130
        final ICommonsList<AS2Exception> aCauses = ((AS2ProcessorException) ex).getAllCauses();
        if (aCauses.size() == 1) {
            final AS2Exception aFirst = aCauses.getFirst();
            if (aFirst instanceof AS2DispositionException)
                return (AS2DispositionException) aFirst;
        }
    }
    return new AS2DispositionException(aDispositionTypeSupplier.get(), aTextSupplier.get(), ex);
}
Also used : AS2Exception(com.helger.as2lib.exception.AS2Exception) AS2ProcessorException(com.helger.as2lib.processor.AS2ProcessorException) Nonnull(javax.annotation.Nonnull)

Example 7 with AS2Exception

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

the class AbstractMessageProcessor method executeAction.

/**
 * Execution the provided action with the registered modules.
 *
 * @param sAction
 *        Action to execute. Never <code>null</code>.
 * @param aMsg
 *        Message it is about. Never <code>null</code>.
 * @param aOptions
 *        Optional options map to be used. May be <code>null</code>.
 * @throws AS2ProcessorException
 *         In case of error
 */
protected final void executeAction(@Nonnull final String sAction, @Nonnull final IMessage aMsg, @Nullable final Map<String, Object> aOptions) throws AS2Exception {
    final ICommonsList<AS2Exception> aCauses = new CommonsArrayList<>();
    final ICommonsList<IProcessorModule> aModulesFound = new CommonsArrayList<>();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("  handling action '" + sAction + "' on message '" + aMsg.getMessageID() + "' with options " + aOptions);
    final ICommonsList<IProcessorModule> aAllModules = getAllModules();
    for (final IProcessorModule aModule : aAllModules) if (aModule.canHandle(sAction, aMsg, aOptions)) {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("  handling action '" + sAction + "' with module " + aModule);
        try {
            aModulesFound.add(aModule);
            aModule.handle(sAction, aMsg, aOptions);
        } catch (final AS2Exception ex) {
            aCauses.add(ex);
        }
    } else {
        if (LOGGER.isTraceEnabled())
            LOGGER.trace("  Not handling action '" + sAction + "' with module " + aModule);
    }
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("  action '" + sAction + "' was handled by modules: " + aModulesFound);
    if (aCauses.isNotEmpty()) {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("  action '" + sAction + "' was handled but failed: " + aCauses);
        throw new AS2ProcessorException(this, aCauses);
    }
    if (aModulesFound.isEmpty()) {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("  no modules found for '" + sAction + "'; modules are: " + aAllModules);
        throw new AS2NoModuleException(sAction, aMsg, aOptions);
    }
}
Also used : AS2Exception(com.helger.as2lib.exception.AS2Exception) IProcessorModule(com.helger.as2lib.processor.module.IProcessorModule) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

Example 8 with AS2Exception

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

the class MainOpenAS2Server method start.

public void start(@Nullable final String sConfigFilePath) {
    AS2ServerXMLSession aXMLSession = null;
    try {
        LOGGER.info(SERVER_NAME + " - starting ...");
        // create the OpenAS2 Session object
        // this is used by all other objects to access global configs and
        // functionality
        LOGGER.info("Loading configuration...");
        if (StringHelper.hasText(sConfigFilePath)) {
            // Load config file
            aXMLSession = new AS2ServerXMLSession(sConfigFilePath);
        } else {
            LOGGER.info("Usage:");
            LOGGER.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
        LOGGER.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) {
            LOGGER.info("Loading Command Processor " + cmd.getClass().getName() + "");
            cmd.init();
            cmd.addCommands(aCommandRegistry);
            new Thread(cmd, ClassHelper.getClassLocalName(cmd)).start();
        }
        // enter the command processing loop
        LOGGER.info(SERVER_NAME + " Started");
        // Start waiting for termination
        final AtomicBoolean aRunning = new AtomicBoolean(true);
        while (aRunning.get()) {
            for (final AbstractCommandProcessor cmd : aCommandProcessors) {
                if (cmd.isTerminated()) {
                    aRunning.set(false);
                    break;
                }
            }
            if (aRunning.get()) {
                // Wait outside loop in case no command processor is present
                Thread.sleep(100);
            }
        }
        LOGGER.info("- " + SERVER_NAME + " Stopped -");
    } catch (final InterruptedException ex) {
        LOGGER.error("Error running " + SERVER_NAME, ex);
        Thread.currentThread().interrupt();
    } catch (final Exception ex) {
        LOGGER.error("Error running " + SERVER_NAME, ex);
    } finally {
        if (aXMLSession != null) {
            try {
                aXMLSession.getMessageProcessor().stopActiveModules();
            } catch (final AS2Exception same) {
                same.terminate();
            }
        }
        LOGGER.info(SERVER_NAME + " has shut down");
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandManager(com.helger.as2.cmd.CommandManager) AS2Exception(com.helger.as2lib.exception.AS2Exception) AS2ServerXMLSession(com.helger.as2.app.session.AS2ServerXMLSession) ICommandRegistry(com.helger.as2.cmd.ICommandRegistry) AS2Exception(com.helger.as2lib.exception.AS2Exception) AbstractCommandProcessor(com.helger.as2.cmdprocessor.AbstractCommandProcessor)

Example 9 with AS2Exception

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

the class SocketCommandProcessor method initDynamicComponent.

@Override
public void initDynamicComponent(@Nonnull final IAS2Session aSession, @Nullable final IStringMap aParams) throws AS2Exception {
    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 AS2Exception(e);
    } catch (final NumberFormatException e) {
        throw new AS2Exception("Error converting portid parameter '" + sPort + "': " + e);
    }
    m_sUserID = aParameters.getAsString(ATTR_USERID);
    if (StringHelper.hasNoText(m_sUserID))
        throw new AS2Exception("missing 'userid' parameter");
    m_sPassword = aParameters.getAsString(ATTR_PASSWORD);
    if (StringHelper.hasNoText(m_sPassword))
        throw new AS2Exception("missing 'password' parameter");
    try {
        m_aParser = new SocketCommandParser();
    } catch (final Exception e) {
        throw new AS2Exception(e);
    }
}
Also used : StringMap(com.helger.commons.collection.attr.StringMap) IStringMap(com.helger.commons.collection.attr.IStringMap) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) IOException(java.io.IOException) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IOException(java.io.IOException)

Example 10 with AS2Exception

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

the class SocketCommandProcessor method processCommand.

@Override
public void processCommand() throws AS2Exception {
    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 AS2Exception("Bad userid");
        }
        if (!m_aParser.getPassword().equals(m_sPassword)) {
            m_aWriter.write("Bad userid/password");
            throw new AS2Exception("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 IOException ex) {
        throw WrappedAS2Exception.wrap(ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SSLSocket(javax.net.ssl.SSLSocket) NonBlockingBufferedWriter(com.helger.commons.io.stream.NonBlockingBufferedWriter) IOException(java.io.IOException) CommandResult(com.helger.as2.cmd.CommandResult) CommandTokenizer(com.helger.as2.util.CommandTokenizer) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) ICommand(com.helger.as2.cmd.ICommand) OutputStreamWriter(java.io.OutputStreamWriter) NonBlockingBufferedReader(com.helger.commons.io.stream.NonBlockingBufferedReader) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

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