diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java index f2ad2c5..5cdc373 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java @@ -58,6 +58,7 @@ public class SmbSession implements Session { private final Log logger = LogFactory.getLog(SmbSession.class); private static final String FILE_SEPARATOR = System.getProperty("file.separator"); + private static final String SMB_FILE_SEPARATOR = "/"; static { configureJcifs(); @@ -378,29 +379,31 @@ public class SmbSession implements Session { /** * Factory method for new SmbFile objects under this session's share for the specified path. - * @param _path remote path - * @param _isDirectory Boolean object to indicate the path is a directory, may be null + * @param path remote path + * @param isDirectory Boolean object to indicate the path is a directory, may be null * @return SmbFile object for path * @throws IOException in case of I/O errors */ - private SmbFile createSmbFileObject(String _path, Boolean _isDirectory) throws IOException { - String path = StringUtils.cleanPath(_path); - if (!StringUtils.hasText(path)) { + private SmbFile createSmbFileObject(String path, Boolean isDirectory) throws IOException { + + final String cleanedPath = StringUtils.cleanPath(path); + + if (!StringUtils.hasText(cleanedPath)) { return smbShare; } - SmbFile smbFile = new SmbFile(smbShare, path); + SmbFile smbFile = new SmbFile(smbShare, cleanedPath); - boolean appendFileSeparator = !path.endsWith(FILE_SEPARATOR); + boolean appendFileSeparator = !cleanedPath.endsWith(SMB_FILE_SEPARATOR); if (appendFileSeparator) { try { - appendFileSeparator = smbFile.isDirectory() || (_isDirectory != null && _isDirectory); - } catch (Exception _ex) { - appendFileSeparator = false; + appendFileSeparator = smbFile.isDirectory() || (isDirectory != null && isDirectory); + } catch (SmbException ex) { + appendFileSeparator = false; } } if (appendFileSeparator) { - smbFile = createSmbFileObject(path + FILE_SEPARATOR); + smbFile = createSmbFileObject(cleanedPath + SMB_FILE_SEPARATOR); } if (logger.isDebugEnabled()) { logger.debug("Created new " + SmbFile.class.getName() + "[" + smbFile + "] for path [" + path + "]."); diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java index 4d3aa78..3796ec6 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.core.NestedIOException; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * @author Markus Spann @@ -40,8 +41,8 @@ public class SmbShare extends SmbFile { private final AtomicBoolean useTempFile = new AtomicBoolean(false); - public SmbShare(String _url) throws IOException { - super(_url); + public SmbShare(String url) throws IOException { + super(StringUtils.cleanPath(url)); } public SmbShare(SmbConfig _smbConfig) throws IOException { diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/MySmbSessionTest.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/MySmbSessionTest.java deleted file mode 100644 index 5f0035b..0000000 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/MySmbSessionTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2002-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.integration.smb.session; - - -public class MySmbSessionTest { - -// @Test -// public void testRemove() { -// fail("Not yet implemented"); -// } -// -// @Test -// public void testRead() { -// fail("Not yet implemented"); -// } -// -// @Test -// public void testWrite() { -// fail("Not yet implemented"); -// } -// -// @Test -// public void testMkdir() { -// fail("Not yet implemented"); -// } -// -// @Test -// public void testRename() { -// fail("Not yet implemented"); -// } -// -// @Test -// public void testClose() { -// fail("Not yet implemented"); -// } -// -// @Test -// public void testIsOpen() { -// fail("Not yet implemented"); -// } - -} diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionTest.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionTest.java new file mode 100644 index 0000000..0127c9f --- /dev/null +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/session/SmbSessionTest.java @@ -0,0 +1,109 @@ +/** + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.smb.session; + +import java.io.IOException; + +import jcifs.smb.SmbFile; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * + * @author Gunnar Hillert + * + */ +public class SmbSessionTest { + + /** + * Test Case reproduces INTEXT-37 (https://jira.springsource.org/browse/INTEXT-37) + * @throws IOException + */ + @Test + public void testCreateSmbFileObjectWithBackSlash1() throws IOException { + + System.setProperty("file.separator", "\\"); + SmbShare smbShare = new SmbShare("smb://myshare/shared/"); + SmbSession smbSession = new SmbSession(smbShare); + SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba\\"); + assertEquals("smb://myshare/blubba/", smbFile.getCanonicalPath()); + } + + @Test + public void testCreateSmbFileObjectWithBackSlash2() throws IOException { + + System.setProperty("file.separator", "\\"); + SmbShare smbShare = new SmbShare("smb://myshare\\shared\\"); + SmbSession smbSession = new SmbSession(smbShare); + SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba\\"); + assertEquals("smb://myshare/blubba/", smbFile.getCanonicalPath()); + } + + @Test + public void testCreateSmbFileObjectWithBackSlash3() throws IOException { + + System.setProperty("file.separator", "\\"); + SmbShare smbShare = new SmbShare("smb://myshare\\shared\\"); + SmbSession smbSession = new SmbSession(smbShare); + SmbFile smbFile = smbSession.createSmbFileObject("..\\another"); + assertEquals("smb://myshare/another/", smbFile.getCanonicalPath()); + } + + @Test + public void testCreateSmbFileObjectWithBackSlash4() throws IOException { + + System.setProperty("file.separator", "/"); + SmbShare smbShare = new SmbShare("smb://myshare/shared/"); + SmbSession smbSession = new SmbSession(smbShare); + SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba\\"); + assertEquals("smb://myshare/blubba/", smbFile.getCanonicalPath()); + } + + + @Test + public void testCreateSmbFileObjectwithMissingTrailingSlash1() throws IOException { + + SmbShare smbShare = new SmbShare("smb://myshare/shared"); + SmbSession smbSession = new SmbSession(smbShare); + + SmbFile smbFile = smbSession.createSmbFileObject("smb://myshare\\blubba"); + assertEquals("smb://myshare/blubba/", smbFile.getCanonicalPath()); + + } + + @Test + public void testCreateSmbFileObjectwithMissingTrailingSlash2() throws IOException { + + SmbShare smbShare = new SmbShare("smb://myshare/shared/"); + SmbSession smbSession = new SmbSession(smbShare); + + SmbFile smbFile = smbSession.createSmbFileObject("."); + assertEquals("smb://myshare/shared/", smbFile.getCanonicalPath()); + + } + + @Test + public void testCreateSmbFileObjectwithMissingTrailingSlash3() throws IOException { + + SmbShare smbShare = new SmbShare("smb://myshare/shared/"); + SmbSession smbSession = new SmbSession(smbShare); + + SmbFile smbFile = smbSession.createSmbFileObject("../anotherShare"); + assertEquals("smb://myshare/anotherShare/", smbFile.getCanonicalPath()); + + } +}