org.asteriskjava.manager.internal.ActionBuilderImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of asterisk-java Show documentation
Show all versions of asterisk-java Show documentation
The free Java library for Asterisk PBX integration.
/*
* Copyright 2004-2006 Stefan Reuter and others
*
* 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.internal;
import org.asteriskjava.AsteriskVersion;
import org.asteriskjava.manager.AsteriskMapping;
import org.asteriskjava.manager.action.ManagerAction;
import org.asteriskjava.manager.action.UserEventAction;
import org.asteriskjava.manager.event.UserEvent;
import org.asteriskjava.util.Log;
import org.asteriskjava.util.LogFactory;
import org.asteriskjava.util.ReflectionUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
* Default implementation of the ActionBuilder interface.
*
* @author srt
* @version $Id$
*/
class ActionBuilderImpl implements ActionBuilder {
private static final String LINE_SEPARATOR = "\r\n";
private static final String ATTRIBUTES_PROPERTY_NAME = "attributes";
/**
* Instance logger.
*/
private final Log logger = LogFactory.getLog(getClass());
private AsteriskVersion targetVersion;
private final Set membersToIgnore = new HashSet<>();
/**
* Creates a new ActionBuilder for Asterisk 1.0.
*/
ActionBuilderImpl() {
this.targetVersion = AsteriskVersion.ASTERISK_1_0;
/*
* When using the Reflection API to get all of the getters for building
* actions to send, we ignore some of the getters
*/
this.membersToIgnore.add("class");
this.membersToIgnore.add("action");
this.membersToIgnore.add("actionid");
this.membersToIgnore.add(ATTRIBUTES_PROPERTY_NAME);
}
public void setTargetVersion(AsteriskVersion targetVersion) {
this.targetVersion = targetVersion;
}
public String buildAction(final ManagerAction action) {
return buildAction(action, null);
}
@SuppressWarnings("unchecked")
public String buildAction(final ManagerAction action, final String internalActionId) {
StringBuilder sb = new StringBuilder();
sb.append("action: ");
sb.append(action.getAction());
sb.append(LINE_SEPARATOR);
if (internalActionId != null) {
sb.append("actionid: ");
sb.append(ManagerUtil.addInternalActionId(action.getActionId(), internalActionId));
sb.append(LINE_SEPARATOR);
} else if (action.getActionId() != null) {
sb.append("actionid: ");
sb.append(action.getActionId());
sb.append(LINE_SEPARATOR);
}
Map getters;
// if this is a user event action, we need to grab the internal event,
// otherwise do below as normal
if (action instanceof UserEventAction) {
UserEvent userEvent = ((UserEventAction) action).getUserEvent();
appendUserEvent(sb, userEvent);
getters = ReflectionUtil.getGetters(userEvent.getClass());
// eventually we may want to add more Map keys for events to ignore
// when appending
appendGetters(sb, userEvent, getters);
} else {
getters = ReflectionUtil.getGetters(action.getClass());
appendGetters(sb, action, getters);
}
// actions that have the special getAttributes method will
// have their Map appended without a singular key or separator
if (getters.containsKey(ATTRIBUTES_PROPERTY_NAME)) {
Method getter = getters.get(ATTRIBUTES_PROPERTY_NAME);
Object value = null;
try {
value = getter.invoke(action);
} catch (Exception ex) {
logger.error("Unable to retrieve property '" + ATTRIBUTES_PROPERTY_NAME + "' of " + action.getClass(), ex);
}
if (value instanceof Map) {
Map