Example usage for org.dom4j Element attributeValue

List of usage examples for org.dom4j Element attributeValue

Introduction

In this page you can find the example usage for org.dom4j Element attributeValue.

Prototype

String attributeValue(QName qName);

Source Link

Document

This returns the attribute value for the attribute with the given fully qualified name or null if there is no such attribute or the empty string if the attribute value is empty.

Usage

From source file:com.devoteam.srit.xmlloader.msrp.StackMsrp.java

License:Open Source License

/** Creates a Channel specific to each Stack */
@Override//from  w ww .  j  ava 2 s .  co  m
public synchronized Channel parseChannelFromXml(Element root, String protocol) throws Exception {
    String name = root.attributeValue("name");
    String localHost = root.attributeValue("localHost");
    String localPort = root.attributeValue("localPort");
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");
    String transport = root.attributeValue("transport");

    if ((remoteHost == null) || (remotePort == null)) {
        throw new Exception("Missing one of the remoteHost or remotePort parameter to create channel.");
    }

    if (localHost == null) {
        localHost = Utils.getLocalAddress().getHostAddress();
    }

    if (null == transport) {
        transport = getConfig().getString("listenpoint.TRANSPORT");
        if (null == transport) {
            throw new Exception("Transport(tcp or tls) not set in openChannelMSRP nor in msrp.properties");
        }
    } else if (!transport.toUpperCase().equals(StackFactory.PROTOCOL_TCP)
            && !transport.toUpperCase().equals(StackFactory.PROTOCOL_TLS)) {
        throw new Exception("Transport in openChannelMSRP must be tcp or tls");
    }

    if (existsChannel(name)) {
        return getChannel(name);
    } else {
        return new ChannelMsrp(name, localHost, localPort, remoteHost, remotePort, protocol,
                transport.toUpperCase());
    }
}

From source file:com.devoteam.srit.xmlloader.pcp.StackPcp.java

License:Open Source License

/** Creates a Channel specific to each Stack */
@Override/*from   w w  w.  j av  a2  s.c om*/
public synchronized Channel parseChannelFromXml(Element root, String protocol) throws Exception {
    String name = root.attributeValue("name");
    String localHost = root.attributeValue("localHost");
    String localPort = root.attributeValue("localPort");
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");
    String infranetConnection = root.attributeValue("infranetConnection");
    String loginType = root.attributeValue("loginType");

    if (((infranetConnection != null) && (loginType == null))
            || ((infranetConnection == null) && (loginType != null)))
        throw new Exception("infranetConnection and loginType must be set both or not set.");

    if ((remoteHost == null) || (remotePort == null)) {
        throw new Exception("Missing one of the remoteHost or remotePort parameter to create channel.");
    }

    if (localHost == null) {
        localHost = Utils.getLocalAddress().getHostAddress();
    }

    if (existsChannel(name)) {
        return getChannel(name);
    } else {
        return new ChannelPcp(name, localHost, localPort, remoteHost, remotePort, protocol, infranetConnection,
                loginType);
    }
}

From source file:com.devoteam.srit.xmlloader.pop.StackPop.java

License:Open Source License

/** Creates a Channel specific to each Stack */
@Override/*w w w. j a  va  2 s .c o  m*/
public synchronized Channel parseChannelFromXml(Element root, String protocol) throws Exception {
    String name = root.attributeValue("name");
    String localHost = root.attributeValue("localHost");
    String localPort = root.attributeValue("localPort");
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");

    if ((remoteHost == null) || (remotePort == null)) {
        throw new Exception("Missing one of the remoteHost or remotePort parameter to create channel.");
    }

    if (localHost == null) {
        localHost = Utils.getLocalAddress().getHostAddress();
    }

    if (existsChannel(name)) {
        return getChannel(name);
    } else {
        return new ChannelPop(name, localHost, localPort, remoteHost, remotePort, protocol);
    }
}

From source file:com.devoteam.srit.xmlloader.pop.StackPop.java

License:Open Source License

/** Creates a specific Msg */
@Override/*from  w w w  .  jav a2 s . c  o m*/
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {

    MsgPop msg = new MsgPop(root.getText().trim());

    String channelName = root.attributeValue("channel");
    String transactionId = root.attributeValue("transactionId");
    if (null != channelName && existsChannel(channelName)) {
        msg.setChannel(getChannel(channelName));
    }

    if (null != transactionId) {
        Trans trans = super.getInTransaction(new TransactionId(transactionId));
        msg.setChannel(trans.getBeginMsg().getChannel());

    }

    ChannelPop channel = (ChannelPop) msg.getChannel();

    if (channel.isServer()) {
        channel.checkTransactionResponse(msg);
    } else {
        channel.checkTransactionRequest(msg);
    }

    return msg;
}

