Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Revert version 1.1.5" #91

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Add this dependency to your project's POM:
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<packaging>jar</packaging>
<version>1.1.4-SNAPSHOT</version>
<version>1.1.5</version>

<name>browserstack-local-java</name>
<description>Java bindings for BrowserStack Local</description>
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/browserstack/local/Local.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Local {
private LocalProcess proc = null;

// Current version of binding package, used for --source option of binary
private final String packageVersion = "1.1.4";
private static final String packageVersion = "1.1.5";
private final Map<String, String> parameters;
private final Map<String, String> avoidValueParameters;

Expand Down Expand Up @@ -130,6 +130,15 @@ public boolean isRunning() throws Exception {
return isProcessRunning(pid);
}

/**
* Returns the package version
*
* @return {String} package version
*/
public static String getPackageVersion() {
return packageVersion;
}

/**
* Creates a list of command-line arguments for the Local instance
*
Expand Down
46 changes: 44 additions & 2 deletions src/main/java/com/browserstack/local/LocalBinary.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.browserstack.local;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;

class LocalBinary {

Expand Down Expand Up @@ -58,7 +65,8 @@ private void initialize() throws LocalException {
throw new LocalException("Failed to detect OS type");
}

httpPath = BIN_URL + binFileName;
String sourceURL = BIN_URL;
httpPath = sourceURL + binFileName;
}

private boolean isAlpine() {
Expand Down Expand Up @@ -174,7 +182,7 @@ private void downloadBinary(String destParentDir, Boolean custom) throws LocalEx
URL url = new URL(httpPath);

File f = new File(source);
FileUtils.copyURLToFile(url, f);
newCopyToFile(url, f);

changePermissions(binaryPath);
} catch (Exception e) {
Expand All @@ -192,4 +200,38 @@ private void changePermissions(String path) {
public String getBinaryPath() {
return binaryPath;
}

private static void newCopyToFile(URL url, File f) throws IOException {
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "browserstack-local-java/" + Local.getPackageVersion());
conn.setRequestProperty("Accept-Encoding", "gzip, *");
String contentEncoding = conn.getContentEncoding();

if (contentEncoding == null || !contentEncoding.toLowerCase().contains("gzip")) {
customCopyInputStreamToFile(conn.getInputStream(), f, url);
return;
}

try (InputStream stream = new GZIPInputStream(conn.getInputStream())) {
if (System.getenv().containsKey("BROWSERSTACK_LOCAL_DEBUG_GZIP")) {
System.out.println("using gzip in " + conn.getRequestProperty("User-Agent"));
}

customCopyInputStreamToFile(stream, f, url);
} catch (ZipException e) {
FileUtils.copyURLToFile(url, f);
}
}

private static void customCopyInputStreamToFile(InputStream stream, File file, URL url) throws IOException {
try {
FileUtils.copyInputStreamToFile(stream, file);
} catch (Throwable e) {
try (FileOutputStream fos = new FileOutputStream(file)) {
IOUtils.copy(stream, fos);
} catch (Throwable th) {
FileUtils.copyURLToFile(url, file);
}
}
}
}
Loading