Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public void expectResponseClass(String internalActionId, Class<? extends Manager
* as soon as it is received and dispatches the received events and
* responses via the associated dispatcher.
*
* @see org.asteriskjava.manager.internal.Dispatcher#dispatchEvent(ManagerEvent)
* @see org.asteriskjava.manager.internal.Dispatcher#dispatchResponse(ManagerResponse)
* @see org.asteriskjava.manager.internal.Dispatcher#dispatchEvent(ManagerEvent, Integer)
* @see org.asteriskjava.manager.internal.Dispatcher#dispatchResponse(ManagerResponse, Integer)
*/
public void run() {
long timeOfLastEvent = 0;
Expand Down Expand Up @@ -177,27 +177,16 @@ public void run() {
continue;
}

if (line.length() > 0) {
// begin of workaround for Astersik bug 13319
// see AJ-77
// Use this workaround only when line starts from "From "
// and "To "
int isFromAtStart = line.indexOf("From ");
int isToAtStart = line.indexOf("To ");

int delimiterIndex = isFromAtStart == 0 || isToAtStart == 0 ? line.indexOf(" ") : line.indexOf(":");
// end of workaround for Astersik bug 13319

int delimiterLength = 1;

if (delimiterIndex > 0 && line.length() > delimiterIndex + delimiterLength) {
String name = line.substring(0, delimiterIndex).toLowerCase(Locale.ENGLISH).trim();
String value = line.substring(delimiterIndex + delimiterLength).trim();

addToBuffer(buffer, name, value);
// TODO tracing
// logger.debug("Got name [" + name + "], value: [" +
// value + "]");
if (!line.isEmpty()) {
String[] parts = line.split(":", 2);
if (parts.length == 2) {
parts[0] = parts[0].toLowerCase(Locale.ENGLISH).trim();
parts[1] = parts[1].trim();
if (!parts[0].isEmpty()) {
addToBuffer(buffer, parts[0], parts[1]);
// TODO tracing
// logger.debug("Got name [" + name + "], value: [" + value + "]");
}
}
}

Expand Down
42 changes: 6 additions & 36 deletions src/main/java/org/asteriskjava/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -161,52 +162,21 @@ private static Map<String, Method> getSettersInternal(Class<?> clazz) {
return accessors;
}

private static final Pattern ILLEGAL_CHARS = Pattern.compile("[^a-z0-9]+");

/**
* Strips all illegal charaters from the given lower case string. Illegal
* characters are all characters that are neither characters ('a' to 'z')
* Strips all illegal characters from the given lower case string. Illegal
* characters are all characters that are neither alphabetic ('a' to 'z')
* nor digits ('0' to '9').
*
* @param s the original string
* @return the string with all illegal characters stripped
*/
public static String stripIllegalCharacters(String s) {
char c;
boolean needsStrip = false;
StringBuilder sb;

if (s == null) {
return null;
}

for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c >= '0' && c <= '9') {
// continue
} // NOPMD
else if (c >= 'a' && c <= 'z') {
// continue
} // NOPMD
else {
needsStrip = true;
break;
}
}

if (!needsStrip) {
return s;
}

sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c >= '0' && c <= '9') {
sb.append(c);
} else if (c >= 'a' && c <= 'z') {
sb.append(c);
}
}

return sb.toString();
return ILLEGAL_CHARS.matcher(s).replaceAll("");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,6 @@ void testRunReceivingEventWithMapPropertyAndOnlyOneEntry() throws Exception {
assertEquals(DisconnectEvent.class, dispatcher.dispatchedEvents.get(1).getClass(), "second event must be an DisconnectEvent");
}

@Test
void testWorkaroundForAsteriskBug13319() throws Exception {
when(socketConnectionFacade.readLine())
.thenReturn("Event: RTCPReceived")
.thenReturn("From 192.168.0.1:1234")
.thenReturn("HighestSequence: 999")
.thenReturn("")
.thenReturn(null);

managerReader.setSocket(socketConnectionFacade);
managerReader.run();

assertEquals(2, dispatcher.dispatchedEvents.size(), "not exactly two events dispatched");

assertEquals(RtcpReceivedEvent.class, dispatcher.dispatchedEvents.get(0).getClass(), "first event must be a RtcpReceivedEvent");

RtcpReceivedEvent rtcpReceivedEvent = (RtcpReceivedEvent) dispatcher.dispatchedEvents.get(0);
assertEquals("192.168.0.1", rtcpReceivedEvent.getFromAddress().getHostAddress(), "Invalid from address on RtcpReceivedEvent");
assertEquals(Integer.valueOf(1234), rtcpReceivedEvent.getFromPort(), "Invalid from port on RtcpReceivedEvent");
assertEquals(Long.valueOf(999), rtcpReceivedEvent.getHighestSequence(), "Invalid highest sequence on RtcpReceivedEvent");

assertEquals(DisconnectEvent.class, dispatcher.dispatchedEvents.get(1).getClass(), "second event must be a DisconnectEvent");
}

// todo fix testRunReceivingUserEvent
void XtestRunReceivingUserEvent() throws Exception {
managerReader.registerEventClass(MyUserEvent.class);
Expand Down