/
AvpImpl.java
304 lines (268 loc) · 8.03 KB
/
AvpImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2016, TeleStax Inc. and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* JBoss, Home of Professional Open Source
* Copyright 2007-2011, Red Hat, Inc. and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jdiameter.client.impl.parser;
import java.net.InetAddress;
import java.net.URISyntaxException;
import java.net.UnknownServiceException;
import java.util.Date;
import org.jdiameter.api.Avp;
import org.jdiameter.api.AvpDataException;
import org.jdiameter.api.AvpSet;
import org.jdiameter.api.InternalException;
import org.jdiameter.api.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author erick.svenson@yahoo.com
* @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
* @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
*/
class AvpImpl implements Avp {
private static final long serialVersionUID = 1L;
private static final ElementParser parser = new ElementParser();
int avpCode;
long vendorID;
boolean isMandatory = false;
boolean isEncrypted = false;
boolean isVendorSpecific = false;
byte[] rawData = new byte[0];
AvpSet groupedData;
private static final Logger logger = LoggerFactory.getLogger(AvpImpl.class);
AvpImpl(int code, int flags, long vnd, byte[] data) {
avpCode = code;
//
isMandatory = (flags & 0x40) != 0;
isEncrypted = (flags & 0x20) != 0;
isVendorSpecific = (flags & 0x80) != 0;
//
vendorID = vnd;
rawData = data;
}
AvpImpl(Avp avp) {
avpCode = avp.getCode();
vendorID = avp.getVendorId();
isMandatory = avp.isMandatory();
isEncrypted = avp.isEncrypted();
isVendorSpecific = avp.isVendorId();
try {
rawData = avp.getRaw();
if (rawData == null || rawData.length == 0) {
groupedData = avp.getGrouped();
}
}
catch (AvpDataException e) {
logger.debug("Can not create Avp", e);
}
}
AvpImpl (int newCode, Avp avp) {
this(avp);
avpCode = newCode;
}
@Override
public int getCode() {
return avpCode;
}
@Override
public boolean isVendorId() {
return isVendorSpecific;
}
@Override
public boolean isMandatory() {
return isMandatory;
}
@Override
public boolean isEncrypted() {
return isEncrypted;
}
@Override
public long getVendorId() {
return vendorID;
}
@Override
public byte[] getRaw() throws AvpDataException {
return rawData;
}
@Override
public byte[] getOctetString() throws AvpDataException {
return rawData;
}
@Override
public String getUTF8String() throws AvpDataException {
try {
return parser.bytesToUtf8String(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public int getInteger32() throws AvpDataException {
try {
return parser.bytesToInt(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public long getInteger64() throws AvpDataException {
try {
return parser.bytesToLong(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public long getUnsigned32() throws AvpDataException {
try {
byte[] u32ext = new byte[8];
System.arraycopy(rawData, 0, u32ext, 4, 4);
return parser.bytesToLong(u32ext);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public long getUnsigned64() throws AvpDataException {
try {
return parser.bytesToLong(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public float getFloat32() throws AvpDataException {
try {
return parser.bytesToFloat(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public double getFloat64() throws AvpDataException {
try {
return parser.bytesToDouble(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public InetAddress getAddress() throws AvpDataException {
try {
return parser.bytesToAddress(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public Date getTime() throws AvpDataException {
try {
return parser.bytesToDate(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public String getDiameterIdentity() throws AvpDataException {
try {
return parser.bytesToOctetString(rawData);
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public URI getDiameterURI() throws AvpDataException {
try {
return new URI(parser.bytesToOctetString(rawData));
}
catch (URISyntaxException e) {
throw new AvpDataException(e, this);
}
catch (UnknownServiceException e) {
throw new AvpDataException(e, this);
}
}
@Override
public AvpSet getGrouped() throws AvpDataException {
try {
if (groupedData == null) {
groupedData = parser.decodeAvpSet(rawData);
rawData = new byte[0];
}
return groupedData;
}
catch (Exception e) {
throw new AvpDataException(e, this);
}
}
@Override
public boolean isWrapperFor(Class<?> aClass) throws InternalException {
return false;
}
@Override
public <T> T unwrap(Class<T> aClass) throws InternalException {
return null;
}
@Override
public byte[] getRawData() {
return (rawData == null || rawData.length == 0) ? parser.encodeAvpSet(groupedData) : rawData;
}
// Caching toString.. Avp shouldn't be modified once created.
private String toString;
@Override
public String toString() {
if (toString == null) {
this.toString = new StringBuffer("AvpImpl [avpCode=").append(avpCode).append(", vendorID=").append(vendorID).
append(", len=").append((rawData != null) ? rawData.length : null).append("]@").append(super.hashCode()).toString();
}
return this.toString;
}
}