Search in sources :

Example 1 with MessageRepresentation

use of org.jdiameter.api.validation.MessageRepresentation in project jain-slee.diameter by RestComm.

the class ValidatorImpl method validate.

// NOTE: this class possibly should be Singleton, however some impl may use something more to perform validation
// hence, its not static, each RA provides instance through RA Sbb Interface.
/*
   * (non-Javadoc)
   * 
   * @see
   * net.java.slee.resource.diameter.Validator#validate(net.java.slee.resource
   * .diameter.base.events.DiameterMessage)
   */
@Override
public void validate(DiameterMessage msg) throws AvpNotAllowedException {
    Dictionary dictionary = DictionarySingleton.getDictionary();
    if (dictionary.isConfigured() && dictionary.isEnabled()) {
        DiameterCommand com = msg.getCommand();
        MessageRepresentation rep = dictionary.getMessage(com.getCode(), com.getApplicationId(), com.isRequest());
        if (rep != null) {
            DiameterMessageImpl impl = (DiameterMessageImpl) msg;
            try {
                rep.validate(impl.getGenericData(), ValidatorLevel.ALL);
            } catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
                throw new AvpNotAllowedException("Failed to validate message.", e, e.getAvpCode(), e.getVendorId());
            }
        }
    }
}
Also used : DiameterCommand(net.java.slee.resource.diameter.base.events.DiameterCommand) Dictionary(org.jdiameter.api.validation.Dictionary) MessageRepresentation(org.jdiameter.api.validation.MessageRepresentation) DiameterMessageImpl(org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl) AvpNotAllowedException(net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException)

Example 2 with MessageRepresentation

use of org.jdiameter.api.validation.MessageRepresentation in project jain-slee.diameter by RestComm.

the class AvpUtilities method performPreAddOperations.

/**
 * @param parent the Message/Grouped AVP where AVP will be added to, for validation purposes. if null, no validation is performed.
 * @param avpCode
 * @param vendorId
 * @param set
 * @throws AvpNotAllowedException
 */
private static void performPreAddOperations(Object parent, int avpCode, long vendorId, AvpSet set) throws AvpNotAllowedException {
    if (!dictionary.isEnabled()) {
        // no need to proceed any further.. no validation
        return;
    }
    if (parent instanceof Message) {
        Message msg = (Message) parent;
        MessageRepresentation msgRep = dictionary.getMessage(msg.getCommandCode(), msg.getApplicationId(), msg.isRequest());
        // if we don't know anything about this message, let's just move on..
        if (msgRep == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to find message in dictionary, skipping validation. (Command Code: " + msg.getCommandCode() + ", Application-Id: " + msg.getApplicationId() + ")");
            }
            return;
        }
        if (!msgRep.isAllowed(avpCode, vendorId)) {
            throw new AvpNotAllowedException("Avp defined by code: " + avpCode + ", vendorId: " + vendorId + " is not allowed in message - code: " + msg.getCommandCode() + ", appId: " + msg.getApplicationId() + ", isRequest: " + msg.isRequest(), avpCode, vendorId);
        }
        if (msgRep.isCountValidForMultiplicity(msg.getAvps(), avpCode, vendorId, 1)) {
            // its ok.
            return;
        } else {
            throw new AvpNotAllowedException("Avp not allowed, count exceeded.", avpCode, vendorId);
        }
    } else if (parent instanceof GroupedAvp) {
        GroupedAvpImpl gAvp = (GroupedAvpImpl) parent;
        org.jdiameter.api.validation.AvpRepresentation parentAvpRep = dictionary.getAvp(gAvp.getCode(), gAvp.getVendorId());
        // if we don't know anything about this avp, let's just move on..
        if (parentAvpRep == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to find parent AVP in dictionary, skipping validation. (AVP Code: " + gAvp.getCode() + ", Vendor-Id: " + gAvp.getVendorId() + ")");
            }
            return;
        }
        if (!parentAvpRep.isAllowed(avpCode, vendorId)) {
            throw new AvpNotAllowedException("AVP with Code '" + avpCode + "' and Vendor-Id '" + vendorId + "' is not allowed as a child of AVP with Code '" + parentAvpRep.getCode() + "' and Vendor-Id '" + parentAvpRep.getVendorId() + "'.", avpCode, vendorId);
        }
        // Now we get the actual AVP to add representation...
        org.jdiameter.api.validation.AvpRepresentation avpRep = dictionary.getAvp(avpCode, vendorId);
        // ..to check if it can be added (multiplicity) to the parent.
        if (avpRep.isCountValidForMultiplicity(gAvp.getGenericData(), 1)) {
            // its ok.
            return;
        } else {
            throw new AvpNotAllowedException("Avp not allowed, count exceeded.", avpCode, vendorId);
        }
    }
}
Also used : GroupedAvpImpl(org.mobicents.slee.resource.diameter.base.events.avp.GroupedAvpImpl) Message(org.jdiameter.api.Message) MessageRepresentation(org.jdiameter.api.validation.MessageRepresentation) AvpRepresentation(org.mobicents.diameter.dictionary.AvpRepresentation)

Aggregations

MessageRepresentation (org.jdiameter.api.validation.MessageRepresentation)2 DiameterCommand (net.java.slee.resource.diameter.base.events.DiameterCommand)1 AvpNotAllowedException (net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException)1 Message (org.jdiameter.api.Message)1 Dictionary (org.jdiameter.api.validation.Dictionary)1 AvpRepresentation (org.mobicents.diameter.dictionary.AvpRepresentation)1 DiameterMessageImpl (org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl)1 GroupedAvpImpl (org.mobicents.slee.resource.diameter.base.events.avp.GroupedAvpImpl)1