Skip to content

Commit d390722

Browse files
committed
#34 Add initial support for wires (Marionette).
Refactor Extract file from archive code
1 parent cfd3460 commit d390722

File tree

13 files changed

+449
-216
lines changed

13 files changed

+449
-216
lines changed

pom.xml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,53 +87,53 @@
8787
<dependency>
8888
<groupId>org.apache.maven</groupId>
8989
<artifactId>maven-core</artifactId>
90-
<version>3.2.1</version>
90+
<version>3.3.9</version>
9191
</dependency>
9292
<dependency>
9393
<groupId>org.apache.maven</groupId>
9494
<artifactId>maven-plugin-api</artifactId>
95-
<version>3.2.1</version>
95+
<version>3.3.9</version>
9696
</dependency>
9797
<dependency>
9898
<groupId>org.apache.maven.plugin-tools</groupId>
9999
<artifactId>maven-plugin-annotations</artifactId>
100-
<version>3.2</version>
100+
<version>3.4</version>
101101
</dependency>
102102
<dependency>
103103
<groupId>commons-io</groupId>
104104
<artifactId>commons-io</artifactId>
105105
<version>2.4</version>
106106
</dependency>
107-
<dependency>
108-
<groupId>org.apache.httpcomponents</groupId>
109-
<artifactId>httpclient</artifactId>
110-
<version>4.3.3</version>
111-
</dependency>
112107
<dependency>
113108
<groupId>commons-codec</groupId>
114109
<artifactId>commons-codec</artifactId>
115-
<version>1.6</version>
110+
<version>1.10</version>
116111
</dependency>
117112
<dependency>
118113
<groupId>org.apache.commons</groupId>
119114
<artifactId>commons-compress</artifactId>
120-
<version>1.5</version>
115+
<version>1.10</version>
121116
</dependency>
122117
<dependency>
123118
<groupId>log4j</groupId>
124119
<artifactId>log4j</artifactId>
125120
<version>1.2.17</version>
126121
</dependency>
127122
<dependency>
128-
<groupId>com.google.guava</groupId>
129-
<artifactId>guava</artifactId>
130-
<version>10.0.1</version>
123+
<groupId>org.apache.httpcomponents</groupId>
124+
<artifactId>httpclient</artifactId>
125+
<version>4.3.3</version>
131126
</dependency>
132127
<dependency>
133128
<groupId>org.apache.httpcomponents</groupId>
134129
<artifactId>httpcore</artifactId>
135130
<version>4.3.2</version>
136131
</dependency>
132+
<dependency>
133+
<groupId>com.google.guava</groupId>
134+
<artifactId>guava</artifactId>
135+
<version>10.0.1</version>
136+
</dependency>
137137
<!-- Test Dependancies -->
138138
<dependency>
139139
<groupId>org.hamcrest</groupId>
@@ -161,7 +161,7 @@
161161
<dependency>
162162
<groupId>com.github.stefanbirkner</groupId>
163163
<artifactId>system-rules</artifactId>
164-
<version>1.7.0</version>
164+
<version>1.16.0</version>
165165
<scope>test</scope>
166166
</dependency>
167167
</dependencies>

src/main/java/com/lazerycode/selenium/download/DownloadHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lazerycode.selenium.download;
22

3+
import com.lazerycode.selenium.extract.FileExtractor;
34
import com.lazerycode.selenium.hash.HashType;
45
import com.lazerycode.selenium.repository.DriverContext;
56
import com.lazerycode.selenium.repository.DriverDetails;
@@ -14,8 +15,6 @@
1415
import java.net.URISyntaxException;
1516
import java.net.URL;
1617

