Search in sources :

Example 1 with ICommand

use of com.helger.as2.cmd.ICommand 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 2 with ICommand

use of com.helger.as2.cmd.ICommand in project as2-server by phax.

the class StreamCommandProcessor method processCommand.

@Override
public void processCommand() throws OpenAS2Exception {
    try {
        final String sLine = readLine();
        if (sLine != null) {
            final CommandTokenizer aTokenizer = new CommandTokenizer(sLine);
            if (aTokenizer.hasMoreTokens()) {
                final String sCommandName = aTokenizer.nextToken().toLowerCase(Locale.US);
                if (sCommandName.equals(EXIT_COMMAND)) {
                    terminate();
                } else {
                    final ICommonsList<String> aParams = new CommonsArrayList<>();
                    while (aTokenizer.hasMoreTokens()) {
                        aParams.add(aTokenizer.nextToken());
                    }
                    final ICommand aCommand = getCommand(sCommandName);
                    if (aCommand != null) {
                        final CommandResult aResult = aCommand.execute(aParams.toArray());
                        if (aResult.getType().isSuccess()) {
                            writeLine(aResult.toString());
                        } else {
                            writeLine(COMMAND_ERROR);
                            writeLine(aResult.getResultAsString());
                        }
                    } else {
                        writeLine(COMMAND_NOT_FOUND + "> " + sCommandName);
                        writeLine("List of commands:");
                        writeLine(EXIT_COMMAND);
                        for (final String sCurCmd : getAllCommands().keySet()) writeLine(sCurCmd);
                    }
                }
            }
            write(PROMPT);
        } else {
            ThreadHelper.sleep(100);
        }
    } catch (final IOException ex) {
        throw WrappedOpenAS2Exception.wrap(ex);
    }
}
Also used : CommandTokenizer(com.helger.as2.util.CommandTokenizer) ICommand(com.helger.as2.cmd.ICommand) IOException(java.io.IOException) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) CommandResult(com.helger.as2.cmd.CommandResult)

Example 3 with ICommand

use of com.helger.as2.cmd.ICommand in project as2-server by phax.

the class XMLCommandRegistry method loadCommand.

protected void loadCommand(final IMicroElement eCommand, @Nullable final MultiCommand aParent) throws OpenAS2Exception {
    final IAS2Session aSession = getSession();
    final String sBaseDirectory = aSession instanceof AS2ServerXMLSession ? ((AS2ServerXMLSession) aSession).getBaseDirectory() : null;
    final ICommand aCommand = AS2XMLHelper.createComponent(eCommand, ICommand.class, aSession, sBaseDirectory);
    if (aParent != null)
        aParent.getCommands().add(aCommand);
    else
        addCommand(aCommand);
}
Also used : AS2ServerXMLSession(com.helger.as2.app.session.AS2ServerXMLSession) IAS2Session(com.helger.as2lib.session.IAS2Session)

Aggregations

CommandResult (com.helger.as2.cmd.CommandResult)2 ICommand (com.helger.as2.cmd.ICommand)2 CommandTokenizer (com.helger.as2.util.CommandTokenizer)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 IOException (java.io.IOException)2 AS2ServerXMLSession (com.helger.as2.app.session.AS2ServerXMLSession)1 OpenAS2Exception (com.helger.as2lib.exception.OpenAS2Exception)1 WrappedOpenAS2Exception (com.helger.as2lib.exception.WrappedOpenAS2Exception)1 IAS2Session (com.helger.as2lib.session.IAS2Session)1 NonBlockingBufferedReader (com.helger.commons.io.stream.NonBlockingBufferedReader)1 NonBlockingBufferedWriter (com.helger.commons.io.stream.NonBlockingBufferedWriter)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 SSLSocket (javax.net.ssl.SSLSocket)1