11/*
2- Copyright (C) 2016 Bengt Martensson.
2+ Copyright (C) 2016, 2025 Bengt Martensson.
33
44This program is free software: you can redistribute it and/or modify
55it under the terms of the GNU General Public License as published by
2020import java .io .IOException ;
2121import java .io .InputStream ;
2222import java .io .InputStreamReader ;
23- import java .io .OutputStream ;
2423import java .io .Reader ;
2524import java .io .UnsupportedEncodingException ;
26- import java .net .HttpURLConnection ;
2725import java .net .MalformedURLException ;
2826import java .net .URI ;
2927import java .net .URISyntaxException ;
3836import javax .json .Json ;
3937import javax .json .JsonArray ;
4038import javax .json .JsonObject ;
41- import javax .json .JsonObjectBuilder ;
4239import javax .json .JsonValue ;
4340import javax .json .stream .JsonParser ;
4441import org .harctoolbox .girr .Command ;
@@ -57,80 +54,39 @@ public class ControlTowerIrDatabase extends DatabaseImporter implements IRemoteS
5754
5855 // See https://www.globalcache.com/files/docs/API-GlobalIRDB_ver1.pdf page 6f.
5956 private static String httpEncode (String s ) throws UnsupportedEncodingException {
60- String str = s .replaceAll ("&" , "xampx" ).replaceAll ("/" , "xfslx" )
57+ return s .replaceAll ("&" , "xampx" ).replaceAll ("/" , "xfslx" )
6158 .replaceAll (">" , "xgtx" ).replaceAll ("<" , "xltx" )
6259 .replaceAll (":" , "xcolx" ).replaceAll ("\\ ?" , "xquex" )
6360 .replaceAll ("%" , "xmodx" ).replaceAll ("\\ +" , "xaddx" );
64- return str ;
6561 }
6662
6763 @ SuppressWarnings ("UseOfSystemOutOrSystemErr" )
6864 public static void main (String [] args ) {
6965 Props props = new Props (null );
7066 Importer .setProperties (props );
7167 try {
72- ControlTowerIrDatabase gcdb = new ControlTowerIrDatabase (args [0 ], args [1 ], true );
73- //System.out.println(gcdb.getTypes());
74- //System.out.println(gcdb.getManufacturers());
68+ ControlTowerIrDatabase gcdb = new ControlTowerIrDatabase (true );
7569 System .out .println (gcdb .getDeviceTypes ("Sony" ));
7670 System .out .println (gcdb .getModels ("Sony" , "Projector" ));
7771 System .out .println (gcdb .getManufacturers ("Blu Ray" ));
7872 System .out .println (gcdb .getModels ("Oppo Digital" , "Blu Ray" ));
7973 System .out .println (gcdb .getModel (1758 ));
80- gcdb .login ();
81- System .out .println (gcdb .getStatus ());
82- gcdb .getCodeset (1758 , false );
83- System .out .println (gcdb .getStatus ());
84- gcdb .logout ();
8574 } catch (MalformedURLException ex ) {
8675 Logger .getLogger (ControlTowerIrDatabase .class .getName ()).log (Level .SEVERE , null , ex );
87- } catch (LoginException ex ) {
88- System .err .println ("Login failed: " + ex .getMessage ());
8976 } catch (IOException | URISyntaxException ex ) {
9077 System .err .println (ex .getMessage ());
9178 }
9279 }
9380
9481 private boolean verbose = false ;
95- private String apiKey ;
96- private final String email ;
97- private final String password ;
98- private int codesRequestedToday ;
99- private String accountType ;
100- private String name ;
101- private String company ;
102-
10382 private Map <String , String > manufacturerMap ;
10483 private Map <String , String > typesMap ;
10584 private String manufacturer ;
10685 private String deviceType ;
10786 private RemoteSet remoteSet ;
10887
109- public ControlTowerIrDatabase (String apiKey , boolean verbose ) {
110- super (globalCacheDbOrigin );
111- this .company = null ;
112- this .name = null ;
113- this .accountType = null ;
114- this .codesRequestedToday = -1 ;
115- this .email = null ;
116- this .password = null ;
117- this .apiKey = apiKey ;
118- this .verbose = verbose ;
119- }
120-
12188 public ControlTowerIrDatabase (boolean verbose ) {
122- this (null , null , verbose );
123- }
124-
125- public ControlTowerIrDatabase (String email , String password , boolean verbose ) {
12689 super (globalCacheDbOrigin );
127- this .company = null ;
128- this .name = null ;
129- this .accountType = null ;
130- this .codesRequestedToday = -1 ;
131- this .email = email ;
132- this .password = password ;
133- this .apiKey = null ;
13490 this .verbose = verbose ;
13591 }
13692
@@ -150,109 +106,24 @@ private JsonValue readFrom(String str) throws IOException, URISyntaxException {
150106
151107 private JsonValue readFrom (Reader reader ) throws IOException {
152108 JsonParser parser = Json .createParser (reader );
153- JsonParser . Event x = parser .next ();
109+ parser .next ();
154110 return parser .getValue ();
155111 }
156112
157- @ SuppressWarnings ("UseOfSystemOutOrSystemErr" )
158- private JsonObject postAndGetObject (String str , String payload ) throws MalformedURLException , IOException , LoginException , URISyntaxException {
159- URL url = new URI (protocol , null , controlTowerIrDatabaseHost , portNo , path + str , null , null ).toURL ();
160- if (verbose )
161- System .err .println ("Opening (POST) " + url .toString ());
162- HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
163- connection .setRequestMethod ("POST" );
164- connection .setRequestProperty ("Accept" , "application/json" );
165- //if (payload != null) {
166- connection .setRequestProperty ("Content-Type" , "application/json; charset=US-ASCII" );
167- connection .setRequestProperty ("Content-Length" , "" + Integer .toString (payload == null ? 0 : payload .getBytes (Charset .forName ("US-ASCII" )).length ));
168- connection .setRequestProperty ("Content-Language" , "en-US" );
169- //}
170-
171- connection .setUseCaches (false );
172- connection .setDoInput (true );
173- connection .setDoOutput (payload != null );
174- if (payload != null ) {
175- try (OutputStream outputStream = connection .getOutputStream ()) {
176- outputStream .write (payload .getBytes ("US-ASCII" ));
177- outputStream .flush ();
178- }
179- }
180-
181- int httpResult = connection .getResponseCode ();
182-
183- if (httpResult != HttpURLConnection .HTTP_OK ) {
184- connection .disconnect ();
185- throw new LoginException ("HTTP error " + httpResult );
186- }
187-
188- InputStream is = connection .getInputStream ();
189- InputStreamReader isr = new InputStreamReader (is , Charset .forName ("US-ASCII" ));
190- JsonObject response = readFrom (isr ).asJsonObject ();
191- connection .disconnect ();
192- return response ;
193- }
194-
195- private String apiKeyString (char separator ) {
196- return apiKey == null ? "" : (separator + "apiKey=" + apiKey );
197- }
198-
199- private void evaluateAccount (JsonObject acct ) {
200- accountType = acct .getString ("AccountType" );
201- apiKey = acct .getString ("ApiKey" );
202- codesRequestedToday = acct .getInt ("CodesRequestedToday" );
203- //this.name = acct.get("Name").asString();
204- //this.company = acct.get("Company").asString();
205- }
206-
207- private void evaluateAccount () {
208- accountType = null ;
209- apiKey = null ;
210- codesRequestedToday = -1 ;
211- }
212-
213- // UNTESTED!!
214- public void login () throws MalformedURLException , IOException , LoginException , URISyntaxException {
215- JsonObjectBuilder builder = Json .createObjectBuilder ();
216- builder .add ("Email" , email );
217- builder .add ("Password" , password );
218- JsonObject jsonObject = builder .build ();
219- String payload = jsonObject .toString ();
220-
221- JsonObject response = postAndGetObject ("/account/login" , payload );
222- String status = response .getString ("Status" );
223- if (!status .equals ("success" ))
224- throw new LoginException (response .getString ("Message" ));
225- JsonObject acct = response .getJsonObject ("Account" );
226- evaluateAccount (acct );
227- }
228-
229- public void logout () throws MalformedURLException , IOException , LoginException , URISyntaxException {
230- if (apiKey == null )
231- throw new LoginException ("Not logged in" );
232- JsonObject response = postAndGetObject ("/account/logout" + apiKeyString ('?' ), "" );
233- String status = response .getString ("Status" );
234- if (!status .equals ("success" ))
235- throw new LoginException (response .getString ("Message" ));
236- evaluateAccount ();
237- }
238-
239-
240113 private JsonArray getJsonArray (String str ) throws IOException , URISyntaxException {
241114 return readFrom (str ).asJsonArray ();
242115 }
243116
244-
245117 private JsonObject getJsonObject (String str ) throws IOException , URISyntaxException {
246118 return readFrom (str ).asJsonObject ();
247119 }
248120
249121 private Map <String , String > getMap (String urlFragment , String keyName , String valueName ) throws IOException , URISyntaxException {
250122 JsonArray array = getJsonArray (urlFragment );
251123 Map <String ,String > map = new HashMap <>(array .size ());
252- for (JsonValue val : array ) {
253- JsonObject obj = val .asJsonObject ();
124+ array .stream ().map (val -> val .asJsonObject ()).forEachOrdered (obj -> {
254125 map .put (obj .getString (keyName ), obj .getString (valueName ));
255- }
126+ });
256127 return map ;
257128 }
258129
@@ -264,14 +135,6 @@ private void loadTypes() throws IOException, URISyntaxException {
264135 typesMap = getMap ("types" , "Name" , "$id" );
265136 }
266137
267- public String getStatus () throws IOException , URISyntaxException {
268- if (apiKey == null )
269- return "Not logged in" ;
270- JsonObject obj = getJsonObject ("account" + apiKeyString ('?' ));
271- evaluateAccount (obj );
272- return obj .toString ();
273- }
274-
275138 public Collection <String > getManufacturers () throws IOException , URISyntaxException {
276139 if (manufacturerMap == null )
277140 loadManufacturers ();
@@ -313,28 +176,6 @@ public Collection<String> getCommands(int setId) throws IOException, URISyntaxEx
313176 "Function" , "$id" ).keySet ();
314177 }
315178
316- @ SuppressWarnings ("UseOfSystemOutOrSystemErr" )
317- public void getCodeset (int setId , boolean email ) throws IOException , LoginException , URISyntaxException {
318- if (apiKey == null )
319- throw new LoginException ("Must be logged in" );
320- if (email ) {
321- JsonObject obj = getJsonObject ("codesets/" + Integer .toString (setId ) + "?output=email" + apiKeyString ('&' ));
322- if (!obj .getString ("Status" ).equals ("success" )) {
323- System .err .println (obj .getString ("Message" ));
324- }
325- String str = obj .toString ();
326- System .out .println (str );
327- } else {
328- JsonObject obj = getJsonObject ("codesets/" + Integer .toString (setId ) + "?output=direct" + apiKeyString ('&' ));
329- System .out .println (obj .toString ());
330- if (!obj .getString ("Status" ).equals ("success" )) {
331- System .err .println (obj .getString ("Message" ));
332- }
333- String str = obj .toString ();
334- System .out .println (str );
335- }
336- }
337-
338179 public void load (String manufacturerKey , String deviceTypeKey , String codeSet ) throws IOException , URISyntaxException {
339180 clearCommands ();
340181 manufacturer = manufacturerKey ;
@@ -367,13 +208,11 @@ public void load(String manufacturerKey, String deviceTypeKey, String codeSet) t
367208 remoteSet = new RemoteSet (getCreatingUser (), origin , remote );
368209 }
369210
370-
371211 @ Override
372212 public RemoteSet getRemoteSet () {
373213 return remoteSet ;
374214 }
375215
376-
377216 @ Override
378217 public String getFormatName () {
379218 return "Global Caché Control Tower IR Database" ;
0 commit comments