Added UniSkin API
This commit is contained in:
77
app/src/main/java/org/skinner/UniSkinAPI.java
Normal file
77
app/src/main/java/org/skinner/UniSkinAPI.java
Normal file
@ -0,0 +1,77 @@
|
||||
package org.skinner;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.skinner.json.JSON;
|
||||
import org.skinner.json.JSONArray;
|
||||
import org.skinner.json.JSONObject;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
public class UniSkinAPI extends SafeHttpHandler {
|
||||
|
||||
public UniSkinAPI(String root) {
|
||||
super(root);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(HttpExchange exchange, URI uri) throws Exception {
|
||||
String path = uri.getPath();
|
||||
String[] pathParts = path.split("/");
|
||||
|
||||
if (pathParts.length == 2 && pathParts[1].endsWith(".json")) {
|
||||
String username = pathParts[1].substring(0, pathParts[1].length()-5);
|
||||
Profile profile = Database.getProfileByName(username);
|
||||
|
||||
if (profile == null) {
|
||||
notfound(exchange);
|
||||
return;
|
||||
}
|
||||
|
||||
JSONArray preference = new JSONArray();
|
||||
if (profile.getSkin().getSlim())
|
||||
preference.add(JSON.from("slim"));
|
||||
else
|
||||
preference.add(JSON.from("default"));
|
||||
|
||||
JSONObject skins = new JSONObject();
|
||||
if (profile.getSkin().getSlim())
|
||||
skins.put("slim", JSON.from(profile.getSkin().getHash()));
|
||||
else
|
||||
skins.put("default", JSON.from(profile.getSkin().getHash()));
|
||||
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("player_name", JSON.from(profile.getUsername()));
|
||||
object.put("last_update", JSON.from(0));
|
||||
object.put("model_preference", preference);
|
||||
object.put("skins", skins);
|
||||
|
||||
String response = object.toString();
|
||||
exchange.getResponseHeaders().add("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(200, response.length());
|
||||
exchange.getResponseBody().write(response.getBytes());
|
||||
exchange.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/textures/")) {
|
||||
String hash = pathParts[2];
|
||||
Skin skin = Database.getSkin(hash);
|
||||
if (skin == null) {
|
||||
notfound(exchange);
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] response = skin.getPng();
|
||||
exchange.getResponseHeaders().add("Content-Type", "image/png");
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
exchange.getResponseBody().write(response);
|
||||
exchange.close();
|
||||
return;
|
||||
}
|
||||
|
||||
notfound(exchange);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
@ -14,6 +14,7 @@ public class WebApp {
|
||||
// Create a context for a specific path and set the handler
|
||||
server.createContext("/api/", new RestAPI("/api/"));
|
||||
server.createContext("/skins/", new SkinPortSkinAPI("/skins/"));
|
||||
server.createContext("/uniskin/", new UniSkinAPI("/uniskin/"));
|
||||
server.createContext("/", new ResourceServer("/", "/www"));
|
||||
|
||||
// Start the server
|
||||
|
||||
Reference in New Issue
Block a user