Skip to content

Commit a5ca8f1

Browse files
authored
Merge pull request #10 from SEPIA-Framework/dev
Core tools update to 2.2.0 for SEPIA-Home v2.2.0
2 parents 8495c47 + 7e51ffb commit a5ca8f1

30 files changed

+1311
-184
lines changed

pom.xml

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>net.b07z.sepia.server.core</groupId>
66
<artifactId>sepia-core-tools</artifactId>
7-
<version>2.1.0</version>
7+
<version>2.2.0</version>
88
<name>SEPIA Core-Tools</name>
99
<description>The Core-Tools repository contains classes and methods that are shared by all SEPIA servers and other tools like the SDK.</description>
1010
<url>https://github.com/SEPIA-Framework/sepia-core-tools-java</url>
@@ -61,6 +61,23 @@
6161
<finalName>sepia-core-tools-v${project.version}</finalName>
6262
</configuration>
6363
</plugin>
64+
<!-- force certain surefire version for testing -->
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-surefire-plugin</artifactId>
68+
<version>3.0.0-M3</version>
69+
</plugin>
70+
71+
<!-- Java docs, use with: mvn javadoc:jar -->
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-javadoc-plugin</artifactId>
75+
<version>3.0.1</version>
76+
<configuration>
77+
<doclint>none</doclint> <!-- TODO: fix errors found by linter -->
78+
<finalName>sepia-core-tools-v${project.version}</finalName>
79+
</configuration>
80+
</plugin>
6481
</plugins>
6582
</build>
6683

@@ -88,7 +105,7 @@
88105
<dependency>
89106
<groupId>com.fasterxml.jackson.core</groupId>
90107
<artifactId>jackson-databind</artifactId>
91-
<version>2.9.7</version>
108+
<version>2.9.8</version>
92109
</dependency>
93110
<dependency>
94111
<groupId>commons-lang</groupId>
@@ -121,6 +138,14 @@
121138
<version>4.5.3</version>
122139
</dependency>
123140

141+
<!-- Java 9+ support -->
142+
<dependency>
143+
<groupId>javax.xml.bind</groupId>
144+
<artifactId>jaxb-api</artifactId>
145+
<version>2.3.1</version>
146+
</dependency>
147+
148+
<!-- Testing -->
124149
<dependency>
125150
<groupId>junit</groupId>
126151
<artifactId>junit</artifactId>
@@ -164,4 +189,4 @@
164189
<url>https://github.com/SEPIA-Framework/sepia-core-tools-java/tree/master</url>
165190
</scm>
166191

167-
</project>
192+
</project>

