All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.component.snmp.SnmpConverters Maven / Gradle / Ivy

There is a newer version: 4.8.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.camel.component.snmp;

import java.util.StringTokenizer;

import org.apache.camel.Converter;
import org.apache.camel.Exchange;
import org.snmp4j.PDU;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;

@Converter
public final class SnmpConverters {
    public static final String SNMP_TAG = "snmp";
    public static final String ENTRY_TAG = "entry";
    public static final String OID_TAG = "oid";
    public static final String VALUE_TAG = "value";
    
    private static final String SNMP_TAG_OPEN  = '<' + SNMP_TAG + '>';
    private static final String SNMP_TAG_CLOSE = "';
    private static final String ENTRY_TAG_OPEN  = '<' + ENTRY_TAG + '>';
    private static final String ENTRY_TAG_CLOSE = "';
    private static final String OID_TAG_OPEN  = '<' + OID_TAG + '>';
    private static final String OID_TAG_CLOSE = "';
    private static final String VALUE_TAG_OPEN  = '<' + VALUE_TAG + '>';
    private static final String VALUE_TAG_CLOSE = "';
    
    private SnmpConverters() {
        //Utility Class
    }

    @Converter
    public static OIDList toOIDList(String s, Exchange exchange) {
        OIDList list = new OIDList();

        if (s != null && s.indexOf(",") != -1) {
            // seems to be a comma separated oid list
            StringTokenizer strTok = new StringTokenizer(s, ",");
            while (strTok.hasMoreTokens()) {
                String tok = strTok.nextToken();
                if (tok != null && tok.trim().length() > 0) {
                    list.add(new OID(tok.trim()));
                } else {
                    // empty token - skip
                }
            }
        } else if (s != null) {
            // maybe a single oid
            list.add(new OID(s.trim()));
        }

        return list;
    }

    /**
     * Converts the given snmp pdu to a String body.
     *
     * @param pdu       the snmp pdu
     * @return  the text content
     */
    @Converter
    public static String toString(PDU pdu) {
     // the output buffer
        StringBuilder sb = new StringBuilder();

        // prepare the header
        sb.append(SNMP_TAG_OPEN);

        // now loop all variables of the response
        for (Object o : pdu.getVariableBindings()) {
            VariableBinding b = (VariableBinding)o;

            sb.append(ENTRY_TAG_OPEN);
            sb.append(OID_TAG_OPEN);
            sb.append(b.getOid().toString());
            sb.append(OID_TAG_CLOSE);
            sb.append(VALUE_TAG_OPEN);
            sb.append(getXmlSafeString(b.getVariable().toString()));
            sb.append(VALUE_TAG_CLOSE);
            sb.append(ENTRY_TAG_CLOSE);
        }

        // prepare the footer
        sb.append(SNMP_TAG_CLOSE);

        return sb.toString();
    }

    private static String getXmlSafeString(String string) {
        return string.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&").replaceAll("\"", """).replaceAll("'", "'");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy