Source Code Cross Referenced for SNMPMessage.java in  » Net » Java-SNMP » snmp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Java SNMP » snmp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * SNMP Package
003:         *
004:         * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
005:         *
006:         * This is free software. Redistribution and use in source and binary forms, with
007:         * or without modification, are permitted provided that the following conditions
008:         * are met:
009:         *
010:         *  1. Redistributions of source code must retain the above copyright notice, this
011:         *     list of conditions and the following disclaimer.
012:         *  2. Redistributions in binary form must reproduce the above copyright notice,
013:         *     this list of conditions and the following disclaimer in the documentation 
014:         *     and/or other materials provided with the distribution.
015:         *  3. The name of the author may not be used to endorse or promote products 
016:         *     derived from this software without specific prior written permission.
017:         *
018:         * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
019:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
020:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
021:         * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
023:         * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
024:         * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
025:         * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
026:         * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:         *
028:         */
029:
030:        package snmp;
031:
032:        import java.util.*;
033:
034:        /**
035:         *     Defines the SNMPMessage class as a special case of SNMPSequence. Defines a
036:         *     top-level SNMP message, as per the following definitions from RFC 1157 and
037:         *     RFC 1901.
038:
039:
040:         RFC1157-SNMP DEFINITIONS
041:
042:         IMPORTS FROM RFC1155-SMI;
043:
044:         -- top-level message
045:
046:         Message ::=
047:         SEQUENCE {
048:         version        -- version-1 for this RFC
049:         INTEGER {
050:         version-1(0)
051:         },
052:
053:         community      -- community name
054:         OCTET STRING,
055:
056:         data           -- e.g., PDUs if trivial
057:         ANY        -- authentication is being used
058:         }
059:        
060:        
061:         -- From RFC 1901:
062:        
063:         COMMUNITY-BASED-SNMPv2 DEFINITIONS ::= BEGIN
064:
065:         -- top-level message
066:
067:         Message ::=
068:         SEQUENCE {
069:         version
070:         INTEGER {
071:         version(1)  -- modified from RFC 1157
072:         },
073:
074:         community           -- community name
075:         OCTET STRING,
076:
077:         data                -- PDUs as defined in [4]
078:         ANY
079:         }
080:         }
081:
082:         END                   
083:
084:         */
085:
086:        public class SNMPMessage extends SNMPSequence {
087:
088:            /**
089:             *    Create an SNMP message with specified version, community, and pdu.
090:             *    Use version = 0 for SNMP version 1, or version = 1 for enhanced capapbilities
091:             *    provided through RFC 1157.
092:             */
093:
094:            public SNMPMessage(int version, String community, SNMPPDU pdu) {
095:                super ();
096:                Vector contents = new Vector();
097:                contents.insertElementAt(new SNMPInteger(version), 0);
098:                contents.insertElementAt(new SNMPOctetString(community), 1);
099:                contents.insertElementAt(pdu, 2);
100:
101:                try {
102:                    this .setValue(contents);
103:                } catch (SNMPBadValueException e) {
104:                    // can't happen! all supplied Vector elements are SNMP Object subclasses
105:                }
106:            }
107:
108:            /**
109:             *    Create an SNMP message with specified version, community, and trap pdu.
110:             *    Use version = 0 for SNMP version 1, or version = 1 for enhanced capapbilities
111:             *    provided through RFC 1157.
112:             */
113:
114:            public SNMPMessage(int version, String community, SNMPv1TrapPDU pdu) {
115:                super ();
116:                Vector contents = new Vector();
117:                contents.insertElementAt(new SNMPInteger(version), 0);
118:                contents.insertElementAt(new SNMPOctetString(community), 1);
119:                contents.insertElementAt(pdu, 2);
120:
121:                try {
122:                    this .setValue(contents);
123:                } catch (SNMPBadValueException e) {
124:                    // can't happen! all supplied Vector elements are SNMP Object subclasses
125:                }
126:            }
127:
128:            /**
129:             *    Create an SNMP message with specified version, community, and v2 trap pdu.
130:             *    Use version = 1.
131:             */
132:
133:            public SNMPMessage(int version, String community, SNMPv2TrapPDU pdu) {
134:                super ();
135:                Vector contents = new Vector();
136:                contents.insertElementAt(new SNMPInteger(version), 0);
137:                contents.insertElementAt(new SNMPOctetString(community), 1);
138:                contents.insertElementAt(pdu, 2);
139:
140:                try {
141:                    this .setValue(contents);
142:                } catch (SNMPBadValueException e) {
143:                    // can't happen! all supplied Vector elements are SNMP Object subclasses
144:                }
145:            }
146:
147:            /**
148:             *    Construct an SNMPMessage from a received ASN.1 byte representation.
149:             *    @throws SNMPBadValueException Indicates invalid SNMP message encoding supplied.
150:             */
151:
152:            protected SNMPMessage(byte[] enc) throws SNMPBadValueException {
153:                super (enc);
154:
155:                // validate the message: make sure we have the appropriate pieces
156:                Vector contents = (Vector) (this .getValue());
157:
158:                if (contents.size() != 3) {
159:                    throw new SNMPBadValueException("Bad SNMP message");
160:                }
161:
162:                if (!(contents.elementAt(0) instanceof  SNMPInteger)) {
163:                    throw new SNMPBadValueException(
164:                            "Bad SNMP message: bad version");
165:                }
166:
167:                if (!(contents.elementAt(1) instanceof  SNMPOctetString)) {
168:                    throw new SNMPBadValueException(
169:                            "Bad SNMP message: bad community name");
170:                }
171:
172:                if (!(contents.elementAt(2) instanceof  SNMPPDU)
173:                        && !(contents.elementAt(2) instanceof  SNMPv1TrapPDU)
174:                        && !(contents.elementAt(2) instanceof  SNMPv2TrapPDU)) {
175:                    throw new SNMPBadValueException("Bad SNMP message: bad PDU");
176:                }
177:
178:            }
179:
180:            /** 
181:             *    Utility method which returns the PDU contained in the SNMP message as a plain Java Object. 
182:             *     The pdu is the third component of the sequence, after the version and community name.
183:             */
184:
185:            public Object getPDUAsObject() throws SNMPBadValueException {
186:                Vector contents = (Vector) (this .getValue());
187:                Object pdu = contents.elementAt(2);
188:                return pdu;
189:            }
190:
191:            /** 
192:             *    Utility method which returns the PDU contained in the SNMP message. The pdu is the third component
193:             *     of the sequence, after the version and community name.
194:             */
195:
196:            public SNMPPDU getPDU() throws SNMPBadValueException {
197:                Vector contents = (Vector) (this .getValue());
198:                Object pdu = contents.elementAt(2);
199:
200:                if (!(pdu instanceof  SNMPPDU)) {
201:                    throw new SNMPBadValueException(
202:                            "Wrong PDU type in message: expected SNMPPDU, have "
203:                                    + pdu.getClass().toString());
204:                }
205:
206:                return (SNMPPDU) pdu;
207:            }
208:
209:            /** 
210:             *    Utility method which returns the PDU contained in the SNMP message as an SNMPv1TrapPDU. The pdu is the 
211:             *   third component of the sequence, after the version and community name.
212:             */
213:
214:            public SNMPv1TrapPDU getv1TrapPDU() throws SNMPBadValueException {
215:                Vector contents = (Vector) (this .getValue());
216:                Object pdu = contents.elementAt(2);
217:
218:                if (!(pdu instanceof  SNMPv1TrapPDU)) {
219:                    throw new SNMPBadValueException(
220:                            "Wrong PDU type in message: expected SNMPTrapPDU, have "
221:                                    + pdu.getClass().toString());
222:                }
223:
224:                return (SNMPv1TrapPDU) pdu;
225:            }
226:
227:            /** 
228:             *    Utility method which returns the PDU contained in the SNMP message as an SNMPv2TrapPDU. The pdu is the 
229:             *   third component of the sequence, after the version and community name.
230:             */
231:
232:            public SNMPv2TrapPDU getv2TrapPDU() throws SNMPBadValueException {
233:                Vector contents = (Vector) (this .getValue());
234:                Object pdu = contents.elementAt(2);
235:
236:                if (!(pdu instanceof  SNMPv2TrapPDU)) {
237:                    throw new SNMPBadValueException(
238:                            "Wrong PDU type in message: expected SNMPv2TrapPDU, have "
239:                                    + pdu.getClass().toString());
240:                }
241:
242:                return (SNMPv2TrapPDU) pdu;
243:            }
244:
245:            /** 
246:             *    Utility method which returns the community name contained in the SNMP message. The community name is the 
247:             *   second component of the sequence, after the version.
248:             */
249:
250:            public String getCommunityName() throws SNMPBadValueException {
251:                Vector contents = (Vector) (this .getValue());
252:                Object communityName = contents.elementAt(1);
253:
254:                if (!(communityName instanceof  SNMPOctetString)) {
255:                    throw new SNMPBadValueException(
256:                            "Wrong SNMP type for community name in message: expected SNMPOctetString, have "
257:                                    + communityName.getClass().toString());
258:                }
259:
260:                return ((SNMPOctetString) communityName).toString();
261:            }
262:
263:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.