From 2457ebae6cd903d5fbcbb50da8b9ee287a50c33a Mon Sep 17 00:00:00 2001 From: Tim203 Date: Tue, 24 Nov 2020 12:58:39 +0100 Subject: [PATCH] Added an User-Agent and removed a custom exception which wasn't needed --- .../geysermc/floodgate/skin/SkinUploader.java | 3 +- .../util/FloodgateHttpException.java | 42 ---------- .../geysermc/floodgate/util/HttpUtils.java | 77 +++++++------------ 3 files changed, 28 insertions(+), 94 deletions(-) delete mode 100644 common/src/main/java/org/geysermc/floodgate/util/FloodgateHttpException.java diff --git a/common/src/main/java/org/geysermc/floodgate/skin/SkinUploader.java b/common/src/main/java/org/geysermc/floodgate/skin/SkinUploader.java index a3c56bc7..7e19d4bc 100644 --- a/common/src/main/java/org/geysermc/floodgate/skin/SkinUploader.java +++ b/common/src/main/java/org/geysermc/floodgate/skin/SkinUploader.java @@ -33,7 +33,6 @@ import java.util.concurrent.Executors; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; -import org.geysermc.floodgate.util.FloodgateHttpException; import org.geysermc.floodgate.util.HttpUtils; import org.geysermc.floodgate.util.HttpUtils.HttpPostResponse; import org.geysermc.floodgate.util.RawSkin; @@ -73,7 +72,7 @@ public final class SkinUploader { uploadSkinInner(rawSkin, times); } return result; - } catch (FloodgateHttpException exception) { + } catch (RuntimeException exception) { return UploadResult.exception(exception); } } diff --git a/common/src/main/java/org/geysermc/floodgate/util/FloodgateHttpException.java b/common/src/main/java/org/geysermc/floodgate/util/FloodgateHttpException.java deleted file mode 100644 index 456ac020..00000000 --- a/common/src/main/java/org/geysermc/floodgate/util/FloodgateHttpException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2019-2020 GeyserMC. http://geysermc.org - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * @author GeyserMC - * @link https://github.com/GeyserMC/Floodgate - */ - -package org.geysermc.floodgate.util; - -import lombok.Getter; - -@Getter -public class FloodgateHttpException extends Exception { - private final int httpCode; - - public FloodgateHttpException(String message, Throwable exception, int httpCode) { - super(message, exception); - this.httpCode = httpCode; - } - - public FloodgateHttpException(String message, Throwable exception) { - this(message, exception, -1); - } -} diff --git a/common/src/main/java/org/geysermc/floodgate/util/HttpUtils.java b/common/src/main/java/org/geysermc/floodgate/util/HttpUtils.java index 679d207c..03e0c3ab 100644 --- a/common/src/main/java/org/geysermc/floodgate/util/HttpUtils.java +++ b/common/src/main/java/org/geysermc/floodgate/util/HttpUtils.java @@ -30,7 +30,6 @@ import com.google.gson.JsonObject; import java.awt.image.BufferedImage; import java.io.DataOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; @@ -48,73 +47,52 @@ public class HttpUtils { private static final String BOUNDARY = "******"; private static final String END = "\r\n"; - public static HttpPostResponse post(String urlString, BufferedImage... images) - throws FloodgateHttpException { - + public static HttpPostResponse post(String urlString, BufferedImage... images) { HttpURLConnection connection; try { URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); - } catch (Exception e) { - throw new FloodgateHttpException("Failed to create connection", e); + } catch (Exception exception) { + throw new RuntimeException("Failed to create connection", exception); } - OutputStream outputStream = null; - DataOutputStream dataOutputStream = null; + DataOutputStream outputStream = null; try { - connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); + connection.setRequestProperty("User-Agent", USER_AGENT); connection.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + BOUNDARY ); - outputStream = connection.getOutputStream(); - dataOutputStream = new DataOutputStream(outputStream); - writeDataFor(dataOutputStream, images); - } catch (Exception e) { - try { - outputStream.close(); - dataOutputStream.close(); - } catch (Exception ignored) { - } - throw new FloodgateHttpException("Failed to create request", e); - } - - int responseCode = -1; - try { - responseCode = connection.getResponseCode(); - } catch (Exception ignored) { - } - - InputStream inputStream = null; - InputStreamReader inputStreamReader = null; - - try { - inputStream = connection.getInputStream(); - inputStreamReader = new InputStreamReader(inputStream); - - JsonObject response = GSON.fromJson(inputStreamReader, JsonObject.class); - - inputStreamReader.close(); - inputStream.close(); - - dataOutputStream.close(); - outputStream.close(); - - return new HttpPostResponse(responseCode, response); - } catch (Exception e) { - throw new FloodgateHttpException("Failed to read response", e, responseCode); + outputStream = new DataOutputStream(connection.getOutputStream()); + writeDataFor(outputStream, images); + } catch (Exception exception) { + throw new RuntimeException("Failed to create request", exception); } finally { try { outputStream.close(); - dataOutputStream.close(); + } catch (Exception ignored) { + } + } + InputStreamReader inputStream = null; + + try { + inputStream = new InputStreamReader(connection.getInputStream()); + int responseCode = connection.getResponseCode(); + + JsonObject response = GSON.fromJson(inputStream, JsonObject.class); + + return new HttpPostResponse(responseCode, response); + } catch (Exception exception) { + throw new RuntimeException("Failed to read response", exception); + } finally { + try { inputStream.close(); - inputStreamReader.close(); } catch (Exception ignored) { } } @@ -132,9 +110,8 @@ public class HttpUtils { outputStream.writeBytes(END); } outputStream.writeBytes(CONNECTION_STRING + BOUNDARY + CONNECTION_STRING + END); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); + } catch (Exception exception) { + throw new RuntimeException(exception); } }