Skip to content

Commit

Permalink
Merge branch 'develop' into 1.4.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/build.yml
#	build.gradle
#	src/main/java/com/github/alexdlaird/ngrok/NgrokClient.java
#	src/main/java/com/github/alexdlaird/ngrok/installer/NgrokInstaller.java
  • Loading branch information
alexdlaird committed Mar 7, 2024
2 parents 3da8e5b + 896dea6 commit 7c295ac
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 74 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ docs:
$(GRADLE_BIN) javadoc

check:
$(GRADLE_BIN) checkstyleMain checkstyleTest spotbugsMain spotbugsTest
$(GRADLE_BIN) checkstyleMain spotbugsMain

local:
$(GRADLE_BIN) publishToMavenLocal
Expand All @@ -38,7 +38,6 @@ validate-release:
@if [[ "${VERSION}" == "" ]]; then echo "VERSION is not set" & exit 1 ; fi

@if [[ $$(grep "version \"${VERSION}\"" build.gradle) == "" ]] ; then echo "Version not bumped in build.gradle" & exit 1 ; fi
@if [[ $$(grep "VERSION = \"${VERSION}\"" src/main/java/com/github/alexdlaird/ngrok/NgrokClient.java) == "" ]] ; then echo "Version not bumped in NgrokClient.java" & exit 1 ; fi

test-downstream:
@if [[ "${VERSION}" == "" ]]; then echo "VERSION is not set" & exit 1 ; fi
Expand Down
19 changes: 16 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ dependencies {
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.2"
}

tasks.register("createProperties", WriteProperties) {
outputFile new File(projectDir, "src/main/resources/version.properties")
property "version", version
finalizedBy sourcesJar
}
processResources.dependsOn createProperties

test {
useJUnitPlatform()
finalizedBy jacocoTestReport
Expand All @@ -47,22 +54,28 @@ jacocoTestReport {
}
}

checkstyle {
ignoreFailures = false
maxWarnings = 0
}

spotbugs {
ignoreFailures = true
excludeFilter = file("config/spotbugs/exclude.xml")
}

spotbugsMain {
reports {
html {
enabled = true
required = true
}
}
}

