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: import java.io.*;
034:
035: /**
036: * One of the most important SNMP classes. Represents a sequence of other SNMP data types.
037: * Virtually all compound structures are subclasses of SNMPSequence - for example, the
038: * top-level SNMPMessage, and the SNMPPDU it contains, are both just specializations of
039: * SNMPSequence. Sequences are frequently nested within other sequences.
040: */
041:
042: public class SNMPSequence extends SNMPObject {
043: protected Vector sequence; // Vector of whatever is in sequence
044:
045: protected byte tag = SNMPBERCodec.SNMPSEQUENCE;
046:
047: /**
048: * Create a new empty sequence.
049: */
050:
051: public SNMPSequence() {
052: sequence = new Vector();
053: }
054:
055: /**
056: * Create a new SNMP sequence from the supplied Vector of SNMPObjects.
057: * @throws SNMPBadValueException Thrown if non-SNMP object supplied in Vector v.
058: */
059:
060: public SNMPSequence(Vector v) throws SNMPBadValueException {
061:
062: Enumeration e = v.elements();
063:
064: while (e.hasMoreElements()) {
065: if (!(e.nextElement() instanceof SNMPObject))
066: throw new SNMPBadValueException(
067: "Non-SNMPObject supplied to SNMPSequence.");
068: }
069:
070: sequence = v;
071: }
072:
073: /**
074: * Construct an SNMPMessage from a received ASN.1 byte representation.
075: * @throws SNMPBadValueException Indicates invalid SNMP sequence encoding supplied.
076: */
077:
078: protected SNMPSequence(byte[] enc) throws SNMPBadValueException {
079: extractFromBEREncoding(enc);
080: }
081:
082: /**
083: * Returns a Vector containing the SNMPObjects in the sequence.
084: */
085:
086: public Object getValue() {
087: return sequence;
088: }
089:
090: /**
091: * Used to set the contained SNMP objects from a supplied Vector.
092: * @throws SNMPBadValueException Indicates an incorrect object type supplied, or that the supplied
093: * Vector contains non-SNMPObjects.
094: */
095:
096: public void setValue(Object newSequence)
097: throws SNMPBadValueException {
098: if (newSequence instanceof Vector) {
099:
100: // check that all objects in vector are SNMPObjects
101:
102: Enumeration e = ((Vector) newSequence).elements();
103:
104: while (e.hasMoreElements()) {
105: if (!(e.nextElement() instanceof SNMPObject))
106: throw new SNMPBadValueException(
107: "Non-SNMPObject supplied to SNMPSequence.");
108: }
109:
110: this .sequence = (Vector) newSequence;
111: } else
112: throw new SNMPBadValueException(
113: " Sequence: bad object supplied to set value ");
114: }
115:
116: /**
117: * Return the number of SNMPObjects contained in the sequence.
118: */
119:
120: public int size() {
121: return sequence.size();
122: }
123:
124: /**
125: * Add the SNMP object to the end of the sequence.
126: * @throws SNMPBadValueException Relevant only in subclasses
127: */
128:
129: public void addSNMPObject(SNMPObject newObject)
130: throws SNMPBadValueException {
131: sequence.insertElementAt(newObject, sequence.size());
132: }
133:
134: /**
135: * Insert the SNMP object at the specified position in the sequence.
136: * @throws SNMPBadValueException Relevant only in subclasses
137: */
138:
139: public void insertSNMPObjectAt(SNMPObject newObject, int index)
140: throws SNMPBadValueException {
141: sequence.insertElementAt(newObject, index);
142: }
143:
144: /**
145: * Return the SNMP object at the specified index. Indices are 0-based.
146: */
147:
148: public SNMPObject getSNMPObjectAt(int index) {
149: return (SNMPObject) (sequence.elementAt(index));
150: }
151:
152: /**
153: * Return the BER encoding for the sequence.
154: */
155:
156: protected byte[] getBEREncoding() {
157: ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
158:
159: // recursively write contents of Vector
160: byte[] data = encodeVector();
161:
162: // calculate encoding for length of data
163: byte[] len = SNMPBERCodec.encodeLength(data.length);
164:
165: // encode T,L,V info
166: outBytes.write(tag);
167: outBytes.write(len, 0, len.length);
168: outBytes.write(data, 0, data.length);
169:
170: return outBytes.toByteArray();
171: }
172:
173: private byte[] encodeVector() {
174: ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
175:
176: int numElements = sequence.size();
177: for (int i = 0; i < numElements; ++i) {
178: byte[] nextBytes = ((SNMPObject) (sequence.elementAt(i)))
179: .getBEREncoding();
180: outBytes.write(nextBytes, 0, nextBytes.length);
181: }
182:
183: return outBytes.toByteArray();
184: }
185:
186: protected void extractFromBEREncoding(byte[] enc)
187: throws SNMPBadValueException {
188: Vector newVector = new Vector();
189:
190: int totalLength = enc.length;
191: int position = 0;
192:
193: while (position < totalLength) {
194: SNMPTLV nextTLV = SNMPBERCodec
195: .extractNextTLV(enc, position);
196: newVector.insertElementAt(SNMPBERCodec
197: .extractEncoding(nextTLV), newVector.size());
198: position += nextTLV.totalLength;
199: }
200:
201: sequence = newVector;
202:
203: }
204:
205: /**
206: * Return a sequence of representations of the contained objects, separated by spaces
207: * and enclosed in parentheses.
208: */
209:
210: public String toString() {
211: StringBuffer valueStringBuffer = new StringBuffer("(");
212:
213: for (int i = 0; i < sequence.size(); ++i) {
214: valueStringBuffer.append(" ");
215: valueStringBuffer.append(((SNMPObject) sequence
216: .elementAt(i)).toString());
217: valueStringBuffer.append(" ");
218: }
219:
220: valueStringBuffer.append(")");
221: return valueStringBuffer.toString();
222: }
223:
224: }
|