Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2016, TeleStax Inc. and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* JBoss, Home of Professional Open Source
* Copyright 2007-2011, Red Hat, Inc. and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jdiameter.client.impl.annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdiameter.api.ApplicationId;
import org.jdiameter.api.Avp;
import org.jdiameter.api.AvpDataException;
import org.jdiameter.api.AvpSet;
import org.jdiameter.api.InternalException;
import org.jdiameter.api.Message;
import org.jdiameter.api.MetaData;
import org.jdiameter.api.Request;
import org.jdiameter.api.SessionFactory;
import org.jdiameter.api.annotation.AvpDscr;
import org.jdiameter.api.annotation.AvpFlag;
import org.jdiameter.api.annotation.AvpType;
import org.jdiameter.api.annotation.Child;
import org.jdiameter.api.annotation.CommandDscr;
import org.jdiameter.api.annotation.CommandFlag;
import org.jdiameter.api.annotation.Getter;
import org.jdiameter.api.annotation.Setter;
import org.jdiameter.client.api.IMessage;
import org.jdiameter.client.api.annotation.IRecoder;
import org.jdiameter.client.api.annotation.RecoderException;
import org.jdiameter.client.impl.RawSessionImpl;
import org.jdiameter.client.impl.annotation.internal.ClassInfo;
import org.jdiameter.client.impl.annotation.internal.ConstructorInfo;
import org.jdiameter.client.impl.annotation.internal.MethodInfo;
import org.jdiameter.client.impl.annotation.internal.Storage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author [email protected]
* @author Alexandre Mendonca
* @author Bartosz Baranowski
*/
@SuppressWarnings("all") //3rd party lib
public class Recoder implements IRecoder {
// TODO full min/max/position constrains and optimization (caching)
private static final Logger log = LoggerFactory.getLogger(Recoder.class);
private Storage storage = new Storage();
private final RawSessionImpl rawSession;
private final MetaData metaData;
public Recoder(SessionFactory factory, MetaData metaData) {
this.metaData = metaData;
try {
this.rawSession = (RawSessionImpl) factory.getNewRawSession();
} catch (InternalException e) {
throw new IllegalArgumentException(e);
}
}
// =======================================================================================
//@Override
@Override
public Message encodeToRequest(Object yourDomainMessageObject, Avp... additionalAvp) throws RecoderException {
return encode(yourDomainMessageObject, null, 0, additionalAvp);
}
//@Override
@Override
public Message encodeToAnswer(Object yourDomainMessageObject, Request request, long resultCode) throws RecoderException {
return encode(yourDomainMessageObject, request, resultCode);
}
public Message encode(Object yourDomainMessageObject, Request request, long resultCode, Avp... addAvp)
throws RecoderException {
IMessage message = null;
ClassInfo classInfo = storage.getClassInfo(yourDomainMessageObject.getClass());
CommandDscr commandDscr = classInfo.getAnnotation(CommandDscr.class);
if (commandDscr != null) {
// Get command parameters
if (request == null) {
message = (IMessage) rawSession.createMessage(commandDscr.code(), ApplicationId.createByAccAppId(0));
message.setRequest(true);
message.getAvps().addAvp(addAvp);
try {
if (message.getAvps().getAvp(Avp.AUTH_APPLICATION_ID) != null) {
message.setHeaderApplicationId(message.getAvps().getAvp(Avp.AUTH_APPLICATION_ID).getUnsigned32());
} else if (message.getAvps().getAvp(Avp.ACCT_APPLICATION_ID) != null) {
message.setHeaderApplicationId(message.getAvps().getAvp(Avp.ACCT_APPLICATION_ID).getUnsigned32());
} else if (message.getAvps().getAvp(Avp.VENDOR_SPECIFIC_APPLICATION_ID) != null) {
message.setHeaderApplicationId(message.getAvps().getAvp(Avp.VENDOR_SPECIFIC_APPLICATION_ID).getGrouped()
.getAvp(Avp.VENDOR_ID).getUnsigned32());
}
} catch (Exception exc) {
throw new RecoderException(exc);
}
if (message.getAvps().getAvp(Avp.ORIGIN_HOST) == null) {
message.getAvps().addAvp(Avp.ORIGIN_HOST, metaData.getLocalPeer().getUri().getFQDN(), true, false, true);
}
if (message.getAvps().getAvp(Avp.ORIGIN_REALM) == null) {
message.getAvps().addAvp(Avp.ORIGIN_REALM, metaData.getLocalPeer().getRealmName(), true, false, true);
}
} else {
message = (IMessage) request.createAnswer(resultCode);
}
for (CommandFlag f : commandDscr.flags()) {
switch (f) {
case E:
message.setError(true);
break;
case P:
message.setProxiable(true);
break;
case R:
message.setRequest(true);
break;
case T:
message.setReTransmitted(true);
break;
}
}
// Find top level avp in getter-annotation methods
Map chMap = getChildInstance(yourDomainMessageObject, classInfo, null);
// Fill
for (Child ch : commandDscr.childs()) {
fillChild(message.getAvps(), ch, chMap);
}
} else {
log.debug("Can not found annotation for object {}", yourDomainMessageObject);
}
return message;
}
private Map getChildInstance(Object yourDomainMessageObject, ClassInfo c, Map chMap)
throws RecoderException {
if (chMap == null) {
chMap = new HashMap();
}
for (MethodInfo mi : c.getMethodsInfo()) {
if (mi.getAnnotation(Getter.class) != null) {
try {
Object value = mi.getMethod().invoke(yourDomainMessageObject);
if (value != null) {
Class mc = value.getClass().isArray() ? value.getClass().getComponentType() : value.getClass();
chMap.put(mc.getName(), value);
for (Class> i : mc.getInterfaces()) {
chMap.put(i.getName(), value);
}
}
} catch (IllegalAccessException e) {
throw new RecoderException(e);
} catch (InvocationTargetException e) {
throw new RecoderException(e);
}
}
}
return chMap;
}
private void fillChild(AvpSet as, Child ci, Map childs) throws RecoderException {
Object c = childs.get(ci.ref().getName());
if (c != null) {
ClassInfo cc = storage.getClassInfo(ci.ref());
AvpDscr ad = cc.getAnnotation(AvpDscr.class);
if (ad != null) {
boolean m = false, p = false;
// cast <=> getter for primitive
switch (ad.type()) {
case Integer32:
case Enumerated: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
// find in getter
Collection cv = getValue(c, Integer.class);
for (Integer v : cv) {
as.addAvp(ad.code(), v, ad.vendorId(), m, p);
}
}
break;
case Unsigned32: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
Collection cv = getValue(c, Long.class);
for (Long v : cv) {
as.addAvp(ad.code(), v, ad.vendorId(), m, p, true);
}
}
break;
case Unsigned64:
case Integer64: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
Collection cv = getValue(c, Long.class);
for (Long v : cv) {
as.addAvp(ad.code(), v, ad.vendorId(), m, p);
}
}
break;
case Float32: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
Collection cv = getValue(c, Float.class);
for (Float v : cv) {
as.addAvp(ad.code(), v, ad.vendorId(), m, p);
}
}
break;
case Float64: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
Collection cv = getValue(c, Double.class);
for (Double v : cv) {
as.addAvp(ad.code(), v, ad.vendorId(), m, p);
}
}
break;
case OctetString:
case Address:
case Time:
case DiameterIdentity:
case DiameterURI:
case IPFilterRule:
case QoSFilterRule: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
Collection cv = getValue(c, String.class);
for (String v : cv) {
as.addAvp(ad.code(), v, ad.vendorId(), m, p, true);
}
}
break;
case UTF8String: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
Collection cv = getValue(c, String.class);
for (String v : cv) {
as.addAvp(ad.code(), v, ad.vendorId(), m, p, false);
}
}
break;
case Grouped: {
for (AvpFlag f : ad.must()) {
if (AvpFlag.M.equals(f)) {
m = true;
} else if (AvpFlag.P.equals(f)) {
p = true;
}
}
Collection