spotbugsTest {
reports {
html {
enabled = true
required = true
}
}
}
Expand Down Expand Up @@ -126,5 +139,5 @@ signing {

javadoc {
options.overview = "src/main/java/overview.html"
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption("Xdoclint:none", "-quiet")
}
4 changes: 2 additions & 2 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
value="Type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="MemberName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="format" value="^[a-z][a-zA-Z0-9_]*[a-zA-Z0-9]+$"/>
<message key="name.invalidPattern"
value="Member name ''{0}'' must match pattern ''{1}''."/>
</module>
Expand Down Expand Up @@ -246,7 +246,7 @@
</module>
<module name="AbbreviationAsWordInName">
<property name="ignoreFinal" value="false"/>
<property name="allowedAbbreviationLength" value="0"/>
<property name="allowedAbbreviationLength" value="4"/>
<property name="tokens"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF,
PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, PATTERN_VARIABLE_DEF, RECORD_DEF,
Expand Down
22 changes: 22 additions & 0 deletions config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<FindBugsFilter>
<Match>
<Package name="com.github.alexdlaird.ngrok.protocol"/>
<Bug pattern="UWF_UNWRITTEN_FIELD"/>
</Match>
<Match>
<Package name="com.github.alexdlaird.ngrok.protocol"/>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
<Match>
<Package name="com.github.alexdlaird.ngrok.protocol"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
<Match>
<Class name="com.github.alexdlaird.http.DefaultHttpClient"/>
<Bug pattern="DCN_NULLPOINTER_EXCEPTION"/>
</Match>
<Match>
<Class name="com.github.alexdlaird.ngrok.installer.NgrokInstaller"/>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Root exception for the {@link NgrokClient} and the <code>java-ngrok</code> library.
*/
public class JavaNgrokException extends RuntimeException {

/**
* An exception with a message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Thrown from {@link NgrokClient} when an error occurs making a request to the <code>ngrok</code> web interface.
*/
public class JavaNgrokHTTPException extends JavaNgrokException {

private final String url;
private final int statusCode;
private final String body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* binary.
*/
public class JavaNgrokInstallerException extends JavaNgrokException {

/**
* An exception with a message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Thrown from {@link NgrokProcess} when a security error occurs.
*/
public class JavaNgrokSecurityException extends JavaNgrokException {

/**
* An exception with a message.
*
Expand Down
44 changes: 15 additions & 29 deletions src/main/java/com/github/alexdlaird/http/DefaultHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -69,8 +68,8 @@ private DefaultHttpClient(final Builder builder) {
this.timeout = builder.timeout;
this.retryCount = builder.retryCount;
this.gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
}

@Override
Expand All @@ -80,7 +79,7 @@ public <B> Response<B> get(final String url,
final Class<B> clazz) {
try {
return execute(urlWithParameters(url, parameters), null, "GET",
additionalHeaders, clazz);
additionalHeaders, clazz);
} catch (final IOException e) {
throw new HttpClientException("HTTP GET error", e);
}
Expand All @@ -93,13 +92,13 @@ public void get(final String url,
final Path dest,
final int retries) throws InterruptedException {
HttpURLConnection httpUrlConnection = null;
InputStream inputStream = null;

try {
httpUrlConnection = createHttpUrlConnection(urlWithParameters(url, parameters));

inputStream = getInputStream(httpUrlConnection, null, "GET", additionalHeaders);
Files.copy(inputStream, dest, StandardCopyOption.REPLACE_EXISTING);
try (final InputStream inputStream = getInputStream(httpUrlConnection, null, "GET", additionalHeaders)) {
Files.copy(inputStream, dest, StandardCopyOption.REPLACE_EXISTING);
}
} catch (final Exception ex) {
if (retries < retryCount) {
LOGGER.warning("GET failed, retrying in 0.5 seconds ...");
Expand Down Expand Up @@ -132,13 +131,6 @@ public void get(final String url,
if (nonNull(httpUrlConnection)) {
httpUrlConnection.disconnect();
}
try {
if (nonNull(inputStream)) {
inputStream.close();
}
} catch (final IOException ex) {
LOGGER.log(Level.INFO, "Unable to close connection", ex);
}
}
}

Expand All @@ -150,7 +142,7 @@ public <R, B> Response<B> post(final String url,
final Class<B> clazz) {
try {
return execute(urlWithParameters(url, parameters), convertRequestToString(request), "POST",
additionalHeaders, clazz);
additionalHeaders, clazz);
} catch (final IOException e) {
throw new HttpClientException("HTTP POST error", e);
}
Expand All @@ -164,7 +156,7 @@ public <R, B> Response<B> put(final String url,
final Class<B> clazz) {
try {
return execute(urlWithParameters(url, parameters), convertRequestToString(request), "PUT",
additionalHeaders, clazz);
additionalHeaders, clazz);
} catch (final IOException e) {
throw new HttpClientException("HTTP PUT error", e);
}
Expand All @@ -177,7 +169,7 @@ public <B> Response<B> delete(final String url,
final Class<B> clazz) {
try {
return execute(urlWithParameters(url, parameters), null, "DELETE",
additionalHeaders, clazz);
additionalHeaders, clazz);
} catch (final IOException e) {
throw new HttpClientException("HTTP DELETE error", e);
}
Expand Down Expand Up @@ -226,7 +218,7 @@ private <T> T convertResponseFromString(final String response, final Class<T> cl

protected String urlWithParameters(final String url,
final List<Parameter> parameters)
throws UnsupportedEncodingException {
throws UnsupportedEncodingException {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(url);

Expand Down Expand Up @@ -282,18 +274,18 @@ private <B> Response<B> execute(final String url,
final Map<String, String> additionalHeaders,
final Class<B> clazz) throws IOException {
HttpURLConnection httpUrlConnection = null;
InputStream inputStream = null;

try {
httpUrlConnection = createHttpUrlConnection(url);

inputStream = getInputStream(httpUrlConnection, body, method, additionalHeaders);
final String responseBody = StringUtils.streamToString(inputStream, Charset.forName(encoding));
try (final InputStream inputStream = getInputStream(httpUrlConnection, body, method, additionalHeaders)) {
final String responseBody = StringUtils.streamToString(inputStream, Charset.forName(encoding));

return new Response<>(httpUrlConnection.getResponseCode(),
return new Response<>(httpUrlConnection.getResponseCode(),
convertResponseFromString(responseBody, clazz),
responseBody,
httpUrlConnection.getHeaderFields());
}
} catch (final Exception ex) {
String msg = "An unknown error occurred when performing the operation";

Expand All @@ -317,20 +309,14 @@ private <B> Response<B> execute(final String url,
if (nonNull(httpUrlConnection)) {
httpUrlConnection.disconnect();
}
try {
if (nonNull(inputStream)) {
inputStream.close();
}
} catch (final IOException ex) {
LOGGER.log(Level.INFO, "Unable to close connection", ex);
}
}
}

/**
* Builder for a {@link DefaultHttpClient}, see docs for that class for example usage.
*/
public static class Builder {

private String encoding = "UTF-8";
private String contentType = "application/json";
public int timeout = 4000;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/github/alexdlaird/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.github.alexdlaird.http;

import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand All @@ -32,7 +33,7 @@ public Response(final int statusCode, final T body, final String bodyRaw,
this.statusCode = statusCode;
this.body = body;
this.bodyRaw = bodyRaw;
this.headerFields = headerFields;
this.headerFields = Collections.unmodifiableMap(headerFields);
}

/**
Expand Down
Loading

0 comments on commit 7c295ac

Please sign in to comment.