Skip to content

Commit 51fcff8

Browse files
authored
Merge pull request #78 from ramindu90/master
Fix bugs including giving non existing paths as destination
2 parents b78d84d + 7a73bc1 commit 51fcff8

File tree

16 files changed

+138
-64
lines changed

16 files changed

+138
-64
lines changed

component/pom.xml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@
8080
<groupId>commons-io</groupId>
8181
<artifactId>commons-io</artifactId>
8282
</dependency>
83+
<dependency>
84+
<groupId>org.apache.commons</groupId>
85+
<artifactId>commons-compress</artifactId>
86+
</dependency>
8387
<dependency>
8488
<groupId>org.testng</groupId>
8589
<artifactId>testng</artifactId>
8690
<scope>test</scope>
8791
</dependency>
88-
<dependency>
89-
<groupId>org.apache.commons</groupId>
90-
<artifactId>commons-compress</artifactId>
91-
</dependency>
9292
<dependency>
9393
<groupId>io.siddhi.extension.execution.list</groupId>
9494
<artifactId>siddhi-execution-list</artifactId>
95+
<scope>test</scope>
9596
</dependency>
9697
</dependencies>
9798
<profiles>
@@ -146,16 +147,20 @@
146147
org.wso2.transport.remotefilesystem.*,
147148
org.wso2.carbon.messaging.*,
148149
org.apache.commons.vfs2.*,
150+
org.apache.commons.compress.*,
149151
org.wso2.transport.file.*,
150152
org.apache.commons.net.*,
151-
org.quartz.*
153+
org.quartz.*,
154+
io.siddhi.extension.util.*
152155
</Private-Package>
153156
<Export-Package>
154157
io.siddhi.extension.io.file.*,
158+
io.siddhi.extension.execution.file.*,
155159
!org.wso2.carbon.connector.framework.*,
156160
!org.wso2.transport.remotefilesystem.*,
157161
!org.wso2.carbon.messaging.*,
158162
!org.apache.commons.vfs2.*,
163+
!org.apache.commons.compress.*,
159164
!org.wso2.transport.file.*,
160165
!org.apache.commons.net.*,
161166
!org.quartz.*

component/src/main/java/io/siddhi/extension/execution/file/FileArchiveExtension.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
import io.siddhi.core.query.processor.stream.function.StreamFunctionProcessor;
3131
import io.siddhi.core.util.config.ConfigReader;
3232
import io.siddhi.core.util.snapshot.state.StateFactory;
33+
import io.siddhi.extension.util.Utils;
3334
import io.siddhi.query.api.definition.AbstractDefinition;
3435
import io.siddhi.query.api.definition.Attribute;
3536
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
3637
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
3738
import org.apache.commons.io.IOUtils;
39+
import org.apache.commons.vfs2.FileObject;
40+
import org.apache.commons.vfs2.FileSystemException;
3841
import org.apache.log4j.Logger;
3942

