[java] Support for sending firefox addon directory as temporary in remote sessions
import org.openqa.selenium.remote.ExecuteMethod;
import org.openqa.selenium.remote.http.HttpMethod;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.Base64;
import java.util.Map;
import java.util.function.Predicate;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
import static org.openqa.selenium.remote.Browser.FIREFOX;
@@ -77,7 +84,12 @@ public String installExtension(Path path, Boolean temporary) {
String encoded;
try {
+ if (Files.isDirectory(path)) {
+ encoded = Base64.getEncoder().encodeToString(Files.readAllBytes(zipDirectory(path)));
+ }
+ else {
encoded = Base64.getEncoder().encodeToString(Files.readAllBytes(path));
+ }
} catch (IOException e) {
throw new InvalidArgumentException(path + " is an invalid path", e);
}
@@ -87,6 +99,22 @@ public String installExtension(Path path, Boolean temporary) {
ImmutableMap.of("addon", encoded, "temporary", temporary));
}
+ private Path zipDirectory(Path path) throws IOException {
+ Path extZip = Paths.get(path.getFileName().toString()+".zip");
+ try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(extZip.toFile()))) {
+ Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ zos.putNextEntry(new ZipEntry(path.relativize(file).toString()));
+ Files.copy(file, zos);
+ zos.closeEntry();
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+ return extZip;
+ }
+
@Override
public void uninstallExtension(String extensionId) {
Require.nonNull("Extension ID", extensionId);
