Skip to content

Commit 41f8a13

Browse files
Allow directories in path
1 parent 83d1b8b commit 41f8a13

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

core/src/main/java/oracle/weblogic/deploy/util/WLSDeployArchive.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,55 @@ public boolean containsFile(String path) throws WLSDeployArchiveIOException {
277277
return result;
278278
}
279279

280+
/**
281+
* Determines whether or not the provided path is a directory in the archive file.
282+
*
283+
* @param path the path into the archive file to test
284+
* @return true if the specified location was found in the archive file and is a directory
285+
* @throws WLSDeployArchiveIOException if an error occurs reading the archive file
286+
* @throws IllegalArgumentException if the path is null or empty
287+
*/
288+
public boolean containsPath(String path) throws WLSDeployArchiveIOException {
289+
final String METHOD = "isAPath";
290+
291+
LOGGER.entering(CLASS, METHOD, path);
292+
validateNonEmptyString(path, "path", METHOD);
293+
294+
boolean result = false;
295+
// Verify that the path is into the binary root directory so that we do not allow random content.
296+
if (isPathIntoArchive(path)) {
297+
List<String> entries = getZipFile().listZipEntries();
298+
result = !entries.contains(path) && zipListContainsPath(entries, path);
299+
}
300+
LOGGER.exiting(CLASS, METHOD, result);
301+
return result;
302+
}
303+
304+
/**
305+
* Determines whether or not the provided path is a directory or a file in a directory
306+
* in the archive file.
307+
*
308+
* @param path the path into the archive file to test
309+
* @return true if the specified location was found in the archive file
310+
* @throws WLSDeployArchiveIOException if an error occurs reading the archive file
311+
* @throws IllegalArgumentException if the path is null or empty
312+
*/
313+
public boolean containsFileOrPath(String path) throws WLSDeployArchiveIOException {
314+
final String METHOD = "containsFileOrPath";
315+
316+
LOGGER.entering(CLASS, METHOD, path);
317+
validateNonEmptyString(path, "path", METHOD);
318+
319+
boolean result = false;
320+
// Verify that the path is into the binary root directory so that we do not allow random content.
321+
if (isPathIntoArchive(path)) {
322+
List<String> entries = getZipFile().listZipEntries();
323+
result = entries.contains(path) || zipListContainsPath(entries, path);
324+
}
325+
LOGGER.exiting(CLASS, METHOD, result);
326+
return result;
327+
}
328+
280329
/**
281330
* Extract the specified file to the specified location (which is typically the domain home). For example,
282331
* if the path is wlsdeploy/applications/myapp.ear and the extractToLocation is the domain home, the file
@@ -1091,6 +1140,19 @@ protected void extractFileFromZip(String itemToExtract, String fromDir, String t
10911140
// Private Helper methods used by the protected methods above... //
10921141
///////////////////////////////////////////////////////////////////////////
10931142

1143+
private static boolean zipListContainsPath(List<String> entries, String path) {
1144+
boolean foundInList = false;
1145+
if (path != null && entries != null) {
1146+
for (String entry : entries) {
1147+
if (entry.startsWith(path)) {
1148+
foundInList = true;
1149+
break;
1150+
}
1151+
}
1152+
}
1153+
return foundInList;
1154+
}
1155+
10941156
private static void copyFile(InputStream input, FileOutputStream output) throws IOException {
10951157
byte[] readBuffer = new byte[READ_BUFFER_SIZE];
10961158

core/src/main/python/wlsdeploy/tool/util/archive_helper.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def contains_file(self, path):
7979
"""
8080
Does the archive file contain the specified location?
8181
:param path: the path to test
82-
:return: True, if the path was found in the archive file, False otherwise
82+
:return: True, if the path was found in the archive file and is a file, False otherwise
8383
:raises: BundleAwareException of the appropriate type: if an error occurs
8484
"""
8585
_method_name = 'contains_file'
@@ -95,6 +95,46 @@ def contains_file(self, path):
9595
self.__logger.exiting(class_name=self.__class_name, method_name=_method_name, result=result)
9696
return result
9797

98+
def contains_path(self, path):
99+
"""
100+
Check that the provided path is a path, not a file, contained inside the archive file
101+
:param path: the path to test
102+
:return: True, if the path was found in the archive file and is a path, False otherwise
103+
:raises: BundleAwareException of the appropriate type: if an error occurs
104+
"""
105+
_method_name = 'contains_path'
106+
107+
self.__logger.entering(path, class_name=self.__class_name, method_name=_method_name)
108+
try:
109+
result = self.__archive_file.containsPath(path)
110+
except (IllegalArgumentException, WLSDeployArchiveIOException), e:
111+
ex = exception_helper.create_exception(self.__exception_type, "WLSDPLY-19308", path,
112+
self.__archive_file_name, e.getLocalizedMessage(), error=e)
113+
self.__logger.throwing(ex, class_name=self.__class_name, method_name=_method_name)
114+
raise ex
115+
self.__logger.exiting(class_name=self.__class_name, method_name=_method_name, result=result)
116+
return result
117+
118+
def contains_file_or_path(self, path):
119+
"""
120+
Check that the provided path is a file or path contained inside the archive file
121+
:param path: the path to test
122+
:return: True, if the path was found in the archive file. False otherwise
123+
:raises: BundleAwareException of the appropriate type: if an error occurs
124+
"""
125+
_method_name = 'contains_file_or_path'
126+
127+
self.__logger.entering(path, class_name=self.__class_name, method_name=_method_name)
128+
try:
129+
result = self.__archive_file.containsFileOrPath(path)
130+
except (IllegalArgumentException, WLSDeployArchiveIOException), e:
131+
ex = exception_helper.create_exception(self.__exception_type, "WLSDPLY-19309", path,
132+
self.__archive_file_name, e.getLocalizedMessage(), error=e)
133+
self.__logger.throwing(ex, class_name=self.__class_name, method_name=_method_name)
134+
raise ex
135+
self.__logger.exiting(class_name=self.__class_name, method_name=_method_name, result=result)
136+
return result
137+
98138
def extract_file(self, path, location=None):
99139
"""
100140
Extract the specified file from the archive into the Domain Home directory.

core/src/main/python/wlsdeploy/tool/validate/validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ def __validate_single_path_in_archive(self, path, attribute_name, model_folder_p
890890
#
891891
if WLSDeployArchive.isPathIntoArchive(path):
892892
if self._archive_helper is not None:
893-
archive_has_file = self._archive_helper.contains_file(path)
893+
archive_has_file = self._archive_helper.contains_file_or_path(path)
894894
if not archive_has_file:
895895
validation_result.add_error('WLSDPLY-05024', attribute_name, model_folder_path,
896896
path, self._archive_file_name)

core/src/test/java/oracle/weblogic/deploy/util/WLSDeployArchiveTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ public class WLSDeployArchiveTest {
2222
private static final String APPS_ARCHIVE_FILE_NAME = "target/unit-tests/appsArchive.zip";
2323
private static final String APP1_TO_ADD = "src/test/resources/my-app.war";
2424
private static final String APP2_TO_ADD = "src/test/resources/my-other-app.war";
25+
private static final String APP_DIR_TO_ADD = "src/test/resources/my-app/";
2526
private static final String APP1_ENTRY_NAME1 = "wlsdeploy/applications/my-app.war";
2627
private static final String APP1_ENTRY_NAME2 = "wlsdeploy/applications/my-app(1).war";
2728
private static final String APP1_ENTRY_NAME3 = "wlsdeploy/applications/my-app(2).war";
2829
private static final String APP1_ENTRY_NAME4 = "wlsdeploy/applications/my-app(3).war";
2930
private static final String APP2_ENTRY_NAME1 = "wlsdeploy/applications/my-other-app.war";
3031
private static final String APP2_ENTRY_NAME2 = "wlsdeploy/applications/my-other-app(1).war";
3132
private static final String APP2_ENTRY_NAME3 = "wlsdeploy/applications/my-other-app(2).war";
33+
private static final String INVALID_ENTRY_NAME = "wlsdeploy/applications/does-not-exist.war";
34+
private static final String APP_DIR_ENTRY_NAME = "wlsdeploy/applications/my-app/";
3235

3336
private static final String ZIP_FILE_EXISTING_EMPTY_FILE = "my-empty-zip.zip";
3437
private static final String ZIP_FILE_EXISTING_BINARIES_FILE = "DiscoveredDemoDomain.zip";
@@ -88,6 +91,29 @@ public void testAddRemoveModelWithEmptyZip() throws Exception {
8891
archive.close();
8992
}
9093

94+
@Test
95+
public void testAddDirectory() throws Exception {
96+
WLSDeployArchive archive = new WLSDeployArchive(APPS_ARCHIVE_FILE_NAME);
97+
String appName = archive.addApplication(new File(APP_DIR_TO_ADD));
98+
Assert.assertEquals("unexpected app name: " + appName, APP_DIR_ENTRY_NAME, appName);
99+
archive.close();
100+
}
101+
102+
@Test
103+
public void testIsAFile() throws Exception {
104+
WLSDeployArchive archive = new WLSDeployArchive(APPS_ARCHIVE_FILE_NAME);
105+
archive.addApplication(new File(APP1_TO_ADD));
106+
archive.addApplication(new File(APP_DIR_TO_ADD));
107+
Assert.assertTrue("File not found in archive: " + APP1_ENTRY_NAME1, archive.containsFile(APP1_ENTRY_NAME1));
108+
//Assert.assertTrue("Path not found in archive: " + APP_DIR_ENTRY_NAME, archive.containsPath(APP_DIR_ENTRY_NAME));
109+
Assert.assertTrue("File not found in archive: " + APP1_ENTRY_NAME1, archive.containsFileOrPath(APP1_ENTRY_NAME1));
110+
Assert.assertTrue("Path not found in archive: " + APP_DIR_ENTRY_NAME, archive.containsFileOrPath(APP_DIR_ENTRY_NAME));
111+
Assert.assertFalse("Is not a File", archive.containsFile(APP_DIR_ENTRY_NAME));
112+
Assert.assertFalse("Is not a Path", archive.containsPath(APP1_ENTRY_NAME1));
113+
Assert.assertFalse("File should not exist", archive.containsFileOrPath(INVALID_ENTRY_NAME));
114+
archive.close();
115+
}
116+
91117
@Test
92118
public void testClearAllBinariesWithEmptyZip() throws Exception {
93119
WLSDeployZipFileTest.copyFile(ZIP_FILE_EXISTING_BINARIES_FILE);

0 commit comments

Comments
 (0)