001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.snmp.marshaler;
018    
019    import javax.jbi.messaging.MessageExchange;
020    import javax.jbi.messaging.MessagingException;
021    import javax.jbi.messaging.NormalizedMessage;
022    
023    import org.apache.servicemix.jbi.jaxp.StringSource;
024    import org.snmp4j.PDU;
025    import org.snmp4j.smi.VariableBinding;
026    
027    /**
028     * default marshaler implementation
029     * 
030     * @author lhein
031     */
032    public class DefaultSnmpMarshaler implements SnmpMarshalerSupport {
033    
034        public static final String SNMP_TAG = "snmp";
035        public static final String ENTRY_TAG = "entry";
036        public static final String OID_TAG = "oid";
037        public static final String VALUE_TAG = "value";
038        
039        private static final String SNMP_TAG_OPEN  = '<' + SNMP_TAG + '>';
040        private static final String SNMP_TAG_CLOSE = "</" + SNMP_TAG + '>';
041        private static final String ENTRY_TAG_OPEN  = '<' + ENTRY_TAG + '>';
042        private static final String ENTRY_TAG_CLOSE = "</" + ENTRY_TAG + '>';
043        private static final String OID_TAG_OPEN  = '<' + OID_TAG + '>';
044        private static final String OID_TAG_CLOSE = "</" + OID_TAG + '>';
045        private static final String VALUE_TAG_OPEN  = '<' + VALUE_TAG + '>';
046        private static final String VALUE_TAG_CLOSE = "</" + VALUE_TAG + '>';
047        
048        /* (non-Javadoc)
049         * @see org.apache.servicemix.snmp.marshaler.SnmpMarshalerSupport#convertToJBI(javax.jbi.messaging.MessageExchange, javax.jbi.messaging.NormalizedMessage, org.snmp4j.PDU, org.snmp4j.PDU)
050         */
051        public void convertToJBI(MessageExchange exchange, NormalizedMessage inMsg, PDU request, PDU response)
052            throws MessagingException {
053            // the output buffer
054            StringBuffer sb = new StringBuffer();
055            
056            // prepare the header
057            sb.append(SNMP_TAG_OPEN);
058                    
059            // now loop all variables of the response
060            for (Object o : response.getVariableBindings()) {
061                VariableBinding b = (VariableBinding)o;
062    
063                sb.append(ENTRY_TAG_OPEN);
064                sb.append(OID_TAG_OPEN);
065                sb.append(b.getOid().toString());
066                sb.append(OID_TAG_CLOSE);
067                sb.append(VALUE_TAG_OPEN);
068                sb.append(b.getVariable().toString());
069                sb.append(VALUE_TAG_CLOSE);
070                sb.append(ENTRY_TAG_CLOSE);
071            }
072            
073            // prepare the footer
074            sb.append(SNMP_TAG_CLOSE);
075            
076            // now put the buffer to the message content
077            inMsg.setContent(new StringSource(sb.toString()));
078        }
079    }