private String addDefaultQueryParameters(String uri) { if (connection.hasToken()) { uri += "?" + TOKEN_HEADER + "=" + connection.getToken(); } return uri; }
private void resolveServer() throws UnknownHostException { MediaContainer container = getDocument(API_RESOURCES_URL, MediaContainer.class); // We need the IP-address to find this server in the server list on plex.tv String ip = resolveHostname(connection.getHost()); if (container != null) { for (Device device : container.getDevices()) { if (contains(device.getProvides(), "server")) { for (Connection deviceConnection : device.getConnections()) { boolean uriSet = (connection.getUri() != null); boolean portEqual = String.valueOf(connection.getPort()).equals(deviceConnection.getPort()); boolean hostEqual = ip.equals(deviceConnection.getAddress()); if (!uriSet && portEqual && hostEqual) { connection.setUri(deviceConnection.getUri()); connection.setApiLevel(PlexApiLevel.getApiLevel(device.getProductVersion())); logger.debug( "Server found, version {}, api level {}", device.getProductVersion(), connection.getApiLevel()); } } } } } if (connection.getUri() == null) { logger.warn( "Server not found in plex.tv device list, setting URI from configured data. Try configuring IP-address of host."); connection.setUri(String.format("http://%s:%d", ip, connection.getPort())); } }
private Map<String, Collection<String>> getDefaultHeaders() { Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>(); headers.put("X-Plex-Client-Identifier", Arrays.asList(CLIENT_ID)); headers.put("X-Plex-Product", Arrays.asList("openHAB")); headers.put("X-Plex-Version", Arrays.asList(PlexActivator.getVersion().toString())); headers.put("X-Plex-Device", Arrays.asList(SystemUtils.JAVA_RUNTIME_NAME)); headers.put("X-Plex-Device-Name", Arrays.asList("openHAB")); headers.put("X-Plex-Provides", Arrays.asList("controller")); headers.put("X-Plex-Platform", Arrays.asList("Java")); headers.put("X-Plex-Platform-Version", Arrays.asList(SystemUtils.JAVA_VERSION)); if (connection.hasToken()) { headers.put(TOKEN_HEADER, Arrays.asList(connection.getToken())); } return headers; }
/** * Create a connector for a single connection to a Plex server * * @param connection Connection properties * @param callback Called when a state update is received * @throws UnknownHostException If hostname is not resolvable. */ public PlexConnector(PlexConnectionProperties connection, PlexUpdateReceivedCallback callback) throws UnknownHostException { this.connection = connection; this.callback = callback; requestToken(); resolveServer(); this.wsUri = String.format( "%s://%s:%d/:/websockets/notifications", connection.getUri().getScheme().equals("https") ? "wss" : "ws", connection.getUri().getHost(), connection.getUri().getPort()); this.sessionsUrl = String.format("%s/status/sessions", connection.getUri().toString()); this.clientsUrl = String.format("%s/clients", connection.getUri().toString()); this.client = new AsyncHttpClient(new NettyAsyncHttpProvider(createAsyncHttpClientConfig())); this.handler = createWebSocketHandler(); }
private String getCover(AbstractSessionItem item) { String cover = null; // Only use grandparentThumb if it's present in the session item // and if the session item is not a music track if (!isBlank(item.getGrandparentThumb()) && !item.getClass().equals(Track.class)) { cover = item.getGrandparentThumb(); } else if (!isBlank(item.getThumb())) { cover = item.getThumb(); } if (!isBlank(cover)) { cover = addDefaultQueryParameters(String.format("%s%s", connection.getUri().toString(), cover)); } return cover; }
private void requestToken() { boolean tokenPresent = !isEmpty(connection.getToken()); boolean usernamePresent = !isEmpty(connection.getUsername()); boolean passwordPresent = !isEmpty(connection.getPassword()); if (!tokenPresent && usernamePresent && passwordPresent) { Map<String, String> parameters = new HashMap<String, String>(); String authString = Base64.encode((connection.getUsername() + ":" + connection.getPassword()).getBytes()); parameters.put("Authorization", "Basic " + authString); User user = postDocument(SIGN_IN_URL, parameters, User.class); if (user != null) { logger.debug("Plex login successful"); connection.setToken(user.getAuthenticationToken()); } else { logger.warn("Invalid credentials for Plex account"); } } }