diff --git a/Server/pom.xml b/Server/pom.xml index 86c05f72..271f416c 100644 --- a/Server/pom.xml +++ b/Server/pom.xml @@ -7,7 +7,7 @@ net.sf.openas2 OpenAS2 - 4.8.2 + 4.8.3 ../pom.xml diff --git a/Server/src/main/java/org/openas2/partner/XMLPartnershipFactory.java b/Server/src/main/java/org/openas2/partner/XMLPartnershipFactory.java index 1f0c926d..6e588c5b 100644 --- a/Server/src/main/java/org/openas2/partner/XMLPartnershipFactory.java +++ b/Server/src/main/java/org/openas2/partner/XMLPartnershipFactory.java @@ -44,7 +44,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; - /** * original author unknown *

@@ -107,6 +106,7 @@ void loadPartnershipsFile() throws OpenAS2Exception { } catch (Exception e) { throw new WrappedException(e); } + } void refreshConfig() throws OpenAS2Exception { diff --git a/Server/src/main/java/org/openas2/util/AS2Util.java b/Server/src/main/java/org/openas2/util/AS2Util.java index d92eadc5..47ae7e7d 100644 --- a/Server/src/main/java/org/openas2/util/AS2Util.java +++ b/Server/src/main/java/org/openas2/util/AS2Util.java @@ -369,7 +369,7 @@ public static boolean resend(Session session, Class sourceClass, String how, // Going to try again so increment the try count retries++; } - // Keep a popinter to the passed in msg object in case it is overwritten in this method so that the setting + // Keep a popinter to the passed in msg object in case it is overwritten in this method so that setting // the resend flag to avoid file cleanup is not lost when this method exits and the original initiating // method of this cycle queries the msg object to check if it is ok to call file cleanup Message passed_in_msg = msg; @@ -726,7 +726,7 @@ public static void getMetaData(AS2Message msg, File inFile) throws OpenAS2Except } - public static void cleanupFiles(Message msg, boolean isError) { + public synchronized static void cleanupFiles(Message msg, boolean isError) { Logger logger = LoggerFactory.getLogger(AS2Util.class); if (msg.isFileCleanupCompleted()) { if (logger.isTraceEnabled()) { @@ -760,14 +760,17 @@ public static void cleanupFiles(Message msg, boolean isError) { String pendingFileName = msg.getAttribute(FileAttribute.MA_PENDINGFILE); if (pendingFileName != null) { File fPendingFile = new File(pendingFileName); - try { - IOUtil.deleteFile(new File(pendingFileName + ".object")); - if (logger.isTraceEnabled()) { - logger.trace("The RETRY message object file deleted: " + pendingFileName + ".object" + msg.getLogMsgID()); + File msgObjFile = new File(pendingFileName + ".object"); + if (msgObjFile.exists()) { + try { + IOUtil.deleteFile(msgObjFile); + if (logger.isTraceEnabled()) { + logger.trace("The RETRY message object file deleted: " + pendingFileName + ".object" + msg.getLogMsgID()); + } + } catch (Exception e) { + msg.setLogMsg("The RETRY message object file NOT deleted: " + org.openas2.util.Logging.getExceptionMsg(e)); + logger.warn(msg.getLogMsg(), e); } - } catch (Exception e) { - msg.setLogMsg("The RETRY message object file NOT deleted: " + org.openas2.util.Logging.getExceptionMsg(e)); - logger.warn(msg.getLogMsg(), e); } if (logger.isTraceEnabled()) { logger.trace("Cleaning up pending file : " + fPendingFile.getName() + " ::: From pending folder : " + fPendingFile.getParent() + msg.getLogMsgID()); @@ -775,40 +778,42 @@ public static void cleanupFiles(Message msg, boolean isError) { try { // Move file to error or sent directory if the error or sent saving functionality is enabled boolean isMoved = false; - String tgtDir = null; - String targetFilenameUnparsed = ""; - if (isError) { - tgtDir = msg.getAttribute(FileAttribute.MA_ERROR_DIR); - targetFilenameUnparsed = msg.getAttribute(FileAttribute.MA_ERROR_FILENAME); - } else { - // If the Sent Directory option is set, move the transmitted file to the sent - // directory - tgtDir = msg.getAttribute(FileAttribute.MA_SENT_DIR); - targetFilenameUnparsed = msg.getAttribute(FileAttribute.MA_SENT_FILENAME); - } - if (tgtDir != null && tgtDir.length() > 0) { - File tgtFile = null; - try { - String tgtFileName = fPendingFile.getName(); - if (targetFilenameUnparsed != null && targetFilenameUnparsed.length() > 0) { - CompositeParameters parser = new CompositeParameters(false).add("date", new DateParameters()).add("msg", new MessageParameters(msg)).add("rand", new RandomParameters()); - tgtFileName = ParameterParser.parse(targetFilenameUnparsed, parser); - } - tgtFileName = IOUtil.cleanFilename(tgtFileName); - tgtFile = new File(tgtDir + "/" + tgtFileName); - tgtFile = IOUtil.moveFile(fPendingFile, tgtFile, false); - isMoved = true; - - if (logger.isDebugEnabled()) { - logger.debug("Pending MDN MSG FILE file " + fPendingFile.getAbsolutePath() + " moved to " + tgtFile.getAbsolutePath() + msg.getLogMsgID()); + if (fPendingFile.exists()) { + String tgtDir = null; + String targetFilenameUnparsed = ""; + if (isError) { + tgtDir = msg.getAttribute(FileAttribute.MA_ERROR_DIR); + targetFilenameUnparsed = msg.getAttribute(FileAttribute.MA_ERROR_FILENAME); + } else { + // If the Sent Directory option is set, move the transmitted file to the sent + // directory + tgtDir = msg.getAttribute(FileAttribute.MA_SENT_DIR); + targetFilenameUnparsed = msg.getAttribute(FileAttribute.MA_SENT_FILENAME); + } + if (tgtDir != null && tgtDir.length() > 0) { + File tgtFile = null; + try { + String tgtFileName = fPendingFile.getName(); + if (targetFilenameUnparsed != null && targetFilenameUnparsed.length() > 0) { + CompositeParameters parser = new CompositeParameters(false).add("date", new DateParameters()).add("msg", new MessageParameters(msg)).add("rand", new RandomParameters()); + tgtFileName = ParameterParser.parse(targetFilenameUnparsed, parser); + } + tgtFileName = IOUtil.cleanFilename(tgtFileName); + tgtFile = new File(tgtDir + "/" + tgtFileName); + tgtFile = IOUtil.moveFile(fPendingFile, tgtFile, false); + logger.info("PENDING FILE " + pendingFileName + " AFTER MOVE STILL EXISTS? " + fPendingFile.exists()); + isMoved = true; + + if (logger.isDebugEnabled()) { + logger.debug("Pending MDN MSG FILE file " + fPendingFile.getAbsolutePath() + " moved to " + tgtFile.getAbsolutePath() + msg.getLogMsgID()); + } + + } catch (IOException iose) { + msg.setLogMsg("Error moving file to " + tgtDir + " : " + org.openas2.util.Logging.getExceptionMsg(iose)); + logger.error(msg.getLogMsg(), iose); } - - } catch (IOException iose) { - msg.setLogMsg("Error moving file to " + tgtDir + " : " + org.openas2.util.Logging.getExceptionMsg(iose)); - logger.error(msg.getLogMsg(), iose); } } - if (!isMoved) { // Could not find somewhere to move it to so delete it if it still exists if (fPendingFile.exists()) { diff --git a/Server/src/test/java/org/openas2/app/BaseServerSetup.java b/Server/src/test/java/org/openas2/app/BaseServerSetup.java index cb284336..37977c81 100644 --- a/Server/src/test/java/org/openas2/app/BaseServerSetup.java +++ b/Server/src/test/java/org/openas2/app/BaseServerSetup.java @@ -1,8 +1,13 @@ package org.openas2.app; import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -19,6 +24,9 @@ public class BaseServerSetup { + static String resourcePathPrefix = Paths.get("src","test","resources").toAbsolutePath().toString(); + static Path srcConfigDirPath = Paths.get(resourcePathPrefix + File.separator + "config"); + static String myCompanyOid = "MyCompany_OID"; static String myPartnerOid = "PartnerA_OID"; private boolean startActiveModules = false; @@ -27,7 +35,7 @@ public class BaseServerSetup { protected static Message simpleTestMsg; @TempDir - public static File tmpDir; + public static File configDir; public File openAS2PropertiesFile; public void refresh() throws Exception { @@ -51,7 +59,7 @@ public void addSendPayloadStuffToMsg(String fileName, Message msg) throws Except msg.setHeader("AS2-To", msg.getPartnership().getReceiverID(Partnership.PID_AS2)); msg.setHeader("AS2-From", msg.getPartnership().getSenderID(Partnership.PID_AS2)); msg.updateMessageID(); - File testFile = new File(tmpDir, fileName); + File testFile = new File(configDir, fileName); FileUtils.writeStringToFile(testFile, "Show me the money!", Charset.forName("UTF-8")); frm.buildMessageData(msg, testFile, null); } @@ -60,9 +68,14 @@ public void setStartActiveModules(boolean startActiveModules) { this.startActiveModules = startActiveModules; } - public void createFileSystemResources() throws Exception { - tmpDir = Files.createTempDirectory("testResources").toFile(); - openAS2PropertiesFile = new File(tmpDir, "test.openas2.properties"); + public void createFileSystemResources(String configDirName) throws Exception { + Path destConfigDirPath = Files.createTempDirectory(configDirName); + configDir = destConfigDirPath.toFile(); + openAS2PropertiesFile = new File(configDir, "test.openas2.properties"); + // Just in case there is an existing file... + if (openAS2PropertiesFile.exists()) openAS2PropertiesFile.delete(); + // Copy standard config resources to this folder + this.copyConfig(destConfigDirPath.toString()); } public void setup() throws Exception { @@ -70,8 +83,10 @@ public void setup() throws Exception { //System.setProperty("OPENAS2_LOG_LEVEL", "trace"); if (openAS2PropertiesFile.exists()) { System.setProperty(Properties.OPENAS2_PROPERTIES_FILE_PROP, openAS2PropertiesFile.getAbsolutePath()); + } else { + System.clearProperty(Properties.OPENAS2_PROPERTIES_FILE_PROP); } - session = new XMLSession(TestResource.getResource("config")); + session = new XMLSession(configDir.getAbsolutePath() + "/config.xml"); simpleTestMsg = getSimpleTestMsg(); if (startActiveModules) { session.start(); @@ -83,6 +98,21 @@ public void setup() throws Exception { } } + /** + * Copy standard config files from {@link #resourcePathPrefix} to the target config folder + * @throws IOException + * + */ + public void copyConfig(String destDir) throws IOException { + if (!srcConfigDirPath.toFile().exists()) { + throw new FileNotFoundException("The source config was not found here: " + srcConfigDirPath.getFileName()); + } + for (String f : srcConfigDirPath.toFile().list()) { + Files.copy(Paths.get(srcConfigDirPath.toString(), f), Paths.get(destDir, f), StandardCopyOption.REPLACE_EXISTING); + } + } + + @AfterAll public void tearDown() throws Exception { session.stop(); diff --git a/Server/src/test/java/org/openas2/app/CertificatesTest.java b/Server/src/test/java/org/openas2/app/CertificatesTest.java index 04cd8f80..daff7360 100644 --- a/Server/src/test/java/org/openas2/app/CertificatesTest.java +++ b/Server/src/test/java/org/openas2/app/CertificatesTest.java @@ -44,7 +44,6 @@ public class CertificatesTest extends BaseServerSetup { private String alias = null; private static final String receiverPort = "10443"; private static final String url = "https://localhost:" + receiverPort + "/"; - private File customPropsFile = null; private File sslCertsFile = null; // The private key and certificate for the HTTPS private String sslTrustCertsFilePath = null; // The public key for the SSL private key private X509CertificateFactory trustFx = null; // The trust certificates @@ -78,9 +77,9 @@ public X509CertificateFactory genSelfSignedCert( @BeforeAll public void setUp() throws Exception { - super.createFileSystemResources(); - String tmpDirAbsolutePath = tmpDir.getAbsolutePath(); - sslCertsFile = Files.createFile(Paths.get(tmpDirAbsolutePath, "ssl_certs.p12")).toFile(); + super.createFileSystemResources(this.getClass().getName()); + String configDirAbsolutePath = configDir.getAbsolutePath(); + sslCertsFile = Files.createFile(Paths.get(configDirAbsolutePath, "ssl_certs.p12")).toFile(); String tgtHostName = "test.openas2.org"; this.alias = tgtHostName; // Create the SSL file for the server to use @@ -88,7 +87,7 @@ public void setUp() throws Exception { String sslCertsFilePath = sslCertsFile.getAbsolutePath().replace("\\", "/"); this.certFx = genSelfSignedCert(alias, sslCertsFilePath, "RSA", "SHA256", 2048, tgtHostName); // Create the trust store with the public key so the certificate returned from the server is trusted - File sslTrustCertsFile = Files.createFile(Paths.get(tmpDirAbsolutePath, "ssl_trust_certs.p12")).toFile(); + File sslTrustCertsFile = Files.createFile(Paths.get(configDirAbsolutePath, "ssl_trust_certs.p12")).toFile(); // Switch to forward slash to avoid backslash being dropped when creating property string when on Windows sslTrustCertsFilePath = sslTrustCertsFile.getAbsolutePath().replace("\\", "/"); String trustAlias = "trust-" + tgtHostName; @@ -99,9 +98,7 @@ public void setUp() throws Exception { this.trustFx.setKeyStore(AS2Util.getCryptoHelper().getKeyStore()); this.trustFx.load(); - customPropsFile = Files.createFile(Paths.get(tmpDirAbsolutePath, "openas2.properties")).toFile(); - System.setProperty(Properties.OPENAS2_PROPERTIES_FILE_PROP, customPropsFile.getAbsolutePath()); - FileOutputStream fos = new FileOutputStream(customPropsFile); + FileOutputStream fos = new FileOutputStream(openAS2PropertiesFile); // Switch to forward slash to avoid backslash being dropped when creating property string when on Windows fos.write(("ssl_keystore=" + sslCertsFilePath + "\n").getBytes()); fos.write(("ssl_keystore_password=" + new String(password) + "\n").getBytes()); @@ -149,7 +146,7 @@ public void a1_shouldFailSSLConnect() throws Exception { @Test public void a2_shouldConnect() throws Exception { - FileOutputStream fos = new FileOutputStream(customPropsFile, true); + FileOutputStream fos = new FileOutputStream(openAS2PropertiesFile, true); fos.write(("ssl_trust_keystore.enabled=true\n").getBytes()); fos.write(("ssl_trust_keystore=" + sslTrustCertsFilePath + "\n").getBytes()); fos.write(("ssl_trust_keystore_password=" + new String(password) + "\n").getBytes()); @@ -164,4 +161,4 @@ public void a2_shouldConnect() throws Exception { } assertTrue(200 == resp.getStatusCode(), "Check default is mapping off."); } -} \ No newline at end of file +} diff --git a/Server/src/test/java/org/openas2/app/FilenameParsingTest.java b/Server/src/test/java/org/openas2/app/FilenameParsingTest.java index aee29b03..1d2b4e45 100644 --- a/Server/src/test/java/org/openas2/app/FilenameParsingTest.java +++ b/Server/src/test/java/org/openas2/app/FilenameParsingTest.java @@ -31,7 +31,7 @@ public class FilenameParsingTest extends BaseServerSetup { @BeforeAll public void setup() throws Exception { - super.createFileSystemResources(); + super.createFileSystemResources(this.getClass().getName()); super.setup(); try { simpleTestMsg = new AS2Message(); diff --git a/Server/src/test/java/org/openas2/app/ParallelProcessingTest.java b/Server/src/test/java/org/openas2/app/ParallelProcessingTest.java index a14f0285..ea685b11 100644 --- a/Server/src/test/java/org/openas2/app/ParallelProcessingTest.java +++ b/Server/src/test/java/org/openas2/app/ParallelProcessingTest.java @@ -116,10 +116,10 @@ public static void tearDown() throws Exception { // NOTE: For debugging "missing" files it is best to comment this out for (int i = 0; i < dataFolders.length; i++) { try { - FileUtils.deleteDirectory(new File(dataFolders[i])); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + File dir = new File(dataFolders[i]); + for (File f: dir.listFiles()) f.delete(); + dir.delete(); + } catch (Exception e) { } } } diff --git a/Server/src/test/java/org/openas2/app/RestApiTest.java b/Server/src/test/java/org/openas2/app/RestApiTest.java index 78fa5c49..f02f3132 100644 --- a/Server/src/test/java/org/openas2/app/RestApiTest.java +++ b/Server/src/test/java/org/openas2/app/RestApiTest.java @@ -23,8 +23,11 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.io.TempDir; import org.mockito.junit.jupiter.MockitoExtension; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import org.openas2.app.BaseServerSetup; import org.openas2.TestResource; import org.openas2.TestUtils; import org.openas2.cmd.processor.restapi.AuthenticationRequestFilter; @@ -44,15 +47,14 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +@TestInstance(Lifecycle.PER_CLASS) @ExtendWith(MockitoExtension.class) @TestMethodOrder(MethodOrderer.MethodName.class) -public class RestApiTest { +public class RestApiTest extends BaseServerSetup { // private static File openAS2AHome; private static OpenAS2Server serverInstance; private static String TEST_PARTNER_NAME = "partnerX"; private static String TEST_PARTNERSHIP_NAME = TEST_PARTNER_NAME + "-partnerA"; - @TempDir - private static Path scratchpad; private static CloseableHttpClient httpclient; private static String restHostAddr = "http://127.0.0.1:8080"; private static String baseUrl = restHostAddr + "/api/"; @@ -60,24 +62,23 @@ public class RestApiTest { private static String authPwd = "admin"; @BeforeAll - public static void start_A_Server() throws Exception { + public void start_A_Server() throws Exception { // Set up some override properties so we can use the standard config in tests - // to make sure the release package is fully tested - scratchpad = Files.createTempDirectory("tempResources"); - File customPropsFile = Files.createFile(Paths.get(scratchpad.toString(), "openas2.properties")).toFile(); - System.setProperty(Properties.OPENAS2_PROPERTIES_FILE_PROP, customPropsFile.getAbsolutePath()); - FileOutputStream fos = new FileOutputStream(customPropsFile); + super.createFileSystemResources(this.getClass().getName()); + String configDirAbsolutePath = configDir.getAbsolutePath(); + + FileOutputStream fos = new FileOutputStream(openAS2PropertiesFile); fos.write("restapi.command.processor.enabled=true\n".getBytes()); fos.write(("restapi.command.processor.baseuri=" + restHostAddr + "\n").getBytes()); fos.write(("restapi.command.processor.userid=" + authUser + "\n").getBytes()); fos.write(("restapi.command.processor.password=" + authPwd + "\n").getBytes()); fos.close(); - + System.setProperty(Properties.OPENAS2_PROPERTIES_FILE_PROP, openAS2PropertiesFile.getAbsolutePath()); try { //System.setProperty(OPENAS2_LOG_LEVEL", "TRACE"); //executorService = Executors.newFixedThreadPool(20); - RestApiTest.serverInstance = new OpenAS2Server.Builder().run(TestResource.getResource("config")); + RestApiTest.serverInstance = new OpenAS2Server.Builder().run(configDirAbsolutePath + "/config.xml"); } catch (Throwable e) { // aid for debugging JUnit tests System.err.println("ERROR occurred: " + ExceptionUtils.getStackTrace(e)); @@ -92,11 +93,10 @@ public static void start_B_Client() throws Exception { } @AfterAll - public static void tearDown() throws Exception { + public void tearDown() throws Exception { serverInstance.shutdown(); // executorService.shutdown(); System.clearProperty("openas2.properties.file"); - TestUtils.deleteDirectory(scratchpad.toFile()); httpclient.close(); } diff --git a/Server/src/test/java/org/openas2/message/DynamicContentTypeTest.java b/Server/src/test/java/org/openas2/message/DynamicContentTypeTest.java index d27fc506..98932672 100644 --- a/Server/src/test/java/org/openas2/message/DynamicContentTypeTest.java +++ b/Server/src/test/java/org/openas2/message/DynamicContentTypeTest.java @@ -45,9 +45,9 @@ public class DynamicContentTypeTest extends BaseServerSetup { @BeforeAll public void setUp() throws Exception { - super.createFileSystemResources(); + super.createFileSystemResources(this.getClass().getName()); // Set up the system level mappings - systemContentTypesMappingFile = new File(tmpDir, "content_type_map.properties"); + systemContentTypesMappingFile = new File(configDir, "content_type_map.properties"); systemMappedContentTypes.put(xmlFileExtension, "application/xml"); systemMappedContentTypes.put(ediFileExtension, "application/edifact"); systemMappedContentTypes.put("txt", "text/plain"); @@ -57,7 +57,7 @@ public void setUp() throws Exception { } writer.close(); // Set up the partnership override mappings - partnershipContentTypesMappingFile = new File(tmpDir, "override_content_type_map.properties"); + partnershipContentTypesMappingFile = new File(configDir, "override_content_type_map.properties"); partnershipMappedContentTypes.put(xmlFileExtension, "application/xml-custom"); BufferedWriter writer2 = new BufferedWriter(new FileWriter(partnershipContentTypesMappingFile)); for (Map.Entry entry : partnershipMappedContentTypes.entrySet()) { diff --git a/Server/src/test/java/org/openas2/processor/receiver/ContentDispositionTest.java b/Server/src/test/java/org/openas2/processor/receiver/ContentDispositionTest.java index b4ec48f3..5fecb43d 100644 --- a/Server/src/test/java/org/openas2/processor/receiver/ContentDispositionTest.java +++ b/Server/src/test/java/org/openas2/processor/receiver/ContentDispositionTest.java @@ -16,6 +16,8 @@ import org.openas2.app.BaseServerSetup; import org.openas2.message.FileAttribute; import org.openas2.partner.Partnership; +import java.util.Map; + @TestInstance(Lifecycle.PER_CLASS) @TestMethodOrder(MethodOrderer.MethodName.class) @@ -24,7 +26,7 @@ public class ContentDispositionTest extends BaseServerSetup { @BeforeAll public void setUp() throws Exception { - super.createFileSystemResources(); + super.createFileSystemResources(this.getClass().getName()); super.setup(); this.poller = session.getPartnershipPoller(simpleTestMsg.getPartnership().getName()); } diff --git a/pom.xml b/pom.xml index 0df8d340..0130c0df 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 net.sf.openas2 OpenAS2 - 4.8.2 + 4.8.3 OpenAS2 pom