001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Nam Nguyen
028: */
029:
030: package com.caucho.server.snmp.types;
031:
032: import java.util.Iterator;
033:
034: import com.caucho.server.snmp.SnmpRuntimeException;
035:
036: /*
037: * Parent class for all SNMP PDU types.
038: */
039: abstract public class PduValue extends SnmpValue {
040: IntegerValue _requestId;
041: IntegerValue _error;
042: IntegerValue _errorIndex;
043: VarBindListValue _varBindList;
044:
045: public PduValue() {
046: _requestId = IntegerValue.ZERO;
047: _error = IntegerValue.ZERO;
048: _errorIndex = IntegerValue.ZERO;
049: _varBindList = new VarBindListValue();
050: }
051:
052: public PduValue(int requestId, int error, int errorIndex) {
053: _requestId = new IntegerValue(requestId);
054: _error = new IntegerValue(error);
055: _errorIndex = new IntegerValue(errorIndex);
056: _varBindList = new VarBindListValue();
057: }
058:
059: public PduValue(IntegerValue requestId, IntegerValue error,
060: IntegerValue errorIndex, VarBindListValue varBindList) {
061: _requestId = requestId;
062: _error = error;
063: _errorIndex = errorIndex;
064: _varBindList = varBindList;
065: }
066:
067: @Override
068: public void toAsn1(StringBuilder sb) {
069: StringBuilder innerSb = new StringBuilder();
070:
071: _requestId.toAsn1(innerSb);
072: _error.toAsn1(innerSb);
073: _errorIndex.toAsn1(innerSb);
074: _varBindList.toAsn1(innerSb);
075:
076: header(sb, innerSb.length());
077:
078: sb.append(innerSb.toString());
079: }
080:
081: public void setError(int error) {
082: _error = new IntegerValue(error);
083: }
084:
085: public void setError(IntegerValue error) {
086: _error = error;
087: }
088:
089: public IntegerValue getError() {
090: return _error;
091: }
092:
093: public void setErrorIndex(int errorIndex) {
094: _errorIndex = new IntegerValue(errorIndex);
095: }
096:
097: public void setErrorIndex(IntegerValue errorIndex) {
098: _errorIndex = errorIndex;
099: }
100:
101: public IntegerValue getErrorIndex() {
102: return _errorIndex;
103: }
104:
105: public void setRequestId(int id) {
106: _requestId = new IntegerValue(id);
107: }
108:
109: public void setRequestId(IntegerValue id) {
110: _requestId = id;
111: }
112:
113: public IntegerValue getRequestId() {
114: return _requestId;
115: }
116:
117: public void addVarBind(VarBindValue obj) {
118: _varBindList.add(obj);
119: }
120:
121: public void addVarBindList(VarBindListValue list) {
122: Iterator<VarBindValue> iter = list.iterator();
123:
124: while (iter.hasNext()) {
125: addVarBind(iter.next());
126: }
127: }
128:
129: public VarBindListValue getVarBindList() {
130: return _varBindList;
131: }
132:
133: public static PduValue create(int type) {
134: switch (type) {
135: case SnmpValue.GET_REQUEST_PDU:
136: return new GetRequestPduValue();
137: case SnmpValue.GET_NEXT_REQUEST_PDU:
138: return new GetNextRequestPduValue();
139: case SnmpValue.SET_REQUEST_PDU:
140: return new SetRequestPduValue();
141: case SnmpValue.GET_RESPONSE_PDU:
142: return new GetResponsePduValue();
143: default:
144: throw new SnmpRuntimeException("invalid type: "
145: + typeName(type));
146: }
147: }
148:
149: public static PduValue create(int type, IntegerValue requestId,
150: IntegerValue error, IntegerValue errorIndex,
151: VarBindListValue varBindList) {
152: switch (type) {
153: case SnmpValue.GET_REQUEST_PDU:
154: return new GetRequestPduValue(requestId, error, errorIndex,
155: varBindList);
156: case SnmpValue.GET_NEXT_REQUEST_PDU:
157: return new GetNextRequestPduValue(requestId, error,
158: errorIndex, varBindList);
159: case SnmpValue.SET_REQUEST_PDU:
160: return new SetRequestPduValue(requestId, error, errorIndex,
161: varBindList);
162: case SnmpValue.GET_RESPONSE_PDU:
163: return new GetResponsePduValue(requestId, error,
164: errorIndex, varBindList);
165: default:
166: throw new SnmpRuntimeException("invalid type: "
167: + typeName(type));
168: }
169: }
170: }
|