1 29 30 31 package snmp; 32 33 import java.util.*; 34 35 36 37 88 89 90 public class SNMPMessage extends SNMPSequence 91 { 92 93 94 99 100 public SNMPMessage(int version, String 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 } 116 } 117 118 119 120 125 126 public SNMPMessage(int version, String 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 } 142 } 143 144 145 146 147 151 152 public SNMPMessage(int version, String 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 } 168 } 169 170 171 172 173 177 178 protected SNMPMessage(byte[] enc) 179 throws SNMPBadValueException 180 { 181 super(enc); 182 183 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 213 214 public Object getPDUAsObject() 215 throws SNMPBadValueException 216 { 217 Vector contents = (Vector)(this.getValue()); 218 Object pdu = contents.elementAt(2); 219 return pdu; 220 } 221 222 223 224 228 229 public SNMPPDU getPDU() 230 throws SNMPBadValueException 231 { 232 Vector contents = (Vector)(this.getValue()); 233 Object 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 248 249 public SNMPv1TrapPDU getv1TrapPDU() 250 throws SNMPBadValueException 251 { 252 Vector contents = (Vector)(this.getValue()); 253 Object 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 268 269 public SNMPv2TrapPDU getv2TrapPDU() 270 throws SNMPBadValueException 271 { 272 Vector contents = (Vector)(this.getValue()); 273 Object 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 289 290 public String getCommunityName() 291 throws SNMPBadValueException 292 { 293 Vector contents = (Vector)(this.getValue()); 294 Object 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 |