Skip to content

Commit

Permalink
1.1.7: Added User-Agent for connections
Browse files Browse the repository at this point in the history
- Added 'User-Agent' header property for HTTP connections
- Added 'version' comments in 'messages.yml'
  • Loading branch information
srnyx committed Dec 9, 2022
1 parent 90add51 commit 2bfb547
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
description = "AnnoyingAPI"
version = "1.1.6"
version = "1.1.7"
group = "xyz.srnyx"

repositories {
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
Expand All @@ -34,6 +33,7 @@
*/
public class AnnoyingDownload {
@NotNull private final AnnoyingPlugin plugin;
@NotNull private final String userAgent;
@NotNull private final List<AnnoyingDependency> dependencies;
private int remaining = 0;

Expand All @@ -46,6 +46,7 @@ public class AnnoyingDownload {
@Contract(pure = true)
public AnnoyingDownload(@NotNull AnnoyingPlugin plugin, @NotNull List<AnnoyingDependency> dependencies) {
this.plugin = plugin;
this.userAgent = plugin.getName() + "/" + plugin.getDescription().getVersion() + " via AnnoyingAPI";
this.dependencies = dependencies;
}

Expand Down Expand Up @@ -193,9 +194,9 @@ private JsonElement getJson(@NotNull String urlString) {
final HttpURLConnection connection;
final JsonElement json;
try {
final URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", userAgent);
if (connection.getResponseCode() == 404) return null;
json = new JsonParser().parse(new InputStreamReader(connection.getInputStream()));
} catch (final IOException e) {
Expand All @@ -213,18 +214,21 @@ private JsonElement getJson(@NotNull String urlString) {
* @param urlString the URL of the file
*/
private void downloadFile(@NotNull AnnoyingDependency dependency, @NotNull Platform platform, @NotNull String urlString) {
// Get URL
final URL url;
// Get URL connection
final HttpURLConnection connection;
try {
url = new URL(urlString);
} catch (final MalformedURLException e) {
connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", userAgent);
} catch (final IOException e) {
e.printStackTrace();
dependency.platforms.remove(platform);
attemptDownload(dependency);
return;
}

// Download file
try (final BufferedInputStream in = new BufferedInputStream(url.openStream());
try (final BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
final FileOutputStream out = new FileOutputStream(dependency.getFile())) {
final byte[] buffer = new byte[1024];
int numRead;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ error:
# Command is used with invalid/incorrect arguments
invalid-arguments: "%prefix%&cInvalid arguments!@@&c%command%@@%command%"

version: "%prefix%You are running AnnoyingAPI &3v%version%@@&b%command%@@%command%"
# Message sent when using /annoying version
version: "%prefix%You are running AnnoyingAPI &3v%version%@@&b%command%@@%command%" # %version%

0 comments on commit 2bfb547

Please sign in to comment.