Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- DO NOT CHANGE THIS "groupId" WITHOUT CHANGING XMLSession.getManifestAttributes.MANIFEST_VENDOR_ID_ATTRIB -->
<groupId>net.sf.openas2</groupId>
<artifactId>OpenAS2</artifactId>
<version>4.8.2</version>
<version>4.8.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* original author unknown
* <p>
Expand Down Expand Up @@ -107,6 +106,7 @@ void loadPartnershipsFile() throws OpenAS2Exception {
} catch (Exception e) {
throw new WrappedException(e);
}

}

void refreshConfig() throws OpenAS2Exception {
Expand Down
85 changes: 45 additions & 40 deletions Server/src/main/java/org/openas2/util/AS2Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -760,55 +760,60 @@ 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());
}
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()) {
Expand Down
42 changes: 36 additions & 6 deletions Server/src/test/java/org/openas2/app/BaseServerSetup.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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);
}
Expand All @@ -60,18 +68,25 @@ 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 {
try {
//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();
Expand All @@ -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();
Expand Down
17 changes: 7 additions & 10 deletions Server/src/test/java/org/openas2/app/CertificatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,17 +77,17 @@ 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
// Switch to forward slash to avoid backslash being dropped when creating property string when on Windows
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;
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -164,4 +161,4 @@ public void a2_shouldConnect() throws Exception {
}
assertTrue(200 == resp.getStatusCode(), "Check default is mapping off.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
}
Expand Down
Loading
Loading