src/main/java/net/b07z/sepia/server/core/assistant/ACTIONS.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class ACTIONS {
3232
final public static String OPEN_LIST = "open_list"; //tell client to open a user specific list, param.: "listInfo" : {"indexType":...}
3333
final public static String EXTEND_REQUEST = "extend_request"; //tell client that this request will take longer and he should prepare for that
3434
final public static String SHOW_ABORT_OPTION = "show_abort"; //tell client to show an abort option
35-
//deprecated: final public static String SKIP_TTS = "skip_tts"; //tell client to prevent TTS sound output (best used together with PLAY_AUDIO)
3635
final public static String PLAY_AUDIO_URL = "play_audio_url"; //tell client to play a single audio file from url, parameters: audio_url
3736
final public static String PLAY_AUDIO_STREAM = "play_audio_stream"; //tell client to play a continuous audio stream, parameters: audio_url, audio_title, audio_img, audio_type
3837
final public static String STOP_AUDIO_STREAM = "stop_audio_stream"; //tell client to stop the running audio stream
@@ -42,11 +41,11 @@ public class ACTIONS {
4241
final public static String ALARM = "alarm"; //tell client to set an alarm, parameters: tbd
4342
final public static String REMINDER = "reminder"; //tell client to set an reminder, parameters: tbd
4443
final public static String APPOINTMENT = "appointment"; //tell client to set an appointment in the calendar, parameters: tbd
44+
final public static String SWITCH_LANGUAGE = "switch_language"; //tell client to switch language, parameters: language_code
4545

4646
//possible next actions:
4747
//public static String STREAMING_REQUEST = "streaming_request"; //tell the client that he has to expect an answer that gets streamed
4848
//public static String GET_HTML_META_INFO = "get_html_meta_info"; //tell client to open an url, get meta info when the page is loaded and say it, parameters: meta_tags, add_answer
49-
//public static String EXECUTE_CMD = "execute_cmd"; //tell client to execute an command, parameters: command
5049

5150
//ACTION OPTIONS
5251
final public static String SKIP_TTS = "skipTTS";

src/main/java/net/b07z/sepia/server/core/assistant/CMD.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class CMD {
3434
final public static String LISTS = "lists"; //parameters: type, action, info
3535
final public static String LOCATION = "location"; //parameters: search, poi (like italian restaurant, supermarket etc. ...)
3636
final public static String MATCH = "match"; //parameters: info
37+
final public static String MESH_NODE_PLUGIN = "mesh_node_plugin"; //parameters: node_url, node_plugin_name, node_plugin_data, reply_success, reply_fail
3738
final public static String MOBILITY = "mobility"; //parameters: location_start, location_end, type, time
3839
final public static String MOVIES = "movies"; //parameters: search, type (genre), info (actor,director)
3940
final public static String MUSIC = "music"; //parameters: search, type (genre), info (artist?)

src/main/java/net/b07z/sepia/server/core/assistant/PARAMETERS.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,17 @@ public class PARAMETERS {
104104
final public static String LIST_TYPE = "list_type"; //type of list (e.g. shopping, todo, ...)
105105
final public static String LIST_SUBTYPE = "list_subtype"; //sub-type aka name of list (e.g. my great ..., my macys ...)
106106

107+
//MESH NODE PLUGIN
108+
final public static String MESH_NODE_URL = "node_url"; //URL to mesh node
109+
final public static String MESH_NODE_PLUGIN_NAME = "node_plugin_name"; //name of mesh node plugin
110+
final public static String MESH_NODE_PLUGIN_DATA = "node_plugin_data"; //data (e.g. as JSON string) for node plugin
111+
107112
//------------ control parameters -------------
108113

109114
//CHATS and QUESTIONS
110-
final public static String REPLY = "reply"; //a reply the assistant should give submitted as parameter from client
115+
final public static String REPLY = "reply"; //a reply the assistant should give submitted as parameter from client (for certain services that use it)
116+
final public static String REPLY_SUCCESS = "reply_success"; //a reply used for success (for certain services that use it)
117+
final public static String REPLY_FAIL = "reply_fail"; //a reply used for fail (for certain services that use it)
111118
final public static String ANY_ANSWER = "any_answer"; //if you don't have a particular parameter that is missing but you ask a previously undefined question you can store the answer here. Maybe add dialog_state to it.
112119

113120
//CUSTOM SERVICES (do we still need this?)

src/main/java/net/b07z/sepia/server/core/data/Answer.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414

1515
import org.json.simple.JSONObject;
1616

17+
/**
18+
* This class contains all required info for answers given by the assistant.
19+
* Answers can have a number of parameters like character (neutral, cool, ...), when to trigger (first time, first repetition, etc.)
20+
* and many more. If you create a custom answer pool (e.g. inside a service) use this constructor to get a good set of parameters:<br>
21+
* Answer(Language language, String type, String text, Character character, int repetition, int mood)
22+
*
23+
* @author Florian Quirin
24+
*
25+
*/
1726
public class Answer {
1827

1928
//database types to organize commands
@@ -42,26 +51,61 @@ public class Answer {
4251
private Map<String, Object> data; //any additional data we did not think of right now
4352
private String date;
4453

54+
/**
55+
* Every answer can be assigned to a certain assistant 'character' like 'cool' or 'rude' (or 'neutral').
56+
*/
4557
public enum Character {
4658
neutral, rude, cool, polite
4759
}
4860

61+
/**
62+
* Usually you want to use another constructor: {@link Answer}(Language language, String type, String text, Character character, int repetition, int mood)
63+
*/
4964
public Answer() {
5065
// needed for Jackson
5166
}
5267

68+
/**
69+
* Create an answer with default settings.
70+
* @param language - language code for this answer
71+
* @param type - answer key, basically an ID to find an answer in the pool
72+
* @param text - actual answer (variables like &lt1&gt, &lt2&gt ... are supported)
73+
*/
5374
public Answer(Language language, String type, String text) {
5475
this(language, type, text, Character.neutral);
5576
}
56-
77+
/**
78+
* Create an answer with default settings and custom character.
79+
* @param language - language code for this answer
80+
* @param type - answer key, basically an ID to find an answer in the pool
81+
* @param text - actual answer (variables like &lt1&gt, &lt2&gt ... are supported)
82+
* @param character - answer {@link Character} like "neutral", "rude", "cool"
83+
*/
5784
public Answer(Language language, String type, String text, Character character) {
5885
this(language, type, text, character, 0, 5);
5986
}
6087

88+
/**
89+
* Build a new answer with all relevant information.
90+
* @param language - language code for this answer
91+
* @param type - answer key, basically an ID to find an answer in the pool
92+
* @param text - actual answer (variables like &lt1&gt, &lt2&gt ... are supported)
93+
* @param character - answer {@link Character} like "neutral", "rude", "cool"
94+
* @param repetition - when will this answer be triggered? First try (0), first repeat (1), following repeats (2)
95+
* @param mood - mood level that this answer will be used from 0-10. Sad (0), neutral (5), happy (10)
96+
*/
6197
public Answer(Language language, String type, String text, Character character, int repetition, int mood) {
6298
this(language, type, text, Collections.singletonList(character), repetition, mood);
6399
}
64-
100+
/**
101+
* Build a new answer with all relevant information.
102+
* @param language - language code for this answer
103+
* @param type - answer key, basically an ID to find an answer in the pool
104+
* @param text - actual answer (variables like &lt1&gt, &lt2&gt ... are supported)
105+
* @param characters - multiple answer {@link Character} like "neutral", "rude", "cool"
106+
* @param repetition - when will this answer be triggered? First try (0), first repeat (1), following repeats (2)
107+
* @param mood - mood level that this answer will be used from 0-10. Sad (0), neutral (5), happy (10)
108+
*/
65109
public Answer(Language language, String type, String text, List<Character> characters, int repetition, int mood) {
66110
this(language, type, text, characters, repetition, mood,
67111
ConfigDefaults.defaultAssistantUserId, Defaults.IMPORT_SOURCE, true, false, false, null,
@@ -70,6 +114,7 @@ public Answer(Language language, String type, String text, List<Character> chara
70114
}
71115

72116
/**
117+
* Usually you want to use another constructor: {@link Answer}(Language language, String type, String text, Character character, int repetition, int mood)
73118
* @param isPublic is this visible for everybody, if not, it's a private answer
74119
* @param isLocal is this run on the local home server?
75120
*/
@@ -101,14 +146,23 @@ public Answer(Language language, String type, String text, List<Character> chara
101146
this.date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date());
102147
}
103148

149+
/**
150+
* Language in use for this answer.
151+
*/
104152
public Language getLanguage() {
105153
return language;
106154
}
107155

156+
/**
157+
* This is the answer key, basically an ID to find an answer or list of answers in the pool (DB, file or memory).
158+
*/
108159
public String getType() {
109160
return type;
110161
}
111162

163+
/**
164+
* The actual answer in the given language.
165+
*/
112166
public String getText() {
113167
return text;
114168
}

src/main/java/net/b07z/sepia/server/core/data/CmdMap.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ public class CmdMap {
2525
private List<String> permissions;
2626

2727
@SuppressWarnings("unchecked")
28+
/**
29+
* Import command map from JSON.
30+
* @param entry
31+
*/
2832
public CmdMap(JSONObject entry){
2933
this.command = JSON.getString(entry, "command");
3034
this.services = new ArrayList<>((JSONArray)entry.get("services"));
3135
this.permissions = new ArrayList<>((JSONArray)entry.get("permissions"));
3236
}
37+
/**
38+
* Create a new command map.
39+
* @param command
40+
* @param services
41+
* @param permissions
42+
*/
3343
public CmdMap(String command, List<String> services, List<String> permissions){
3444
this.command = command;
3545
this.services = services;
@@ -52,6 +62,10 @@ public boolean equals(Object map){
5262
return false;
5363
}
5464
}
65+
@Override
66+
public String toString(){
67+
return (this.command + " - services: " + this.services.size());
68+
}
5569

5670
public String getCommand(){
5771
return command;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.b07z.sepia.server.core.java;
2+
3+
import java.util.Collections;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* A Map implementation that uses {@LinkedHashMap} to features to keep the size limited to a maximum and drop the oldest entry on overflow.<br>
9+
* NOTE: Check the constructor on notes about concurrency.
10+
*
11+
* @author Florian Quirin
12+
*
13+
* @param <K>
14+
* @param <V>
15+
*/
16+
public class MaxSizeMap<K, V> extends LinkedHashMap<K, V> {
17+
18+
private static final long serialVersionUID = 1L;
19+
20+
private int maxKeysSize;
21+
22+
/**
23+
* Create a {@LinkedHashMap} that automatically removes the oldest entry when size is exceeded.<br>
24+
* <br>
25+
* IMPORTANT: This map (as is it basically a linked hash map) can run into concurrency problems, use {@link #getSynchronizedMap(int)}
26+
* or build your own Collections.synchronizedMap(...) if you access the list from multiple threads.
27+
* @param maxSize
28+
*/
29+
public MaxSizeMap(int maxSize){
30+
super(maxSize + 1);
31+
this.maxKeysSize = maxSize;
32+
}
33+
/**
34+
* Create a synchronized version of this map.
35+
* @param maxSize
36+
* @return
37+
*/
38+
public static <T> Map<String, T> getSynchronizedMap(int maxSize){
39+
return Collections.synchronizedMap(new MaxSizeMap<String, T>(maxSize));
40+
}
41+
42+
@Override
43+
protected boolean removeEldestEntry(final Map.Entry<K, V> eldest) {
44+
return size() > maxKeysSize;
45+
}
46+
47+
}

src/main/java/net/b07z/sepia/server/core/server/RequestGetOrFormParameters.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ public String[] getStringArray(String key) {
3333
@Override
3434
public JSONObject getJson(String key) {
3535
String jsonString = request.queryParams(key);
36-
return JSON.parseStringOrFail(jsonString);
36+
if (jsonString != null){
37+
return JSON.parseStringOrFail(jsonString);
38+
}else{
39+
return null;
40+
}
3741
}
3842

3943
@Override
4044
public JSONArray getJsonArray(String key) {
4145
String jsonString = request.queryParams(key);
42-
return JSON.parseStringToArrayOrFail(jsonString);
46+
if (jsonString != null){
47+
return JSON.parseStringToArrayOrFail(jsonString);
48+
}else{
49+
return null;
50+
}
4351
}
4452

4553
@Override

src/main/java/net/b07z/sepia/server/core/server/SparkJavaFw.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public static String returnNoAccess(Request request, Response response){
143143

144144
/**
145145
* Return simple access-restricted message with custom error code.
146+
* @param errorCode - code returned from authenticator - 1: communication error, 2: access denied, ...
147+
* @return
146148
*/
147149
public static String returnNoAccess(Request request, Response response, int errorCode){
148150
if (errorCode == 2){

src/main/java/net/b07z/sepia/server/core/server/Validate.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.b07z.sepia.server.core.server;
22

33
import net.b07z.sepia.server.core.tools.Debugger;
4+
import net.b07z.sepia.server.core.tools.Is;
45
import net.b07z.sepia.server.core.tools.Security;
56
import spark.Request;
67

@@ -42,12 +43,13 @@ public static String getLocalSignature(Request request, String challenge, long t
4243
*/
4344
public static boolean validateInternalCall(Request request, String submittedKey, String sharedServerKey) {
4445
//TODO: make this more secure by adding a white-list of IPs or something ...
45-
if (submittedKey != null && submittedKey.equals(sharedServerKey)){
46-
return true;
47-
}else if (submittedKey != null){
48-
Debugger.println("Invalid internal API call from " + request.ip(), 1);
46+
if (Is.nullOrEmpty(submittedKey) || Is.nullOrEmpty(sharedServerKey)){
4947
return false;
48+
}
49+
if (submittedKey.equals(sharedServerKey)){
50+
return true;
5051
}else{
52+
Debugger.println("Invalid internal API call from " + request.ip(), 1);
5153
return false;
5254
}
5355
}

0 commit comments

Comments
 (0)