This repository was archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathUserConfigurationDictionary.java
More file actions
742 lines (664 loc) · 25.8 KB
/
UserConfigurationDictionary.java
File metadata and controls
742 lines (664 loc) · 25.8 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
* The MIT License
* Copyright (c) 2012 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package microsoft.exchange.webservices.data.property.complex;
import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
import microsoft.exchange.webservices.data.core.EwsUtilities;
import microsoft.exchange.webservices.data.core.XmlAttributeNames;
import microsoft.exchange.webservices.data.core.XmlElementNames;
import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
import microsoft.exchange.webservices.data.core.enumeration.property.UserConfigurationDictionaryObjectType;
import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
import microsoft.exchange.webservices.data.misc.OutParam;
import microsoft.exchange.webservices.data.util.DateTimeUtils;
import org.apache.commons.codec.binary.Base64;
import javax.xml.stream.XMLStreamException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Represents a user configuration's Dictionary property.
*/
@EditorBrowsable(state = EditorBrowsableState.Never)
public final class UserConfigurationDictionary extends ComplexProperty
implements Iterable<Object> {
// TODO: Consider implementing IsDirty mechanism in ComplexProperty.
/**
* The dictionary.
*/
private Map<Object, Object> dictionary;
/**
* The is dirty.
*/
private boolean isDirty = false;
/**
* Initializes a new instance of "UserConfigurationDictionary" class.
*/
public UserConfigurationDictionary() {
super();
this.dictionary = new HashMap<Object, Object>();
}
/**
* Gets the element with the specified key.
*
* @param key The key of the element to get or set.
* @return The element with the specified key.
*/
public Object getElements(Object key) {
return this.dictionary.get(key);
}
/**
* Sets the element with the specified key.
*
* @param key The key of the element to get or set
* @param value the value
* @throws Exception the exception
*/
public void setElements(Object key, Object value) throws Exception {
this.validateEntry(key, value);
this.dictionary.put(key, value);
this.changed();
}
/**
* Adds an element with the provided key and value to the user configuration
* dictionary.
*
* @param key The object to use as the key of the element to add.
* @param value The object to use as the value of the element to add.
* @throws Exception the exception
*/
public void addElement(Object key, Object value) throws Exception {
this.validateEntry(key, value);
this.dictionary.put(key, value);
this.changed();
}
/**
* Determines whether the user configuration dictionary contains an element
* with the specified key.
*
* @param key The key to locate in the user configuration dictionary.
* @return true if the user configuration dictionary contains an element
* with the key; otherwise false.
*/
public boolean containsKey(Object key) {
return this.dictionary.containsKey(key);
}
/**
* Removes the element with the specified key from the user configuration
* dictionary.
*
* @param key The key of the element to remove.
* @return true if the element is successfully removed; otherwise false.
*/
public boolean remove(Object key) {
boolean isRemoved = false;
if (key != null) {
this.dictionary.remove(key);
isRemoved = true;
}
if (isRemoved) {
this.changed();
}
return isRemoved;
}
/**
* Gets the value associated with the specified key.
*
* @param key The key whose value to get.
* @param value When this method returns, the value associated with the
* specified key, if the key is found; otherwise, null.
* @return true if the user configuration dictionary contains the key;
* otherwise false.
*/
public boolean tryGetValue(Object key, OutParam<Object> value) {
if (this.dictionary.containsKey(key)) {
value.setParam(this.dictionary.get(key));
return true;
} else {
value.setParam(null);
return false;
}
}
/**
* Gets the number of elements in the user configuration dictionary.
*
* @return the count
*/
public int getCount() {
return this.dictionary.size();
}
/**
* Removes all item from the user configuration dictionary.
*/
public void clear() {
if (this.dictionary.size() != 0) {
this.dictionary.clear();
this.changed();
}
}
/**
* Gets the enumerator.
*
* @return the enumerator
*/
/**
* Returns an enumerator that iterates through
* the user configuration dictionary.
*
* @return An IEnumerator that can be used
* to iterate through the user configuration dictionary.
*/
public Iterator<Object> getEnumerator() {
return (this.dictionary.values().iterator());
}
/**
* Gets the isDirty flag.
*
* @return the checks if is dirty
*/
public boolean getIsDirty() {
return this.isDirty;
}
/**
* Sets the isDirty flag.
*
* @param value the new checks if is dirty
*/
public void setIsDirty(boolean value) {
this.isDirty = value;
}
/**
* Instance was changed.
*/
@Override public void changed() {
super.changed();
this.isDirty = true;
}
/**
* Writes elements to XML.
*
* @param writer accepts EwsServiceXmlWriter
* @throws XMLStreamException the XML stream exception
* @throws ServiceXmlSerializationException the service xml serialization exception
*/
@Override
public void writeElementsToXml(EwsServiceXmlWriter writer)
throws XMLStreamException, ServiceXmlSerializationException {
EwsUtilities.ewsAssert(writer != null, "UserConfigurationDictionary.WriteElementsToXml", "writer is null");
Iterator<Entry<Object, Object>> it = this.dictionary.entrySet()
.iterator();
while (it.hasNext()) {
Entry<Object, Object> dictionaryEntry = it.next();
writer.writeStartElement(XmlNamespace.Types,
XmlElementNames.DictionaryEntry);
this.writeObjectToXml(writer, XmlElementNames.DictionaryKey,
dictionaryEntry.getKey());
this.writeObjectToXml(writer, XmlElementNames.DictionaryValue,
dictionaryEntry.getValue());
writer.writeEndElement();
}
}
/**
* Writes a dictionary object (key or value) to Xml.
*
* @param writer the writer
* @param xmlElementName the Xml element name
* @param dictionaryObject the object to write
* @throws XMLStreamException the XML stream exception
* @throws ServiceXmlSerializationException the service xml serialization exception
*/
private void writeObjectToXml(EwsServiceXmlWriter writer,
String xmlElementName, Object dictionaryObject)
throws XMLStreamException, ServiceXmlSerializationException {
EwsUtilities.ewsAssert(writer != null, "UserConfigurationDictionary.WriteObjectToXml", "writer is null");
EwsUtilities.ewsAssert(xmlElementName != null, "UserConfigurationDictionary.WriteObjectToXml",
"xmlElementName is null");
writer.writeStartElement(XmlNamespace.Types, xmlElementName);
if (dictionaryObject == null) {
EwsUtilities.ewsAssert((!xmlElementName.equals(XmlElementNames.DictionaryKey)),
"UserConfigurationDictionary.WriteObjectToXml", "Key is null");
writer.writeAttributeValue(
EwsUtilities.EwsXmlSchemaInstanceNamespacePrefix,
XmlAttributeNames.Nil, EwsUtilities.XSTrue);
} else {
this.writeObjectValueToXml(writer, dictionaryObject);
}
writer.writeEndElement();
}
/**
* Writes a dictionary Object's value to Xml.
*
* @param writer The writer.
* @param dictionaryObject The dictionary object to write. <br />
* Object values are either: <br />
* an array of strings, an array of bytes (which will be encoded into base64) <br />
* or a single value. Single values can be: <br />
* - datetime, boolean, byte, int, long, string
* @throws XMLStreamException the XML stream exception
* @throws ServiceXmlSerializationException the service xml serialization exception
*/
private void writeObjectValueToXml(final EwsServiceXmlWriter writer,
final Object dictionaryObject) throws XMLStreamException,
ServiceXmlSerializationException {
// Preconditions
if (dictionaryObject == null) {
throw new NullPointerException("DictionaryObject must not be null");
}
if (writer == null) {
throw new NullPointerException(
"EwsServiceXmlWriter must not be null");
}
// Processing
final UserConfigurationDictionaryObjectType dictionaryObjectType;
if (dictionaryObject instanceof String[]) {
dictionaryObjectType = UserConfigurationDictionaryObjectType.StringArray;
this.writeEntryTypeToXml(writer, dictionaryObjectType);
for (String arrayElement : (String[]) dictionaryObject) {
this.writeEntryValueToXml(writer, arrayElement);
}
} else {
final String valueAsString;
if (dictionaryObject instanceof String) {
dictionaryObjectType = UserConfigurationDictionaryObjectType.String;
valueAsString = String.valueOf(dictionaryObject);
} else if (dictionaryObject instanceof Boolean) {
dictionaryObjectType = UserConfigurationDictionaryObjectType.Boolean;
valueAsString = EwsUtilities
.boolToXSBool((Boolean) dictionaryObject);
} else if (dictionaryObject instanceof Byte) {
dictionaryObjectType = UserConfigurationDictionaryObjectType.Byte;
valueAsString = String.valueOf(dictionaryObject);
} else if (dictionaryObject instanceof Date) {
dictionaryObjectType = UserConfigurationDictionaryObjectType.DateTime;
valueAsString = writer.getService()
.convertDateTimeToUniversalDateTimeString(
(Date) dictionaryObject);
} else if (dictionaryObject instanceof Integer) {
// removed unsigned integer because in Java, all types are
// signed, there are no unsigned versions
dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer32;
valueAsString = String.valueOf(dictionaryObject);
} else if (dictionaryObject instanceof Long) {
// removed unsigned integer because in Java, all types are
// signed, there are no unsigned versions
dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer64;
valueAsString = String.valueOf(dictionaryObject);
} else if (dictionaryObject instanceof byte[]) {
dictionaryObjectType = UserConfigurationDictionaryObjectType.ByteArray;
valueAsString = Base64.encodeBase64String((byte[]) dictionaryObject);
} else if (dictionaryObject instanceof Byte[]) {
dictionaryObjectType = UserConfigurationDictionaryObjectType.ByteArray;
// cast Byte[] to byte[]
Byte[] from = (Byte[]) dictionaryObject;
byte[] to = new byte[from.length];
for (int currentIndex = 0; currentIndex < from.length; currentIndex++) {
to[currentIndex] = (byte) from[currentIndex];
}
valueAsString = Base64.encodeBase64String(to);
} else {
throw new IllegalArgumentException(String.format(
"Unsupported type: %s", dictionaryObject.getClass()
.toString()));
}
this.writeEntryTypeToXml(writer, dictionaryObjectType);
this.writeEntryValueToXml(writer, valueAsString);
}
}
/**
* Writes a dictionary entry type to Xml.
*
* @param writer the writer
* @param dictionaryObjectType type to write
* @throws XMLStreamException the XML stream exception
* @throws ServiceXmlSerializationException the service xml serialization exception
*/
private void writeEntryTypeToXml(EwsServiceXmlWriter writer,
UserConfigurationDictionaryObjectType dictionaryObjectType)
throws XMLStreamException, ServiceXmlSerializationException {
writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Type);
writer
.writeValue(dictionaryObjectType.toString(),
XmlElementNames.Type);
writer.writeEndElement();
}
/**
* Writes a dictionary entry value to Xml.
*
* @param writer the writer
* @param value value to write
* @throws XMLStreamException the XML stream exception
* @throws ServiceXmlSerializationException the service xml serialization exception
*/
private void writeEntryValueToXml(EwsServiceXmlWriter writer, String value)
throws XMLStreamException, ServiceXmlSerializationException {
writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Value);
// While an entry value can't be null, if the entry is an array, an
// element of the array can be null.
if (value != null) {
writer.writeValue(value, XmlElementNames.Value);
}
writer.writeEndElement();
}
/*
* (non-Javadoc)
*
* @see
* microsoft.exchange.webservices.ComplexProperty#loadFromXml(microsoft.
* exchange.webservices.EwsServiceXmlReader,
* microsoft.exchange.webservices.XmlNamespace, java.lang.String)
*/
@Override
/**
* Loads this dictionary from the specified reader.
* @param reader The reader.
* @param xmlNamespace The dictionary's XML namespace.
* @param xmlElementName Name of the XML element
* representing the dictionary.
*/ public void loadFromXml(EwsServiceXmlReader reader, XmlNamespace xmlNamespace, String xmlElementName) throws Exception {
super.loadFromXml(reader, xmlNamespace, xmlElementName);
this.isDirty = false;
}
/*
* (non-Javadoc)
*
* @see
* microsoft.exchange.webservices.ComplexProperty#tryReadElementFromXml(
* microsoft.exchange.webservices.EwsServiceXmlReader)
*/
@Override
/**
* Tries to read element from XML.
* @param reader The reader.
* @return True if element was read.
*/
public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
throws Exception {
reader.ensureCurrentNodeIsStartElement(this.getNamespace(),
XmlElementNames.DictionaryEntry);
this.loadEntry(reader);
return true;
}
/**
* Loads an entry, consisting of a key value pair, into this dictionary from
* the specified reader.
*
* @param reader The reader.
* @throws Exception the exception
*/
private void loadEntry(EwsServiceXmlReader reader) throws Exception {
EwsUtilities.ewsAssert(reader != null, "UserConfigurationDictionary.LoadEntry", "reader is null");
Object key;
Object value = null;
// Position at DictionaryKey
reader.readStartElement(this.getNamespace(),
XmlElementNames.DictionaryKey);
key = this.getDictionaryObject(reader);
// Position at DictionaryValue
reader.readStartElement(this.getNamespace(),
XmlElementNames.DictionaryValue);
String nil = reader.readAttributeValue(XmlNamespace.XmlSchemaInstance,
XmlAttributeNames.Nil);
boolean hasValue = (nil == null);
if (hasValue) {
value = this.getDictionaryObject(reader);
}
this.dictionary.put(key, value);
}
/**
* Extracts a dictionary object (key or entry value) from the specified
* reader.
*
* @param reader The reader.
* @return Dictionary object.
* @throws Exception the exception
*/
private Object getDictionaryObject(EwsServiceXmlReader reader)
throws Exception {
EwsUtilities.ewsAssert(reader != null, "UserConfigurationDictionary.loadFromXml", "reader is null");
UserConfigurationDictionaryObjectType type = this.getObjectType(reader);
List<String> values = this.getObjectValue(reader, type);
return this.constructObject(type, values, reader);
}
/**
* Extracts a dictionary object (key or entry value) as a string list from
* the specified reader.
*
* @param reader The reader.
* @param type The object type.
* @return String list representing a dictionary object.
* @throws Exception the exception
*/
private List<String> getObjectValue(EwsServiceXmlReader reader,
UserConfigurationDictionaryObjectType type) throws Exception {
EwsUtilities.ewsAssert(reader != null, "UserConfigurationDictionary.loadFromXml", "reader is null");
List<String> values = new ArrayList<String>();
reader.readStartElement(this.getNamespace(), XmlElementNames.Value);
do {
String value = null;
if (reader.isEmptyElement()) {
// Only string types can be represented with empty values.
if (type.equals(UserConfigurationDictionaryObjectType.String)
|| type
.equals(UserConfigurationDictionaryObjectType.
StringArray)) {
value = "";
} else {
EwsUtilities
.ewsAssert(false, "UserConfigurationDictionary." + "GetObjectValue",
"Empty element passed for type: " + type.toString());
}
} else {
value = reader.readElementValue();
}
values.add(value);
reader.read(); // Position at next element or
// DictionaryKey/DictionaryValue end element
} while (reader.isStartElement(this.getNamespace(),
XmlElementNames.Value));
return values;
}
/**
* Extracts the dictionary object (key or entry value) type from the
* specified reader.
*
* @param reader The reader.
* @return Dictionary object type.
* @throws Exception the exception
*/
private UserConfigurationDictionaryObjectType getObjectType(
EwsServiceXmlReader reader) throws Exception {
EwsUtilities.ewsAssert(reader != null, "UserConfigurationDictionary.loadFromXml", "reader is null");
reader.readStartElement(this.getNamespace(), XmlElementNames.Type);
String type = reader.readElementValue();
return UserConfigurationDictionaryObjectType.valueOf(type);
}
/**
* Constructs a dictionary object (key or entry value) from the specified
* type and string list.
*
* @param type Object type to construct.
* @param value Value of the dictionary object as a string list
* @param reader The reader.
* @return Dictionary object.
*/
private Object constructObject(UserConfigurationDictionaryObjectType type,
List<String> value, EwsServiceXmlReader reader) {
EwsUtilities.ewsAssert(value != null, "UserConfigurationDictionary.ConstructObject", "value is null");
EwsUtilities
.ewsAssert((value.size() == 1 || type == UserConfigurationDictionaryObjectType.StringArray),
"UserConfigurationDictionary.ConstructObject",
"value is array but type is not StringArray");
EwsUtilities
.ewsAssert(reader != null, "UserConfigurationDictionary.ConstructObject", "reader is null");
Object dictionaryObject = null;
if (type.equals(UserConfigurationDictionaryObjectType.Boolean)) {
dictionaryObject = Boolean.parseBoolean(value.get(0));
} else if (type.equals(UserConfigurationDictionaryObjectType.Byte)) {
dictionaryObject = Byte.parseByte(value.get(0));
} else if (type.equals(UserConfigurationDictionaryObjectType.ByteArray)) {
dictionaryObject = Base64.decodeBase64(value.get(0));
} else if (type.equals(UserConfigurationDictionaryObjectType.DateTime)) {
Date dateTime = DateTimeUtils.convertDateTimeStringToDate(value.get(0));
if (dateTime != null) {
dictionaryObject = dateTime;
} else {
EwsUtilities.ewsAssert(false, "UserConfigurationDictionary.ConstructObject", "DateTime is null");
}
} else if (type.equals(UserConfigurationDictionaryObjectType.Integer32)) {
dictionaryObject = Integer.parseInt(value.get(0));
} else if (type.equals(UserConfigurationDictionaryObjectType.Integer64)) {
dictionaryObject = Long.parseLong(value.get(0));
} else if (type.equals(UserConfigurationDictionaryObjectType.String)) {
dictionaryObject = String.valueOf(value.get(0));
} else if (type
.equals(UserConfigurationDictionaryObjectType.StringArray)) {
dictionaryObject = value.toArray();
} else if (type
.equals(UserConfigurationDictionaryObjectType.
UnsignedInteger32)) {
dictionaryObject = Integer.parseInt(value.get(0));
} else if (type
.equals(UserConfigurationDictionaryObjectType.
UnsignedInteger64)) {
dictionaryObject = Long.parseLong(value.get(0));
} else {
EwsUtilities.ewsAssert(false, "UserConfigurationDictionary.ConstructObject",
"Type not recognized: " + type.toString());
}
return dictionaryObject;
}
/**
* Validates the specified key and value.
*
* @param key The key.
* @param value The diction dictionary entry key.ary entry value.
* @throws Exception the exception
*/
private void validateEntry(Object key, Object value) throws Exception {
this.validateObject(key);
this.validateObject(value);
}
/**
* Validates the dictionary object (key or entry value).
*
* @param dictionaryObject Object to validate.
* @throws Exception the exception
*/
private void validateObject(Object dictionaryObject) throws Exception {
// Keys may not be null but we rely on the internal dictionary to throw
// if the key is null.
if (dictionaryObject != null) {
if (dictionaryObject.getClass().isArray()) {
int length = Array.getLength(dictionaryObject);
Class<?> wrapperType = Array.get(dictionaryObject, 0).getClass();
Object[] newArray = (Object[]) Array.
newInstance(wrapperType, length);
for (int i = 0; i < length; i++) {
newArray[i] = Array.get(dictionaryObject, i);
}
this.validateArrayObject(newArray);
} else {
this.validateObjectType(dictionaryObject);
}
} else {
throw new NullPointerException();
}
}
/**
* Validate the array object.
*
* @param dictionaryObjectAsArray Object to validate
* @throws ServiceLocalException the service local exception
*/
private void validateArrayObject(Object[] dictionaryObjectAsArray)
throws ServiceLocalException {
// This logic is based on
// Microsoft.Exchange.Data.Storage.ConfigurationDictionary.
// CheckElementSupportedType().
// if (dictionaryObjectAsArray is string[])
if (dictionaryObjectAsArray instanceof String[]) {
if (dictionaryObjectAsArray.length > 0) {
for (Object arrayElement : dictionaryObjectAsArray) {
if (arrayElement == null) {
throw new ServiceLocalException("The array contains at least one null element.");
}
}
} else {
throw new ServiceLocalException("The array must contain at least one element.");
}
} else if (dictionaryObjectAsArray instanceof Byte[]) {
if (dictionaryObjectAsArray.length <= 0) {
throw new ServiceLocalException("The array must contain at least one element.");
}
} else {
throw new ServiceLocalException(String.format(
"Objects of type %s can't be added to the dictionary. The following types are supported: string array, byte array, boolean, byte, DateTime, integer, long, string, unsigned integer, and unsigned long.", dictionaryObjectAsArray
.getClass()));
}
}
/**
* Validates the dictionary object type.
*
* @param theObject Object to validate.
* @throws ServiceLocalException the service local exception
*/
private void validateObjectType(Object theObject) throws ServiceLocalException {
// This logic is based on
// Microsoft.Exchange.Data.Storage.ConfigurationDictionary.
// CheckElementSupportedType().
boolean isValidType = false;
if (theObject != null) {
if (theObject instanceof String ||
theObject instanceof Boolean ||
theObject instanceof Byte ||
theObject instanceof Long ||
theObject instanceof Date ||
theObject instanceof Integer) {
isValidType = true;
}
}
if (!isValidType) {
throw new ServiceLocalException(
String.format(
"Objects of type %s can't be added to the dictionary. The following types are supported: string array, byte array, boolean, byte, DateTime, integer, long, string, unsigned integer, and unsigned long.", (theObject != null ?
theObject.getClass().toString() : "null")));
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator<Object> iterator() {
return this.dictionary.values().iterator();
}
}