Thanks to visit codestin.com
Credit goes to github.com

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
@@ -1,22 +1,22 @@
/*
* Copyright 2004-2006 Stefan Reuter
* Copyright 2004-2022 Asterisk-Java contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asteriskjava.manager.internal;

import org.asteriskjava.manager.event.*;
import org.asteriskjava.manager.util.EventAttributesHelper;
import org.asteriskjava.util.Log;
import org.asteriskjava.util.LogFactory;
import org.reflections.Reflections;
Expand All @@ -33,15 +33,15 @@
* @version $Id$
* @see org.asteriskjava.manager.event.ManagerEvent
*/
class EventBuilderImpl extends AbstractBuilder implements EventBuilder {
class EventBuilderImpl implements EventBuilder {
private static final Set<String> ignoredAttributes = new HashSet<>(Arrays.asList("event"));
private Map<String, Class<?>> registeredEventClasses;
private final Set<String> eventClassNegativeCache = new HashSet<>();

private static final Log logger = LogFactory.getLog(EventBuilderImpl.class);

private final static Set<Class<? extends ManagerEvent>> knownManagerEventClasses = new Reflections(
"org.asteriskjava.manager.event").getSubTypesOf(ManagerEvent.class);
"org.asteriskjava.manager.event").getSubTypesOf(ManagerEvent.class);

EventBuilderImpl() {
this.registeredEventClasses = new HashMap<>();
Expand Down Expand Up @@ -87,7 +87,7 @@ public final void registerEventClass(Class<? extends ManagerEvent> clazz) throws
* @throws IllegalArgumentException if clazz is not a valid event class.
*/
public final void registerEventClass(String eventType, Class<? extends ManagerEvent> clazz)
throws IllegalArgumentException {
throws IllegalArgumentException {
Constructor<?> defaultConstructor;

if (Modifier.isAbstract(clazz.getModifiers())) {
Expand Down Expand Up @@ -131,8 +131,8 @@ public ManagerEvent buildEvent(Object source, Map<String, Object> attributes) {
// Convert map of lists to list of maps - one map for each
// PeerEntry event
int peersAmount = attributes.get("listitems") != null
? Integer.parseInt((String) attributes.get("listitems"))
: eventNames.size() - 1; // Last event is
? Integer.parseInt((String) attributes.get("listitems"))
: eventNames.size() - 1; // Last event is
// PeerlistComplete
List<Map<String, Object>> peersAttributes = new ArrayList<>();
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
Expand Down Expand Up @@ -188,7 +188,7 @@ public ManagerEvent buildEvent(Object source, Map<String, Object> attributes) {
if (eventClass == null) {
if (eventClassNegativeCache.add(eventType)) {
logger.info("No event class registered for event type '" + eventType + "', attributes: " + attributes
+ ". Please report at https://github.com/asterisk-java/asterisk-java/issues");
+ ". Please report at https://github.com/asterisk-java/asterisk-java/issues");
}
return null;
}
Expand All @@ -214,7 +214,7 @@ public ManagerEvent buildEvent(Object source, Map<String, Object> attributes) {
// being passed around.
for (Map<String, Object> peerAttrs : (List<Map<String, Object>>) attributes.get("peersAttributes")) {
PeerEntryEvent peerEntryEvent = new PeerEntryEvent(source);
setAttributes(peerEntryEvent, peerAttrs, ignoredAttributes);
EventAttributesHelper.setAttributes(peerEntryEvent, peerAttrs, ignoredAttributes);
List<PeerEntryEvent> peerEntryEvents = peersEvent.getChildEvents();
if (peerEntryEvents == null) {
peerEntryEvents = new ArrayList<>();
Expand All @@ -224,7 +224,7 @@ public ManagerEvent buildEvent(Object source, Map<String, Object> attributes) {
}
peersEvent.setActionId(peersEvent.getChildEvents().get(0).getActionId());
} else {
setAttributes(event, attributes, ignoredAttributes);
EventAttributesHelper.setAttributes(event, attributes, ignoredAttributes);
}

// ResponseEvents are sent in response to a ManagerAction if the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
/*
* Copyright 2004-2006 Stefan Reuter
* Copyright 2004-2022 Asterisk-Java contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asteriskjava.manager.internal;

import org.asteriskjava.manager.response.CommandResponse;
import org.asteriskjava.manager.response.ManagerError;
import org.asteriskjava.manager.response.ManagerResponse;
import org.asteriskjava.manager.util.EventAttributesHelper;
import org.slf4j.Logger;

import java.util.*;

import static org.slf4j.LoggerFactory.getLogger;

/**
* Default implementation of the ResponseBuilder interface.
Expand All @@ -30,9 +32,11 @@
* @version $Id$
* @see org.asteriskjava.manager.response.ManagerResponse
*/
class ResponseBuilderImpl extends AbstractBuilder implements ResponseBuilder {
class ResponseBuilderImpl implements ResponseBuilder {
private static final Logger logger = getLogger(ResponseBuilderImpl.class);

private static final Set<String> ignoredAttributes = new HashSet<>(Arrays.asList(
"attributes", "proxyresponse", ManagerReader.COMMAND_RESULT_RESPONSE_KEY));
"attributes", "proxyresponse", ManagerReader.COMMAND_RESULT_RESPONSE_KEY));

private static final String RESPONSE_KEY = "response";
private static final String PROXY_RESPONSE_KEY = "proxyresponse";
Expand All @@ -57,7 +61,7 @@ public ManagerResponse buildResponse(Class<? extends ManagerResponse> responseCl
}
}

setAttributes(response, attributes, ignoredAttributes);
EventAttributesHelper.setAttributes(response, attributes, ignoredAttributes);

if (response instanceof CommandResponse) {
final CommandResponse commandResponse = (CommandResponse) response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package org.asteriskjava.manager.internal;
/*
* Copyright 2004-2022 Asterisk-Java contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asteriskjava.manager.util;

import org.asteriskjava.manager.event.UserEvent;
import org.asteriskjava.manager.response.ManagerResponse;
import org.asteriskjava.util.AstUtil;
import org.asteriskjava.util.Log;
import org.asteriskjava.util.LogFactory;
import org.asteriskjava.util.ReflectionUtil;
import org.slf4j.Logger;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import static org.slf4j.LoggerFactory.getLogger;

/**
* Abstract base class for reflection based builders.
* @author Piotr Olaszewski <piotrooo>
*/
abstract class AbstractBuilder {
protected final Log logger = LogFactory.getLog(getClass());
public class EventAttributesHelper {
private static final Logger logger = getLogger(EventAttributesHelper.class);

private EventAttributesHelper() {
}

protected void setAttributes(Object target, Map<String, Object> attributes, Set<String> ignoredAttributes) {
public static void setAttributes(Object target, Map<String, Object> attributes, Set<String> ignoredAttributes) {
Map<String, Method> setters;

setters = ReflectionUtil.getSetters(target.getClass());
Expand Down Expand Up @@ -64,10 +82,10 @@ protected void setAttributes(Object target, Map<String, Object> attributes, Set<
// intentional
if (setter == null && !(target instanceof UserEvent) && !target.getClass().equals(ManagerResponse.class)) {
logger.warn("Unable to set property '" + entry.getKey() + "' to '" + entry.getValue() + "' on "
+ target.getClass().getName()
+ ": no setter. Please report at https://github.com/asterisk-java/asterisk-java/issues");
+ target.getClass().getName()
+ ": no setter. Please report at https://github.com/asterisk-java/asterisk-java/issues");

for (Entry<String, Object> entry2 : attributes.entrySet()) {
for (Map.Entry<String, Object> entry2 : attributes.entrySet()) {
logger.debug("Key: " + entry2.getKey() + " Value: " + entry2.getValue());
}
}
Expand Down Expand Up @@ -100,21 +118,21 @@ protected void setAttributes(Object target, Map<String, Object> attributes, Set<
} else value = constructor.newInstance(entry.getValue());
} catch (Exception e) {
logger.error("Unable to convert value: Called the constructor of " + dataType + " with value '"
+ entry.getValue() + "' for the attribute '" + entry.getKey() + "'\n of event type "
+ target.getClass().getName() + " with resulting error: " + e.getMessage(), e);
+ entry.getValue() + "' for the attribute '" + entry.getKey() + "'\n of event type "
+ target.getClass().getName() + " with resulting error: " + e.getMessage(), e);
continue;
}
}

setter.invoke(target, value);
} catch (Exception e) {
logger.error("Unable to set property '" + entry.getKey() + "' to '" + entry.getValue() + "' on "
+ target.getClass().getName() + " " + e.getMessage(), e);
+ target.getClass().getName() + " " + e.getMessage(), e);
}
}
}

private Integer parseInteger(Entry<String, Object> entry) {
private static Integer parseInteger(Map.Entry<String, Object> entry) {
Integer value;
String stringValue = (String) entry.getValue();
if (stringValue != null && stringValue.length() > 0) {
Expand All @@ -126,7 +144,7 @@ private Integer parseInteger(Entry<String, Object> entry) {
}

@SuppressWarnings("unchecked")
private Map<String, String> parseMap(Map.Entry<String, Object> entry) {
private static Map<String, String> parseMap(Map.Entry<String, Object> entry) {
Map<String, String> value;
if (entry.getValue() instanceof List) {
List<String> list = (List<String>) entry.getValue();
Expand All @@ -140,7 +158,7 @@ private Map<String, String> parseMap(Map.Entry<String, Object> entry) {
}

@SuppressWarnings("unchecked")
private Object parseString(Map.Entry<String, Object> entry) {
private static Object parseString(Map.Entry<String, Object> entry) {
Object value;
value = entry.getValue();
if (AstUtil.isNull(value)) {
Expand All @@ -159,7 +177,7 @@ private Object parseString(Map.Entry<String, Object> entry) {
return value;
}

private Double parseDouble(Map.Entry<String, Object> entry) {
private static Double parseDouble(Map.Entry<String, Object> entry) {
Double value;
String stringValue = (String) entry.getValue();
if (stringValue != null && stringValue.length() > 0) {
Expand All @@ -170,7 +188,7 @@ private Double parseDouble(Map.Entry<String, Object> entry) {
return value;
}

private Long parseLong(Map.Entry<String, Object> entry) {
private static Long parseLong(Map.Entry<String, Object> entry) {
Long value;
String stringValue = (String) entry.getValue();
if (stringValue != null && stringValue.length() > 0) {
Expand All @@ -181,7 +199,7 @@ private Long parseLong(Map.Entry<String, Object> entry) {
return value;
}

private Map<String, String> buildMap(String... lines) {
private static Map<String, String> buildMap(String... lines) {
if (lines == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2004-2022 Asterisk-Java contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asteriskjava.manager.event;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.HashSet;

import static org.assertj.core.api.Assertions.assertThat;
import static org.asteriskjava.manager.util.EventAttributesHelper.setAttributes;

class AgiExecStartEventTest {
@Test
void shouldCreateEvent() {
//given
AgiExecStartEvent agiExecStartEvent = new AgiExecStartEvent(new Object());

HashMap<String, Object> attributes = new HashMap<>();
attributes.put("linkedid", "1617757665.32");
attributes.put("language", "en");

//when
setAttributes(agiExecStartEvent, attributes, new HashSet<>());

//then
assertThat(agiExecStartEvent.getLinkedid()).isEqualTo("1617757665.32");
assertThat(agiExecStartEvent.getLanguage()).isEqualTo("en");
}
}