4043
import java.io.BufferedInputStream;
@@ -63,12 +66,14 @@
6366
@Parameter(
6467
name = "uri",
6568
description = "Absolute path of the file or the directory",
66-
type = DataType.STRING
69+
type = DataType.STRING,
70+
dynamic = true
6771
),
6872
@Parameter(
6973
name = "destination.dir.uri",
7074
description = "Absolute directory path of the the archived file.",
71-
type = DataType.STRING
75+
type = DataType.STRING,
76+
dynamic = true
7277
),
7378
@Parameter(
7479
name = "archive.type",
@@ -168,6 +173,9 @@ public ProcessingMode getProcessingMode() {
168173
protected Object[] process(Object[] data) {
169174
String uri = (String) data[0];
170175
String destinationDirUri = (String) data[1];
176+
if (destinationDirUri.lastIndexOf(File.separator) != destinationDirUri.length() - 1) {
177+
destinationDirUri = destinationDirUri + File.separator;
178+
}
171179
boolean excludeSubdirectories = false;
172180
String regex = "";
173181
String archiveType = ZIP_FILE_EXTENSION;
@@ -183,20 +191,31 @@ protected Object[] process(Object[] data) {
183191
if (pattern == null) {
184192
pattern = Pattern.compile(regex);
185193
}
194+
FileObject destinationDirUriObject = Utils.getFileObject(destinationDirUri);
195+
try {
196+
if (!destinationDirUriObject.exists() || !destinationDirUriObject.isFolder()) {
197+
destinationDirUriObject.createFolder();
198+
}
199+
} catch (FileSystemException e) {
200+
throw new SiddhiAppRuntimeException(
201+
"Exception occurred when creating the subdirectories for the destination directory " +
202+
destinationDirUriObject.getName().getPath(), e);
203+
}
204+
File sourceFile = new File(uri);
205+
String destinationFile = destinationDirUri + sourceFile.getName();
186206
if (archiveType.compareToIgnoreCase(ZIP_FILE_EXTENSION) == 0) {
187-
File sourceFile = new File(uri);
188207
List<String> fileList = new ArrayList<>();
189208
generateFileList(uri, sourceFile, fileList, excludeSubdirectories);
190209
try {
191-
zip(uri, destinationDirUri, fileList);
210+
zip(uri, destinationFile, fileList);
192211
} catch (IOException e) {
193212
throw new SiddhiAppRuntimeException("IOException occurred when archiving " + uri, e);
194213
}
195214
} else {
196215
try {
197216
if (archiveType.compareToIgnoreCase(TAR_FILE_EXTENSION) == 0) {
198217
addToTarArchiveCompression(
199-
getTarArchiveOutputStream(destinationDirUri), new File(uri), uri);
218+
getTarArchiveOutputStream(destinationFile), sourceFile, uri);
200219
} else {
201220
throw new SiddhiAppRuntimeException("Unsupported archive type: " + archiveType);
202221
}
@@ -233,7 +252,7 @@ private void zip(String sourceFileUri, String zipFile, List<String> fileList) th
233252
ZipOutputStream zos = null;
234253
FileOutputStream fos = null;
235254
try {
236-
if (!zipFile.endsWith(TAR_FILE_EXTENSION)) {
255+
if (!zipFile.endsWith(ZIP_FILE_EXTENSION)) {
237256
zipFile = zipFile.concat("." + ZIP_FILE_EXTENSION);
238257
}
239258
fos = new FileOutputStream(zipFile);

component/src/main/java/io/siddhi/extension/execution/file/FileCopyExtension.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@
5555
@Parameter(
5656
name = "uri",
5757
description = "Absolute path of the File or the directory.",
58-
type = DataType.STRING
58+
type = DataType.STRING,
59+
dynamic = true
5960
),
6061
@Parameter(
6162
name = "destination.dir.uri",
6263
description = "Absolute path of the destination directory.\n" +
6364
"Note: Parent folder structure will be created if it does not exist.",
64-
type = DataType.STRING
65+
type = DataType.STRING,
66+
dynamic = true
6567
),
6668
@Parameter(
6769
name = "include.by.regexp",

component/src/main/java/io/siddhi/extension/execution/file/FileCreateExtension.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@
4949
@Parameter(
5050
name = "uri",
5151
description = "Absolute file path which needs to be created.",
52-
type = DataType.STRING
52+
type = DataType.STRING,
53+
dynamic = true
5354
),
5455
@Parameter(
5556
name = "is.directory",
5657
description = "This flag is used when creating file path is a directory",
57-
type = DataType.STRING
58+
type = DataType.STRING,
59+
dynamic = true
5860
)
5961
},
6062
examples = {

component/src/main/java/io/siddhi/extension/execution/file/FileDeleteExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
@Parameter(
5151
name = "uri",
5252
description = "Absolute path of the file or the directory to be deleted.",
53-
type = DataType.STRING
53+
type = DataType.STRING,
54+
dynamic = true
5455
)
5556
},
5657
examples = {

component/src/main/java/io/siddhi/extension/execution/file/FileExistsExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
@Parameter(
4848
name = "uri",
4949
description = "File path to check for existence.",
50-
type = DataType.STRING
50+
type = DataType.STRING,
51+
dynamic = true
5152
)
5253
},
5354
returnAttributes = {

component/src/main/java/io/siddhi/extension/execution/file/FileIsDirectoryExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
@Parameter(
4848
name = "uri",
4949
description = "The path to be checked for a directory.",
50-
type = DataType.STRING
50+
type = DataType.STRING,
51+
dynamic = true
5152
)
5253
},
5354
returnAttributes = {

component/src/main/java/io/siddhi/extension/execution/file/FileIsFileExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
@Parameter(
4848
name = "file.path",
4949
description = "The path to be checked for a file.",
50-
type = DataType.STRING
50+
type = DataType.STRING,
51+
dynamic = true
5152
)
5253
},
5354
returnAttributes = {

component/src/main/java/io/siddhi/extension/execution/file/FileLastModifiedTimeExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
@Parameter(
5454
name = "uri",
5555
description = "File path to be checked for te last modified time.",
56-
type = DataType.STRING
56+
type = DataType.STRING,
57+
dynamic = true
5758
),
5859
@Parameter(
5960
name = "datetime.format",

component/src/main/java/io/siddhi/extension/execution/file/FileMoveExtension.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@
5454
@Parameter(
5555
name = "uri",
5656
description = "Absolute file or directory path.",
57-
type = DataType.STRING
57+
type = DataType.STRING,
58+
dynamic = true
5859
),
5960
@Parameter(
6061
name = "destination.dir.uri",
6162
description = "Absolute file path to the destination directory.\n" +
6263
"Note: Parent folder structure will be created if it does not exist.",
63-
type = DataType.STRING
64+
type = DataType.STRING,
65+
dynamic = true
6466
),
6567
@Parameter(
6668
name = "include.by.regexp",

0 commit comments

Comments
 (0)