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