17-
import static com.lazerycode.selenium.extract.ExtractFilesFromArchive.extractFileFromArchive;
18-
1918
public class DownloadHandler {
2019

2120
private static final Logger LOG = Logger.getLogger(DownloadHandler.class);
@@ -51,13 +50,13 @@ private boolean checkFileHash(File fileToCheck, String hash, HashType hashType)
5150

5251
/**
5352
* Perform the file download
54-
* @param driverDetails Driver details extracted from Repositorymap.xml
55-
* @param shouldWeCheckFileHash true if file hash should be checked
5653
*
54+
* @param driverDetails Driver details extracted from Repositorymap.xml
55+
* @param shouldWeCheckFileHash true if file hash should be checked
5756
* @return File
5857
* @throws MojoExecutionException Unable to download file
59-
* @throws IOException Error writing to file system
60-
* @throws URISyntaxException Invalid URI
58+
* @throws IOException Error writing to file system
59+
* @throws URISyntaxException Invalid URI
6160
*/
6261
protected File downloadFile(DriverDetails driverDetails, boolean shouldWeCheckFileHash) throws MojoExecutionException, IOException, URISyntaxException {
6362

@@ -124,7 +123,8 @@ private void downloadAndExtractExecutableFiles(DriverContext driverContext, Driv
124123
}
125124

126125
String extractedFileLocation = this.rootStandaloneServerDirectory.getAbsolutePath() + File.separator + driverContext.buildExtractionPathFromDriverContext();
127-
driverDetails.extractedLocation = extractFileFromArchive(localZipFile, extractedFileLocation, this.overwriteFilesThatExist, driverContext.getBinaryTypeForContext());
126+
FileExtractor fileExtractor = new FileExtractor(this.overwriteFilesThatExist);
127+
driverDetails.extractedLocation = fileExtractor.extractFileFromArchive(localZipFile, extractedFileLocation, driverContext.getBinaryTypeForContext());
128128
}
129129

130130
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.lazerycode.selenium.exceptions;
2+
3+
import org.apache.maven.plugin.MojoFailureException;
4+
5+
public class ExpectedFileNotFoundException extends MojoFailureException {
6+
public ExpectedFileNotFoundException(String message) {
7+
super(message);
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.lazerycode.selenium.exceptions;
2+
3+
import org.apache.maven.plugin.MojoFailureException;
4+
5+
public class InvalidFileTypeException extends MojoFailureException {
6+
public InvalidFileTypeException(String message) {
7+
super(message);
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.lazerycode.selenium.extract;
2+
3+
public enum ArchiveType {
4+
GZ("gz"),
5+
BZ2("bz2"),
6+
ZIP("zip"),
7+
TAR("tar");
8+
9+
private final String archiveFileExtension;
10+
11+
ArchiveType(String archiveFileExtension) {
12+
this.archiveFileExtension = archiveFileExtension;
13+
}
14+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.lazerycode.selenium.extract;
2+
3+
import com.lazerycode.selenium.exceptions.InvalidFileTypeException;
4+
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
5+
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
6+
import org.apache.commons.io.FilenameUtils;
7+
import org.apache.log4j.Logger;
8+
9+
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
14+
import static com.lazerycode.selenium.extract.ArchiveType.BZ2;
15+
import static com.lazerycode.selenium.extract.ArchiveType.GZ;
16+
17+
public class CompressedFile {
18+
19+
private static final Logger LOG = Logger.getLogger(FileExtractor.class);
20+
File compressedFile;
21+
String decompressedFilename;
22+
ArchiveType filetype = null;
23+
24+
/**
25+
* A class that will take a compressed file.
26+
*
27+
* @param compressedFile A compressed file of type .gz or .bz2
28+
* @throws InvalidFileTypeException
29+
*/
30+
public CompressedFile(File compressedFile) throws InvalidFileTypeException {
31+
filetype = ArchiveType.valueOf(FilenameUtils.getExtension(compressedFile.getName()).toUpperCase());
32+
if (filetype != GZ && filetype != BZ2) {
33+
throw new InvalidFileTypeException(compressedFile.getName() + " is an archive, not a known compressed file type");
34+
}
35+
this.compressedFile = compressedFile;
36+
decompressedFilename = FilenameUtils.getBaseName(compressedFile.getName());
37+
}
38+
39+
/**
40+
* Get the filename of the decompressed file.
41+
*
42+
* @return The decompressed filename as a String.
43+
*/
44+
public String getDecompressedFilename() {
45+
return decompressedFilename;
46+
}
47+
48+
/**
49+
* Get an InputStream for the decompressed file.
50+
*
51+
* @return An InputStream, the type could be BZip2CompressorInputStream, or GzipCompressorInputStream depending on what type of file was initially supplied
52+
* @throws IOException
53+
*/
54+
public InputStream getInputStream() throws IOException {
55+
switch (filetype) {
56+
case GZ:
57+
LOG.debug("Decompressing .gz file");
58+
return new GzipCompressorInputStream(new FileInputStream(compressedFile));
59+
case BZ2:
60+
LOG.debug("Decompressing .bz2 file");
61+
return new BZip2CompressorInputStream(new FileInputStream(compressedFile));
62+
}
63+
return null;
64+
}
65+
66+
/**
67+
* Find out if the uncompressed file is an archive
68+
*
69+
* @return ArchiveType.TAR if a tar, or null if not an archive
70+
*/
71+
public ArchiveType getArchiveType() {
72+
try {
73+
return ArchiveType.valueOf(FilenameUtils.getExtension(decompressedFilename).toUpperCase());
74+
} catch (IllegalArgumentException e) {
75+
LOG.debug("Not a recognised Archive type");
76+
return null;
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)