9
9
package infrastructure .wot ;
10
10
11
11
import city .sane .wot .DefaultWot ;
12
- import city .sane .wot .Wot ;
13
12
import city .sane .wot .WotException ;
14
13
import city .sane .wot .thing .ConsumedThing ;
15
14
import okhttp3 .OkHttpClient ;
@@ -45,13 +44,15 @@ public class WotClient {
45
44
public WotClient (final String thingId ) {
46
45
try {
47
46
final String thingUrl = System .getenv (THING_DIRECTORY_VARIABLE ) + thingId ;
48
- final Wot wotClient = DefaultWot .clientOnly ();
49
- final String thingDescription = this .obtainThingDescription (thingUrl );
50
- this .thing = wotClient .consume (thingDescription );
47
+ final String thingDescription = this .obtainThingDescription (thingUrl ).orElse ("" );
48
+ if (thingDescription .isEmpty ()) {
49
+ throw new IllegalArgumentException ("Thing Description for " + thingId + " not available" );
50
+ }
51
+ this .thing = DefaultWot .clientOnly ().consume (thingDescription );
51
52
} catch (final WotException e ) {
52
53
Logger .getLogger (WotClient .class .toString ()).info ("Wot TDD configuration error" );
53
54
} catch (IOException e ) {
54
- Logger .getLogger (WotClient .class .toString ()).info (thingId + " does not have a Thing Description associated" );
55
+ Logger .getLogger (WotClient .class .toString ()).info ("Error in obtaining Thing Description for " + thingId );
55
56
}
56
57
}
57
58
@@ -61,25 +62,34 @@ public WotClient(final String thingId) {
61
62
* @param inputs the inputs of the action.
62
63
*/
63
64
public void invoke (final String action , final Map <String , Object > inputs ) {
64
- try {
65
- this .thing .getActions ().get (action ).invoke (inputs ).get ();
66
- } catch (final InterruptedException | ExecutionException e ) {
67
- Logger .getLogger (WotClient .class .toString ()).info ("Error in invoking '" + action + "' on " + thing .getId ());
68
- }
65
+ Optional .ofNullable (this .thing .getActions ().get (action )).ifPresentOrElse (thingAction -> {
66
+ try {
67
+ thingAction .invoke (inputs ).get ();
68
+ } catch (final InterruptedException | ExecutionException e ) {
69
+ Logger .getLogger (WotClient .class .toString ())
70
+ .info ("Error in invoking '" + action + "' on " + this .thing .getId ());
71
+ }
72
+ },
73
+ () -> Logger .getLogger (WotClient .class .toString ())
74
+ .info ("Action " + action + " not available in " + this .thing .getId ())
75
+ );
69
76
}
70
77
71
- private String obtainThingDescription (final String thingUrl ) throws IOException {
78
+ private Optional < String > obtainThingDescription (final String thingUrl ) throws IOException {
72
79
// We need that because sane-city wot doesn't support application/td+json schema.
73
80
final OkHttpClient httpClient = new OkHttpClient ();
74
81
final Request request = new Request .Builder ().url (thingUrl ).get ().build ();
75
82
try (Response response = httpClient .newCall (request ).execute ()) {
76
- return Optional .ofNullable (response .body ()).map (responseBody -> {
77
- try {
78
- return responseBody .string ();
79
- } catch (IOException e ) {
80
- return "" ;
81
- }
82
- }).orElse ("" );
83
+ return Optional .of (response )
84
+ .filter (Response ::isSuccessful )
85
+ .map (Response ::body )
86
+ .map (responseBody -> {
87
+ try {
88
+ return responseBody .string ();
89
+ } catch (IOException e ) {
90
+ return "" ;
91
+ }
92
+ });
83
93
}
84
94
}
85
95
}
0 commit comments