|
| 1 | +package xyz.gianlu.librespot.api.handlers; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import io.undertow.server.HttpHandler; |
| 5 | +import io.undertow.server.HttpServerExchange; |
| 6 | +import org.apache.log4j.Logger; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | +import xyz.gianlu.librespot.api.Utils; |
| 10 | +import xyz.gianlu.librespot.common.ProtobufToJson; |
| 11 | +import xyz.gianlu.librespot.core.Session; |
| 12 | +import xyz.gianlu.librespot.dealer.ApiClient; |
| 13 | +import xyz.gianlu.librespot.mercury.MercuryClient; |
| 14 | +import xyz.gianlu.librespot.mercury.model.*; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Deque; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Gianlu |
| 23 | + */ |
| 24 | +public final class MetadataHandler implements HttpHandler { |
| 25 | + private static final Logger LOGGER = Logger.getLogger(MetadataHandler.class); |
| 26 | + private final Session session; |
| 27 | + |
| 28 | + public MetadataHandler(@NotNull Session session) { |
| 29 | + this.session = session; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void handleRequest(HttpServerExchange exchange) throws Exception { |
| 34 | + exchange.startBlocking(); |
| 35 | + if (exchange.isInIoThread()) { |
| 36 | + exchange.dispatch(this); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + Map<String, Deque<String>> params = Utils.readParameters(exchange); |
| 41 | + String typeStr = Utils.getFirstString(params, "type"); |
| 42 | + if (typeStr == null) { |
| 43 | + Utils.invalidParameter(exchange, "type"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + MetadataType type = MetadataType.parse(typeStr); |
| 48 | + if (type == null) { |
| 49 | + Utils.invalidParameter(exchange, "type"); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + String uri = Utils.getFirstString(params, "uri"); |
| 54 | + if (uri == null) { |
| 55 | + Utils.invalidParameter(exchange, "uri"); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + JsonObject obj = handle(type, uri); |
| 61 | + exchange.getResponseSender().send(obj.toString()); |
| 62 | + } catch (IOException | MercuryClient.MercuryException ex) { |
| 63 | + if (ex instanceof ApiClient.StatusCodeException) { |
| 64 | + if (((ApiClient.StatusCodeException) ex).code == 404) { |
| 65 | + Utils.invalidParameter(exchange, "uri", "404: Unknown uri"); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Utils.internalError(exchange, ex); |
| 71 | + LOGGER.error(String.format("Failed handling api request. {type: %s, uri: %s}", type, uri), ex); |
| 72 | + } catch (IllegalArgumentException ex) { |
| 73 | + Utils.invalidParameter(exchange, "uri", "Invalid uri for type: " + type); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @NotNull |
| 78 | + private JsonObject handle(@NotNull MetadataType type, @NotNull String uri) throws IOException, MercuryClient.MercuryException, IllegalArgumentException { |
| 79 | + switch (type) { |
| 80 | + case ALBUM: |
| 81 | + return ProtobufToJson.convert(session.api().getMetadata4Album(AlbumId.fromUri(uri))); |
| 82 | + case ARTIST: |
| 83 | + return ProtobufToJson.convert(session.api().getMetadata4Artist(ArtistId.fromUri(uri))); |
| 84 | + case SHOW: |
| 85 | + return ProtobufToJson.convert(session.api().getMetadata4Show(ShowId.fromUri(uri))); |
| 86 | + case EPISODE: |
| 87 | + return ProtobufToJson.convert(session.api().getMetadata4Episode(EpisodeId.fromUri(uri))); |
| 88 | + case TRACK: |
| 89 | + return ProtobufToJson.convert(session.api().getMetadata4Track(TrackId.fromUri(uri))); |
| 90 | + default: |
| 91 | + throw new IllegalArgumentException(type.name()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private enum MetadataType { |
| 96 | + EPISODE("episode"), TRACK("track"), ALBUM("album"), |
| 97 | + ARTIST("artist"), SHOW("show"); |
| 98 | + |
| 99 | + private final String val; |
| 100 | + |
| 101 | + MetadataType(String val) { |
| 102 | + this.val = val; |
| 103 | + } |
| 104 | + |
| 105 | + @Nullable |
| 106 | + private static MetadataType parse(@NotNull String val) { |
| 107 | + for (MetadataType type : values()) |
| 108 | + if (Objects.equals(type.val, val)) |
| 109 | + return type; |
| 110 | + |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments