Skip to content

Commit 1594be5

Browse files
RESTCOMM-2064: Modify Sid.Type, add getType static method. Add unit test
1 parent 02923e8 commit 1594be5

2 files changed

Lines changed: 99 additions & 57 deletions

File tree

  • restcomm/restcomm.commons/src
    • main/java/org/restcomm/connect/commons/dao
    • test/java/org/restcomm/connect/commons/sid

restcomm/restcomm.commons/src/main/java/org/restcomm/connect/commons/dao/Sid.java

Lines changed: 83 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,59 @@
3131
*/
3232
@Immutable
3333
public final class Sid {
34+
35+
private static String CALL_SID_STRING = "ID[a-zA-Z0-9]{32}-CA[a-zA-Z0-9]{32}";
3436
public static final Pattern pattern = Pattern.compile("[a-zA-Z0-9]{34}");
35-
public static final Pattern callSidPattern = Pattern.compile("ID[a-zA-Z0-9]{32}-CA[a-zA-Z0-9]{32}");
37+
public static final Pattern callSidPattern = Pattern.compile(CALL_SID_STRING);
38+
3639
private final String id;
3740

3841
public enum Type {
39-
ACCOUNT, APPLICATION, ANNOUNCEMENT, CALL, CLIENT, CONFERENCE, GATEWAY, INVALID, NOTIFICATION, PHONE_NUMBER, RECORDING, REGISTRATION, SHORT_CODE, SMS_MESSAGE, TRANSCRIPTION, INSTANCE, EXTENSION_CONFIGURATION, GEOLOCATION, ORGANIZATION, PROFILE
42+
CALL(null, CALL_SID_STRING),
43+
ACCOUNT("AC"),
44+
APPLICATION("AP"),
45+
ANNOUNCEMENT("AN"),
46+
47+
CLIENT("CL"),
48+
CONFERENCE("CF"),
49+
GATEWAY("GW"),
50+
51+
NOTIFICATION("NO"),
52+
PHONE_NUMBER("PN"),
53+
RECORDING("RE"),
54+
REGISTRATION("RG"),
55+
SHORT_CODE("SC"),
56+
SMS_MESSAGE("SM"),
57+
TRANSCRIPTION("TR"),
58+
INSTANCE("ID"),
59+
EXTENSION_CONFIGURATION("EX"),
60+
GEOLOCATION("GL"),
61+
ORGANIZATION("OR"),
62+
PROFILE("PR"),
63+
INVALID("IN");
64+
65+
private final String prefix;
66+
private final String regex;
67+
private final Pattern pattern;
68+
private static final String UUID_PATTERN = "[a-zA-Z0-9]{32}";
69+
private Type(final String prefix) {
70+
this(prefix, null);
71+
}
72+
private Type(final String prefix, String regex) {
73+
this.prefix = prefix;
74+
if(regex==null) {
75+
this.regex = prefix + UUID_PATTERN;
76+
}else{
77+
this.regex = regex;
78+
}
79+
pattern = Pattern.compile(this.regex);
80+
}
81+
public String getPrefix() {
82+
return prefix;
83+
}
84+
public boolean isType(Sid sid) {
85+
return pattern.matcher(sid.toString()).matches();
86+
}
4087
};
4188

4289
private static final Sid INVALID_SID = new Sid("IN00000000000000000000000000000000");
@@ -51,6 +98,18 @@ public Sid(final String id) throws IllegalArgumentException {
5198
}
5299
}
53100

