Gives the interface the ability to pull information from the YouTube page.
This will make filling in song data much easier.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package edu.regis.universeplayer.addon;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
|
||||
import edu.regis.universeplayer.browserCommands.BrowserError;
|
||||
|
||||
public class BrowserErrorSerializer implements JsonSerializer<BrowserError>, JsonDeserializer<BrowserError>
|
||||
{
|
||||
@Override
|
||||
public JsonElement serialize(BrowserError src, Type typeOfSrc,
|
||||
JsonSerializationContext context)
|
||||
{
|
||||
JsonObject ob = new JsonObject();
|
||||
ob.addProperty("message", src.getMessage());
|
||||
String stack =
|
||||
Arrays.stream(src.getStackTrace())
|
||||
.map(trace -> trace.getMethodName() + "@" + trace
|
||||
.getFileName() + ":" + trace
|
||||
.getLineNumber() + ":0").reduce("",
|
||||
(s1, s2) -> s1.isEmpty() ? s2 : s1 + "\n" + s2);
|
||||
ob.addProperty("stack", stack);
|
||||
JsonArray suppressed = new JsonArray();
|
||||
for (Throwable throwable : src.getSuppressed())
|
||||
{
|
||||
suppressed.add(context.serialize(throwable));
|
||||
}
|
||||
return ob;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BrowserError deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
JsonObject ob = json.getAsJsonObject();
|
||||
return new BrowserError(ob.get("name").getAsString(), ob.get(
|
||||
"message").getAsString(), ob.get("stack").getAsString());
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,7 @@ package edu.regis.universeplayer.addon;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import edu.regis.universeplayer.browserCommands.CommandConfirmation;
|
||||
import edu.regis.universeplayer.browserCommands.MessageRunner;
|
||||
import edu.regis.universeplayer.browserCommands.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -37,6 +36,8 @@ public class BrowserLink extends MessageRunner
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeAdapter(StackTraceElement.class, new StackTraceElementSerializer());
|
||||
builder.registerTypeAdapter(Throwable.class, new ThrowableSerializer());
|
||||
builder.registerTypeAdapter(BrowserError.class, new BrowserErrorSerializer());
|
||||
builder.registerTypeAdapter(CommandReturn.class, new CommandReturnSerializer());
|
||||
gson = builder.create();
|
||||
}
|
||||
|
||||
@@ -144,6 +145,7 @@ public class BrowserLink extends MessageRunner
|
||||
*/
|
||||
private Object getMessage(JsonElement message) throws IOException
|
||||
{
|
||||
logger.debug("Deserializing {}", message);
|
||||
if (message.isJsonPrimitive())
|
||||
{
|
||||
return getPrimitiveMessage((JsonPrimitive) message);
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package edu.regis.universeplayer.addon;
|
||||
|
||||
import com.google.gson.*;
|
||||
import edu.regis.universeplayer.browserCommands.CommandConfirmation;
|
||||
import edu.regis.universeplayer.browserCommands.CommandReturn;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandReturnSerializer implements JsonSerializer<CommandReturn<?>>, JsonDeserializer<CommandReturn<?>>
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(CommandReturnSerializer.class);
|
||||
|
||||
@Override
|
||||
public CommandReturn<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
logger.debug("Deserializing return value {}", json);
|
||||
CommandReturn<?> returnVal;
|
||||
CommandConfirmation confirmation;
|
||||
Object value = null;
|
||||
JsonObject jsonOb = json.getAsJsonObject();
|
||||
JsonElement jsonVal = jsonOb.get("returnValue");
|
||||
confirmation = context.deserialize(jsonOb.getAsJsonObject("confirmation"), CommandConfirmation.class);
|
||||
try
|
||||
{
|
||||
value = deserializeObject(jsonVal, context);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
logger.error("Could not deserialize object {}.", json, e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
returnVal = new CommandReturn<>(value, confirmation);
|
||||
}
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to determine what type of object is provided and deserializes
|
||||
* it.
|
||||
*
|
||||
* @param jsonVal The element to deserialize. If it is an object, a "type"
|
||||
* field will be used to obtain the class.
|
||||
* @param context The deserialization context.
|
||||
* @return The deserialized object.
|
||||
* @throws ClassNotFoundException If the provided "type" field did not
|
||||
* contain a known class.
|
||||
*/
|
||||
private Object deserializeObject(JsonElement jsonVal, JsonDeserializationContext context) throws ClassNotFoundException
|
||||
{
|
||||
Object value;
|
||||
if (jsonVal.isJsonPrimitive())
|
||||
{
|
||||
JsonPrimitive primVal = jsonVal.getAsJsonPrimitive();
|
||||
if (primVal.isString())
|
||||
{
|
||||
value = primVal.getAsString();
|
||||
}
|
||||
else if (primVal.isBoolean())
|
||||
{
|
||||
value = primVal.getAsBoolean();
|
||||
}
|
||||
else if (primVal.isNumber())
|
||||
{
|
||||
value = primVal.getAsNumber();
|
||||
}
|
||||
else
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
}
|
||||
else if (jsonVal.isJsonArray())
|
||||
{
|
||||
JsonArray arrVal = jsonVal.getAsJsonArray();
|
||||
if (arrVal.size() > 0)
|
||||
{
|
||||
value = new ArrayList<>();
|
||||
for (JsonElement el : arrVal)
|
||||
{
|
||||
((ArrayList) value).add(deserializeObject(el, context));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
value = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
else if (jsonVal.isJsonObject())
|
||||
{
|
||||
JsonObject obVal = jsonVal.getAsJsonObject();
|
||||
Class type;
|
||||
if (obVal.has("type"))
|
||||
{
|
||||
type = Class.forName(obVal.get("type").getAsString());
|
||||
value = context.deserialize(obVal, type);
|
||||
logger.debug("Deserializing object of type {} {}", obVal.get("type").getAsString(), value);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = context.deserialize(obVal, Object.class);
|
||||
logger.debug("Deserializing object of type {} {}", value.getClass(), value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
value = null;
|
||||
}
|
||||
if (!jsonVal.isJsonObject())
|
||||
{
|
||||
logger.debug("Deserializing non-object {}", jsonVal);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(CommandReturn<?> src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return serializeObject(src, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to determine what type of object is provided and deserializes
|
||||
* it.
|
||||
*
|
||||
* @param value The element to deserialize. If it is an object, a "type"
|
||||
* field will be used to obtain the class.
|
||||
* @param context The serialization context.
|
||||
* @return The serialized object.
|
||||
*/
|
||||
private JsonElement serializeObject(Object value, JsonSerializationContext context)
|
||||
{
|
||||
JsonElement jsonVal;
|
||||
if (value == null)
|
||||
{
|
||||
jsonVal = JsonNull.INSTANCE;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value.getClass().isArray())
|
||||
{
|
||||
value = Arrays.stream((Object[]) value).collect(Collectors.toList());
|
||||
}
|
||||
if (value instanceof String || value instanceof Number || value instanceof Boolean)
|
||||
{
|
||||
jsonVal = context.serialize(value);
|
||||
}
|
||||
else if (value instanceof Collection)
|
||||
{
|
||||
jsonVal = new JsonArray();
|
||||
for (Object val : (Collection) value)
|
||||
{
|
||||
jsonVal.getAsJsonArray().add(serializeObject(val, context));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonVal = context.serialize(value, value.getClass());
|
||||
jsonVal.getAsJsonObject().addProperty("type", value.getClass().getName());
|
||||
}
|
||||
}
|
||||
return jsonVal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package edu.regis.universeplayer.addon;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import edu.regis.universeplayer.browserCommands.BrowserError;
|
||||
|
||||
public class GenericSerializer implements JsonSerializer<Object>,
|
||||
JsonDeserializer<Object>
|
||||
{
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GenericSerializer.class);
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Object src, Type typeOfSrc,
|
||||
JsonSerializationContext context)
|
||||
{
|
||||
JsonObject val = new JsonObject();
|
||||
for (Field field : src.getClass().getFields())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Modifier.isTransient(field.getModifiers()))
|
||||
{
|
||||
val.add(field.getName(), context.serialize(field.get(src)));
|
||||
}
|
||||
}
|
||||
catch (IllegalAccessException e)
|
||||
{
|
||||
logger.error("Could not store field " + field.getName() + " " +
|
||||
"of class " + src.getClass().getName());
|
||||
}
|
||||
}
|
||||
val.addProperty("type", src.getClass().getName());
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
Class<?> clazz;
|
||||
Field field;
|
||||
Object ob = null;
|
||||
if (json.isJsonObject() && json.getAsJsonObject().has("type"))
|
||||
{
|
||||
JsonObject jsonOb = json.getAsJsonObject();
|
||||
try
|
||||
{
|
||||
clazz = Class.forName(jsonOb.remove("type").getAsString());
|
||||
ob = clazz.getConstructor().newInstance();
|
||||
for (Map.Entry<String, JsonElement> fields: jsonOb.entrySet())
|
||||
{
|
||||
field = clazz.getField(fields.getKey());
|
||||
if (field.getType().isAssignableFrom(String.class))
|
||||
{
|
||||
field.set(ob, fields.getValue().getAsString());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(int.class))
|
||||
{
|
||||
field.setInt(ob, fields.getValue().getAsInt());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(double.class))
|
||||
{
|
||||
field.setDouble(ob, fields.getValue().getAsDouble());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(byte.class))
|
||||
{
|
||||
field.setByte(ob, fields.getValue().getAsByte());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(String.class))
|
||||
{
|
||||
field.set(ob, fields.getValue().getAsString());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(boolean.class))
|
||||
{
|
||||
field.setBoolean(ob, fields.getValue().getAsBoolean());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(float.class))
|
||||
{
|
||||
field.setFloat(ob, fields.getValue().getAsFloat());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(long.class))
|
||||
{
|
||||
field.setLong(ob, fields.getValue().getAsLong());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(short.class))
|
||||
{
|
||||
field.setShort(ob, fields.getValue().getAsShort());
|
||||
}
|
||||
else if (field.getType().isAssignableFrom(URL.class))
|
||||
{
|
||||
try
|
||||
{
|
||||
field.set(ob, new URL(fields.getValue().getAsString()));
|
||||
}
|
||||
catch (MalformedURLException e)
|
||||
{
|
||||
logger.error("Could not create url {}",
|
||||
fields.getValue().getAsString(), e);
|
||||
}
|
||||
}
|
||||
else if (fields.getValue().isJsonObject())
|
||||
{
|
||||
// clazz = fields.getValue().getAsJsonObject().
|
||||
// field.set(ob, context.);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e)
|
||||
{
|
||||
logger.error("Could not deserialize {} ", json, e);
|
||||
}
|
||||
}
|
||||
return ob;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user