From source file:com.devoteam.srit.xmlloader.radius.ChannelRadius.java

License:Open Source License

/** 
 * Parse the message from XML element //from www.j a v a2s  . com
 */
@Override
public void parseFromXml(Element root, Runner runner, String protocol) throws Exception {
    super.parseFromXml(root, runner, protocol);

    String secret = root.attributeValue("secret");
    this.secret = new ReadOnlyDefaultArray(secret.getBytes());
    this.identifierHandler = new IdentifierHandler();

    // DEPRECATED begin
    String name = root.attributeValue("socketName");
    if (name != null) {
        this.name = name;
    }
    // DEPRECATED end
}

From source file:com.devoteam.srit.xmlloader.radius.ListenpointRadius.java

License:Open Source License

/** Creates a Listenpoint specific from XML tree*/
public ListenpointRadius(Stack stack, Element root) throws Exception {
    super(stack, root);

    String secret = root.attributeValue("secret");
    this.secret = new ReadOnlyDefaultArray(secret.getBytes());
    this.identifierHandler = new IdentifierHandler();
}

From source file:com.devoteam.srit.xmlloader.radius.StackRadius.java

License:Open Source License

/** Creates a Listenpoint specific to each Stack */
@Override//from  w  w w .j a  v  a 2s.  com
public synchronized Listenpoint parseListenpointFromXml(Element root) throws Exception {
    String name = root.attributeValue("name");
    Listenpoint listenpoint = getListenpoint(name);
    if (listenpoint != null)
        return listenpoint;
    else
        return new ListenpointRadius(this, root);
}

From source file:com.devoteam.srit.xmlloader.radius.StackRadius.java

License:Open Source License

/** Creates a Channel specific to each Stack */
// deprecated part //
@Override//ww w  .  j  a va2s  .c o m
public Channel parseChannelFromXml(Element root, String protocol) throws Exception {
    String socketName = root.attributeValue("socketName");
    String localHost = root.attributeValue("localHost");
    String localPort = root.attributeValue("localPort");
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");
    String secret = root.attributeValue("secret");

    if (existsChannel(socketName)) {
        return getChannel(socketName);
    } else {
        if (null != localHost)
            localHost = InetAddress.getByName(localHost).getHostAddress();
        else
            localHost = "0.0.0.0";

        if (null != remoteHost)
            remoteHost = InetAddress.getByName(remoteHost).getHostAddress();

        return new ChannelRadius(socketName, localHost, localPort, remoteHost, remotePort, protocol, secret);
    }
}

From source file:com.devoteam.srit.xmlloader.radius.StackRadius.java

License:Open Source License