101+
public static Type getType(Sid sid) {
102+
Type res = Type.INVALID;
103+
104+
for(Type type: Type.values()) {
105+
if(type.isType(sid)) {
106+
res = type;
107+
break;
108+
}
109+
}
110+
return res;
111+
}
112+
54113
@Override
55114
public boolean equals(Object object) {
56115
if (this == object) {
@@ -74,7 +133,7 @@ public static Sid generate(final Type type, String string) {
74133
String token = new Md5Hash(string).toString();
75134
switch (type) {
76135
case ACCOUNT: {
77-
return new Sid("AC" + token);
136+
return new Sid(type.getPrefix() + token);
78137
}
79138
default: {
80139
return generate(type);
@@ -85,66 +144,32 @@ public static Sid generate(final Type type, String string) {
85144
public static Sid generate(final Type type) {
86145
final String uuid = UUID.randomUUID().toString().replace("-", "");
87146
switch (type) {
88-
case ACCOUNT: {
89-
return new Sid("AC" + uuid);
90-
}
91-
case APPLICATION: {
92-
return new Sid("AP" + uuid);
93-
}
94-
case ANNOUNCEMENT: {
95-
return new Sid("AN" + uuid);
96-
}
97147
case CALL: {
98148
//https://github.com/RestComm/Restcomm-Connect/issues/1907
99149
return new Sid(RestcommConfiguration.getInstance().getMain().getInstanceId() + "-CA" + uuid);
100150
}
101-
case CLIENT: {
102-
return new Sid("CL" + uuid);
103-
}
104-
case CONFERENCE: {
105-
return new Sid("CF" + uuid);
106-
}
107-
case GATEWAY: {
108-
return new Sid("GW" + uuid);
109-
}
110-
case INVALID: {
151+
case INVALID:{
111152
return INVALID_SID;
112153
}
113-
case NOTIFICATION: {
114-
return new Sid("NO" + uuid);
115-
}
116-
case PHONE_NUMBER: {
117-
return new Sid("PN" + uuid);
118-
}
119-
case RECORDING: {
120-
return new Sid("RE" + uuid);
121-
}
122-
case REGISTRATION: {
123-
return new Sid("RG" + uuid);
124-
}
125-
case SHORT_CODE: {
126-
return new Sid("SC" + uuid);
127-
}
128-
case SMS_MESSAGE: {
129-
return new Sid("SM" + uuid);
130-
}
131-
case TRANSCRIPTION: {
132-
return new Sid("TR" + uuid);
133-
}
134-
case INSTANCE: {
135-
return new Sid("ID" + uuid);
136-
}
137-
case EXTENSION_CONFIGURATION: {
138-
return new Sid("EX" + uuid);
139-
}
140-
case GEOLOCATION: {
141-
return new Sid("GL" + uuid);
142-
}
143-
case ORGANIZATION: {
144-
return new Sid("OR" + uuid);
145-
}
146-
case PROFILE: {
147-
return new Sid("PR" + uuid);
154+
case ACCOUNT:
155+
case APPLICATION:
156+
case ANNOUNCEMENT:
157+
case CLIENT:
158+
case CONFERENCE:
159+
case GATEWAY:
160+
case NOTIFICATION:
161+
case PHONE_NUMBER:
162+
case RECORDING:
163+
case REGISTRATION:
164+
case SHORT_CODE:
165+
case SMS_MESSAGE:
166+
case TRANSCRIPTION:
167+
case INSTANCE:
168+
case EXTENSION_CONFIGURATION:
169+
case GEOLOCATION:
170+
case ORGANIZATION:
171+
case PROFILE:{
172+
return new Sid(type.getPrefix() + uuid);
148173
}
149174
default: {
150175
return null;
@@ -164,4 +189,5 @@ public int hashCode() {
164189
public String toString() {
165190
return id;
166191
}
192+
167193
}

restcomm/restcomm.commons/src/test/java/org/restcomm/connect/commons/sid/SidTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.junit.Test;
66
import org.restcomm.connect.commons.dao.Sid;
7+
import org.restcomm.connect.commons.dao.Sid.Type;
78

89
public class SidTest {
910

@@ -51,4 +52,19 @@ public void testOldCallSid() {
5152
}
5253
}
5354

55+
@Test
56+
public void testGetType() {
57+
try{
58+
for(Type t : Sid.Type.values()) {
59+
//TODO: mock static class RestcommConfiguration
60+
if(!t.equals(Type.CALL)) {
61+
Sid s = Sid.generate(t);
62+
Type t2 = Sid.getType(s);
63+
64+
assertEquals(t, t2);
65+
}
66+
}
67+
}catch(Exception e){
68+
}
69+
}
5470
}

0 commit comments

Comments
 (0)