KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPMessage


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33 import java.util.*;
34
35
36
37 /**
38 * Defines the SNMPMessage class as a special case of SNMPSequence. Defines a
39 * top-level SNMP message, as per the following definitions from RFC 1157 and
40 * RFC 1901.
41
42
43 RFC1157-SNMP DEFINITIONS
44
45      IMPORTS FROM RFC1155-SMI;
46
47      -- top-level message
48
49              Message ::=
50                      SEQUENCE {
51                           version -- version-1 for this RFC
52                              INTEGER {
53                                  version-1(0)
54                              },
55
56                          community -- community name
57                              OCTET STRING,
58
59                          data -- e.g., PDUs if trivial
60                              ANY -- authentication is being used
61                      }
62                      
63                      
64   -- From RFC 1901:
65   
66   COMMUNITY-BASED-SNMPv2 DEFINITIONS ::= BEGIN
67
68     -- top-level message
69
70         Message ::=
71                 SEQUENCE {
72                      version
73                         INTEGER {
74                             version(1) -- modified from RFC 1157
75                         },
76
77                     community -- community name
78                         OCTET STRING,
79
80                     data -- PDUs as defined in [4]
81                         ANY
82                 }
83         }
84
85     END
86
87 */

88
89
90 public class SNMPMessage extends SNMPSequence
91 {
92     
93     
94     /**
95     * Create an SNMP message with specified version, community, and pdu.
96     * Use version = 0 for SNMP version 1, or version = 1 for enhanced capapbilities
97     * provided through RFC 1157.
98     */

99     
100     public SNMPMessage(int version, String JavaDoc community, SNMPPDU pdu)
101     {
102         super();
103         Vector contents = new Vector();
104         contents.insertElementAt(new SNMPInteger(version), 0);
105         contents.insertElementAt(new SNMPOctetString(community), 1);
106         contents.insertElementAt(pdu, 2);
107         
108         try
109         {
110             this.setValue(contents);
111         }
112         catch (SNMPBadValueException e)
113         {
114             // can't happen! all supplied Vector elements are SNMP Object subclasses
115
}
116     }
117     
118     
119     
120     /**
121     * Create an SNMP message with specified version, community, and trap pdu.
122     * Use version = 0 for SNMP version 1, or version = 1 for enhanced capapbilities
123     * provided through RFC 1157.
124     */

125     
126     public SNMPMessage(int version, String JavaDoc community, SNMPv1TrapPDU pdu)
127     {
128         super();
129         Vector contents = new Vector();
130         contents.insertElementAt(new SNMPInteger(version), 0);
131         contents.insertElementAt(new SNMPOctetString(community), 1);
132         contents.insertElementAt(pdu, 2);
133         
134         try
135         {
136             this.setValue(contents);
137         }
138         catch (SNMPBadValueException e)
139         {
140             // can't happen! all supplied Vector elements are SNMP Object subclasses
141
}
142     }
143     
144     
145     
146     
147     /**
148     * Create an SNMP message with specified version, community, and v2 trap pdu.
149     * Use version = 1.
150     */

151     
152     public SNMPMessage(int version, String JavaDoc community, SNMPv2TrapPDU pdu)
153     {
154         super();
155         Vector contents = new Vector();
156         contents.insertElementAt(new SNMPInteger(version), 0);
157         contents.insertElementAt(new SNMPOctetString(community), 1);
158         contents.insertElementAt(pdu, 2);
159         
160         try
161         {
162             this.setValue(contents);
163         }
164         catch (SNMPBadValueException e)
165         {
166             // can't happen! all supplied Vector elements are SNMP Object subclasses
167
}
168     }
169     
170     
171     
172     
173     /**
174     * Construct an SNMPMessage from a received ASN.1 byte representation.
175     * @throws SNMPBadValueException Indicates invalid SNMP message encoding supplied.
176     */

177     
178     protected SNMPMessage(byte[] enc)
179         throws SNMPBadValueException
180     {
181         super(enc);
182         
183         // validate the message: make sure we have the appropriate pieces
184
Vector contents = (Vector)(this.getValue());
185         
186         if (contents.size() != 3)
187         {
188             throw new SNMPBadValueException("Bad SNMP message");
189         }
190         
191         if (!(contents.elementAt(0) instanceof SNMPInteger))
192         {
193             throw new SNMPBadValueException("Bad SNMP message: bad version");
194         }
195         
196         if (!(contents.elementAt(1) instanceof SNMPOctetString))
197         {
198             throw new SNMPBadValueException("Bad SNMP message: bad community name");
199         }
200         
201         if (!(contents.elementAt(2) instanceof SNMPPDU) && !(contents.elementAt(2) instanceof SNMPv1TrapPDU) && !(contents.elementAt(2) instanceof SNMPv2TrapPDU))
202         {
203             throw new SNMPBadValueException("Bad SNMP message: bad PDU");
204         }
205         
206     }
207     
208     
209     /**
210     * Utility method which returns the PDU contained in the SNMP message as a plain Java Object.
211     * The pdu is the third component of the sequence, after the version and community name.
212     */

213     
214     public Object JavaDoc getPDUAsObject()
215         throws SNMPBadValueException
216     {
217         Vector contents = (Vector)(this.getValue());
218         Object JavaDoc pdu = contents.elementAt(2);
219         return pdu;
220     }
221     
222     
223     
224     /**
225     * Utility method which returns the PDU contained in the SNMP message. The pdu is the third component
226     * of the sequence, after the version and community name.
227     */

228     
229     public SNMPPDU getPDU()
230         throws SNMPBadValueException
231     {
232         Vector contents = (Vector)(this.getValue());
233         Object JavaDoc pdu = contents.elementAt(2);
234         
235         if (!(pdu instanceof SNMPPDU))
236         {
237             throw new SNMPBadValueException("Wrong PDU type in message: expected SNMPPDU, have " + pdu.getClass().toString());
238         }
239         
240         return (SNMPPDU)pdu;
241     }
242     
243     
244     /**
245     * Utility method which returns the PDU contained in the SNMP message as an SNMPv1TrapPDU. The pdu is the
246     * third component of the sequence, after the version and community name.
247     */

248     
249     public SNMPv1TrapPDU getv1TrapPDU()
250         throws SNMPBadValueException
251     {
252         Vector contents = (Vector)(this.getValue());
253         Object JavaDoc pdu = contents.elementAt(2);
254         
255         if (!(pdu instanceof SNMPv1TrapPDU))
256         {
257             throw new SNMPBadValueException("Wrong PDU type in message: expected SNMPTrapPDU, have " + pdu.getClass().toString());
258         }
259         
260         return (SNMPv1TrapPDU)pdu;
261     }
262     
263     
264     /**
265     * Utility method which returns the PDU contained in the SNMP message as an SNMPv2TrapPDU. The pdu is the
266     * third component of the sequence, after the version and community name.
267     */

268     
269     public SNMPv2TrapPDU getv2TrapPDU()
270         throws SNMPBadValueException
271     {
272         Vector contents = (Vector)(this.getValue());
273         Object JavaDoc pdu = contents.elementAt(2);
274         
275         if (!(pdu instanceof SNMPv2TrapPDU))
276         {
277             throw new SNMPBadValueException("Wrong PDU type in message: expected SNMPv2TrapPDU, have " + pdu.getClass().toString());
278         }
279         
280         return (SNMPv2TrapPDU)pdu;
281     }
282     
283     
284     
285     /**
286     * Utility method which returns the community name contained in the SNMP message. The community name is the
287     * second component of the sequence, after the version.
288     */

289     
290     public String JavaDoc getCommunityName()
291         throws SNMPBadValueException
292     {
293         Vector contents = (Vector)(this.getValue());
294         Object JavaDoc communityName = contents.elementAt(1);
295         
296         if (!(communityName instanceof SNMPOctetString))
297         {
298             throw new SNMPBadValueException("Wrong SNMP type for community name in message: expected SNMPOctetString, have " + communityName.getClass().toString());
299         }
300         
301         return ((SNMPOctetString)communityName).toString();
302     }
303     
304 }
Popular Tags