@Override public boolean isAllowed(int avpCode, long vendorId) { if (this.isGrouped()) { // make better get ? for (AvpRepresentation rep : this.children) { if (rep.getCode() == avpCode && rep.getVendorId() == vendorId) { return rep.isAllowed(); } else { continue; } } return true; } else { return false; } }
protected void printAvpTree(AvpRepresentation rep, String tab) { String x = tab + "+-- " + rep.getCode() + "/" + rep.getVendorId(); while (x.length() < 25) { x += "."; } System.out.println(x + rep.getName() + " > " + rep.getType()); if (rep.isGrouped()) { for (AvpRepresentation repC : rep.getChildren()) { printAvpTree(repC, " " + tab); } } }
@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()); } } } }
public void sendReAuthAnswer(ReAuthAnswer rar) throws IOException { DiameterMessageImpl msg = (DiameterMessageImpl) rar; try { session.sendReAuthAnswer(new ReAuthAnswerImpl((Answer) msg.getGenericData())); } catch (org.jdiameter.api.validation.AvpNotAllowedException e) { throw new AvpNotAllowedException("Message validation failed.", e, e.getAvpCode(), e.getVendorId()); } catch (Exception e) { throw new IOException("Failed to send message, due to: " + e); } }
@Override public void validate(AvpSet avpSet) throws AvpNotAllowedException { //this is used in RAs, cause ... AvpSet is asexual AVP, no code, no vendor // let it rip for (AvpRepresentation childrenVAvp : getChildren()) { AvpSet childSset = avpSet.getAvps(childrenVAvp.getCode(), childrenVAvp.getVendorId()); int count = childSset.size(); if (!childrenVAvp.isCountValidForMultiplicity(count)) { throw new AvpNotAllowedException("AVP: " + childrenVAvp + " has wrong count, in grouped parent avp - " + (count) + ", allowed: " + childrenVAvp.getMultiplicityIndicator(), getCode(), getVendorId()); } if (childrenVAvp.isGrouped()) { for (int index = 0; index < childSset.size(); index++) { Avp presumablyGrouped = childSset.getAvpByIndex(index); childrenVAvp.validate(presumablyGrouped); } } // else we are good ? } }
@Override public void validate(Message msg, ValidatorLevel validatorLevel) throws AvpNotAllowedException { if (validatorLevel == ValidatorLevel.OFF) { return; } // if its !OFF, we will go down, at least to this section for (AvpRepresentation ap : this.unmuttableMessageAvps.values()) { AvpSet innerSet = msg.getAvps().getAvps(ap.getCode(), ap.getVendorId()); int count = 0; if (innerSet != null) { count = innerSet.size(); } if (!ap.isCountValidForMultiplicity(count)) { throw new AvpNotAllowedException("AVP: \n" + ap + "\n, has wrong count in message - " + (count), ap.getCode(), ap.getVendorId()); } // if its ALL, we need to go down deeper in AVPs if (validatorLevel != ValidatorLevel.ALL) { continue; } if (count != 0 && ap.isGrouped()) { // we are grouped validateGrouped(ap, innerSet); } } }
@Override public void validate(DiameterAvp avp) throws AvpNotAllowedException { Dictionary dictionary = DictionarySingleton.getDictionary(); if (dictionary.isConfigured() && dictionary.isEnabled()) { AvpRepresentation rep = dictionary.getAvp(avp.getCode(), avp.getVendorId()); // check for grouped? if (rep != null && rep.isGrouped()) { try { GroupedAvpImpl impl = (GroupedAvpImpl) avp; rep.validate(impl.getGenericData()); } catch (ClassCastException cce) { throw new AvpNotAllowedException("Failed to validate avp, its not grouped!", cce, avp.getCode(), avp.getVendorId()); } } } }
@Override public boolean sendMessage(IMessage message) throws TransportException, OverloadException, InternalException { if (dictionary != null && dictionary.isEnabled()) { logger.debug("Message validation is ENABLED. Going to validate message before sending."); dictionary.validate(message, false); } return !stopping && fsm.handleEvent(new FsmEvent(EventTypes.SEND_MSG_EVENT, message)); }
@Override public boolean isCountValidForMultiplicity(int code, long vendorId, int avpCount) { AvpRepresentation avpRep = getAvp(code, vendorId); if (avpRep == null) { return true; } return avpRep.isCountValidForMultiplicity(avpCount); }
static void init(String clazz, boolean validatorEnabled, ValidatorLevel validatorSendLevel, ValidatorLevel validatorReceiveLevel) throws InternalException { try { Class.forName(clazz).getMethod("getInstance", String.class).invoke(null, new Object[] {null}); DictionaryImpl.INSTANCE.setEnabled(validatorEnabled); DictionaryImpl.INSTANCE.setSendLevel(validatorSendLevel); DictionaryImpl.INSTANCE.setReceiveLevel(validatorReceiveLevel); } catch (Exception e) { throw new InternalException(e); } } }
@Override public boolean isAllowed(int code, long vendorId) { AvpRepresentation avpRep = new AvpRepresentationImpl(code, vendorId); avpRep = this.unmuttableMessageAvps.get(avpRep); if (avpRep == null) { return true; } return avpRep.isAllowed(); }
/** * @param ap * @param innerSet */ private void validateGrouped(AvpRepresentation ap, AvpSet innerSet) { // we have set of grouped avps, and ap is grouped, lets validate // NOTE this methods can be called multiple time, until we dont have for (int index = 0; index < innerSet.size(); index++) { Avp presumablyGrouped = innerSet.getAvpByIndex(index); ap.validate(presumablyGrouped); // AvpSet groupedPart = null; // try { // groupedPart = presumablyGrouped.getGrouped(); // } // catch (AvpDataException e) { // logger.debug("Failed to get grouped AVP.", e); // } // // if (groupedPart == null) { // logger.error("Avp should be grouped, but its not: " + ap); // // continue; // } // else { // validateGroupedChildren(ap, groupedPart); // } } }
@Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append(isRequest ? "Request" : "Answer").append(" code: ").append(this.commandCode).append(" applicationId: ").append(this.applicationId) .append(" name: ").append(this.name); for (AvpRepresentation childAvp : this.getMessageAvps().values()) { sb.append("\n").append(childAvp.toString()); } return sb.toString(); }
@Override public void validate(Avp avp) throws AvpNotAllowedException { if (isGrouped()) { try { AvpSet avpAsGrouped = avp.getGrouped(); validate(avpAsGrouped); } catch (AvpDataException e) { throw new AvpNotAllowedException("Failed to parse AVP to grouped!", e, code, vendor); } } else { // dont care } }
public void sendUserDataRequest(UserDataRequest message) throws IOException { try { DiameterMessageImpl msg = (DiameterMessageImpl) message; this.clientSession.sendUserDataRequest(new UserDataRequestImpl((Request) msg.getGenericData())); } catch (org.jdiameter.api.validation.AvpNotAllowedException e) { throw new AvpNotAllowedException("Message validation failed.", e, e.getAvpCode(), e.getVendorId()); } catch (Exception e) { throw new IOException("Failed to send message, due to: " + e); } }
@Override public boolean isCountValidForMultiplicity(AvpSet destination, int code, long vendorId, int numberToAdd) { AvpRepresentation avpRep = getAvp(code, vendorId); if (avpRep == null) { return true; } return avpRep.isCountValidForMultiplicity(destination, numberToAdd); }
@Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("name: ").append(this.getName()).append(", code: ").append(this.getCode()).append(", vendor: ").append(this.getVendorId()).append(", weak: ") .append(this.isWeak()).append(", grouped: ").append(this.isGrouped()).append(", type: ").append(this.getType()).append(", multiplicity: ") .append(this.getMultiplicityIndicator()); if (this.isGrouped()) { for (AvpRepresentation child : this.getChildren()) { String childStr = child.toString().replace("\n", "\n---"); sb.append("\n---" + childStr); } } return sb.toString(); }