2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2026-06-24 03:46:43 +00:00

Convert RedisBungee to okhttp.

This commit is contained in:
Tux
2015-01-25 00:17:52 -05:00
parent b375b5402d
commit ba825c1880
6 changed files with 37 additions and 58 deletions
@@ -6,17 +6,14 @@
*/
package com.imaginarycode.minecraft.redisbungee.util;
import com.google.common.io.ByteStreams;
import com.google.gson.reflect.TypeToken;
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import com.squareup.okhttp.Request;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -24,16 +21,12 @@ import java.util.UUID;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NameFetcher {
public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
URLConnection connection = new URL("https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "").toLowerCase() + "/names").openConnection();
String text;
try (InputStream is = connection.getInputStream()) {
text = new String(ByteStreams.toByteArray(is));
}
String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names";
Request request = new Request.Builder().url(url).get().build();
String response = RedisBungee.getHttpClient().newCall(request).execute().body().string();
Type listType = new TypeToken<List<Name>>() {}.getType();
List<Name> names = RedisBungee.getGson().fromJson(text, listType);
List<Name> names = RedisBungee.getGson().fromJson(response, listType);
List<String> humanNames = new ArrayList<>();
for (Name name : names) {
@@ -15,31 +15,20 @@ import java.util.concurrent.Callable;
import java.util.logging.Level;
@AllArgsConstructor
public abstract class RedisCallable<T> implements Callable<T>, Runnable {
public abstract class RedisCallable<T> implements Callable<T> {
private final RedisBungee plugin;
@Override
public void run() {
run(false);
}
@Override
public T call() {
return run(false);
}
private T run(boolean retry) {
Jedis jedis = null;
try {
jedis = plugin.getPool().getResource();
try (Jedis jedis = plugin.getPool().getResource()) {
return call(jedis);
} catch (JedisConnectionException e) {
plugin.getLogger().log(Level.SEVERE, "Unable to get connection", e);
if (jedis != null)
plugin.getPool().returnBrokenResource(jedis);
if (!retry) {
// Wait one second before retrying the task
try {
@@ -49,10 +38,6 @@ public abstract class RedisCallable<T> implements Callable<T>, Runnable {
}
run(true);
}
} finally {
if (jedis != null) {
plugin.getPool().returnResource(jedis);
}
}
throw new RuntimeException("task failed to run");
@@ -8,12 +8,10 @@ package com.imaginarycode.minecraft.redisbungee.util;
import com.google.common.collect.ImmutableList;
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.Callable;
@@ -21,6 +19,7 @@ import java.util.concurrent.Callable;
class UUIDFetcher implements Callable<Map<String, UUID>> {
private static final double PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private static final MediaType JSON = MediaType.parse("application/json");
private final List<String> names;
private final boolean rateLimiting;
@@ -33,24 +32,6 @@ class UUIDFetcher implements Callable<Map<String, UUID>> {
this(names, true);
}
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes());
stream.flush();
stream.close();
}
private static HttpURLConnection createConnection() throws Exception {
URL url = new URL(PROFILE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
return connection;
}
public static UUID getUUID(String id) {
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
}
@@ -59,10 +40,10 @@ class UUIDFetcher implements Callable<Map<String, UUID>> {
Map<String, UUID> uuidMap = new HashMap<>();
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection();
String body = RedisBungee.getGson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
writeBody(connection, body);
Profile[] array = RedisBungee.getGson().fromJson(new InputStreamReader(connection.getInputStream()), Profile[].class);
Request request = new Request.Builder().url(PROFILE_URL).post(RequestBody.create(JSON, body)).build();
String response = RedisBungee.getHttpClient().newCall(request).execute().body().string();
Profile[] array = RedisBungee.getGson().fromJson(response, Profile[].class);
for (Profile profile : array) {
UUID uuid = UUIDFetcher.getUUID(profile.id);
uuidMap.put(profile.name, uuid);