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);
}
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);
}
}
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");
}
}
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);
}
}
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);
}
}
Aggregations