1
0
mirror of https://github.com/GeyserMC/Floodgate.git synced 2025-12-30 12:19:22 +00:00

Added an User-Agent and removed a custom exception which wasn't needed

This commit is contained in:
Tim203
2020-11-24 12:58:39 +01:00
parent 885fd16972
commit 2457ebae6c
3 changed files with 28 additions and 94 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}