private AVPBytes parseAVP(Integer vendorCode, Element avp) throws Exception {
    String avpCodeAttribute = avp.attributeValue("code");
    String avpTypeAttribute = avp.attributeValue("type");
    String avpValueAttribute = avp.attributeValue("value");

    RadiusAttributes radiusAttributes = this.radiusDictionary.getRadiusVendors()
            .getRadiusAttributes(vendorCode);

    /*/*w  w w . j  a va2  s.  c o  m*/
     * Read this AVP code, if the code is not an integer, then try to decode
     * it using the dictionary for the given vendorCode.
     */
    Integer avpCode;
    if (!Utils.isInteger(avpCodeAttribute)) {
        if (null == radiusAttributes)
            throw new Exception("Could not get attribute list from dictionary for vendor" + vendorCode);

        avpCode = radiusAttributes.getAttributeCode(avpCodeAttribute);

        if (null == avpCode)
            throw new Exception("Could not get code from dictionary for avp named" + avpCodeAttribute);
    } else {
        avpCode = Integer.valueOf(avpCodeAttribute);
    }

    /*
     * Override the AVP type to vendor-specific if the code is 26
     */
    if (26 == avpCode) {
        avpTypeAttribute = "vendor-specific";
    }

    /*
     * Read this AVP type. if the type is not set, try to read it from the
     * dictionary, based on the AVP code.
     */
    if (null == avpTypeAttribute) {
        if (null == radiusAttributes)
            throw new Exception("Could not get attribute list from dictionary for vendor" + vendorCode);

        avpTypeAttribute = radiusAttributes.getAttributeType(avpCode);

        if (null == avpTypeAttribute)
            throw new Exception("Could not get type from dictionary for avp " + avp.asXML()
                    + " please extend the dictionary or set the type in XML");
    }

    /*
     * Safety check, for now only vendor-specific data can contain data
     * defined using <data> or <avp> elements.
     */
    if (26 != avpCode && (null != avp.element("avp") || null != avp.element("data"))) {
        throw new ParsingException("A non vendor-specific AVP can't contain vendor-specific avp nor data");
    }

    /*
     * Now create the AVP object depending on type.
     */

    if (avpTypeAttribute.equalsIgnoreCase("vendor-specific")) {
        /*
         * Create a vendor-specific AVP.
         */
        String vendorDataVendorAttribute = avpValueAttribute;

        Integer vendorDataVendor;
        if (!Utils.isInteger(vendorDataVendorAttribute))
            vendorDataVendor = this.radiusDictionary.getRadiusVendors()
                    .getVendorCode(vendorDataVendorAttribute);
        else
            vendorDataVendor = Integer.parseInt(vendorDataVendorAttribute);

        if (null == vendorDataVendor)
            throw new Exception("Could not get vendor code for vendor named " + vendorDataVendorAttribute);

        SupArray vendorData = new SupArray();

        for (Object object : avp.elements()) {
            Element element = (Element) object;
            if (element.getName().equals("data")) {
                String vendorDataValueAttribute = element.attributeValue("value");
                DefaultArray vendorDataValueArray = new DefaultArray(
                        Utils.parseBinaryString(vendorDataValueAttribute));
                vendorData.addLast(vendorDataValueArray);
            } else {
                vendorData.addLast(this.parseAVP(vendorDataVendor, element).getArray());
            }
        }

        return new AVPVendorSpecific(26, vendorDataVendor, vendorData);
    } else if (avpTypeAttribute.equalsIgnoreCase("integer") || avpTypeAttribute.equalsIgnoreCase("date")) {
        Integer avpValue;

        /*
         * If the value is not an integer then try to decode it using the
         * dictionary (contains list of enumerated values).
         */
        if (!Utils.isInteger(avpValueAttribute)) {
            if (null == radiusAttributes)
                throw new Exception("Could not get attribute list from dictionary for vendor" + vendorCode);
            RadiusValues radiusValues = radiusAttributes.getAttributeRadiusValues(avpCode);
            if (null == radiusValues)
                throw new Exception("There is no input in dictionary for value " + avpValueAttribute
                        + " of avp " + avp.asXML());
            avpValue = radiusValues.getValueCode(avpValueAttribute);
            if (null == avpValue)
                throw new Exception("There is no input in dictionary for value " + avpValueAttribute
                        + " of avp " + avp.asXML());
        } else {
            avpValue = (int) Long.parseLong(avpValueAttribute);
        }
        return new AVPInteger(avpCode, avpValue);
    } else if (avpTypeAttribute.equalsIgnoreCase("octets") || avpTypeAttribute.equalsIgnoreCase("octet")) {
        return new AVPBytes(avpCode, new DefaultArray(Utils.parseBinaryString(avpValueAttribute)));
    } else if (avpTypeAttribute.equalsIgnoreCase("ipaddr")) {
        return new AVPBytes(avpCode, new DefaultArray(InetAddress.getByName(avpValueAttribute).getAddress()));
    } else if (avpTypeAttribute.equalsIgnoreCase("text") || avpTypeAttribute.equalsIgnoreCase("string")) {
        return new AVPString(avpCode, avpValueAttribute, "UTF8");
    } else {
        throw new ParsingException("Unknown AVP type : " + avpTypeAttribute);
    }
}

From source file:com.devoteam.srit.xmlloader.radius.StackRadius.java

License:Open Source License

/** Creates a specific Msg */
@Override/* w w  w .j a va  2  s.  c o  m*/
public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    RadiusMessage radiusMessage = new RadiusMessage();

    // header
    Element header = root.element("header");
    String codeAttribute = header.attributeValue("code");
    String authenticatorAttribute = header.attributeValue("authenticator");
    String identifierAttribute = header.attributeValue("identifier");

    if (Utils.isInteger(codeAttribute)) {
        radiusMessage.setCode(Integer.valueOf(codeAttribute));
    } else {
        RadiusCodes radiusCodes = this.radiusDictionary.getRadiusCodes();
        Integer code = radiusCodes.getCode(codeAttribute);
        if (null != code)
            radiusMessage.setCode(code);
        else
            throw new Exception("Unknown message code " + codeAttribute);
    }

    List<Element> avps = root.elements("avp");
    for (Element avp : avps) {
        radiusMessage.addAVP(this.parseAVP(-1, avp));
    }

    String name = root.attributeValue("listenpoint");
    boolean isListenpoint = true;
    // deprecated part //
    if (name == null) {
        name = root.attributeValue("socketName");
        isListenpoint = false;
    }
    // deprecated part //
    String remoteHost = root.attributeValue("remoteHost");
    String remotePort = root.attributeValue("remotePort");

    if ((null != remoteHost) && (null != remotePort)) {
        InetAddress inetAddress = InetAddress.getByName(remoteHost);
        InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, Integer.parseInt(remotePort));

        radiusMessage.setRemoteAddress(inetSocketAddress);
    }

    MsgRadius msgRadius = new MsgRadius(radiusMessage);
    ListenpointRadius listenpoint = null;
    ChannelRadius channel = null;

    if (null != identifierAttribute) {
        radiusMessage.setIdentifier(Integer.valueOf(identifierAttribute));
    }

    if (isListenpoint) {
        listenpoint = (ListenpointRadius) getListenpoint(name);
        if (listenpoint == null) {
            throw new ExecutionException("StackRadius: The listenpoint " + name + " does not exist");
        }
        if ((null == remoteHost) || (null == remotePort)) {
            throw new ExecutionException(
                    "StackRadius: The \"remoteXXX\" attributes are mandatory; please define them when sending the message.");
        }
        if (radiusMessage.getIdentifier() == 0)
            radiusMessage.setIdentifier(listenpoint.getIdentifierHandler().getIdentifier());
        msgRadius.setListenpoint(listenpoint);
    } else// deprecated part //
    {
        channel = (ChannelRadius) getChannel(name);
        if (channel == null) {
            throw new ExecutionException("StackRadius: The channel " + name + " does not exist");
        }
        if (radiusMessage.getIdentifier() == 0)
            radiusMessage.setIdentifier(channel.getIdentifierHandler().getIdentifier());
        msgRadius.setChannel(channel);
    }
    // deprecated part //

    if (isListenpoint) {
        radiusMessage.setSecret(listenpoint.getSecret());
        if (null == radiusMessage.getRemoteAddress() && null != listenpoint.getHost()
                && 0 != listenpoint.getPort()) {
            InetAddress inetAddress = InetAddress.getByName(listenpoint.getHost());
            InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, listenpoint.getPort());
            radiusMessage.setRemoteAddress(inetSocketAddress);
        }
    } else// deprecated part //
    {
        radiusMessage.setSecret(channel.getSecret());
        if (radiusMessage.getRemoteAddress() == null) {
            InetAddress inetAddress = InetAddress.getByName(channel.getRemoteHost());
            InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, channel.getRemotePort());
            radiusMessage.setRemoteAddress(inetSocketAddress);
        }
        if ((channel.getRemoteHost() == null) || (channel.getRemotePort() == 0)) {
            channel.setRemoteHost(radiusMessage.getRemoteAddress().getAddress().getHostAddress());
            channel.setRemotePort(radiusMessage.getRemoteAddress().getPort());
        }

    } // deprecated part //

    if (null != authenticatorAttribute) {
        radiusMessage.setAuthenticator(new DefaultArray(Utils.parseBinaryString(authenticatorAttribute)));
    } else {
        if (msgRadius.isRequest()) {
            radiusMessage.computeRequestAuthenticator();
        } else {
            MsgRadius msgRadiusRequest = (MsgRadius) this.getInTransaction(msgRadius.getTransactionId())
                    .getBeginMsg();

            radiusMessage.computeResponseAuthenticator(msgRadiusRequest.getRadiusMessage().getAuthenticator());
        }
    }

    // encode the User-Password AVPs if necessary
    if (this.getConfig().getBoolean("radius.ENCODE_USER_PASSWORD", true)) {
        if (1 == radiusMessage.getCode()) {
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.PROTOCOL,
                    "ChannelRadius, encode user-password avps");
            radiusMessage.encodeUserPasswordAvps();
        }
    }

    return msgRadius;
}